Foros de discusión

Sharing objects across portlets

Ian Harrigan, modificado hace 12 años.

Sharing objects across portlets

New Member Mensajes: 19 Fecha de incorporación: 22/11/11 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Liferay Master Mensajes: 679 Fecha de incorporación: 17/04/09 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Regular Member Mensajes: 102 Fecha de incorporación: 16/08/11 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Liferay Legend Mensajes: 14917 Fecha de incorporación: 2/09/06 Mensajes recientes
Hmm, could it actually be a class loader issue rather than a cast issue?
Ian Harrigan, modificado hace 12 años.

RE: Sharing objects across portlets

New Member Mensajes: 19 Fecha de incorporación: 22/11/11 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Liferay Legend Mensajes: 14917 Fecha de incorporación: 2/09/06 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

New Member Mensajes: 19 Fecha de incorporación: 22/11/11 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Liferay Legend Mensajes: 14917 Fecha de incorporación: 2/09/06 Mensajes recientes
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 hace 12 años.

RE: Sharing objects across portlets

Junior Member Mensajes: 29 Fecha de incorporación: 25/02/11 Mensajes recientes
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 hace 10 años.

RE: Sharing objects across portlets

Expert Mensajes: 329 Fecha de incorporación: 23/10/08 Mensajes recientes
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 hace 10 años.

RE: Sharing objects across portlets

Expert Mensajes: 329 Fecha de incorporación: 23/10/08 Mensajes recientes
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.