KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > ClassLoaderRepositoryImpl


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.management.loading.ClassLoaderRepository JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * Resin implementation for a class loader repository.
40  */

41 public class ClassLoaderRepositoryImpl implements ClassLoaderRepository JavaDoc {
42   private static final L10N L = new L10N(ClassLoaderRepositoryImpl.class);
43   private static final Logger JavaDoc log = Log.open(ClassLoaderRepositoryImpl.class);
44
45   private ArrayList JavaDoc<ClassLoader JavaDoc> _loaders = new ArrayList JavaDoc<ClassLoader JavaDoc>();
46   
47   /**
48    * Adds a class loader to the repository.
49    */

50   void addClassLoader(ClassLoader JavaDoc loader)
51   {
52     _loaders.add(loader);
53   }
54   
55   /**
56    * Loads a class from the repository
57    */

58   public Class JavaDoc loadClass(String JavaDoc className)
59     throws ClassNotFoundException JavaDoc
60   {
61     for (int i = 0; i < _loaders.size(); i++) {
62       ClassLoader JavaDoc loader = _loaders.get(i);
63
64       try {
65     Class JavaDoc cl = loader.loadClass(className);
66
67     if (cl != null)
68       return cl;
69       } catch (ClassNotFoundException JavaDoc e) {
70       }
71     }
72
73     throw new ClassNotFoundException JavaDoc(L.l("can't load class {0}",
74                      className));
75   }
76   
77   /**
78    * Loads a class from the repository, stopping at a given one
79    */

80   public Class JavaDoc loadClassBefore(ClassLoader JavaDoc stop, String JavaDoc className)
81     throws ClassNotFoundException JavaDoc
82   {
83     for (int i = 0; i < _loaders.size(); i++) {
84       ClassLoader JavaDoc loader = _loaders.get(i);
85
86       if (loader == stop)
87     break;
88
89       try {
90     Class JavaDoc cl = loader.loadClass(className);
91
92     if (cl != null)
93       return cl;
94       } catch (ClassNotFoundException JavaDoc e) {
95       }
96     }
97
98     throw new ClassNotFoundException JavaDoc(L.l("can't load class {0}",
99                      className));
100   }
101   
102   /**
103    * Loads a class from the repository, excluding a given one
104    */

105   public Class JavaDoc loadClassWithout(ClassLoader JavaDoc exclude, String JavaDoc className)
106     throws ClassNotFoundException JavaDoc
107   {
108     for (int i = 0; i < _loaders.size(); i++) {
109       ClassLoader JavaDoc loader = _loaders.get(i);
110
111       if (loader == exclude)
112     continue;
113
114       try {
115     Class JavaDoc cl = loader.loadClass(className);
116
117     if (cl != null)
118       return cl;
119       } catch (ClassNotFoundException JavaDoc e) {
120       }
121     }
122
123     throw new ClassNotFoundException JavaDoc(L.l("can't load class {0}",
124                      className));
125   }
126 }
127
Popular Tags