KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > bin > ClassesPreloadManager


1 package org.jahia.bin;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import org.apache.commons.digester.Digester;
10 import org.apache.commons.digester.Rule;
11 import org.xml.sax.SAXException JavaDoc;
12
13 /**
14  * <p>Title: A manager class that preloads classes into memory for a given
15  * configuration loaded from an XML file </p>
16  * <p>Description: This class is used to preload classes into the default
17  * class loader in order to statisfy static dependencies between Jahia's
18  * classes. This allows us to use our factory pattern that is used notably
19  * in the ObjectKey and ContentObject class hierarchies, that use static
20  * initializers to register class names into the factories.</p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: </p>
23  * @author Serge Huber
24  * @version 1.0
25  */

26
27 class ClassesPreloadManager {
28
29     ArrayList JavaDoc preloadClassNames = new ArrayList JavaDoc();
30
31     /**
32      * Constructor for the manager. This constructor actually does all the work
33      * of parsing the configuration file that contains the list of classes to
34      * preload into the default class loader, and then actually calls the
35      * class loader to load the classes, and therefore also call all the static
36      * initializers of those classes.
37      *
38      * The format of the XML file can be seen in this example :
39      * <?xml version="1.0" encoding="ISO-8859-1"?>
40      * <preload-classes>
41      * <class-name>org.jahia.content.ObjectKey</class-name>
42      * <class-name>org.jahia.content.ContentFieldKey</class-name>
43      * <class-name>org.jahia.content.ContentContainerListKey</class-name>
44      * <class-name>org.jahia.content.ContentObject</class-name>
45      * </preload-classes>
46      *
47      * @param configurationFileName the name of the XML configuration file that
48      * contains a simple list of classes to preload into the default class
49      * loader.
50      *
51      * @throws IOException thrown if there was an IO error while reading or
52      * opening the given XML configuration file
53      * @throws SAXException thrown if there was an XML error while parsing the
54      * configuration file
55      * @throws ClassNotFoundException thrown if one of the classes given in the
56      * configuration file couldn't be found by the default class loader.
57      */

58     public ClassesPreloadManager(String JavaDoc configurationFileName)
59         throws IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
60         loadXMLConfiguration(configurationFileName);
61         preloadClasses();
62     }
63
64     private void loadXMLConfiguration (String JavaDoc configurationFileName)
65         throws IOException JavaDoc, SAXException JavaDoc {
66
67         Digester digester = new Digester();
68
69         // This set of rules calls the addClassName method and passes
70
// in a single parameter to the method.
71
digester.addRule("preload-classes/class-name", new AddClassNameRule());
72
73         // This method starts the parsing of the document.
74
File JavaDoc configurationFile = new File JavaDoc(configurationFileName);
75         digester.parse(configurationFile);
76     }
77
78     private void preloadClasses()
79         throws ClassNotFoundException JavaDoc {
80         // we can now actually preload the classes into the default class
81
// loader.
82
Iterator JavaDoc preloadClassNameIter = preloadClassNames.iterator();
83         while (preloadClassNameIter.hasNext()) {
84             String JavaDoc curClassName = (String JavaDoc) preloadClassNameIter.next();
85             Class.forName(curClassName);
86         }
87     }
88
89     final class AddClassNameRule extends Rule {
90         public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
91                 throws Exception JavaDoc {
92             preloadClassNames.add(text);
93         }
94     }
95
96 }
Popular Tags