|
|||||||||||||||||||
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 | 80% | 91.3% | 100% | 89.7% |
|
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.commons.logging.Log;
|
|
20 |
import org.apache.commons.logging.LogFactory;
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.ClassResolver;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Default implementation of {@link org.apache.hivemind.ClassResolver}based around
|
|
26 |
* {@link Thread#getContextClassLoader()}(which is set by the servlet container).
|
|
27 |
*
|
|
28 |
* @author Howard Lewis Ship
|
|
29 |
*/
|
|
30 |
public class DefaultClassResolver implements ClassResolver |
|
31 |
{ |
|
32 |
private static final Log LOG = LogFactory.getLog(DefaultClassResolver.class); |
|
33 |
|
|
34 |
private ClassLoader _loader;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Constructs a new instance using {@link Thread#getContextClassLoader()}.
|
|
38 |
*/
|
|
39 |
|
|
40 | 538 |
public DefaultClassResolver()
|
41 |
{ |
|
42 | 538 |
this(Thread.currentThread().getContextClassLoader());
|
43 |
} |
|
44 |
|
|
45 | 539 |
public DefaultClassResolver(ClassLoader loader)
|
46 |
{ |
|
47 | 539 |
_loader = loader; |
48 |
} |
|
49 |
|
|
50 | 13 |
public URL getResource(String name)
|
51 |
{ |
|
52 | 13 |
boolean debug = LOG.isDebugEnabled();
|
53 |
|
|
54 | 13 |
if (debug)
|
55 | 5 |
LOG.debug("getResource(" + name + ")"); |
56 |
|
|
57 | 13 |
String stripped = removeLeadingSlash(name); |
58 |
|
|
59 | 13 |
URL result = _loader.getResource(stripped); |
60 |
|
|
61 | 13 |
if (debug)
|
62 |
{ |
|
63 | 5 |
if (result == null) |
64 | 3 |
LOG.debug("Not found.");
|
65 |
else
|
|
66 | 2 |
LOG.debug("Found as " + result);
|
67 |
} |
|
68 |
|
|
69 | 13 |
return result;
|
70 |
} |
|
71 |
|
|
72 | 13 |
private String removeLeadingSlash(String name)
|
73 |
{ |
|
74 | 13 |
if (name.startsWith("/")) |
75 | 13 |
return name.substring(1);
|
76 |
|
|
77 | 0 |
return name;
|
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Invokes {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)}.
|
|
82 |
*
|
|
83 |
* @param type
|
|
84 |
* the complete class name to locate and load; alternately, may be a primitive name
|
|
85 |
* or an array type (primitive or object)
|
|
86 |
* @return The loaded class
|
|
87 |
* @throws ApplicationRuntimeException
|
|
88 |
* if loading the class throws an exception (typically
|
|
89 |
* {@link ClassNotFoundException}or a security exception)
|
|
90 |
* @see JavaTypeUtils
|
|
91 |
*/
|
|
92 |
|
|
93 | 7980 |
public Class findClass(String type)
|
94 |
{ |
|
95 | 7980 |
Class result = JavaTypeUtils.getPrimtiveClass(type); |
96 |
|
|
97 | 7980 |
if (result != null) |
98 | 0 |
return result;
|
99 |
|
|
100 |
// This does some magic to handle arrays of primitives or objects in the
|
|
101 |
// format needed by Class.forName().
|
|
102 |
|
|
103 | 7980 |
String jvmName = JavaTypeUtils.getJVMClassName(type); |
104 |
|
|
105 | 7980 |
try
|
106 |
{ |
|
107 | 7980 |
return Class.forName(jvmName, true, _loader); |
108 |
} |
|
109 |
catch (Throwable t)
|
|
110 |
{ |
|
111 | 6 |
throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t), |
112 |
t); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 | 106 |
public ClassLoader getClassLoader()
|
117 |
{ |
|
118 | 106 |
return _loader;
|
119 |
} |
|
120 |
|
|
121 |
} |
|