KickJava   Java API By Example, From Geeks To Geeks.

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


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.vfs.Depend;
33 import com.caucho.vfs.Dependency;
34 import com.caucho.vfs.Path;
35
36 import java.security.Permission JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Static utility classes.
41  */

42 public class Environment {
43   private static ArrayList JavaDoc<EnvironmentListener> _globalEnvironmentListeners =
44     new ArrayList JavaDoc<EnvironmentListener>();
45   
46   private static ArrayList JavaDoc<ClassLoaderListener> _globalLoaderListeners =
47     new ArrayList JavaDoc<ClassLoaderListener>();
48   
49   /**
50    * Returns the local environment.
51    */

52   public static EnvironmentClassLoader getEnvironmentClassLoader()
53   {
54     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
55     
56     for (; loader != null; loader = loader.getParent()) {
57       if (loader instanceof EnvironmentClassLoader)
58         return (EnvironmentClassLoader) loader;
59     }
60
61     return null;
62   }
63   
64   /**
65    * Add listener.
66    *
67    * @param listener object to listen for environment start/stop
68    */

69   public static void addEnvironmentListener(EnvironmentListener listener)
70   {
71     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
72
73     addEnvironmentListener(listener, loader);
74   }
75   
76   /**
77    * Add listener.
78    *
79    * @param listener object to listen for environment create/destroy
80    * @param loader the context class loader
81    */

82   public static void addEnvironmentListener(EnvironmentListener listener,
83                         ClassLoader JavaDoc loader)
84   {
85     for (; loader != null; loader = loader.getParent()) {
86       if (loader instanceof EnvironmentClassLoader) {
87         ((EnvironmentClassLoader) loader).addListener(listener);
88         return;
89       }
90     }
91
92     _globalEnvironmentListeners.add(listener);
93   }
94   
95   /**
96    * Remove listener.
97    *
98    * @param listener object to listen for environment start/stop
99    */

100   public static void removeEnvironmentListener(EnvironmentListener listener)
101   {
102     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
103
104     removeEnvironmentListener(listener, loader);
105   }
106   
107   /**
108    * Remove listener.
109    *
110    * @param listener object to listen for environment create/destroy
111    * @param loader the context class loader
112    */

113   public static void removeEnvironmentListener(EnvironmentListener listener,
114                         ClassLoader JavaDoc loader)
115   {
116     for (; loader != null; loader = loader.getParent()) {
117       if (loader instanceof EnvironmentClassLoader) {
118         ((EnvironmentClassLoader) loader).removeListener(listener);
119         return;
120       }
121     }
122
123     _globalEnvironmentListeners.remove(listener);
124   }
125   
126   /**
127    * Add listener.
128    *
129    * @param listener object to listen for environment create/destroy
130    * @param loader the context class loader
131    */

132   public static void addChildEnvironmentListener(EnvironmentListener listener)
133   {
134     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
135     
136     addChildEnvironmentListener(listener, loader);
137   }
138   
139   /**
140    * Add listener.
141    *
142    * @param listener object to listen for environment create/destroy
143    * @param loader the context class loader
144    */

145   public static void addChildEnvironmentListener(EnvironmentListener listener,
146                          ClassLoader JavaDoc loader)
147   {
148     for (; loader != null; loader = loader.getParent()) {
149       if (loader instanceof EnvironmentClassLoader) {
150         ((EnvironmentClassLoader) loader).addChildListener(listener);
151         return;
152       }
153     }
154   }
155   
156   /**
157    * Add listener.
158    *
159    * @param listener object to listen for environment create/destroy
160    */

161   public static void addClassLoaderListener(ClassLoaderListener listener)
162   {
163     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
164
165     addClassLoaderListener(listener, loader);
166   }
167   
168   /**
169    * Add listener.
170    *
171    * @param listener object to listen for environment create/destroy
172    * @param loader the context class loader
173    */

174   public static void addClassLoaderListener(ClassLoaderListener listener,
175                                             ClassLoader JavaDoc loader)
176   {
177     for (; loader != null; loader = loader.getParent()) {
178       if (loader instanceof EnvironmentClassLoader) {
179         ((EnvironmentClassLoader) loader).addListener(listener);
180         return;
181       }
182     }
183
184     _globalLoaderListeners.add(listener);
185   }
186   
187   /**
188    * Add close listener.
189    *
190    * @param listener object to listen for environment create/destroy
191    * @param loader the context class loader
192    */

193   public static void addCloseListener(Object JavaDoc obj)
194   {
195     addClassLoaderListener(new CloseListener(obj));
196   }
197
198   /**
199    * Starts the current environment.
200    */

201   public static void init()
202   {
203     init(Thread.currentThread().getContextClassLoader());
204   }
205
206   /**
207    * Starts the current environment.
208    */

209   public static void init(ClassLoader JavaDoc loader)
210   {
211     for (; loader != null; loader = loader.getParent()) {
212       if (loader instanceof EnvironmentClassLoader) {
213         ((EnvironmentClassLoader) loader).init();
214         return;
215       }
216     }
217
218     for (int i = 0; i < _globalLoaderListeners.size(); i++) {
219       ClassLoaderListener listener = _globalLoaderListeners.get(i);
220
221       listener.classLoaderInit(null);
222     }
223   }
224
225   /**
226    * Starts the current environment.
227    */

228   public static void start()
229     throws Throwable JavaDoc
230   {
231     start(Thread.currentThread().getContextClassLoader());
232   }
233
234   /**
235    * Starts the current environment.
236    */

237   public static void start(ClassLoader JavaDoc loader)
238     throws Throwable JavaDoc
239   {
240     for (; loader != null; loader = loader.getParent()) {
241       if (loader instanceof EnvironmentClassLoader) {
242         ((EnvironmentClassLoader) loader).start();
243         return;
244       }
245     }
246
247     init(loader);
248     
249     for (int i = 0; i < _globalEnvironmentListeners.size(); i++) {
250       EnvironmentListener listener = _globalEnvironmentListeners.get(i);
251
252       listener.environmentStart(null);
253     }
254   }
255
256   /**
257    * Starts the current environment.
258    */

259   public static void stop()
260   {
261     stop(Thread.currentThread().getContextClassLoader());
262   }
263
264   /**
265    * Starts the current environment.
266    */

267   public static void stop(ClassLoader JavaDoc loader)
268   {
269     for (; loader != null; loader = loader.getParent()) {
270       if (loader instanceof EnvironmentClassLoader) {
271         ((EnvironmentClassLoader) loader).stop();
272         return;
273       }
274     }
275
276     ArrayList JavaDoc<EnvironmentListener> listeners;
277     listeners = new ArrayList JavaDoc<EnvironmentListener>();
278     listeners.addAll(_globalEnvironmentListeners);
279     _globalEnvironmentListeners.clear();
280     
281     for (int i = 0; i < listeners.size(); i++) {
282       EnvironmentListener listener = listeners.get(i);
283
284       listener.environmentStop(null);
285     }
286   }
287   
288   /**
289    * Adds a dependency to the current environment.
290    *
291    * @param depend the dependency to add
292    */

293   public static void addDependency(Dependency depend)
294   {
295     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
296
297     addDependency(depend, loader);
298   }
299
300   /**
301    * Adds a dependency to the current environment.
302    *
303    * @param depend the dependency to add
304    * @param loader the context loader
305    */

306   public static void addDependency(Dependency depend,
307                                    ClassLoader JavaDoc loader)
308   {
309     for (; loader != null; loader = loader.getParent()) {
310       if (loader instanceof EnvironmentClassLoader) {
311         ((EnvironmentClassLoader) loader).addDependency(depend);
312         return;
313       }
314     }
315   }
316
317   /**
318    * Returns the topmost dynamic class loader.
319    */

320   public static DynamicClassLoader getDynamicClassLoader()
321   {
322     Thread JavaDoc thread = Thread.currentThread();
323     
324     return getDynamicClassLoader(thread.getContextClassLoader());
325   }
326
327   /**
328    * Returns the topmost dynamic class loader.
329    *
330    * @param loader the context loader
331    */

332   public static DynamicClassLoader getDynamicClassLoader(ClassLoader JavaDoc loader)
333   {
334     for (; loader != null; loader = loader.getParent()) {
335       if (loader instanceof DynamicClassLoader) {
336         return (DynamicClassLoader) loader;
337       }
338     }
339
340     return null;
341   }
342   
343   /**
344    * Adds a dependency to the current environment.
345    *
346    * @param depend the dependency to add
347    */

348   public static void addDependency(Path path)
349   {
350     addDependency(new Depend(path));
351   }
352
353   /**
354    * Adds a dependency to the current environment.
355    *
356    * @param path the dependency to add
357    * @param loader the context loader
358    */

359   public static void addDependency(Path path, ClassLoader JavaDoc loader)
360   {
361     addDependency(new Depend(path), loader);
362   }
363
364   /**
365    * Gets a local variable for the current environment.
366    *
367    * @param name the attribute name
368    *
369    * @return the attribute value
370    */

371   public static Object JavaDoc getAttribute(String JavaDoc name)
372   {
373     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
374
375     return getAttribute(name, loader);
376   }
377
378   /**
379    * Returns the current dependency check interval.
380    */

381   public static long getDependencyCheckInterval()
382   {
383     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
384     
385     for (; loader != null; loader = loader.getParent()) {
386       if (loader instanceof DynamicClassLoader)
387         return ((DynamicClassLoader) loader).getDependencyCheckInterval();
388     }
389
390     return DynamicClassLoader.getGlobalDependencyCheckInterval();
391   }
392
393   /**
394    * Returns the current dependency check interval.
395    */

396   public static long getDependencyCheckInterval(ClassLoader JavaDoc loader)
397   {
398     for (; loader != null; loader = loader.getParent()) {
399       if (loader instanceof DynamicClassLoader)
400         return ((DynamicClassLoader) loader).getDependencyCheckInterval();
401     }
402
403     return DynamicClassLoader.getGlobalDependencyCheckInterval();
404   }
405
406   /**
407    * Gets a local variable for the current environment.
408    *
409    * @param name the attribute name
410    * @param loader the context loader
411    *
412    * @return the attribute value
413    */

414   public static Object JavaDoc getAttribute(String JavaDoc name, ClassLoader JavaDoc loader)
415   {
416     for (; loader != null; loader = loader.getParent()) {
417       if (loader instanceof EnvironmentClassLoader) {
418         Object JavaDoc value = ((EnvironmentClassLoader) loader).getAttribute(name);
419
420         if (value != null)
421           return value;
422       }
423     }
424
425     return null;
426   }
427
428   /**
429    * Gets a local variable for the current environment.
430    *
431    * @param name the attribute name
432    *
433    * @return the attribute value
434    */

435   public static Object JavaDoc getLevelAttribute(String JavaDoc name)
436   {
437     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
438
439     return getLevelAttribute(name, loader);
440   }
441
442   /**
443    * Gets a local variable for the current environment.
444    *
445    * @param name the attribute name
446    * @param loader the context loader
447    *
448    * @return the attribute value
449    */

450   public static Object JavaDoc getLevelAttribute(String JavaDoc name, ClassLoader JavaDoc loader)
451   {
452     for (; loader != null; loader = loader.getParent()) {
453       if (loader instanceof EnvironmentClassLoader) {
454         return ((EnvironmentClassLoader) loader).getAttribute(name);
455       }
456     }
457
458     return null;
459   }
460
461   /**
462    * Sets a local variable for the current environment.
463    *
464    * @param name the attribute name
465    * @param value the new attribute value
466    *
467    * @return the old attribute value
468    */

469   public static Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value)
470   {
471     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
472
473     return setAttribute(name, value, loader);
474   }
475
476   /**
477    * Sets a local variable for the current environment.
478    *
479    * @param name the attribute name
480    * @param value the new attribute value
481    * @param loader the context loader
482    *
483    * @return the old attribute value
484    */

