Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Dmitry Fengoff
ServiceBuilder returning custom class (classloader problem)
November 14, 2012 1:47 AM
Answer

Dmitry Fengoff

Rank: New Member

Posts: 16

Join Date: March 19, 2012

Recent Posts

I have service which should returns custom class (for example MyClass)

1
2public List<MyClass> getMyClasses() {
3...
4}


I put MyClass to WEB-INF/service/com/mypackage/model

If i work with it in current portlet it works correctly. But if I call this method from another portlet i have error:


java.lang.ClassCastException: com.mypackage.model.MyClass cannot be cast to com.mypackage.model.MyClass


I have configured it correctly (move *-service.jar to tomcat/lib/ext and etc.). Moreover with model classes it works correctly from another portlets.
As I understood i need manipulation with *Clp classes. But what is proper way?

How can i fix it?
David H Nebinger
RE: ServiceBuilder returning custom class (classloader problem)
November 14, 2012 5:12 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7238

Join Date: September 1, 2006

Recent Posts

Dmitry Fengoff:
How can i fix it?


You really can't. If you dig into the service jar you'll see that those are actually interfaces in the model package, not classes. The implementation classes are left for the plugin's jar.

At runtime when you invoke a normal SB entity method, the service impl code will end up w/ one (or more) implementations of the interface which it will return.

The CLP layer in between takes care of marshalling the object across the class loader boundary between two plugins.

When you have the service jar in both plugins (by packaging the way you did), you actually have two different classes (across the class loader boundary) and cannot cast between them, resulting in the error.

When you moved the service jar to the global lib directory, you ensured that there is only one version of the class that is loaded at the app container level, so it's easier to cross the class loader boundary that way.

Now if you painstakingly go through each part of the SB generated code and re-engineer your custom class so it works the same way, you're probably still going to have issues as there is one (and only one) CLP instance per service jar, and that file gets re-written every time you run service builder (resulting in your hand-coded changes getting lost).
Dmitry Fengoff
RE: ServiceBuilder returning custom class (classloader problem)
November 14, 2012 6:32 AM
Answer

Dmitry Fengoff

Rank: New Member

Posts: 16

Join Date: March 19, 2012

Recent Posts

Thanks for the answer. I thought as much

For all who will have same problem: now I use workaround in another portlets:

1
2for(Object o : MyService.getMyClasses()) {
3    MyClass myObj = new MyClass();
4    BeanUtils.copyProperties(myObj, o);
5     ....
6}


It's like a manipulation as liferay generates in *Clp tier