KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > BasicLoaderRepository


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mx.loading;
23
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Implements a simple classloader repository for the MBean server. The basic
31  * loader repository uses an unordered list of classloaders to try and load
32  * the required class. There is no attempt made to resolve conflicts between
33  * classes loaded by different classloaders. <p>
34  *
35  * A thread's context class loader is always searched first. Context class loader
36  * is not required to be registered to the repository.
37  *
38  * @see org.jboss.mx.loading.LoaderRepository
39  *
40  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
41  * @version $Revision: 37459 $
42  */

43 public class BasicLoaderRepository
44    extends LoaderRepository
45 {
46
47    // Public --------------------------------------------------------
48

49    /**
50     * Loads a class from the repository. This method attempts to load the class
51     * using all the classloader registered to the repository.
52     *
53     * @param className the class to load
54     * @return the found class
55     * @exception ClassNotFoundException when there is no such class
56     */

57    public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc
58    {
59       return loadClassWithout(null, className);
60    }
61
62    /**
63     * Loads a class from the repository, excluding the given
64     * classloader.
65     *
66     * @param skipLoader the classloader to exclude
67     * @param className the class to load
68     * @return the found class
69     * @exception ClassNotFoundException when there is no such class
70     */

71    public Class JavaDoc loadClassWithout(ClassLoader JavaDoc skipLoader, String JavaDoc className) throws ClassNotFoundException JavaDoc
72    {
73       // try ctx cl first
74
ClassLoader JavaDoc ctxLoader = Thread.currentThread().getContextClassLoader();
75       if (ctxLoader != skipLoader)
76       {
77          try
78          {
79             return ctxLoader.loadClass(className);
80          }
81          catch (ClassNotFoundException JavaDoc e)
82          {
83             // ignore and move on to the loader list
84
}
85       }
86
87       Iterator JavaDoc it = loaders.iterator();
88       while (it.hasNext())
89       {
90          ClassLoader JavaDoc cl = (ClassLoader JavaDoc) it.next();
91          if (cl != skipLoader)
92          {
93             try
94             {
95                return cl.loadClass(className);
96             }
97             catch (ClassNotFoundException JavaDoc ignored)
98             {
99                // go on and try the next loader
100
}
101          }
102       }
103
104       // at last try a native
105
Class JavaDoc clazz = getNativeClassForName(className);
106       if (clazz != null) return clazz;
107
108       throw new ClassNotFoundException JavaDoc(className);
109    }
110
111    /**
112     * Loads a class from the repository, using the classloaders that were
113     * registered before the given classloader.
114     *
115     * @param stop consult all the classloaders registered before this one
116     * in an attempt to load a class
117     * @param className name of the class to load
118     *
119     * @return loaded class instance
120     *
121     * @throws ClassNotFoundException if none of the consulted classloaders were
122     * able to load the requested class
123     */

124    public Class JavaDoc loadClassBefore(ClassLoader JavaDoc stop, String JavaDoc className) throws ClassNotFoundException JavaDoc
125    {
126       Iterator JavaDoc it = loaders.iterator();
127       while (it.hasNext())
128       {
129          ClassLoader JavaDoc cl = (ClassLoader JavaDoc) it.next();
130          if (cl == stop)
131             break;
132
133          try
134          {
135             return cl.loadClass(className);
136          }
137          catch (ClassNotFoundException JavaDoc ignored)
138          {
139             // go on and try the next loader
140
}
141       }
142
143       // at last try a native
144
Class JavaDoc clazz = getNativeClassForName(className);
145       if (clazz != null) return clazz;
146
147       throw new ClassNotFoundException JavaDoc(className);
148    }
149
150    public void addClassLoader(ClassLoader JavaDoc cl)
151    {
152       loaders.add(cl);
153    }
154
155    public boolean addClassLoaderURL(ClassLoader JavaDoc cl, URL JavaDoc url)
156    {
157       // This is a noop here
158
return false;
159    }
160
161    public void removeClassLoader(ClassLoader JavaDoc cl)
162    {
163       loaders.remove(cl);
164    }
165
166    public RepositoryClassLoader newClassLoader(final URL JavaDoc url, boolean addToRepository)
167       throws Exception JavaDoc
168    {
169       UnifiedClassLoader ucl = new UnifiedClassLoader(url);
170       if( addToRepository )
171          this.addClassLoader(ucl);
172       return ucl;
173    }
174    public RepositoryClassLoader newClassLoader(final URL JavaDoc url, final URL JavaDoc origURL, boolean addToRepository)
175       throws Exception JavaDoc
176    {
177       UnifiedClassLoader ucl = new UnifiedClassLoader(url, origURL);
178       if( addToRepository )
179          this.addClassLoader(ucl);
180       return ucl;
181    }
182    public Class JavaDoc loadClass(String JavaDoc name, boolean resolve, ClassLoader JavaDoc cl)
183       throws ClassNotFoundException JavaDoc
184    {
185       throw new ClassNotFoundException JavaDoc("loadClass(String,boolean,ClassLoader) not supported");
186    }
187    public URL JavaDoc getResource(String JavaDoc name, ClassLoader JavaDoc cl)
188    {
189       URL JavaDoc res = null;
190       if( cl instanceof UnifiedClassLoader )
191       {
192          UnifiedClassLoader ucl = (UnifiedClassLoader) cl;
193          res = ucl.getResourceLocally(name);
194       }
195       else
196       {
197          res = cl.getResource(name);
198       }
199       return res;
200    }
201    public void getResources(String JavaDoc name, ClassLoader JavaDoc cl, List JavaDoc urls)
202    {
203       Enumeration JavaDoc resURLs = null;
204       try
205       {
206          if( cl instanceof UnifiedClassLoader )
207          {
208             UnifiedClassLoader ucl = (UnifiedClassLoader) cl;
209             resURLs = ucl.findResourcesLocally(name);
210          }
211          else
212          {
213             resURLs = cl.getResources(name);
214          }
215          while( resURLs.hasMoreElements() )
216             urls.add(resURLs.nextElement());
217       }
218       catch(Exception JavaDoc e)
219       {
220       }
221    }
222 }
223
Popular Tags