485   public static Object JavaDoc setAttribute(String JavaDoc name,
486                                     Object JavaDoc value,
487                                     ClassLoader JavaDoc loader)
488   {
489     for (; loader != null; loader = loader.getParent()) {
490       if (loader instanceof EnvironmentClassLoader) {
491         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
492
493         Object JavaDoc oldValue = envLoader.getAttribute(name);
494
495         envLoader.setAttribute(name, value);
496         
497         if (oldValue != null)
498           return oldValue;
499       }
500     }
501
502     return null;
503   }
504
505   /**
506    * Adds a permission to the current environment.
507    *
508    * @param perm the permission to add.
509    *
510    * @return the old attribute value
511    */

512   public static void addPermission(Permission JavaDoc perm)
513   {
514     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
515     
516     addPermission(perm, loader);
517   }
518
519   /**
520    * Adds a permission to the current environment.
521    *
522    * @param perm the permission to add.
523    *
524    * @return the old attribute value
525    */

526   public static void addPermission(Permission JavaDoc perm, ClassLoader JavaDoc loader)
527   {
528     for (; loader != null; loader = loader.getParent()) {
529       if (loader instanceof EnvironmentClassLoader) {
530         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
531
532     envLoader.addPermission(perm);
533       }
534     }
535   }
536
537   /**
538    * Gets the class loader owner.
539    */

