KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > LoaderManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: LoaderManager.java,v 1.14 2005/07/26 17:14:54 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.server;
27
28
29 import java.io.File JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import org.objectweb.jonas.common.JProp;
35
36 /**
37  * This class create all the ClassLoader necessary for JOnAS.
38  *
39  * @author Guillaume Sauthier (initial developer)
40  */

41 public class LoaderManager {
42
43     /**
44      * Singleton instance of LoaderManager
45      */

46     private static LoaderManager instance = null;
47
48     /**
49      * ClassLoader name for Commons libs
50      */

51     private static final String JavaDoc COMMONS = "loader.commons";
52
53     /**
54      * ClassLoader name for Tools libs
55      */

56     private static final String JavaDoc TOOLS = "loader.tools";
57
58     /**
59      * ClassLoader name for Applications
60      */

61     private static final String JavaDoc APPS = "loader.apps";
62
63     /**
64      * ClassLoader name for Catalina
65      */

66     private static final String JavaDoc CATALINA = "loader.catalina";
67
68     /**
69      * Catalina Bootstrap.jar (used to know if we're using external revision of Tomcat)
70      */

71     private static final String JavaDoc CATALINA_BOOTSTRAP_JAR = "bin" + File.separator + "bootstrap.jar";
72
73     /**
74      * Jetty org.mortbay.jetty.jar (used to know if we're using external revision of Jetty)
75      */

76     private static final String JavaDoc MORTBAY_JETTY_JAR = "lib" + File.separator + "org.mortbay.jetty.jar";
77
78     /**
79      * JProp instance
80      */

81     private JProp jp = null;
82
83     /**
84      * ClassLoaders
85      */

86     private Map JavaDoc loaders = null;
87
88     /**
89      * $JONAS_ROOT/lib
90      */

91     private static File JavaDoc jonasLib = null;
92
93     /**
94      * $JONAS_ROOT/lib/catalina
95      */

96     private static File JavaDoc catalinaLib = null;
97
98     /**
99      * $JONAS_ROOT/lib/jetty
100      */

101     private static File JavaDoc jettyLib = null;
102
103     /**
104      * $JONAS_BASE
105      */

106     private static String JavaDoc jonasBase = null;
107
108     /**
109      * $CATALINA_HOME
110      */

111     private static String JavaDoc catalinaHome = null;
112
113     /**
114      * $JETTY_HOME
115      */

116     private static String JavaDoc jettyHome = null;
117
118     /**
119      * true if JOnAS is launched with web container
120      */

121     private boolean hasWeb = false;
122
123     /**
124      * Use the default Classpath ?
125      */

126     private boolean useDefaultClassPath = false;
127
128     /**
129      * is the web container provided by Catalina
130      */

131     private boolean isCatalina = false;
132
133     /**
134      * ${jonas.root}/lib/commons
135      */

136     private File JavaDoc jonasCommonsDir;
137
138     /**
139      * ${jonas.root}/lib/tools
140      */

141     private File JavaDoc jonasToolsDir;
142
143     /**
144      * ${jonas.root}/lib/apps
145      */

146     private File JavaDoc jonasAppsDir;
147
148     /**
149      * Create a LoaderManager instance
150      */

151     private LoaderManager() {
152         // Get the $JONAS_ROOT/lib directories
153
String JavaDoc jonasRoot = System.getProperty("install.root");
154         if (jonasRoot == null) {
155             System.err.println("JONAS_ROOT is not set");
156             System.exit(2);
157         }
158
159         jonasLib = new File JavaDoc(jonasRoot, "lib");
160         jonasCommonsDir = new File JavaDoc(jonasLib, "commons");
161         jonasToolsDir = new File JavaDoc(jonasLib, "tools");
162         jonasAppsDir = new File JavaDoc(jonasLib, "apps");
163         catalinaLib = new File JavaDoc(jonasLib, "catalina");
164         jettyLib = new File JavaDoc(jonasLib, "jetty");
165
166         // Init loaders map
167
loaders = new Hashtable JavaDoc();
168     }
169
170     /**
171      * @return Returns the unique instance of LoaderManager.
172      */

173     public static LoaderManager getInstance() {
174         if (instance == null) {
175             instance = new LoaderManager();
176         }
177         return instance;
178     }
179
180     /**
181      * Initialize the LoaderManager with a JProp instance.
182      *
183      * @param props JProp used to configure JOnAS
184      */

185     public void init(JProp props) {
186         jp = props;
187         // Get $JONAS_BASE
188
jonasBase = JProp.getJonasBase();
189
190         useDefaultClassPath = jp.getValueAsBoolean("jonas.default.classloader", false);
191
192         // find web services
193
String JavaDoc[] services = props.getValueAsArray("jonas.services");
194         if (services != null) {
195             for (int i = 0; i < services.length; i++) {
196                 if (services[i].equals("web")) {
197                     hasWeb = true;
198                     String JavaDoc classname = props.getValue("jonas.service.web.class");
199                     if (classname.matches(".*Catalina.*")) {
200                         isCatalina = true;
201                     }
202                 }
203             }
204         }
205
206         catalinaHome = System.getProperty("catalina.home");
207         jettyHome = System.getProperty("jetty.home");
208
209         // Set in all case catalina.home and base to $JONAS_BASE
210
// if library is found. Useful if a user want to run in
211
// a same environment JOnAS/Tomcat and Tomcat with different
212
// versions
213
if (hasEmbeddedCatalina()) {
214             System.setProperty("catalina.home", catalinaLib.getPath());
215             System.setProperty("catalina.base", jonasBase);
216             catalinaHome = catalinaLib.getPath();
217         }
218
219         // Set jetty.home to $JONAS_BASE if library is found
220
// and if jetty.home isn't set.
221
if (jettyHome == null) {
222             if (hasEmbeddedJetty()) {
223                 System.setProperty("jetty.home", jettyLib.getPath());
224                 jettyHome = jettyLib.getPath();
225             }
226         }
227
228     }
229
230     /**
231      * @return Returns true if using embedded catalina classes.
232      */

233     private boolean hasEmbeddedCatalina() {
234         return new File JavaDoc(catalinaLib, CATALINA_BOOTSTRAP_JAR).exists();
235     }
236
237     /**
238      * @return Returns true if using embedded jetty classes.
239      */

240     private boolean hasEmbeddedJetty() {
241         return new File JavaDoc(jettyLib, MORTBAY_JETTY_JAR).exists();
242     }
243
244     /**
245      * @return Returns the Commons ClassLoader
246      * @throws Exception When ClassLoader cannot be created.
247      */

248     public JClassLoader getCommonsLoader() throws Exception JavaDoc {
249         JClassLoader commons = getLoader(COMMONS);
250         if (commons == null) {
251             commons = createCommonsClassLoader();
252             setLoader(COMMONS, commons);
253         }
254         return commons;
255     }
256
257     /**
258      * @return returns the Tools ClassLoader
259      * @throws Exception When ClassLoader cannot be created
260      */

261     public JClassLoader getToolsLoader() throws Exception JavaDoc {
262         JClassLoader tools = getLoader(TOOLS);
263         if (tools == null) {
264             tools = createToolsClassLoader();
265             setLoader(TOOLS, tools);
266         }
267         return tools;
268     }
269
270     /**
271      * @return Returns the Application ClassLoader
272      * @throws Exception When ClassLoader cannot be created
273      */

274     public JClassLoader getAppsLoader() throws Exception JavaDoc {
275         JClassLoader apps = getLoader(APPS);
276         if (apps == null) {
277             apps = createAppsClassLoader();
278             setLoader(APPS, apps);
279         }
280         return apps;
281     }
282
283     /**
284      * @return Returns the Catalina ClassLoader
285      * @throws Exception When ClassLoader cannot be created.
286      */

287     public JClassLoader getCatalinaLoader() throws Exception JavaDoc {
288         JClassLoader catalina = getLoader(CATALINA);
289         if (catalina == null) {
290             catalina = createCatalinaClassLoader();
291             setLoader(CATALINA, catalina);
292         }
293         return catalina;
294     }
295
296     /**
297      * Return an array containing all the resources declared in the classpath
298      * environment variable.
299      *
300      * @param xtracp XTRA classpath
301      *
302      * @return a list containing all the resources declared in the classpath
303      * environment variable.
304      *
305      * @throws Exception if a resource cannot be found.
306      */

307     private static JURLs getClassPath(boolean xtracp) throws Exception JavaDoc {
308         String JavaDoc cpprop = "java.class.path";
309         if (xtracp) {
310             cpprop = "jonas.classpath";
311         }
312         String JavaDoc cpValue = System.getProperty(cpprop);
313         //System.out.println("classpath=" + cpValue); // DEBUG
314
String JavaDoc sep = File.pathSeparator;
315         JURLs jurls = new JURLs();
316         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(cpValue, sep);
317         while (tokenizer.hasMoreTokens()) {
318             File JavaDoc file = new File JavaDoc(tokenizer.nextToken());
319             if (file.isDirectory()) {
320                 jurls.addDir(file);
321             } else {
322                 // TODO check for ${jonas.root}/lib/bootstrap
323
// do not re-add bootstrap.jar inside classloader
324
if (!file.getName().equals("ow_jonas_bootstrap.jar")) {
325                     jurls.add(file);
326                 }
327             }
328         }
329         return jurls;
330     }
331
332     /**
333      * Returns a JClassLoader.
334      *
335      * @param name name of the loader
336      *
337      * @return Returns the JClassLoader
338      */

339     private JClassLoader getLoader(String JavaDoc name) {
340         return (JClassLoader) loaders.get(name);
341     }
342
343     /**
344      * Add a couple (name, ClassLoader).
345      *
346      * @param name ClassLoader name
347      * @param loader the ClassLoader
348      */

349     private void setLoader(String JavaDoc name, JClassLoader loader) {
350         loaders.put(name, loader);
351     }
352
353     /**
354      * Constructs the Commons ClassLoader.
355      *
356      * @return Returns the Commons ClassLoader.
357      * @throws Exception When ClassLoader cannot be created from CLASSPATH or resources cannot be added to URLs List.
358      */

359     private JClassLoader createCommonsClassLoader() throws Exception JavaDoc {
360
361         JURLs jurls = new JURLs();
362
363         // add classpath in loader
364
jurls.merge(getClassPath(useDefaultClassPath));
365
366         // Load $JONAS_ROOT/lib/commons/**/*.jar
367
jurls.add(jonasCommonsDir, ".jar");
368
369         // add jonas.base/lib/ext only if jonas.base != jonas.root
370
// jonas.base/lib/ext is added before jonas.root/lib/ext
371
if (!System.getProperty("install.root").toLowerCase().equals(jonasBase.toLowerCase())) {
372             File JavaDoc jonasBaseLibExt = new File JavaDoc(jonasBase + File.separator + "lib" + File.separator + "ext");
373             if (jonasBaseLibExt.exists()) {
374                 jurls.add(jonasBaseLibExt, ".jar");
375             }
376         }
377
378         // add jonas.root/lib/ext
379
jurls.add(new File JavaDoc(jonasLib, "ext"), ".jar");
380
381         if (hasWeb) {
382             // resources for catalina have been put in apps ClassLoader
383

384             // add resources for jetty if required
385
if (jettyHome != null && !isCatalina) {
386                 File JavaDoc jettyLibDir = new File JavaDoc(jettyHome, "lib");
387                 File JavaDoc jettyExtDir = new File JavaDoc(jettyHome, "ext");
388                 File JavaDoc jettyExtraDir = new File JavaDoc(new File JavaDoc(jettyHome, "extra"), "lib");
389                 jurls.add(jettyLibDir, ".jar");
390                 jurls.add(jettyExtDir, ".jar");
391                 jurls.add(jettyExtraDir, "jsr77.jar");
392                 if (!hasEmbeddedJetty()) {
393                     // load ${jonas.root}/jetty/lib/ow_jonas_jetty.jar
394
File JavaDoc jonasJetty5Glue = new File JavaDoc(new File JavaDoc(jettyLib, "lib"), "ow_jonas_jetty.jar");
395                     jurls.add(jonasJetty5Glue);
396                 }
397             }
398             // else user has not set the webcontainer HOME var
399
}
400
401         return new JClassLoader("Commons", jurls.toURLs());
402     }
403
404     /**
405      * Constructs the Tools ClassLoader using Commons ClassLoader as parent.
406      *
407      * @return Returns the Tools ClassLoader
408      *
409      * @throws Exception When resources cannot be added to URL list.
410      */

411     private JClassLoader createToolsClassLoader() throws Exception JavaDoc {
412         JURLs jurls = new JURLs();
413
414         jurls.add(jonasToolsDir, ".jar");
415
416         return new JClassLoader("Tools", jurls.toURLs(), getCommonsLoader());
417     }
418
419     /**
420      * Constructs the Application ClassLoader with Commons ClassLoader as parent.
421      * At this time, this is an empty ClassLoader
422      *
423      * @return the Application ClassLoader
424      *
425      * @throws Exception When resources cannot be added to URL list.
426      */

427     private JClassLoader createAppsClassLoader() throws Exception JavaDoc {
428         JURLs jurls = new JURLs();
429
430         // ${jonas.root}/lib/apps
431
jurls.add(jonasAppsDir, ".jar");
432
433         if (hasWeb) {
434             // add resources for catalina if required
435
if (catalinaHome != null && isCatalina) {
436                 File JavaDoc catalinaBinDir = new File JavaDoc(catalinaHome, "bin");
437                 File JavaDoc catalinaCommonLibDir = new File JavaDoc(catalinaHome, "common" + File.separator + "lib");
438                 File JavaDoc catalinaSharedLibDir = new File JavaDoc(catalinaHome, "shared" + File.separator + "lib");
439                 jurls.add(new File JavaDoc(catalinaBinDir, "bootstrap.jar"));
440                 jurls.add(catalinaCommonLibDir, ".jar");
441                 jurls.add(catalinaSharedLibDir, ".jar");
442             }
443         }
444         return new JClassLoader("Apps", jurls.toURLs(), getCommonsLoader());
445     }
446
447     /**
448      * Constructs the Catalina ClassLoader with Applications ClassLoader as parent.
449      *
450      * @return the Catalina ClassLoader
451      *
452      * @throws Exception When resources cannot be added to URL list.
453      */

454     private JClassLoader createCatalinaClassLoader() throws Exception JavaDoc {
455         JURLs jurls = new JURLs();
456
457         // ${catalina.home}/server/lib/*jar
458
File JavaDoc catalinaServerLibDir = new File JavaDoc(catalinaHome, "server" + File.separator + "lib");
459         jurls.add(catalinaServerLibDir, ".jar");
460
461         // ${jonas.root}/catalina/server/lib/ow_jonas_tomcat5.jar
462
if (!hasEmbeddedCatalina()) {
463             // add ow_jonas_tomcat.jar only if using external Tomcat
464
File JavaDoc jonasTomcat5Glue = new File JavaDoc(catalinaLib, "server" + File.separator + "lib" + File.separator + "ow_jonas_tomcat5.jar");
465             File JavaDoc jonasTomcat55Glue = new File JavaDoc(catalinaLib, "server" + File.separator + "lib" + File.separator + "ow_jonas_tomcat55.jar");
466             if (jonasTomcat5Glue.exists()) {
467                 jurls.add(jonasTomcat5Glue);
468             } else if (jonasTomcat55Glue.exists()) {
469                 jurls.add(jonasTomcat55Glue);
470             } else {
471                 System.out.println("WARN : No ow_jonas_tomcat5.jar found in " + catalinaLib + "/server/lib directory.");
472             }
473         }
474         return new JClassLoader("Catalina", jurls.toURLs(), getAppsLoader());
475     }
476 }
477
Popular Tags