Third Party Libraries

This page describes how to work with third-party Java libraries in NetKernel modules.

Using third-party library classes as request representation objects

If requests using a third-party library class stay within a single NK module, there is no pronlem, because all of the endpoints in the module use the same Java classloader.

But, if the endpoints can be reached by other NK modules, you have to be careful about ensuring those calling modules have access to the SAME COPY of the representation class in their classloader. Otherwise, when calling endpoints try to source one of your endpoints, the Java runtime will complain “Cannot cast Foo into Foo” errors. That is because Java cannot cast objects of the same type between two classloaders .. as far as Java is concerned, they are two different types.

The answer is to ensure that all spaces that contain endpoints that use the same third party library class as a request representation (either sending or receiving) share the same copy of the class definition. This can be done in several ways:

  1. Ensure all of these spaces are declared in the same NK module. They all share the same module classloader. You have to ensure that the domain class requests cannot leak outside the module.
  2. Create a library module that exports the relevant domain classes. Then import that library space into each of the application modules that use that domain class. NetKernel will ensure that those module’s classloaders reference the same, exported definition of those classes.
  3. Add the third-party library JARs to the NetKernel classpath at launch time. Then, all modules will be able to resolve the classes.