540   public static Object JavaDoc getOwner()
541   {
542     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
543
544     return getOwner(loader);
545   }
546
547   /**
548    * Gets the class loader owner.
549    */

550   public static Object JavaDoc getOwner(ClassLoader JavaDoc loader)
551   {
552     for (; loader != null; loader = loader.getParent()) {
553       if (loader instanceof EnvironmentClassLoader) {
554     EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
555
556     Object JavaDoc owner = envLoader.getOwner();
557     
558     if (owner != null)
559       return owner;
560       }
561     }
562
563     return null;
564   }
565
566   /**
567    * Sets a configuration exception.
568    */

569   public static void setConfigException(Throwable JavaDoc e)
570   {
571     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
572     
573     for (; loader != null; loader = loader.getParent()) {
574       if (loader instanceof EnvironmentClassLoader) {
575     EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
576
577     envLoader.setConfigException(e);
578
579     return;
580       }
581     }
582   }
583
584   /**
585    * Returns any configuration exception.
586    */

587   public static Throwable JavaDoc getConfigException()
588   {
589     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
590     
591     for (; loader != null; loader = loader.getParent()) {
592       if (loader instanceof EnvironmentClassLoader) {
593     EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
594
595     if (envLoader.getConfigException() != null)
596       return envLoader.getConfigException();
597       }
598     }
599
600     return null;
601   }
602
603   /**
604    * destroys the current environment.
605    */

606   public static void closeGlobal()
607   {
608     ArrayList JavaDoc<ClassLoaderListener> listeners;
609     listeners = new ArrayList JavaDoc<ClassLoaderListener>();
610     listeners.addAll(_globalLoaderListeners);
611     _globalLoaderListeners.clear();
612     
613     for (int i = 0; i < listeners.size(); i++) {
614       ClassLoaderListener listener = listeners.get(i);
615
616       listener.classLoaderDestroy(null);
617     }
618   }
619 }
620
Popular Tags