KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > util > ClassLoader


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.util;
18
19
20 /** <p>Helper class for working with class loaders.</p>
21  *
22  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
23  * @version $Id: ClassLoader.java,v 1.4 2004/07/27 11:19:25 jochen Exp $
24  */

25 public class ClassLoader {
26   /** <p>Loads a class with the given name. First attempts to use
27    * the context class loader, then its own class loader.</p>
28    *
29    * @param pName The fully qualified name of the class being loaded.
30    * @throws ClassNotFoundException Loading the class failed.
31    * @return The class with the name <code>pName</code>.
32    */

33   public static Class JavaDoc getClass(String JavaDoc pName) throws ClassNotFoundException JavaDoc {
34     // First try: The default ClassLoader
35
try {
36       return Class.forName(pName);
37     } catch (ClassNotFoundException JavaDoc e) {
38       // Second try: The threads context ClassLoader
39
try {
40         java.lang.ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
41         if (cl == null) {
42           throw new ClassNotFoundException JavaDoc(pName);
43         }
44         return cl.loadClass(pName);
45       } catch (ClassNotFoundException JavaDoc f) {
46         throw e;
47       }
48     }
49   }
50
51   /** <p>Loads a class with the given name using <code>getClass(String)</code>.
52    * If an instance of the returned class cannot be assigned to the
53    * class or interface <code>pAssignableTo</code>, throws an
54    * IllegalArgumentException.</p>
55    *
56    * @throws ClassNotFoundException The class with the name <code>pName</code>
57    * could not be loaded.
58    * @throws IllegalArgumentException Instances of the class with the name
59    * <code>pName</code> are not assignable to the interface or class
60    * <code>pAssignableTo</code>.
61    */

62   public static Class JavaDoc getClass(String JavaDoc pName, Class JavaDoc pAssignableTo)
63       throws ClassNotFoundException JavaDoc {
64     Class JavaDoc result = getClass(pName);
65     if (pAssignableTo != null && !pAssignableTo.isAssignableFrom(result)) {
66       throw new IllegalArgumentException JavaDoc("The class " + result.getName() +
67                                           " is not implementing or extending " +
68                                           pAssignableTo.getName());
69     }
70     return result;
71   }
72 }
73
Popular Tags