KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > core > common > lib > ClassResolver


1 /*===========================================================================
2
3 ObjectWeb Explorer Framework
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@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 $Id: ClassResolver.java,v 1.2 2005/07/06 15:36:00 moroy Exp $
27 ===========================================================================*/

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

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

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

70     public static Class JavaDoc resolve(String JavaDoc className)
71         throws ClassNotFoundException JavaDoc {
72         return Class.forName(className, true, current_);
73     }
74     
75     /**
76      * Finds the resource with the given name.
77      * @param name The resource name
78      * @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.
79      */

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

89     public static InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
90         return current_.getResourceAsStream(name);
91     }
92
93 }
94
Popular Tags