Defining a new class loader for the XOM

If you do not want the XOM classes to be in the class path of your project, you can define a new class loader for the XOM.

About this task

You can create your own class loader to load the XOM classes at run time.

An IlrReflect object uses a class loader to load the XOM classes when a rule is parsed. The class loader is not the only one used but it is used in priority. If the class loader that you set cannot retrieve a class, the class loader used to load the IlrReflect class is used. The getClassLoader() and setClassLoader(ClassLoader classLoader) methods are defined by the IlrObjectModel interface, which is implemented by IlrReflect.

Procedure

  1. Add the methods to define the classloader:
    package ilog.rules.bom;
    
    public interface IlrObjectModel
    {
      public ClassLoader getClassLoader();
      public void setClassLoader(ClassLoader classLoader);
    }
    Note:
    • Java™ classes can be loaded only after the class loader has been set.
    • Only one class loader can be set. Since JDK 1.2, class loaders are supposed to delegate class loading to the parent class loader.

    The class loader is lost if the IlrReflect object is serialized and then deserialized. To avoid losing the class loader, before deserializing an IlrReflect object, you must specify a link between the ObjectInputStream instance that contains the serialized IlrReflect object and the class loader to be used when the IlrReflect object gets deserialized.

  2. Call the IlrReflect.linkClassLoader method to specify the link to avoid losing the class loader:
    public class IlrReflect
    {
      public static void linkClassLoader(ObjectInputStream str,
                                         ClassLoader loader) { ... }
    };

    Often, when an IlrReflect object is deserialized, some Java classes are loaded. Therefore, you must set the class loader to be able to load them. As a result, the setClassLoader method cannot be called after the IlrReflect object has been completely deserialized.