1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.qedeq.base.io; |
17 |
|
|
18 |
|
import java.io.File; |
19 |
|
import java.io.IOException; |
20 |
|
import java.io.InputStream; |
21 |
|
import java.lang.reflect.InvocationTargetException; |
22 |
|
import java.lang.reflect.Method; |
23 |
|
import java.net.URL; |
24 |
|
|
25 |
|
import org.qedeq.base.trace.Trace; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
@author |
33 |
|
|
|
|
| 50% |
Uncovered Elements: 47 (94) |
Complexity: 31 |
Complexity Density: 0.55 |
|
34 |
|
public final class ResourceLoaderUtility { |
35 |
|
|
36 |
|
|
37 |
|
private static final Class CLASS = ResourceLoaderUtility.class; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
42 |
0
|
private ResourceLoaderUtility() {... |
43 |
|
|
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@param |
52 |
|
@return |
53 |
|
@exception |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
0
|
public static Class loadClass(final String name) throws ClassNotFoundException {... |
56 |
0
|
return loadClass(name, null); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@param |
65 |
|
@return |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
2
|
public static URL getResourceUrl(final String name) {... |
68 |
2
|
return getResourceUrl(name, ResourceLoaderUtility.class.getClassLoader()); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@return |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
1
|
public static InputStream getResourceAsStream(final String name) {... |
80 |
1
|
return getResourceAsStream(name, null); |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
@param |
91 |
|
@return |
92 |
|
@exception |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
94 |
0
|
public static Class loadClass(final String name, final ClassLoader callerClassLoader)... |
95 |
|
throws ClassNotFoundException { |
96 |
0
|
Class clazz = null; |
97 |
|
|
98 |
0
|
try { |
99 |
0
|
final ClassLoader loader = getContextClassLoader(); |
100 |
|
|
101 |
0
|
if (loader != null) { |
102 |
0
|
clazz = loader.loadClass(name); |
103 |
|
} |
104 |
|
} catch (ClassNotFoundException e) { |
105 |
|
|
106 |
|
} |
107 |
|
|
108 |
0
|
if (clazz == null) { |
109 |
0
|
if (callerClassLoader != null) { |
110 |
0
|
clazz = callerClassLoader.loadClass(name); |
111 |
|
} else { |
112 |
0
|
clazz = Class.forName(name); |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
0
|
return clazz; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@param |
126 |
|
@param |
127 |
|
@return |
128 |
|
|
|
|
| 56.2% |
Uncovered Elements: 7 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
129 |
2
|
public static URL getResourceUrl(final String name, final ClassLoader callerClassLoader) {... |
130 |
2
|
checkResourceName(name); |
131 |
|
|
132 |
2
|
URL url = null; |
133 |
|
|
134 |
2
|
final ClassLoader loader = getContextClassLoader(); |
135 |
|
|
136 |
2
|
if (loader != null) { |
137 |
2
|
url = loader.getResource(name); |
138 |
|
} |
139 |
|
|
140 |
2
|
if (url == null) { |
141 |
|
|
142 |
0
|
if (callerClassLoader != null) { |
143 |
0
|
url = callerClassLoader.getResource(name); |
144 |
|
} else { |
145 |
|
|
146 |
0
|
url = ClassLoader.getSystemResource(name); |
147 |
|
} |
148 |
|
} |
149 |
2
|
return url; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@param |
159 |
|
@param |
160 |
|
@return |
161 |
|
|
|
|
| 56.2% |
Uncovered Elements: 7 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
162 |
1
|
public static InputStream getResourceAsStream(final String name,... |
163 |
|
final ClassLoader callerClassLoader) { |
164 |
1
|
checkResourceName(name); |
165 |
|
|
166 |
1
|
InputStream stream = null; |
167 |
|
|
168 |
1
|
final ClassLoader loader = getContextClassLoader(); |
169 |
|
|
170 |
1
|
if (loader != null) { |
171 |
1
|
stream = loader.getResourceAsStream(name); |
172 |
|
} |
173 |
1
|
if (stream == null) { |
174 |
0
|
if (callerClassLoader != null) { |
175 |
0
|
stream = callerClassLoader.getResourceAsStream(name); |
176 |
|
} else { |
177 |
0
|
stream = ClassLoader.getSystemResourceAsStream(name); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
1
|
return stream; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@return |
190 |
|
|
|
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 5 |
Complexity Density: 0.71 |
|
191 |
3
|
public static ClassLoader getContextClassLoader() {... |
192 |
3
|
try { |
193 |
3
|
final Method method = Thread.class.getMethod("getContextClassLoader", null); |
194 |
3
|
return (ClassLoader) method.invoke(Thread.currentThread(), null); |
195 |
|
} catch (RuntimeException e) { |
196 |
0
|
return null; |
197 |
|
} catch (IllegalAccessException e) { |
198 |
0
|
return null; |
199 |
|
} catch (InvocationTargetException e) { |
200 |
0
|
return null; |
201 |
|
} catch (NoSuchMethodException e) { |
202 |
0
|
return null; |
203 |
|
} |
204 |
|
} |
205 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
206 |
3
|
private static void checkResourceName(final String name) {... |
207 |
3
|
if ((name != null) && name.startsWith("/")) { |
208 |
0
|
Trace.info(CLASS, "ClassLoaderUtility", "checkResourceName", |
209 |
|
"resource name not portable: " + name); |
210 |
|
|
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
@param |
220 |
|
|
221 |
|
@param |
222 |
|
@return |
223 |
|
|
|
|
| 68.2% |
Uncovered Elements: 7 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
224 |
3
|
public static File getResourceFile(final File startDirectory,... |
225 |
|
final String resourceDirectoryName, final String resourceName) { |
226 |
3
|
final File resourceDir = new File(startDirectory, resourceDirectoryName); |
227 |
3
|
final File resource = new File(resourceDir, resourceName); |
228 |
3
|
if (!resource.exists()) { |
229 |
1
|
final URL url = getResourceUrl(resourceDirectoryName + "/" + resourceName); |
230 |
1
|
if (url == null) { |
231 |
0
|
Trace.info(ResourceLoaderUtility.class, "getResourceUrlAndMakeLocalCopy", |
232 |
|
"URL not found for: " + resourceDirectoryName + "/" + resourceName); |
233 |
0
|
return null; |
234 |
|
} |
235 |
1
|
try { |
236 |
1
|
if (!resourceDir.exists()) { |
237 |
1
|
if (!resourceDir.mkdirs()) { |
238 |
0
|
Trace.info(ResourceLoaderUtility.class, "getResourceUrlAndMakeLocalCopy", |
239 |
|
"creation failed: " + resourceDir); |
240 |
|
} |
241 |
|
} |
242 |
1
|
IoUtility.saveFile(url, resource); |
243 |
|
} catch (IOException e) { |
244 |
0
|
Trace.fatal(ResourceLoaderUtility.class, "getResourceUrlAndMakeLocalCopy", |
245 |
|
"resource can not be saved", e); |
246 |
|
} |
247 |
|
} |
248 |
3
|
return resource; |
249 |
|
} |
250 |
|
|
251 |
|
} |