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