1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.jdk;
18
19 import java.io.IOException;
20 import java.net.URL;
21 import java.security.AccessController;
22 import java.security.PrivilegedAction;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33 public class JDK12Hooks extends JDKHooks {
34
35
36
37
38 private static Log log = LogFactory.getLog(JDK12Hooks.class);
39
40 private static final ClassLoader systemClassLoader = findSystemClassLoader();
41
42
43
44
45
46
47
48 @Deprecated
49 public static void setLog(Log _log) {
50 log = _log;
51 }
52
53
54
55
56 @Override
57 public String getSystemProperty(final String propName) {
58 return AccessController.doPrivileged(new PrivilegedAction<String>() {
59 public String run() {
60 try {
61 return System.getProperty(propName);
62 } catch (SecurityException se){
63 return null;
64 }
65 }
66 });
67 }
68
69
70
71
72 @Override
73 public ClassLoader getThreadContextClassLoader() {
74 ClassLoader classLoader;
75
76 try {
77 classLoader = Thread.currentThread().getContextClassLoader();
78 } catch (SecurityException e) {
79
80
81
82
83
84
85
86
87
88
89
90 classLoader = null;
91 }
92
93
94 return classLoader;
95 }
96
97
98
99
100 @Override
101 public ClassLoader getSystemClassLoader() {
102 return systemClassLoader;
103 }
104
105
106
107
108 @Override
109 public Enumeration<URL> getResources(ClassLoader loader, String resourceName) throws IOException {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 final URL first = loader.getResource(resourceName);
135
136
137
138 Enumeration<URL> resources;
139
140 if (first == null) {
141 if (log.isDebugEnabled()) {
142 log.debug("Could not find resource: " + resourceName);
143 }
144 List<URL> emptyURL = Collections.emptyList();
145 resources = Collections.enumeration(emptyURL);
146
147 } else {
148
149 try {
150
151 resources = loader.getResources(resourceName);
152
153 } catch (RuntimeException ex) {
154 log.error("Exception occured during attept to get " + resourceName
155 + " from " + first, ex);
156 List<URL> emptyURL = Collections.emptyList();
157 resources = Collections.enumeration(emptyURL);
158 }
159
160 resources = getResourcesFromUrl(first, resources);
161 }
162
163 return resources;
164 }
165
166
167
168
169
170
171
172
173 private static Enumeration<URL> getResourcesFromUrl(final URL first, final Enumeration<URL> rest) {
174 return new Enumeration<URL>() {
175
176 private boolean firstDone = (first == null);
177
178 private URL next = getNext();
179
180 public URL nextElement() {
181 URL o = next;
182 next = getNext();
183 return o;
184 }
185
186 public boolean hasMoreElements() {
187 return next != null;
188 }
189
190 private URL getNext() {
191 URL n;
192
193 if (!firstDone) {
194
195
196
197
198 firstDone = true;
199 n = first;
200 } else {
201
202
203
204
205
206
207
208
209 n = null;
210 while (rest.hasMoreElements() && n == null) {
211 n = rest.nextElement();
212 if (first != null &&
213 n != null &&
214 n.equals(first))
215 {
216 n = null;
217 }
218 }
219 }
220
221 return n;
222 }
223 };
224 }
225
226
227
228
229
230
231 static private ClassLoader findSystemClassLoader() {
232
233 ClassLoader classLoader;
234
235 try {
236 classLoader = ClassLoader.getSystemClassLoader();
237 } catch (SecurityException e) {
238
239
240
241 classLoader = null;
242 }
243
244 if (classLoader == null) {
245 SecurityManager security = System.getSecurityManager();
246 if (security != null) {
247 try {
248 security.checkCreateClassLoader();
249 classLoader = new PsuedoSystemClassLoader();
250 } catch (SecurityException se){
251 }
252 }
253 }
254
255
256 return classLoader;
257 }
258
259 }