KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > DefaultClassResolver


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

15 package org.apache.hivemind.impl;
16
17 import java.net.URL JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ClassResolver;
21
22 /**
23  * Default implementation of {@link org.apache.hivemind.ClassResolver} based around
24  * {@link Thread#getContextClassLoader()} (which is set by the servlet container).
25  *
26  * @author Howard Lewis Ship
27  */

28 public class DefaultClassResolver implements ClassResolver
29 {
30     private ClassLoader JavaDoc _loader;
31
32     /**
33      * Constructs a new instance using {@link Thread#getContextClassLoader()}.
34      */

35
36     public DefaultClassResolver()
37     {
38         this(Thread.currentThread().getContextClassLoader());
39     }
40
41     public DefaultClassResolver(ClassLoader JavaDoc loader)
42     {
43         _loader = loader;
44     }
45
46     public URL JavaDoc getResource(String JavaDoc name)
47     {
48         String JavaDoc stripped = removeLeadingSlash(name);
49
50         URL JavaDoc result = _loader.getResource(stripped);
51
52         return result;
53     }
54
55     private String JavaDoc removeLeadingSlash(String JavaDoc name)
56     {
57         if (name.startsWith("/"))
58             return name.substring(1);
59
60         return name;
61     }
62
63     /**
64      * Invokes {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)}.
65      *
66      * @param type
67      * the complete class name to locate and load; alternately, may be a primitive name
68      * or an array type (primitive or object)
69      * @return The loaded class
70      * @throws ApplicationRuntimeException
71      * if loading the class throws an exception (typically
72      * {@link ClassNotFoundException} or a security exception)
73      * @see JavaTypeUtils
74      */

75
76     public Class JavaDoc findClass(String JavaDoc type)
77     {
78         try
79         {
80             return lookupClass(type);
81         }
82         catch (Throwable JavaDoc t)
83         {
84             throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t),
85                     t);
86         }
87     }
88
89     private Class JavaDoc lookupClass(String JavaDoc type) throws ClassNotFoundException JavaDoc
90     {
91         Class JavaDoc result = JavaTypeUtils.getPrimtiveClass(type);
92
93         if (result != null)
94             return result;
95
96         // This does some magic to handle arrays of primitives or objects in the
97
// format needed by Class.forName().
98

99         String JavaDoc jvmName = JavaTypeUtils.getJVMClassName(type);
100
101         return Class.forName(jvmName, true, _loader);
102     }
103
104     public Class JavaDoc checkForClass(String JavaDoc type)
105     {
106         try
107         {
108             return lookupClass(type);
109         }
110         catch (ClassNotFoundException JavaDoc ex)
111         {
112             return null;
113         }
114         catch (Throwable JavaDoc t)
115         {
116             throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t),
117                     t);
118         }
119
120     }
121
122     public ClassLoader JavaDoc getClassLoader()
123     {
124         return _loader;
125     }
126
127 }
Popular Tags