Difference between revisions of "The CTP JarClassLoader"
Line 22: | Line 22: | ||
Parenthetic note: For two Class objects to be equal, they must be the same class and they must have been loaded by the same ClassLoader. | Parenthetic note: For two Class objects to be equal, they must be the same class and they must have been loaded by the same ClassLoader. | ||
− | <b> | + | <b>CTP startup</b> |
− | |||
− | |||
− | |||
− | |||
CTP is packaged in the <b><tt>CTP.jar</tt></b> file. The main class is <b><tt>org.rsna.ctp.ClinicalTrialProcessor</tt></b>. When the program starts, the <b><tt>main</tt></b> method of the main class is called. That method is shown below. | CTP is packaged in the <b><tt>CTP.jar</tt></b> file. The main class is <b><tt>org.rsna.ctp.ClinicalTrialProcessor</tt></b>. When the program starts, the <b><tt>main</tt></b> method of the main class is called. That method is shown below. | ||
Line 54: | Line 50: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | The key objective of the main method is to get the program started, with a ClassLoader in place which knows about the JARs in the <b><tt>libraries</tt></b> directory. There are several subtleties in this method, so it is worthwhile to examine each instruction. | ||
+ | |||
+ | <pre> | ||
+ | //Make sure the libraries directory is present | ||
+ | libraries.mkdirs(); | ||
+ | </pre> | ||
+ | This simply ensures that the <b><tt>libraries</tt></b> directory exists. | ||
+ | |||
+ | <pre> | ||
+ | //Get a JarClassLoader pointing to this program plus the libraries directory | ||
+ | JarClassLoader cl = | ||
+ | JarClassLoader.getInstance(new File[] { new File("CTP.jar"), libraries }); | ||
+ | </pre> | ||
+ | This gets an instance of a JarClassLoader which knows about the <b><tt>CTP.jar</tt></b> file (which is located in the top-level <b><tt>CTP</tt></b> directory) and all the JAR files located in the <b><tt>libraries</tt></b> directory. This is a ClassLoader with a special delegation feature that takes precedence over its parent ClassLoader, thus ensuring that if it can load a class it becomes the class's ClassLoader and not one of the parent ClassLoaders. The JarClassLoader is described in detail below. | ||
+ | |||
+ | <pre> | ||
+ | //Set the context classloader to the JarClassLoader | ||
+ | Thread.currentThread().setContextClassLoader(cl); | ||
+ | </pre> | ||
+ | This sets the context ClassLoader of the Thread in which the program is starting to the JarClassLoader. This is necessary in the case of CTP because the DICOM library (<b><tt>dcm4che</tt></b>) has certain methods which use the context ClassLoader to load classes which are in JARs in the <b><tt>libraries</tt></b> directory. When the program starts, the context ClassLoader is the Application classLoader, which doesn't know about those JARs, so it is necessary to replace it with one that does. | ||
+ | |||
+ | <pre> | ||
+ | //Load the class and instantiate it | ||
+ | try { | ||
+ | Class ctpClass = cl.loadClass(ctp); | ||
+ | ctpClass.getConstructor( new Class[0] ).newInstance( new Object[0] ); | ||
+ | } | ||
+ | catch (Exception unable) { unable.printStackTrace(); } | ||
+ | </pre> | ||
+ | This instantiates the <b><tt>ClinicalTrialProcessor</tt></b> class. Note that when the program starts, the ApplicationClassLoader is the one which loaded the main class, so it would be the one that is used if an instruction like the following were used: | ||
+ | |||
+ | <pre> | ||
+ | new ClinicalTrialProcessor(); | ||
+ | </pre> | ||
+ | This would have the effect of making the ApplicationClassLoader the ClassLoader for the entire application, so it is necessary to load the class again with the JarClassLoader to ensure that the JarClassLOADER | ||
+ | |||
+ | <b>The java.net.URLClassLoader</b> | ||
+ | |||
+ | The java.net package has a URLClassLoader which loads classes from an array of URLs pointing to JARs. Like all standard ClassLoaders, it obeys the standard delegation rules. | ||
+ | |||
+ | |||
... more to come ... | ... more to come ... |
Revision as of 19:17, 28 May 2009
This article describes the startup mechanism used by CTP. Nobody needs to read this article in order to use, or even to extend, CTP. The purpose is simply to capture the thought process in case changes are contemplated in the future. This article contains highly technical, secret information which will make you extremely attractive to women, which as all programmers know, is a heavy burden to bear. Proceed at your own risk.
CTP is a pipeline processor which is designed to be extensible. The idea is that programmers can implement new PipelineStages or database interfaces and couple them into the program without having to modify CTP itself. See Extending CTP for more information. Once an extension has been created, it must be recognized by CTP in order to be available for loading. The recommended way to do that is to build extensions into JAR files and place those files in the libraries directory of the CTP installation. To support this mechanism, CTP must have a way to load classes that are not on the classpath.
A brief diversion on ClassLoaders
ClassLoaders load classes and resources. There are three standard ClassLoaders in Java:
- The primordial ClassLoader is responsible for loading the classes of the Java language. This ClassLoader is part of the JVM, and it is written in native code for the platform.
- The extension ClassLoader is responsible for loading Java extensions. These are typically classes in JARs in the JVM/lib/ext directory, although it is possible to specify additional extensions directories when starting a Java program.
- The application ClassLoader is responsible for loading classes found on the classpath.
The classpath is defined by an environment variable or an attribute in the manifest of a JAR file. It can also be specified on the command line when starting a Java program.
A ClassLoader (except for the primordial ClassLoader) is a Java class, and it must be loaded by another ClassLoader, which becomes its parent. ClassLoaders typically obey delegation rules along these lines:
- When asked to load a class (by a call to its loadClass method), the ClassLoader first checks to see if it has already loaded the class. If so, it must return exactly the same Class object for it that it returned before.
- If the requested class has not been previously loaded, the ClassLoader calls the loadClass method of its parent. If the parent returns a Class object for the requested class, the ClassLoader returns it.
- If the parent failed to load the requested class (indicated by its receiving a ClassNotFoundException from the parent), the ClassLoader searches its domain to find the class. If it finds the class, it returns it. If not, it throws a ClassNotFoundException.
The effect of the delegation mechanism is that the highest level ClassLoader which can load a class is the one that loads it. Every Class object knows the ClassLoader which loaded it. When an object attempts to instantiate another object using the new instruction, the instantiating object's ClassLoader is used to load the Class of the instantiated class, but the delegation mechanism may result in a higher level ClassLoader being the one that does the actual loading (and being the one that the loaded class knows as its ClassLoader). This little bit of arcana will be significant later.
Parenthetic note: For two Class objects to be equal, they must be the same class and they must have been loaded by the same ClassLoader.
CTP startup
CTP is packaged in the CTP.jar file. The main class is org.rsna.ctp.ClinicalTrialProcessor. When the program starts, the main method of the main class is called. That method is shown below.
static final File libraries = new File("libraries"); static final String ctp = "org.rsna.ctp.ClinicalTrialProcessor"; public static void main(String[] args) { //Make sure the libraries directory is present libraries.mkdirs(); //Get a JarClassLoader pointing to this program plus the libraries directory JarClassLoader cl = JarClassLoader.getInstance(new File[] { new File("CTP.jar"), libraries }); //Set the context classloader to the JarClassLoader Thread.currentThread().setContextClassLoader(cl); //Load the class and instantiate it try { Class ctpClass = cl.loadClass(ctp); ctpClass.getConstructor( new Class[0] ).newInstance( new Object[0] ); } catch (Exception unable) { unable.printStackTrace(); } }
The key objective of the main method is to get the program started, with a ClassLoader in place which knows about the JARs in the libraries directory. There are several subtleties in this method, so it is worthwhile to examine each instruction.
//Make sure the libraries directory is present libraries.mkdirs();
This simply ensures that the libraries directory exists.
//Get a JarClassLoader pointing to this program plus the libraries directory JarClassLoader cl = JarClassLoader.getInstance(new File[] { new File("CTP.jar"), libraries });
This gets an instance of a JarClassLoader which knows about the CTP.jar file (which is located in the top-level CTP directory) and all the JAR files located in the libraries directory. This is a ClassLoader with a special delegation feature that takes precedence over its parent ClassLoader, thus ensuring that if it can load a class it becomes the class's ClassLoader and not one of the parent ClassLoaders. The JarClassLoader is described in detail below.
//Set the context classloader to the JarClassLoader Thread.currentThread().setContextClassLoader(cl);
This sets the context ClassLoader of the Thread in which the program is starting to the JarClassLoader. This is necessary in the case of CTP because the DICOM library (dcm4che) has certain methods which use the context ClassLoader to load classes which are in JARs in the libraries directory. When the program starts, the context ClassLoader is the Application classLoader, which doesn't know about those JARs, so it is necessary to replace it with one that does.
//Load the class and instantiate it try { Class ctpClass = cl.loadClass(ctp); ctpClass.getConstructor( new Class[0] ).newInstance( new Object[0] ); } catch (Exception unable) { unable.printStackTrace(); }
This instantiates the ClinicalTrialProcessor class. Note that when the program starts, the ApplicationClassLoader is the one which loaded the main class, so it would be the one that is used if an instruction like the following were used:
new ClinicalTrialProcessor();
This would have the effect of making the ApplicationClassLoader the ClassLoader for the entire application, so it is necessary to load the class again with the JarClassLoader to ensure that the JarClassLOADER
The java.net.URLClassLoader
The java.net package has a URLClassLoader which loads classes from an array of URLs pointing to JARs. Like all standard ClassLoaders, it obeys the standard delegation rules.
... more to come ...