Fórum

Sharing objects across portlets

Ian Harrigan, modificado 12 Anos atrás.

Sharing objects across portlets

New Member Postagens: 19 Data de Entrada: 22/11/11 Postagens 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:

<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:


       SomeClass c1 = new SomeClass();
        c1.setSomeValue("new value");
        session.setAttribute("someClass", c1, PortletSession.APPLICATION_SCOPE);
        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:

Object someClass = (Object) portletSession.getAttribute("someClass",  PortletSession.APPLICATION_SCOPE);
System.out.println("&gt; " + someClass);
SomeClass 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
thumbnail
Sagar A Vyas, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Liferay Master Postagens: 679 Data de Entrada: 17/04/09 Postagens 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, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Regular Member Postagens: 102 Data de Entrada: 16/08/11 Postagens 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
thumbnail
David H Nebinger, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Liferay Legend Postagens: 14918 Data de Entrada: 02/09/06 Postagens Recentes
Hmm, could it actually be a class loader issue rather than a cast issue?
Ian Harrigan, modificado 12 Anos atrás.

RE: Sharing objects across portlets

New Member Postagens: 19 Data de Entrada: 22/11/11 Postagens Recentes
Do we think 6.1 might clear up this issue? I havent tested it yet, will do and get back.

Ian
thumbnail
David H Nebinger, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Liferay Legend Postagens: 14918 Data de Entrada: 02/09/06 Postagens 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, modificado 12 Anos atrás.

RE: Sharing objects across portlets

New Member Postagens: 19 Data de Entrada: 22/11/11 Postagens 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
thumbnail
David H Nebinger, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Liferay Legend Postagens: 14918 Data de Entrada: 02/09/06 Postagens 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, modificado 12 Anos atrás.

RE: Sharing objects across portlets

Junior Member Postagens: 29 Data de Entrada: 25/02/11 Postagens 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
thumbnail
Brian Scott Schupbach, modificado 10 Anos atrás.

RE: Sharing objects across portlets

Expert Postagens: 329 Data de Entrada: 23/10/08 Postagens 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..
thumbnail
Brian Scott Schupbach, modificado 10 Anos atrás.

RE: Sharing objects across portlets

Expert Postagens: 329 Data de Entrada: 23/10/08 Postagens 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.