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