tags:

views:

59

answers:

3

Hi,

I have to provide an interface to a set of web services for which I have generated the code stubs and object binding. Many of the transactions share a common object model and thus a large part of the generated code overlaps. Normally this would not be a concern as I would just re-use the same code since the wsdl would link the same xsd files.

Unfortunately here the provider of these services has separated the xsd so that each services binds to a separate set (basically a copy) of the same files. In their specification they state that the implementation of the client side should isolate each web-services for easier maintenance. Basically they want to be able to modify the xsd for a single web service while leaving all others untouched.

The question is as follows :

How could I integrate these different set of generated classes in the same program so that each services exposes their functionality without interfering with their brethren ?

One solution I thought was to create a facade for each of them that would expose the desired functionality and object model so that the actual implementation remains hidden. Then though clever use of custom class loaders each facade would load a specific jar containing the generated code for this particular service.

Any thought ? ideas ? what was your experience facing a similar problem ?

thanks

+2  A: 

Have you considered using OSGi based bundles (e.g. Eclipse Equinox runtime)? Using an OSGi based implementation each set of generated classes would be in a separate bundle (.jar file) that gets its own class loader and can selectively export parts of its API to the rest of the application. So it provides a ready implementation of the solution you proposed.

ChrisH
indeed OSGi would be quite suitable for this purpose, thanks for the suggestion. However given this particular settings I went with a simpler solution of having the generator place the code in separate packages thereby working around the problem rather than through it.
Newtopian
+1  A: 

In general, the easiest way to handle this is to use a jaxb binding file (or the -p param to CXF's wsdl2java tool) to map the generated code into a package specific for the particular service. Thus, each service would have it's own objects to work with.

The complexity would come if you have to integrate the various services. Like take the return from one to call into another. Since the objects would be completely different, you would need to write code to copy data from one to the other. Possibly not fun. :-(

Daniel Kulp
yes, this is what I ended up doing, by digging more on the codegen options I stumbled on this possibility. Though not a direct solution to the question, it does provide a simple and safe way around the problem.
Newtopian
A: 

For Java two classes are different from eachother when they are loaded by separate classloaders. You can take advantage of this by loading the 2 versions of the classes yourself from 2 URLClassLoaders which get the URL of the respective jars as classpath. You might not need to build 2 facades if both sets of objects implement the same interfaces.

update

Yes, the jars should not be on the classpath of the main application. If you set a classloader of the main application as parent to your URLClassLoaders, the classes loaded still will have access to the rest of the classpath your application uses.

For statics if they are constants they will probably be inlined by the compiler. For static variables you will get 2, 1 for each jar loaded. If those variables are used outside of the context of the jars, you will probably have to access them via reflection off their class objects. Wether you need to select one over the other or combine them depends on the application, it would be my guess that if you can load and use 2 versions in 1 application that any static variables would only be used in the context of the respective jars.

rsp
for this to work I guess I would have to isolate the jars from the main classpath of the application so that they are not loaded automatically. Yet there is still the issue around static members and variables that can still clash.
Newtopian
@Newtopian, see update.
rsp