KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > SystemClassLoader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader;
31
32 import com.caucho.config.Config;
33 import com.caucho.config.ConfigException;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.Vfs;
36
37 import java.io.File JavaDoc;
38 import java.net.URLClassLoader JavaDoc;
39
40 /**
41  * ClassLoader that initalizes the environment and allows byte code
42  * enhancement of classes in the system classpath.
43  * <pre>
44  * java -Djava.system.class.loader=com.caucho.loader.SystemClassLoader ...
45  * </pre>
46  * If the system property "system.conf" is defined, it is used as a path
47  * to a configuration file that initializes the enviornment. Relative paths
48  * are relative to the current directory (See {@link com.caucho.vfs.Vfs#getPwd()}.
49  * <p/>
50  * Resources defined in system.conf are available to all classes loaded within the jvm.
51  * <pre>
52  * java -Dsystem.conf=tests/system.conf -Djava.system.class.loader=com.caucho.loader.SystemClassLoader ...
53  * </pre>
54  */

55 public class SystemClassLoader
56   extends EnvironmentClassLoader
57   implements EnvironmentBean
58 {
59   private boolean _isInit;
60
61   private URLClassLoader JavaDoc _loader;
62
63   /**
64    * Creates a new SystemClassLoader.
65    */

66   public SystemClassLoader(ClassLoader JavaDoc parent)
67   {
68     super(parent);
69   }
70
71   public ClassLoader JavaDoc getClassLoader()
72   {
73     return this;
74   }
75
76   public void init()
77   {
78     if (_isInit)
79       return;
80
81     _isInit = true;
82
83     initClasspath();
84
85     super.init();
86
87     String JavaDoc systemConf = System.getProperty("system.conf");
88
89     if (systemConf != null) {
90       try {
91         Path path = Vfs.lookup(systemConf);
92
93         Config config = new Config();
94
95         config.configure(this, path, getSchema());
96       }
97       catch (Exception JavaDoc ex) {
98         ex.printStackTrace();
99
100         throw new RuntimeException JavaDoc(ex.toString());
101       }
102     }
103   }
104
105   private void initClasspath()
106   {
107     String JavaDoc classpath = System.getProperty("java.class.path");
108
109     String JavaDoc[] classpathElements = classpath.split(File.pathSeparator, 512);
110
111     for (String JavaDoc classpathElement : classpathElements) {
112       SimpleLoader loader = new SimpleLoader(Vfs.lookup(classpathElement));
113
114       try {
115         loader.init();
116         addLoader(loader);
117       }
118       catch (ConfigException ex) {
119         System.err.println("bad classpath elenent " + classpathElement);
120         ex.printStackTrace();
121       }
122     }
123   }
124
125   protected String JavaDoc getSchema()
126   {
127     return "com/caucho/loader/system.rnc";
128   }
129 }
130
131
132
Popular Tags