KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > common > ClassResolver


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2003 USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library 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 library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.common;
28
29 /** The Java API's imports */
30 import java.io.InputStream JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33
34 /**
35  * This class is used to resolve class from the ClassLoader. It contains the jar loaded from the config files.
36  *
37  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
38  * @version 0.1
39  */

40 public class ClassResolver {
41
42     /** The current ClassLoader (Default value : The thread's classLoader) */
43     protected static URLClassLoader JavaDoc current_ =
44         (URLClassLoader JavaDoc) Thread.currentThread().getContextClassLoader();
45
46     /**
47      * Adds the given URLs into the context (ClassLoader)
48      * @param urls The URLs to search for classes and resources
49      */

50     public static void addContext(URL JavaDoc[] urls) {
51         if (urls != null) {
52             URLClassLoader JavaDoc newCL = URLClassLoader.newInstance(urls, current_);
53             current_ = newCL;
54             Thread.currentThread().setContextClassLoader(current_);
55         }
56     }
57
58     /**
59      * Returns the Class object associated with the class or interface with the given string name.
60      * @param className The fully qualified name of the desired class
61      * @return A class object representing the desired class
62      * @exception ClassNotFoundException If the class cannot be located
63      * @see java.lang.ClassLoader
64      */

65     public static Class JavaDoc resolve(String JavaDoc className)
66         throws ClassNotFoundException JavaDoc {
67         return Class.forName(className, true, current_);
68     }
69     
70     /**
71      * Finds the resource with the given name.
72      * @param name The resource name
73      * @return A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
74      */

75     public static URL JavaDoc getResource(String JavaDoc name) {
76         return current_.getResource(name);
77     }
78         
79     /**
80      * Returns an input stream for reading the specified resource.
81      * @param name The resource name
82      * @return An input stream for reading the resource, or null if the resource could not be found
83      */

84     public static InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
85         return current_.getResourceAsStream(name);
86     }
87
88 }
89
Popular Tags