Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axis2 Namespace/Classpath Issue

I work on a web application that calls multiple web service clients from within its code.

Each web service has some common namespaces, however I am currently mapping these namespaces to different packages when I generate each client

e.g.

Web Service 1's namespace1 -> com.company.webservice.client1.service

Web Service 2's namespace1 -> com.company.webservice.client2.service

Where namespace1 refers to the same namespace URL.

Using Wsdl2Java's namespace2package option:

// Web Service Client 1's namespace parameter
--namespace2package http://www.multispeak.org/Version_3.0=com.company.webservice.client1.service

// Web Service Client 2's namespace parameter
--namespace2package http://www.multispeak.org/Version_3.0=com.company.webservice.client2.service

I can generate web service client code for these services without issue and I can call each client fine on their own, so long as only one of the generated client jars is on the classpath for the given web service call. However, if I place both web service client jars on the classpath, only one of the web service clients will work (the one where its respective client jar is first on the class path).

The other web service client fails when trying to call it, with the following exception:

java.lang.ClassCastException: com.company.webservice.client1.service.impl.GetAllMetersResponseDocumentImpl cannot be cast to com.company.webservice.client2.service.GetAllMetersResponseDocument

I've obfuscated some of the actual values above.

So, the issue seems to be regarding how Axis2/XMLBeans looks up the appropriate class to match the given XML to parse from.

I can change the namespace mappings so that they match each other and it works fine after that. However, the downside to that is that I have multiple web service client jars containing the same generated code in the same package structure, whereby these classes will only be instantiated from the models from the first client jar it finds on the classpath.

Is there a way of doing this so that I can keep the different namespaces for each web service client jar?

Or am I simply going to be forced to have each namespace mapped to the same package for every client that uses that namespace?

Hopefully the issue makes sense, but if I need to provide anything else that would assist, please let me know and I will expand this question with further details, but hopefully someone with knowledge of Axis2/XMLBeans/web service client generation using Wsdl2Java should be able to answer this without much further info.

UPDATE 1: I finally gave in and just made all of the namespace mappings point to the same package rather than bespoke per web service client and took the hit of having multiple copies of the same class in various JARs on the classpath. Not as elegant as I would have liked, but at least it works.

If anyone can come up with a better solution that allows me to use bespoke copies in each client instead, please do let me know.

UPDATE 2: This approach equally does not work as the two web services, despite using the same namespace, produce different versions of the namespace's models which now causes compile time errors dependent on classpath order. So... Back to square one...

like image 256
ManoDestra Avatar asked Aug 14 '18 19:08

ManoDestra


1 Answers

I have the feeling you have two versions of the GetAllMetersResponseDocument in each jar. What is happening is that it is loading the interface from the opposite jar file which ends up in Class cast exception. I may be wrong.

This is the reason why it works when you have one of the jars loaded.

There is also this option where you can have Classloader isolation, resulting in two different classloaders for the two jars you can again end up with two objects of the same type that can not be casted to each other.

UDATE I actualy just checked if axis2 has classloader isolation defined by default and it does. https://axis.apache.org/axis2/java/core/faq.html read Class Loading Issues

I believe also reading service and module isolation from this article will help you. https://www.developer.com/open/article.php/10930_3589126_2/Avoiding-Mistakes-Made-Using-Axis2.htm

like image 159
Alexander Petrov Avatar answered Sep 27 '22 23:09

Alexander Petrov