|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultClassResolver.java | 100% | 95.2% | 100% | 97% |
|
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;
|
|
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 _loader;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Constructs a new instance using {@link Thread#getContextClassLoader()}.
|
|
34 |
*/
|
|
35 |
|
|
36 | 547 |
public DefaultClassResolver()
|
37 |
{ |
|
38 | 547 |
this(Thread.currentThread().getContextClassLoader());
|
39 |
} |
|
40 |
|
|
41 | 548 |
public DefaultClassResolver(ClassLoader loader)
|
42 |
{ |
|
43 | 548 |
_loader = loader; |
44 |
} |
|
45 |
|
|
46 | 20 |
public URL getResource(String name)
|
47 |
{ |
|
48 | 20 |
String stripped = removeLeadingSlash(name); |
49 |
|
|
50 | 20 |
URL result = _loader.getResource(stripped); |
51 |
|
|
52 | 20 |
return result;
|
53 |
} |
|
54 |
|
|
55 | 20 |
private String removeLeadingSlash(String name)
|
56 |
{ |
|
57 | 20 |
if (name.startsWith("/")) |
58 | 14 |
return name.substring(1);
|
59 |
|
|
60 | 6 |
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 | 15 |
public Class findClass(String type)
|
77 |
{ |
|
78 | 15 |
try
|
79 |
{ |
|
80 | 15 |
return lookupClass(type);
|
81 |
} |
|
82 |
catch (Throwable t)
|
|
83 |
{ |
|
84 | 4 |
throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t), |
85 |
t); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 | 15823 |
private Class lookupClass(String type) throws ClassNotFoundException |
90 |
{ |
|
91 | 15823 |
Class result = JavaTypeUtils.getPrimtiveClass(type); |
92 |
|
|
93 | 15823 |
if (result != null) |
94 | 1 |
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 | 15822 |
String jvmName = JavaTypeUtils.getJVMClassName(type); |
100 |
|
|
101 | 15822 |
return Class.forName(jvmName, true, _loader); |
102 |
} |
|
103 |
|
|
104 | 15808 |
public Class checkForClass(String type)
|
105 |
{ |
|
106 | 15808 |
try
|
107 |
{ |
|
108 | 15808 |
return lookupClass(type);
|
109 |
} |
|
110 |
catch (ClassNotFoundException ex)
|
|
111 |
{ |
|
112 | 7201 |
return null; |
113 |
} |
|
114 |
catch (Throwable t)
|
|
115 |
{ |
|
116 | 0 |
throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t), |
117 |
t); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 | 113 |
public ClassLoader getClassLoader()
|
123 |
{ |
|
124 | 113 |
return _loader;
|
125 |
} |
|
126 |
|
|
127 |
} |
|