KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > loader > Module


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.tomcat.util.loader;
19
20
21 import java.io.File JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.net.URL JavaDoc;
25
26 // Based on org.apache.catalina.Loader - removed most of the catalina-specific
27

28 /**
29  * Base representation for "server extensions" ( connectors, realms, etc ), webapps,
30  * libraries.
31  *
32  *
33  * @author Costin Manolache
34  *
35  * @author Craig R. McClanahan
36  * @author Remy Maucherat
37  */

38 public class Module implements Serializable JavaDoc {
39
40     // ----------------------------------------------------------- Constructors
41

42
43     /**
44      *
45      */

46     public Module() {
47     }
48
49
50     // ----------------------------------------------------- Instance Variables
51

52     private static final boolean DEBUG=false; //LoaderProperties.getProperty("loader.Module.debug") != null;
53

54     /**
55      * The class loader being managed by this Loader component.
56      */

57     private transient ModuleClassLoader classLoader = null;
58
59     /**
60      * The "follow standard delegation model" flag that will be used to
61      * configure our ClassLoader.
62      */

63     private boolean delegate = false;
64
65     private Class JavaDoc classLoaderClass;
66
67     /**
68      * The Java class name of the ClassLoader implementation to be used.
69      * This class should extend ModuleClassLoader, otherwise, a different
70      * loader implementation must be used.
71      */

72     private String JavaDoc loaderClass = null;
73 // "org.apache.catalina.loader.WebappClassLoader";
74

75     /**
76      * The parent class loader of the class loader we will create.
77      * Use Repository if the parent is also a repository, otherwise set
78      * the ClassLoader
79      */

80     private transient ClassLoader JavaDoc parentClassLoader = null;
81     private Repository parent;
82
83     private Repository repository;
84
85     /**
86      * The set of repositories associated with this class loader.
87      */

88     private String JavaDoc repositories[] = new String JavaDoc[0];
89     private URL JavaDoc classpath[] ;
90
91     private File JavaDoc workDir;
92
93     /**
94      * Has this component been started?
95      */

96     private boolean started = false;
97
98     boolean hasIndex=false;
99
100     // ------------------------------------------------------------- Properties
101

102
103     /**
104      * Return the Java class loader to be used by this Container.
105      */

106     public ClassLoader JavaDoc getClassLoader() {
107         return classLoader;
108     }
109
110
111     /**
112      * Return the "follow standard delegation model" flag used to configure
113      * our ClassLoader.
114      */

115     public boolean getDelegate() {
116         return (this.delegate);
117     }
118
119
120     /**
121      * Set the "follow standard delegation model" flag used to configure
122      * our ClassLoader.
123      *
124      * @param delegate The new flag
125      */

126     public void setDelegate(boolean delegate) {
127         boolean oldDelegate = this.delegate;
128         this.delegate = delegate;
129         if( classLoader != null ) classLoader.setDelegate(delegate);
130     }
131
132
133     // --------------------------------------------------------- Public Methods
134

135     /**
136      * Has the internal repository associated with this Loader been modified,
137      * such that the loaded classes should be reloaded?
138      */

139     public boolean modified() {
140         return (classLoader.modified());
141     }
142     
143     public boolean isStarted() {
144         return started;
145     }
146
147     public String JavaDoc getClasspathString() {
148         if(classpath==null ) {
149             return null;
150         }
151         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
152         for( int i=0; i<classpath.length; i++ ) {
153             if( i>0 ) sb.append(":");
154             sb.append( classpath[i].getFile() );
155         }
156         return sb.toString();
157     }
158     
159     /**
160      * Start this component, initializing our associated class loader.
161      *
162      * @exception LifecycleException if a lifecycle error occurs
163      */

164     public void start() {
165         // Validate and update our current component state
166
if (started)
167             throw new RuntimeException JavaDoc
168                 ("Already started");
169         started = true;
170
171         // Construct a class loader based on our current repositories list
172
try {
173
174             classLoader = createClassLoader();
175
176             //classLoader.setResources(container.getResources());
177
classLoader.setDelegate(this.delegate);
178
179             for (int i = 0; i < repositories.length; i++) {
180                 classLoader.addRepository(repositories[i]);
181             }
182
183             classLoader.start();
184
185             getRepository().getLoader().notifyModuleStart(this);
186
187         } catch (Throwable JavaDoc t) {
188             log( "LifecycleException ", t );
189             throw new RuntimeException JavaDoc("start: ", t);
190         }
191
192     }
193
194
195     /**
196      * Stop this component, finalizing our associated class loader.
197      *
198      * @exception LifecycleException if a lifecycle error occurs
199      */

200     public void stop() {
201         if (!started)
202             throw new RuntimeException JavaDoc("stop: started=false");
203         
204         //if (DEBUG)
205
log("stop()", null);
206         
207         getRepository().getLoader().notifyModuleStop(this);
208         
209         started = false;
210
211         // unregister this classloader in the server group
212
if( repository != null ) repository.removeClassLoader(classLoader);
213
214         // Throw away our current class loader
215
classLoader.stop();
216
217         classLoader = null;
218
219     }
220
221     // ------------------------------------------------------- Private Methods
222

223     /** Set the class used to construct the class loader.
224      *
225      * The alternative is to set the context class loader to allow loaderClass
226      * to be created.
227      *
228      * @param c
229      */

230     public void setClassLoaderClass( Class JavaDoc c ) {
231         classLoaderClass=c;
232     }
233
234     /**
235      * Create associated classLoader.
236      */

237     ModuleClassLoader createClassLoader()
238         throws Exception JavaDoc
239     {
240
241         if( classLoader != null ) return classLoader;
242         if( classLoaderClass==null && loaderClass!=null) {
243             classLoaderClass = Class.forName(loaderClass);
244         }
245         
246         ModuleClassLoader classLoader = null;
247
248         if (parentClassLoader == null) {
249             if( parent != null ) {
250                 parentClassLoader = parent.getClassLoader();
251             }
252         }
253         if (parentClassLoader == null) {
254             parentClassLoader = Thread.currentThread().getContextClassLoader();
255         }
256         
257         if( classLoaderClass != null ) {
258             Class JavaDoc[] argTypes = { URL JavaDoc[].class, ClassLoader JavaDoc.class };
259             Object JavaDoc[] args = { classpath, parentClassLoader };
260             Constructor JavaDoc constr = classLoaderClass.getConstructor(argTypes);
261             classLoader = (ModuleClassLoader) constr.newInstance(args);
262         } else {
263             classLoader=new ModuleClassLoader( classpath, parentClassLoader);
264         }
265         
266         classLoader.setModule(this);
267         classLoader.setDelegate( delegate );
268         
269         classLoader.start();
270         repository.addClassLoader(classLoader);
271         
272         return classLoader;
273     }
274
275
276     /**
277      * @param parent
278      */

279     public void setParent(Repository parent) {
280         this.parent=parent;
281     }
282
283     /**
284      * @param array
285      */

286     public void setClasspath(URL JavaDoc[] array) {
287         this.classpath=array;
288     }
289
290
291     /**
292      * @param lg
293      */

294     public void setRepository(Repository lg) {
295         this.repository=lg;
296     }
297
298     /**
299      * Return a String representation of this component.
300      */

301     public String JavaDoc toString() {
302
303         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ModuleLoader[");
304         sb.append(getClasspathString());
305         sb.append("]");
306         return (sb.toString());
307
308     }
309
310     private void log( String JavaDoc s ) {
311         log(s,null);
312     }
313     
314     private void log(String JavaDoc s, Throwable JavaDoc t ) {
315         System.err.println("Module: " + s );
316         if( t!=null)
317             t.printStackTrace();
318     }
319
320
321     /**
322      * @return
323      */

324     public Repository getRepository() {
325         return repository;
326     }
327
328
329     /**
330      * @return
331      */

332     public String JavaDoc getName() {
333         return classpath[0].getFile(); // this.toString();
334
}
335
336
337 }
338
Popular Tags