Fóruns

Início » Liferay Portal » English » 3. Development

Visualização combinada Visão plana Exibição em árvore
Tópicos [ Anterior | Próximo ]
toggle
Ian Harrigan
Sharing objects across portlets
28 de Novembro de 2011 10:00
Resposta

Ian Harrigan

Ranking: New Member

Mensagens: 19

Data de entrada: 22 de Novembro de 2011

Mensagens recentes

Hi guys,

Im having an issue where i am trying to pass objects (and lists of objects) via a shared portlet session. I have:

1<private-session-attributes>false</private-session-attributes>


In both liferay-portlet.xml files. Then in one portlet i have an action that does the following:


1       SomeClass c1 = new SomeClass();
2        c1.setSomeValue("new value");
3        session.setAttribute("someClass", c1, PortletSession.APPLICATION_SCOPE);
4        session.setAttribute("someValue", "THIS IS A TEST VALUE", PortletSession.APPLICATION_SCOPE);


Both of these can be read (and cast in the portlets view.jsp that set the var), however, in another portlet i perform the following:

1Object someClass = (Object) portletSession.getAttribute("someClass",  PortletSession.APPLICATION_SCOPE);
2System.out.println("> " + someClass);
3SomeClass theClass = (SomeClass) someClass;


This fails with a "java.lang.ClassCastException" when i try to cast the object back to "SomeClass", but the memory address of the class is valid and equal in both portlets. Could this be something to do with serializing the class? It works completely fine with strings, but i need to pass around custom objects. Is this achievable?

Any help would be greatly appreciated and i have tried most everything i can think of.

Thanks in advance,
Ian
Sagar A Vyas
RE: Sharing objects across portlets
28 de Novembro de 2011 10:38
Resposta

Sagar A Vyas

Ranking: Liferay Master

Mensagens: 657

Data de entrada: 17 de Abril de 2009

Mensagens recentes

Hi Lan ,

This happens number of time.

Even one the blog Jonas has mentions same please find same as below.

http://www.liferay.com/community/wiki/-/wiki/Main/Frequently+Asked+Questions#hmhp_messageScroll11613621


In section How do we use the request to share attributes between portlets deployed as individual WARs? #

I have already ask this Question to Jonas , Hope he will reply on this, you can keep watch on this.

And if you will get answer please share it with us.

Thanks,
Sagar Vyas
neelam bhandari
RE: Sharing objects across portlets
9 de Janeiro de 2012 10:14
Resposta

neelam bhandari

Ranking: Regular Member

Mensagens: 102

Data de entrada: 16 de Agosto de 2011

Mensagens recentes

Hi all,
I am getting the same class cast exception while retrieving object from session. It works fine only for String .

Any solution yet?

Thanks and Regards
Neelam Bhandari
David H Nebinger
RE: Sharing objects across portlets
9 de Janeiro de 2012 10:41
Resposta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensagens: 11295

Data de entrada: 1 de Setembro de 2006

Mensagens recentes

Hmm, could it actually be a class loader issue rather than a cast issue?
Ian Harrigan
RE: Sharing objects across portlets
27 de Janeiro de 2012 06:32
Resposta

Ian Harrigan

Ranking: New Member

Mensagens: 19

Data de entrada: 22 de Novembro de 2011

Mensagens recentes

Do we think 6.1 might clear up this issue? I havent tested it yet, will do and get back.

Ian
David H Nebinger
RE: Sharing objects across portlets
27 de Janeiro de 2012 06:45
Resposta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensagens: 11295

Data de entrada: 1 de Setembro de 2006

Mensagens recentes

It's not an issue w/ Liferay, I'm sure it's going to end up being a class path issue...

Are your portlets in the same war file, or separate war files?
Ian Harrigan
RE: Sharing objects across portlets
27 de Janeiro de 2012 06:49
Resposta

Ian Harrigan

Ranking: New Member

Mensagens: 19

Data de entrada: 22 de Novembro de 2011

Mensagens recentes

They're in seperate .war files, it all works fine when they are deployed as one so i suspect your right.

What would be the correct way to share objects across portlets that are deployed via seperate .wars?

Ian
David H Nebinger
RE: Sharing objects across portlets
27 de Janeiro de 2012 07:52
Resposta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensagens: 11295

Data de entrada: 1 de Setembro de 2006

Mensagens recentes

not possible. the separate wars have boundaries in place so one cannot corrupt or disrupt another. crossing this boundary is typically done either in the browser (javascript magic) or in the database.
Tony Rad
RE: Sharing objects across portlets
27 de Janeiro de 2012 08:15
Resposta

Tony Rad

Ranking: Junior Member

Mensagens: 29

Data de entrada: 25 de Fevereiro de 2011

Mensagens recentes

Hi Ian,

you should be able to share any JDK class (not only String).
Moreover try to create a jar with your custom class and put it into TOMCAT_HOME/lib/ext, and restart the server. You should be able to share that object.
In fact in TOMCAT_HOME/lib/ext you can find also portal-service.jar, Liferay's library used to share core portal services between portlets.

I think the problem is about the JEE class loading hierarchy (more about that here).
In few words: each war has its own class loader and don't share classes with others war, BUT all the wars can access App Server level and System level (i.e. also JDK) classes.

I found an interesting article about sharing services and classes in Liferay here .

Regards
Brian Scott Schupbach
RE: Sharing objects across portlets
13 de Junho de 2013 12:38
Resposta

Brian Scott Schupbach

Ranking: Expert

Mensagens: 329

Data de entrada: 23 de Outubro de 2008

Mensagens recentes

I'm having the same issue with two portlets defined in the same .war file. I would think that since they are in the same .war, they could share that object? Apparently not..
Brian Scott Schupbach
RE: Sharing objects across portlets
8 de Novembro de 2013 06:53
Resposta

Brian Scott Schupbach

Ranking: Expert

Mensagens: 329

Data de entrada: 23 de Outubro de 2008

Mensagens recentes

Update...it was a cache issue for me. I was dealing with objects in the session. So, if I had an open session and re-deployed my portlet, it caused an error. Starting a new session fixed it.