Lines 15-3758
Link Here
|
15 |
* limitations under the License. |
15 |
* limitations under the License. |
16 |
*/ |
16 |
*/ |
17 |
|
17 |
|
18 |
|
|
|
19 |
package org.apache.catalina.loader; |
18 |
package org.apache.catalina.loader; |
20 |
|
19 |
|
21 |
import java.io.ByteArrayInputStream; |
|
|
22 |
import java.io.File; |
23 |
import java.io.FileOutputStream; |
24 |
import java.io.FilePermission; |
25 |
import java.io.IOException; |
26 |
import java.io.InputStream; |
27 |
import java.lang.instrument.ClassFileTransformer; |
28 |
import java.lang.instrument.IllegalClassFormatException; |
29 |
import java.lang.ref.Reference; |
30 |
import java.lang.ref.WeakReference; |
31 |
import java.lang.reflect.Field; |
32 |
import java.lang.reflect.Method; |
33 |
import java.lang.reflect.Modifier; |
34 |
import java.net.MalformedURLException; |
35 |
import java.net.URL; |
36 |
import java.net.URLClassLoader; |
37 |
import java.nio.charset.Charset; |
38 |
import java.security.AccessControlException; |
39 |
import java.security.AccessController; |
40 |
import java.security.CodeSource; |
41 |
import java.security.Permission; |
42 |
import java.security.PermissionCollection; |
43 |
import java.security.Policy; |
44 |
import java.security.PrivilegedAction; |
45 |
import java.security.ProtectionDomain; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collection; |
48 |
import java.util.Collections; |
49 |
import java.util.ConcurrentModificationException; |
50 |
import java.util.Enumeration; |
51 |
import java.util.HashMap; |
52 |
import java.util.Iterator; |
53 |
import java.util.LinkedHashMap; |
54 |
import java.util.LinkedHashSet; |
55 |
import java.util.List; |
56 |
import java.util.Map; |
57 |
import java.util.ResourceBundle; |
58 |
import java.util.Set; |
59 |
import java.util.concurrent.CopyOnWriteArrayList; |
60 |
import java.util.concurrent.ThreadPoolExecutor; |
61 |
import java.util.jar.Attributes; |
62 |
import java.util.jar.Attributes.Name; |
63 |
import java.util.jar.JarEntry; |
64 |
import java.util.jar.JarFile; |
65 |
import java.util.jar.Manifest; |
66 |
|
67 |
import javax.naming.Binding; |
68 |
import javax.naming.NameClassPair; |
69 |
import javax.naming.NamingEnumeration; |
70 |
import javax.naming.NamingException; |
71 |
import javax.naming.directory.DirContext; |
72 |
|
73 |
import org.apache.catalina.Globals; |
74 |
import org.apache.catalina.Lifecycle; |
75 |
import org.apache.catalina.LifecycleException; |
20 |
import org.apache.catalina.LifecycleException; |
76 |
import org.apache.catalina.LifecycleListener; |
|
|
77 |
import org.apache.catalina.LifecycleState; |
78 |
import org.apache.naming.JndiPermission; |
79 |
import org.apache.naming.resources.ProxyDirContext; |
80 |
import org.apache.naming.resources.Resource; |
81 |
import org.apache.naming.resources.ResourceAttributes; |
82 |
import org.apache.tomcat.InstrumentableClassLoader; |
83 |
import org.apache.tomcat.util.ExceptionUtils; |
84 |
import org.apache.tomcat.util.IntrospectionUtils; |
85 |
import org.apache.tomcat.util.res.StringManager; |
86 |
|
21 |
|
87 |
/** |
22 |
/** |
88 |
* Specialized web application class loader. |
23 |
* Serial class loading implementation of WebappClassLoaderBase. |
89 |
* <p> |
24 |
* This class loader will be used by default. |
90 |
* This class loader is a full reimplementation of the |
25 |
* @author Huxing Zhang (huxing.zhx@alibaba-inc.com) |
91 |
* <code>URLClassLoader</code> from the JDK. It is designed to be fully |
|
|
92 |
* compatible with a normal <code>URLClassLoader</code>, although its internal |
93 |
* behavior may be completely different. |
94 |
* <p> |
95 |
* <strong>IMPLEMENTATION NOTE</strong> - By default, this class loader follows |
96 |
* the delegation model required by the specification. The system class |
97 |
* loader will be queried first, then the local repositories, and only then |
98 |
* delegation to the parent class loader will occur. This allows the web |
99 |
* application to override any shared class except the classes from J2SE. |
100 |
* Special handling is provided from the JAXP XML parser interfaces, the JNDI |
101 |
* interfaces, and the classes from the servlet API, which are never loaded |
102 |
* from the webapp repositories. The <code>delegate</code> property |
103 |
* allows an application to modify this behavior to move the parent class loader |
104 |
* ahead of the local repositories. |
105 |
* <p> |
106 |
* <strong>IMPLEMENTATION NOTE</strong> - Due to limitations in Jasper |
107 |
* compilation technology, any repository which contains classes from |
108 |
* the servlet API will be ignored by the class loader. |
109 |
* <p> |
110 |
* <strong>IMPLEMENTATION NOTE</strong> - The class loader generates source |
111 |
* URLs which include the full JAR URL when a class is loaded from a JAR file, |
112 |
* which allows setting security permission at the class level, even when a |
113 |
* class is contained inside a JAR. |
114 |
* <p> |
115 |
* <strong>IMPLEMENTATION NOTE</strong> - Local repositories are searched in |
116 |
* the order they are added via the initial constructor and/or any subsequent |
117 |
* calls to <code>addRepository()</code> or <code>addJar()</code>. |
118 |
* <p> |
119 |
* <strong>IMPLEMENTATION NOTE</strong> - No check for sealing violations or |
120 |
* security is made unless a security manager is present. |
121 |
* <p> |
122 |
* TODO: Is there any requirement to provide a proper Lifecycle implementation |
123 |
* rather than the current stubbed implementation? |
124 |
* <strong>IMPLEMENTATION NOTE</strong> - As of 7.0.64/8.0, this class |
125 |
* loader implements {@link InstrumentableClassLoader}, permitting web |
126 |
* application classes to instrument other classes in the same web |
127 |
* application. It does not permit instrumentation of system or container |
128 |
* classes or classes in other web apps. |
129 |
* |
130 |
* @author Remy Maucherat |
131 |
* @author Craig R. McClanahan |
132 |
*/ |
26 |
*/ |
133 |
public class WebappClassLoader extends URLClassLoader |
27 |
public class WebappClassLoader extends WebappClassLoaderBase { |
134 |
implements Lifecycle, InstrumentableClassLoader { |
|
|
135 |
|
136 |
private static final org.apache.juli.logging.Log log= |
137 |
org.apache.juli.logging.LogFactory.getLog( WebappClassLoader.class ); |
138 |
|
139 |
private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); |
140 |
/** |
141 |
* List of ThreadGroup names to ignore when scanning for web application |
142 |
* started threads that need to be shut down. |
143 |
*/ |
144 |
private static final List<String> JVM_THREAD_GROUP_NAMES = |
145 |
new ArrayList<String>(); |
146 |
|
147 |
private static final String JVM_THREAD_GROUP_SYSTEM = "system"; |
148 |
|
149 |
private static final String SERVICES_PREFIX = "META-INF/services/"; |
150 |
|
151 |
private static final String CLASS_FILE_SUFFIX = ".class"; |
152 |
|
153 |
static { |
154 |
JVM_THREAD_GROUP_NAMES.add(JVM_THREAD_GROUP_SYSTEM); |
155 |
JVM_THREAD_GROUP_NAMES.add("RMI Runtime"); |
156 |
} |
157 |
|
158 |
protected class PrivilegedFindResourceByName |
159 |
implements PrivilegedAction<ResourceEntry> { |
160 |
|
161 |
protected String name; |
162 |
protected String path; |
163 |
protected boolean manifestRequired; |
164 |
|
165 |
PrivilegedFindResourceByName(String name, String path, boolean manifestRequired) { |
166 |
this.name = name; |
167 |
this.path = path; |
168 |
this.manifestRequired = manifestRequired; |
169 |
} |
170 |
|
171 |
@Override |
172 |
public ResourceEntry run() { |
173 |
return findResourceInternal(name, path, manifestRequired); |
174 |
} |
175 |
|
176 |
} |
177 |
|
178 |
|
179 |
protected static final class PrivilegedGetClassLoader |
180 |
implements PrivilegedAction<ClassLoader> { |
181 |
|
182 |
public Class<?> clazz; |
183 |
|
184 |
public PrivilegedGetClassLoader(Class<?> clazz){ |
185 |
this.clazz = clazz; |
186 |
} |
187 |
|
188 |
@Override |
189 |
public ClassLoader run() { |
190 |
return clazz.getClassLoader(); |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
// ------------------------------------------------------- Static Variables |
196 |
|
197 |
|
28 |
|
198 |
/** |
|
|
199 |
* The set of trigger classes that will cause a proposed repository not |
200 |
* to be added if this class is visible to the class loader that loaded |
201 |
* this factory class. Typically, trigger classes will be listed for |
202 |
* components that have been integrated into the JDK for later versions, |
203 |
* but where the corresponding JAR files are required to run on |
204 |
* earlier versions. |
205 |
*/ |
206 |
protected static final String[] triggers = { |
207 |
"javax.servlet.Servlet", "javax.el.Expression" // Servlet API |
208 |
}; |
209 |
|
210 |
|
211 |
/** |
212 |
* Set of package names which are not allowed to be loaded from a webapp |
213 |
* class loader without delegating first. |
214 |
*/ |
215 |
protected static final String[] packageTriggers = { |
216 |
}; |
217 |
|
218 |
|
219 |
/** |
220 |
* The string manager for this package. |
221 |
*/ |
222 |
protected static final StringManager sm = |
223 |
StringManager.getManager(Constants.Package); |
224 |
|
225 |
|
226 |
/** |
227 |
* Use anti JAR locking code, which does URL rerouting when accessing |
228 |
* resources. |
229 |
*/ |
230 |
boolean antiJARLocking = false; |
231 |
|
232 |
// ----------------------------------------------------------- Constructors |
233 |
|
234 |
|
235 |
/** |
236 |
* Construct a new ClassLoader with no defined repositories and no |
237 |
* parent ClassLoader. |
238 |
*/ |
239 |
public WebappClassLoader() { |
29 |
public WebappClassLoader() { |
240 |
|
30 |
super(); |
241 |
super(new URL[0]); |
|
|
242 |
|
243 |
ClassLoader p = getParent(); |
244 |
if (p == null) { |
245 |
p = getSystemClassLoader(); |
246 |
} |
247 |
this.parent = p; |
248 |
|
249 |
ClassLoader j = String.class.getClassLoader(); |
250 |
if (j == null) { |
251 |
j = getSystemClassLoader(); |
252 |
while (j.getParent() != null) { |
253 |
j = j.getParent(); |
254 |
} |
255 |
} |
256 |
this.j2seClassLoader = j; |
257 |
|
258 |
securityManager = System.getSecurityManager(); |
259 |
if (securityManager != null) { |
260 |
refreshPolicy(); |
261 |
} |
262 |
} |
31 |
} |
263 |
|
32 |
|
264 |
|
|
|
265 |
/** |
266 |
* Construct a new ClassLoader with no defined repositories and the given |
267 |
* parent ClassLoader. |
268 |
* <p> |
269 |
* Method is used via reflection - |
270 |
* see {@link WebappLoader#createClassLoader()} |
271 |
* |
272 |
* @param parent Our parent class loader |
273 |
*/ |
274 |
public WebappClassLoader(ClassLoader parent) { |
33 |
public WebappClassLoader(ClassLoader parent) { |
275 |
|
34 |
super(parent); |
276 |
super(new URL[0], parent); |
|
|
277 |
|
278 |
ClassLoader p = getParent(); |
279 |
if (p == null) { |
280 |
p = getSystemClassLoader(); |
281 |
} |
282 |
this.parent = p; |
283 |
|
284 |
ClassLoader j = String.class.getClassLoader(); |
285 |
if (j == null) { |
286 |
j = getSystemClassLoader(); |
287 |
while (j.getParent() != null) { |
288 |
j = j.getParent(); |
289 |
} |
290 |
} |
291 |
this.j2seClassLoader = j; |
292 |
|
293 |
securityManager = System.getSecurityManager(); |
294 |
if (securityManager != null) { |
295 |
refreshPolicy(); |
296 |
} |
297 |
} |
298 |
|
299 |
|
300 |
// ----------------------------------------------------- Instance Variables |
301 |
|
302 |
|
303 |
/** |
304 |
* Associated directory context giving access to the resources in this |
305 |
* webapp. |
306 |
*/ |
307 |
protected DirContext resources = null; |
308 |
|
309 |
|
310 |
/** |
311 |
* The cache of ResourceEntry for classes and resources we have loaded, |
312 |
* keyed by resource name. |
313 |
*/ |
314 |
protected HashMap<String, ResourceEntry> resourceEntries = new HashMap<String, ResourceEntry>(); |
315 |
|
316 |
|
317 |
/** |
318 |
* The list of not found resources. |
319 |
*/ |
320 |
protected HashMap<String, String> notFoundResources = |
321 |
new LinkedHashMap<String, String>() { |
322 |
private static final long serialVersionUID = 1L; |
323 |
@Override |
324 |
protected boolean removeEldestEntry( |
325 |
Map.Entry<String, String> eldest) { |
326 |
return size() > 1000; |
327 |
} |
328 |
}; |
329 |
|
330 |
|
331 |
/** |
332 |
* Should this class loader delegate to the parent class loader |
333 |
* <strong>before</strong> searching its own repositories (i.e. the |
334 |
* usual Java2 delegation model)? If set to <code>false</code>, |
335 |
* this class loader will search its own repositories first, and |
336 |
* delegate to the parent only if the class or resource is not |
337 |
* found locally. Note that the default, <code>false</code>, is |
338 |
* the behavior called for by the servlet specification. |
339 |
*/ |
340 |
protected boolean delegate = false; |
341 |
|
342 |
|
343 |
/** |
344 |
* Last time a JAR was accessed. |
345 |
*/ |
346 |
protected long lastJarAccessed = 0L; |
347 |
|
348 |
|
349 |
/** |
350 |
* The list of local repositories, in the order they should be searched |
351 |
* for locally loaded classes or resources. |
352 |
*/ |
353 |
protected String[] repositories = new String[0]; |
354 |
|
355 |
|
356 |
/** |
357 |
* Repositories URLs, used to cache the result of getURLs. |
358 |
*/ |
359 |
protected URL[] repositoryURLs = null; |
360 |
|
361 |
|
362 |
/** |
363 |
* Repositories translated as path in the work directory (for Jasper |
364 |
* originally), but which is used to generate fake URLs should getURLs be |
365 |
* called. |
366 |
*/ |
367 |
protected File[] files = new File[0]; |
368 |
|
369 |
|
370 |
/** |
371 |
* The list of JARs, in the order they should be searched |
372 |
* for locally loaded classes or resources. |
373 |
*/ |
374 |
protected JarFile[] jarFiles = new JarFile[0]; |
375 |
|
376 |
|
377 |
/** |
378 |
* The list of JARs, in the order they should be searched |
379 |
* for locally loaded classes or resources. |
380 |
*/ |
381 |
protected File[] jarRealFiles = new File[0]; |
382 |
|
383 |
|
384 |
/** |
385 |
* The path which will be monitored for added Jar files. |
386 |
*/ |
387 |
protected String jarPath = null; |
388 |
|
389 |
|
390 |
/** |
391 |
* The list of JARs, in the order they should be searched |
392 |
* for locally loaded classes or resources. |
393 |
*/ |
394 |
protected String[] jarNames = new String[0]; |
395 |
|
396 |
|
397 |
/** |
398 |
* The list of JARs last modified dates, in the order they should be |
399 |
* searched for locally loaded classes or resources. |
400 |
*/ |
401 |
protected long[] lastModifiedDates = new long[0]; |
402 |
|
403 |
|
404 |
/** |
405 |
* The list of resources which should be checked when checking for |
406 |
* modifications. |
407 |
*/ |
408 |
protected String[] paths = new String[0]; |
409 |
|
410 |
|
411 |
/** |
412 |
* A list of read File and Jndi Permission's required if this loader |
413 |
* is for a web application context. |
414 |
*/ |
415 |
protected ArrayList<Permission> permissionList = |
416 |
new ArrayList<Permission>(); |
417 |
|
418 |
|
419 |
/** |
420 |
* Path where resources loaded from JARs will be extracted. |
421 |
*/ |
422 |
protected File loaderDir = null; |
423 |
protected String canonicalLoaderDir = null; |
424 |
|
425 |
/** |
426 |
* The PermissionCollection for each CodeSource for a web |
427 |
* application context. |
428 |
*/ |
429 |
protected HashMap<String, PermissionCollection> loaderPC = new HashMap<String, PermissionCollection>(); |
430 |
|
431 |
|
432 |
/** |
433 |
* Instance of the SecurityManager installed. |
434 |
*/ |
435 |
protected SecurityManager securityManager = null; |
436 |
|
437 |
|
438 |
/** |
439 |
* The parent class loader. |
440 |
*/ |
441 |
protected ClassLoader parent = null; |
442 |
|
443 |
|
444 |
/** |
445 |
* The system class loader. |
446 |
*/ |
447 |
protected ClassLoader system = null; |
448 |
|
449 |
|
450 |
/** |
451 |
* The bootstrap class loader used to load the JavaSE classes. In some |
452 |
* implementations this class loader is always <code>null</null> and in |
453 |
* those cases {@link ClassLoader#getParent()} will be called recursively on |
454 |
* the system class loader and the last non-null result used. |
455 |
*/ |
456 |
protected ClassLoader j2seClassLoader; |
457 |
|
458 |
|
459 |
/** |
460 |
* Has this component been started? |
461 |
*/ |
462 |
protected boolean started = false; |
463 |
|
464 |
|
465 |
/** |
466 |
* Has external repositories. |
467 |
*/ |
468 |
protected boolean hasExternalRepositories = false; |
469 |
|
470 |
/** |
471 |
* Search external repositories first |
472 |
*/ |
473 |
protected boolean searchExternalFirst = false; |
474 |
|
475 |
/** |
476 |
* need conversion for properties files |
477 |
*/ |
478 |
protected boolean needConvert = false; |
479 |
|
480 |
|
481 |
/** |
482 |
* All permission. |
483 |
*/ |
484 |
protected Permission allPermission = new java.security.AllPermission(); |
485 |
|
486 |
|
487 |
/** |
488 |
* Should Tomcat attempt to null out any static or final fields from loaded |
489 |
* classes when a web application is stopped as a work around for apparent |
490 |
* garbage collection bugs and application coding errors? There have been |
491 |
* some issues reported with log4j when this option is true. Applications |
492 |
* without memory leaks using recent JVMs should operate correctly with this |
493 |
* option set to <code>false</code>. If not specified, the default value of |
494 |
* <code>false</code> will be used. |
495 |
*/ |
496 |
private boolean clearReferencesStatic = false; |
497 |
|
498 |
/** |
499 |
* Should Tomcat attempt to terminate threads that have been started by the |
500 |
* web application? Stopping threads is performed via the deprecated (for |
501 |
* good reason) <code>Thread.stop()</code> method and is likely to result in |
502 |
* instability. As such, enabling this should be viewed as an option of last |
503 |
* resort in a development environment and is not recommended in a |
504 |
* production environment. If not specified, the default value of |
505 |
* <code>false</code> will be used. |
506 |
*/ |
507 |
private boolean clearReferencesStopThreads = false; |
508 |
|
509 |
/** |
510 |
* Should Tomcat attempt to terminate any {@link java.util.TimerThread}s |
511 |
* that have been started by the web application? If not specified, the |
512 |
* default value of <code>false</code> will be used. |
513 |
*/ |
514 |
private boolean clearReferencesStopTimerThreads = false; |
515 |
|
516 |
/** |
517 |
* Should Tomcat call {@link org.apache.juli.logging.LogFactory#release()} |
518 |
* when the class loader is stopped? If not specified, the default value |
519 |
* of <code>true</code> is used. Changing the default setting is likely to |
520 |
* lead to memory leaks and other issues. |
521 |
*/ |
522 |
private boolean clearReferencesLogFactoryRelease = true; |
523 |
|
524 |
/** |
525 |
* If an HttpClient keep-alive timer thread has been started by this web |
526 |
* application and is still running, should Tomcat change the context class |
527 |
* loader from the current {@link WebappClassLoader} to |
528 |
* {@link WebappClassLoader#parent} to prevent a memory leak? Note that the |
529 |
* keep-alive timer thread will stop on its own once the keep-alives all |
530 |
* expire however, on a busy system that might not happen for some time. |
531 |
*/ |
532 |
private boolean clearReferencesHttpClientKeepAliveThread = true; |
533 |
|
534 |
/** |
535 |
* Name of associated context used with logging and JMX to associate with |
536 |
* the right web application. Particularly useful for the clear references |
537 |
* messages. Defaults to unknown but if standard Tomcat components are used |
538 |
* it will be updated during initialisation from the resources. |
539 |
*/ |
540 |
private String contextName = "unknown"; |
541 |
|
542 |
/** |
543 |
* Holds the class file transformers decorating this class loader. The |
544 |
* CopyOnWriteArrayList is thread safe. It is expensive on writes, but |
545 |
* those should be rare. It is very fast on reads, since synchronization |
546 |
* is not actually used. Importantly, the ClassLoader will never block |
547 |
* iterating over the transformers while loading a class. |
548 |
*/ |
549 |
private final List<ClassFileTransformer> transformers = new CopyOnWriteArrayList<ClassFileTransformer>(); |
550 |
|
551 |
/** |
552 |
* Code base to use for classes loaded from WEB-INF/classes. |
553 |
*/ |
554 |
private URL webInfClassesCodeBase = null; |
555 |
|
556 |
// ------------------------------------------------------------- Properties |
557 |
|
558 |
|
559 |
/** |
560 |
* Get associated resources. |
561 |
*/ |
562 |
public DirContext getResources() { |
563 |
|
564 |
return this.resources; |
565 |
|
566 |
} |
567 |
|
568 |
|
569 |
/** |
570 |
* Set associated resources. |
571 |
*/ |
572 |
public void setResources(DirContext resources) { |
573 |
|
574 |
this.resources = resources; |
575 |
|
576 |
if (resources instanceof ProxyDirContext) { |
577 |
contextName = ((ProxyDirContext) resources).getContextName(); |
578 |
} |
579 |
} |
580 |
|
581 |
|
582 |
/** |
583 |
* Return the context name for this class loader. |
584 |
*/ |
585 |
public String getContextName() { |
586 |
|
587 |
return (this.contextName); |
588 |
|
589 |
} |
590 |
|
591 |
|
592 |
/** |
593 |
* Return the "delegate first" flag for this class loader. |
594 |
*/ |
595 |
public boolean getDelegate() { |
596 |
|
597 |
return (this.delegate); |
598 |
|
599 |
} |
35 |
} |
600 |
|
36 |
|
601 |
|
|
|
602 |
/** |
37 |
/** |
603 |
* Set the "delegate first" flag for this class loader. |
38 |
* Returns a copy of this class loader without any class file |
604 |
* If this flag is true, this class loader delegates |
39 |
* transformers. This is a tool often used by Java Persistence API |
605 |
* to the parent class loader |
40 |
* providers to inspect entity classes in the absence of any |
606 |
* <strong>before</strong> searching its own repositories, as |
41 |
* instrumentation, something that can't be guaranteed within the |
607 |
* in an ordinary (non-servlet) chain of Java class loaders. |
42 |
* context of a {@link java.lang.instrument.ClassFileTransformer}'s |
608 |
* If set to <code>false</code> (the default), |
43 |
* {@link java.lang.instrument.ClassFileTransformer#transform(ClassLoader, |
609 |
* this class loader will search its own repositories first, and |
44 |
* String, Class, java.security.ProtectionDomain, byte[]) transform} method. |
610 |
* delegate to the parent only if the class or resource is not |
45 |
* <p> |
611 |
* found locally, as per the servlet specification. |
46 |
* The returned class loader's resource cache will have been cleared |
|
|
47 |
* so that classes already instrumented will not be retained or |
48 |
* returned. |
612 |
* |
49 |
* |
613 |
* @param delegate The new "delegate first" flag |
50 |
* @return the transformer-free copy of this class loader. |
614 |
*/ |
51 |
*/ |
615 |
public void setDelegate(boolean delegate) { |
52 |
@Override |
616 |
|
53 |
public WebappClassLoader copyWithoutTransformers() { |
617 |
this.delegate = delegate; |
|
|
618 |
|
619 |
} |
620 |
|
54 |
|
|
|
55 |
WebappClassLoader result = new WebappClassLoader(getParent()); |
621 |
|
56 |
|
622 |
/** |
57 |
super.copyStateWithoutTransformers(result); |
623 |
* @return Returns the antiJARLocking. |
|
|
624 |
*/ |
625 |
public boolean getAntiJARLocking() { |
626 |
return antiJARLocking; |
627 |
} |
628 |
|
58 |
|
|
|
59 |
try { |
60 |
result.start(); |
61 |
} catch (LifecycleException e) { |
62 |
throw new IllegalStateException(e); |
63 |
} |
629 |
|
64 |
|
630 |
/** |
65 |
return result; |
631 |
* @param antiJARLocking The antiJARLocking to set. |
|
|
632 |
*/ |
633 |
public void setAntiJARLocking(boolean antiJARLocking) { |
634 |
this.antiJARLocking = antiJARLocking; |
635 |
} |
66 |
} |
636 |
|
67 |
|
637 |
/** |
|
|
638 |
* @return Returns the searchExternalFirst. |
639 |
*/ |
640 |
public boolean getSearchExternalFirst() { |
641 |
return searchExternalFirst; |
642 |
} |
643 |
|
644 |
/** |
645 |
* @param searchExternalFirst Whether external repositories should be searched first |
646 |
*/ |
647 |
public void setSearchExternalFirst(boolean searchExternalFirst) { |
648 |
this.searchExternalFirst = searchExternalFirst; |
649 |
} |
650 |
|
651 |
|
652 |
/** |
653 |
* If there is a Java SecurityManager create a read FilePermission |
654 |
* or JndiPermission for the file directory path. |
655 |
* |
656 |
* @param filepath file directory path |
657 |
*/ |
658 |
public void addPermission(String filepath) { |
659 |
if (filepath == null) { |
660 |
return; |
661 |
} |
662 |
|
663 |
String path = filepath; |
664 |
|
665 |
if (securityManager != null) { |
666 |
Permission permission = null; |
667 |
if (path.startsWith("jndi:") || path.startsWith("jar:jndi:")) { |
668 |
if (!path.endsWith("/")) { |
669 |
path = path + "/"; |
670 |
} |
671 |
permission = new JndiPermission(path + "*"); |
672 |
addPermission(permission); |
673 |
} else { |
674 |
if (!path.endsWith(File.separator)) { |
675 |
permission = new FilePermission(path, "read"); |
676 |
addPermission(permission); |
677 |
path = path + File.separator; |
678 |
} |
679 |
permission = new FilePermission(path + "-", "read"); |
680 |
addPermission(permission); |
681 |
} |
682 |
} |
683 |
} |
684 |
|
685 |
|
686 |
/** |
687 |
* If there is a Java SecurityManager create a read FilePermission |
688 |
* or JndiPermission for URL. |
689 |
* |
690 |
* @param url URL for a file or directory on local system |
691 |
*/ |
692 |
public void addPermission(URL url) { |
693 |
if (url != null) { |
694 |
addPermission(url.toString()); |
695 |
} |
696 |
} |
697 |
|
698 |
|
699 |
/** |
700 |
* If there is a Java SecurityManager create a Permission. |
701 |
* |
702 |
* @param permission The permission |
703 |
*/ |
704 |
public void addPermission(Permission permission) { |
705 |
if ((securityManager != null) && (permission != null)) { |
706 |
permissionList.add(permission); |
707 |
} |
708 |
} |
709 |
|
710 |
|
711 |
/** |
712 |
* Return the JAR path. |
713 |
*/ |
714 |
public String getJarPath() { |
715 |
|
716 |
return this.jarPath; |
717 |
|
718 |
} |
719 |
|
720 |
|
721 |
/** |
722 |
* Change the Jar path. |
723 |
*/ |
724 |
public void setJarPath(String jarPath) { |
725 |
|
726 |
this.jarPath = jarPath; |
727 |
|
728 |
} |
729 |
|
730 |
|
731 |
/** |
732 |
* Change the work directory. |
733 |
*/ |
734 |
public void setWorkDir(File workDir) { |
735 |
this.loaderDir = new File(workDir, "loader"); |
736 |
if (loaderDir == null) { |
737 |
canonicalLoaderDir = null; |
738 |
} else { |
739 |
try { |
740 |
canonicalLoaderDir = loaderDir.getCanonicalPath(); |
741 |
if (!canonicalLoaderDir.endsWith(File.separator)) { |
742 |
canonicalLoaderDir += File.separator; |
743 |
} |
744 |
} catch (IOException ioe) { |
745 |
canonicalLoaderDir = null; |
746 |
} |
747 |
} |
748 |
} |
749 |
|
750 |
/** |
751 |
* Utility method for use in subclasses. |
752 |
* Must be called before Lifecycle methods to have any effect. |
753 |
* |
754 |
* @deprecated Will be removed in 8.0.x onwards. |
755 |
*/ |
756 |
@Deprecated |
757 |
protected void setParentClassLoader(ClassLoader pcl) { |
758 |
parent = pcl; |
759 |
} |
760 |
|
761 |
/** |
762 |
* Return the clearReferencesStatic flag for this Context. |
763 |
*/ |
764 |
public boolean getClearReferencesStatic() { |
765 |
return (this.clearReferencesStatic); |
766 |
} |
767 |
|
768 |
|
769 |
/** |
770 |
* Set the clearReferencesStatic feature for this Context. |
771 |
* |
772 |
* @param clearReferencesStatic The new flag value |
773 |
*/ |
774 |
public void setClearReferencesStatic(boolean clearReferencesStatic) { |
775 |
this.clearReferencesStatic = clearReferencesStatic; |
776 |
} |
777 |
|
778 |
|
779 |
/** |
780 |
* Return the clearReferencesStopThreads flag for this Context. |
781 |
*/ |
782 |
public boolean getClearReferencesStopThreads() { |
783 |
return (this.clearReferencesStopThreads); |
784 |
} |
785 |
|
786 |
|
787 |
/** |
788 |
* Set the clearReferencesStopThreads feature for this Context. |
789 |
* |
790 |
* @param clearReferencesStopThreads The new flag value |
791 |
*/ |
792 |
public void setClearReferencesStopThreads( |
793 |
boolean clearReferencesStopThreads) { |
794 |
this.clearReferencesStopThreads = clearReferencesStopThreads; |
795 |
} |
796 |
|
797 |
|
798 |
/** |
799 |
* Return the clearReferencesStopTimerThreads flag for this Context. |
800 |
*/ |
801 |
public boolean getClearReferencesStopTimerThreads() { |
802 |
return (this.clearReferencesStopTimerThreads); |
803 |
} |
804 |
|
805 |
|
806 |
/** |
807 |
* Set the clearReferencesStopTimerThreads feature for this Context. |
808 |
* |
809 |
* @param clearReferencesStopTimerThreads The new flag value |
810 |
*/ |
811 |
public void setClearReferencesStopTimerThreads( |
812 |
boolean clearReferencesStopTimerThreads) { |
813 |
this.clearReferencesStopTimerThreads = clearReferencesStopTimerThreads; |
814 |
} |
815 |
|
816 |
|
817 |
/** |
818 |
* Return the clearReferencesLogFactoryRelease flag for this Context. |
819 |
*/ |
820 |
public boolean getClearReferencesLogFactoryRelease() { |
821 |
return (this.clearReferencesLogFactoryRelease); |
822 |
} |
823 |
|
824 |
|
825 |
/** |
826 |
* Set the clearReferencesLogFactoryRelease feature for this Context. |
827 |
* |
828 |
* @param clearReferencesLogFactoryRelease The new flag value |
829 |
*/ |
830 |
public void setClearReferencesLogFactoryRelease( |
831 |
boolean clearReferencesLogFactoryRelease) { |
832 |
this.clearReferencesLogFactoryRelease = |
833 |
clearReferencesLogFactoryRelease; |
834 |
} |
835 |
|
836 |
|
837 |
/** |
838 |
* Return the clearReferencesHttpClientKeepAliveThread flag for this |
839 |
* Context. |
840 |
*/ |
841 |
public boolean getClearReferencesHttpClientKeepAliveThread() { |
842 |
return (this.clearReferencesHttpClientKeepAliveThread); |
843 |
} |
844 |
|
845 |
|
846 |
/** |
847 |
* Set the clearReferencesHttpClientKeepAliveThread feature for this |
848 |
* Context. |
849 |
* |
850 |
* @param clearReferencesHttpClientKeepAliveThread The new flag value |
851 |
*/ |
852 |
public void setClearReferencesHttpClientKeepAliveThread( |
853 |
boolean clearReferencesHttpClientKeepAliveThread) { |
854 |
this.clearReferencesHttpClientKeepAliveThread = |
855 |
clearReferencesHttpClientKeepAliveThread; |
856 |
} |
857 |
|
858 |
|
859 |
// ------------------------------------------------------- Reloader Methods |
860 |
|
861 |
/** |
862 |
* Adds the specified class file transformer to this class loader. The |
863 |
* transformer will then be able to modify the bytecode of any classes |
864 |
* loaded by this class loader after the invocation of this method. |
865 |
* |
866 |
* @param transformer The transformer to add to the class loader |
867 |
*/ |
868 |
@Override |
869 |
public void addTransformer(ClassFileTransformer transformer) { |
870 |
|
871 |
if (transformer == null) { |
872 |
throw new IllegalArgumentException(sm.getString( |
873 |
"webappClassLoader.addTransformer.illegalArgument", getContextName())); |
874 |
} |
875 |
|
876 |
if (this.transformers.contains(transformer)) { |
877 |
// if the same instance of this transformer was already added, bail out |
878 |
log.warn(sm.getString("webappClassLoader.addTransformer.duplicate", |
879 |
transformer, getContextName())); |
880 |
return; |
881 |
} |
882 |
this.transformers.add(transformer); |
883 |
|
884 |
log.info(sm.getString("webappClassLoader.addTransformer", transformer, getContextName())); |
885 |
|
886 |
} |
887 |
|
888 |
/** |
889 |
* Removes the specified class file transformer from this class loader. |
890 |
* It will no longer be able to modify the byte code of any classes |
891 |
* loaded by the class loader after the invocation of this method. |
892 |
* However, any classes already modified by this transformer will |
893 |
* remain transformed. |
894 |
* |
895 |
* @param transformer The transformer to remove |
896 |
*/ |
897 |
@Override |
898 |
public void removeTransformer(ClassFileTransformer transformer) { |
899 |
|
900 |
if (transformer == null) { |
901 |
return; |
902 |
} |
903 |
|
904 |
if (this.transformers.remove(transformer)) { |
905 |
log.info(sm.getString("webappClassLoader.removeTransformer", |
906 |
transformer, getContextName())); |
907 |
return; |
908 |
} |
909 |
|
910 |
} |
911 |
|
912 |
/** |
913 |
* Returns a copy of this class loader without any class file |
914 |
* transformers. This is a tool often used by Java Persistence API |
915 |
* providers to inspect entity classes in the absence of any |
916 |
* instrumentation, something that can't be guaranteed within the |
917 |
* context of a {@link ClassFileTransformer}'s |
918 |
* {@link ClassFileTransformer#transform(ClassLoader, String, Class, |
919 |
* ProtectionDomain, byte[]) transform} method. |
920 |
* <p> |
921 |
* The returned class loader's resource cache will have been cleared |
922 |
* so that classes already instrumented will not be retained or |
923 |
* returned. |
924 |
* |
925 |
* @return the transformer-free copy of this class loader. |
926 |
*/ |
927 |
@Override |
928 |
public WebappClassLoader copyWithoutTransformers() { |
929 |
|
930 |
WebappClassLoader result = new WebappClassLoader(this.parent); |
931 |
|
932 |
result.antiJARLocking = this.antiJARLocking; |
933 |
result.resources = this.resources; |
934 |
result.files = this.files; |
935 |
result.delegate = this.delegate; |
936 |
result.lastJarAccessed = this.lastJarAccessed; |
937 |
result.repositories = this.repositories; |
938 |
result.jarPath = this.jarPath; |
939 |
result.loaderDir = this.loaderDir; |
940 |
result.canonicalLoaderDir = this.canonicalLoaderDir; |
941 |
result.clearReferencesStatic = this.clearReferencesStatic; |
942 |
result.clearReferencesStopThreads = this.clearReferencesStopThreads; |
943 |
result.clearReferencesStopTimerThreads = this.clearReferencesStopTimerThreads; |
944 |
result.clearReferencesLogFactoryRelease = this.clearReferencesLogFactoryRelease; |
945 |
result.clearReferencesHttpClientKeepAliveThread = this.clearReferencesHttpClientKeepAliveThread; |
946 |
result.repositoryURLs = this.repositoryURLs.clone(); |
947 |
result.jarFiles = this.jarFiles.clone(); |
948 |
result.jarRealFiles = this.jarRealFiles.clone(); |
949 |
result.jarNames = this.jarNames.clone(); |
950 |
result.lastModifiedDates = this.lastModifiedDates.clone(); |
951 |
result.paths = this.paths.clone(); |
952 |
result.notFoundResources.putAll(this.notFoundResources); |
953 |
result.permissionList.addAll(this.permissionList); |
954 |
result.loaderPC.putAll(this.loaderPC); |
955 |
result.contextName = this.contextName; |
956 |
result.hasExternalRepositories = this.hasExternalRepositories; |
957 |
result.searchExternalFirst = this.searchExternalFirst; |
958 |
|
959 |
try { |
960 |
result.start(); |
961 |
} catch (LifecycleException e) { |
962 |
throw new IllegalStateException(e); |
963 |
} |
964 |
|
965 |
return result; |
966 |
} |
967 |
|
968 |
/** |
969 |
* Add a new repository to the set of places this ClassLoader can look for |
970 |
* classes to be loaded. |
971 |
* |
972 |
* @param repository Name of a source of classes to be loaded, such as a |
973 |
* directory pathname, a JAR file pathname, or a ZIP file pathname |
974 |
* |
975 |
* @exception IllegalArgumentException if the specified repository is |
976 |
* invalid or does not exist |
977 |
*/ |
978 |
public void addRepository(String repository) { |
979 |
|
980 |
// Ignore any of the standard repositories, as they are set up using |
981 |
// either addJar or addRepository |
982 |
if (repository.startsWith("/WEB-INF/lib") |
983 |
|| repository.startsWith("/WEB-INF/classes")) |
984 |
return; |
985 |
|
986 |
// Add this repository to our underlying class loader |
987 |
try { |
988 |
URL url = new URL(repository); |
989 |
super.addURL(url); |
990 |
hasExternalRepositories = true; |
991 |
repositoryURLs = null; |
992 |
} catch (MalformedURLException e) { |
993 |
IllegalArgumentException iae = new IllegalArgumentException |
994 |
("Invalid repository: " + repository); |
995 |
iae.initCause(e); |
996 |
throw iae; |
997 |
} |
998 |
|
999 |
} |
1000 |
|
1001 |
|
1002 |
/** |
1003 |
* Add a new repository to the set of places this ClassLoader can look for |
1004 |
* classes to be loaded. |
1005 |
* |
1006 |
* @param repository Name of a source of classes to be loaded, such as a |
1007 |
* directory pathname, a JAR file pathname, or a ZIP file pathname |
1008 |
* |
1009 |
* @exception IllegalArgumentException if the specified repository is |
1010 |
* invalid or does not exist |
1011 |
*/ |
1012 |
synchronized void addRepository(String repository, File file) { |
1013 |
|
1014 |
// Note : There should be only one (of course), but I think we should |
1015 |
// keep this a bit generic |
1016 |
|
1017 |
if (repository == null) |
1018 |
return; |
1019 |
|
1020 |
if (log.isDebugEnabled()) |
1021 |
log.debug("addRepository(" + repository + ")"); |
1022 |
|
1023 |
int i; |
1024 |
|
1025 |
// Add this repository to our internal list |
1026 |
String[] result = new String[repositories.length + 1]; |
1027 |
for (i = 0; i < repositories.length; i++) { |
1028 |
result[i] = repositories[i]; |
1029 |
} |
1030 |
result[repositories.length] = repository; |
1031 |
repositories = result; |
1032 |
|
1033 |
// Add the file to the list |
1034 |
File[] result2 = new File[files.length + 1]; |
1035 |
for (i = 0; i < files.length; i++) { |
1036 |
result2[i] = files[i]; |
1037 |
} |
1038 |
result2[files.length] = file; |
1039 |
files = result2; |
1040 |
|
1041 |
} |
1042 |
|
1043 |
|
1044 |
synchronized void addJar(String jar, JarFile jarFile, File file) |
1045 |
throws IOException { |
1046 |
|
1047 |
if (jar == null) |
1048 |
return; |
1049 |
if (jarFile == null) |
1050 |
return; |
1051 |
if (file == null) |
1052 |
return; |
1053 |
|
1054 |
if (log.isDebugEnabled()) |
1055 |
log.debug("addJar(" + jar + ")"); |
1056 |
|
1057 |
int i; |
1058 |
|
1059 |
if ((jarPath != null) && (jar.startsWith(jarPath))) { |
1060 |
|
1061 |
String jarName = jar.substring(jarPath.length()); |
1062 |
while (jarName.startsWith("/")) |
1063 |
jarName = jarName.substring(1); |
1064 |
|
1065 |
String[] result = new String[jarNames.length + 1]; |
1066 |
for (i = 0; i < jarNames.length; i++) { |
1067 |
result[i] = jarNames[i]; |
1068 |
} |
1069 |
result[jarNames.length] = jarName; |
1070 |
jarNames = result; |
1071 |
|
1072 |
} |
1073 |
|
1074 |
try { |
1075 |
|
1076 |
// Register the JAR for tracking |
1077 |
|
1078 |
long lastModified = |
1079 |
((ResourceAttributes) resources.getAttributes(jar)) |
1080 |
.getLastModified(); |
1081 |
|
1082 |
String[] result = new String[paths.length + 1]; |
1083 |
for (i = 0; i < paths.length; i++) { |
1084 |
result[i] = paths[i]; |
1085 |
} |
1086 |
result[paths.length] = jar; |
1087 |
paths = result; |
1088 |
|
1089 |
long[] result3 = new long[lastModifiedDates.length + 1]; |
1090 |
for (i = 0; i < lastModifiedDates.length; i++) { |
1091 |
result3[i] = lastModifiedDates[i]; |
1092 |
} |
1093 |
result3[lastModifiedDates.length] = lastModified; |
1094 |
lastModifiedDates = result3; |
1095 |
|
1096 |
} catch (NamingException e) { |
1097 |
// Ignore |
1098 |
} |
1099 |
|
1100 |
// If the JAR currently contains invalid classes, don't actually use it |
1101 |
// for classloading |
1102 |
if (!validateJarFile(file)) |
1103 |
return; |
1104 |
|
1105 |
JarFile[] result2 = new JarFile[jarFiles.length + 1]; |
1106 |
for (i = 0; i < jarFiles.length; i++) { |
1107 |
result2[i] = jarFiles[i]; |
1108 |
} |
1109 |
result2[jarFiles.length] = jarFile; |
1110 |
jarFiles = result2; |
1111 |
|
1112 |
// Add the file to the list |
1113 |
File[] result4 = new File[jarRealFiles.length + 1]; |
1114 |
for (i = 0; i < jarRealFiles.length; i++) { |
1115 |
result4[i] = jarRealFiles[i]; |
1116 |
} |
1117 |
result4[jarRealFiles.length] = file; |
1118 |
jarRealFiles = result4; |
1119 |
} |
1120 |
|
1121 |
|
1122 |
/** |
1123 |
* Return a String array of the current repositories for this class |
1124 |
* loader. If there are no repositories, a zero-length array is |
1125 |
* returned.For security reason, returns a clone of the Array (since |
1126 |
* String are immutable). |
1127 |
*/ |
1128 |
public String[] findRepositories() { |
1129 |
|
1130 |
return (repositories.clone()); |
1131 |
|
1132 |
} |
1133 |
|
1134 |
|
1135 |
/** |
1136 |
* Have one or more classes or resources been modified so that a reload |
1137 |
* is appropriate? |
1138 |
*/ |
1139 |
public boolean modified() { |
1140 |
|
1141 |
if (log.isDebugEnabled()) |
1142 |
log.debug("modified()"); |
1143 |
|
1144 |
// Checking for modified loaded resources |
1145 |
int length = paths.length; |
1146 |
|
1147 |
// A rare race condition can occur in the updates of the two arrays |
1148 |
// It's totally ok if the latest class added is not checked (it will |
1149 |
// be checked the next time |
1150 |
int length2 = lastModifiedDates.length; |
1151 |
if (length > length2) |
1152 |
length = length2; |
1153 |
|
1154 |
for (int i = 0; i < length; i++) { |
1155 |
try { |
1156 |
long lastModified = |
1157 |
((ResourceAttributes) resources.getAttributes(paths[i])) |
1158 |
.getLastModified(); |
1159 |
if (lastModified != lastModifiedDates[i]) { |
1160 |
if( log.isDebugEnabled() ) |
1161 |
log.debug(" Resource '" + paths[i] |
1162 |
+ "' was modified; Date is now: " |
1163 |
+ new java.util.Date(lastModified) + " Was: " |
1164 |
+ new java.util.Date(lastModifiedDates[i])); |
1165 |
return (true); |
1166 |
} |
1167 |
} catch (NamingException e) { |
1168 |
log.error(" Resource '" + paths[i] + "' is missing"); |
1169 |
return (true); |
1170 |
} |
1171 |
} |
1172 |
|
1173 |
length = jarNames.length; |
1174 |
|
1175 |
// Check if JARs have been added or removed |
1176 |
if (getJarPath() != null) { |
1177 |
|
1178 |
try { |
1179 |
NamingEnumeration<Binding> enumeration = |
1180 |
resources.listBindings(getJarPath()); |
1181 |
int i = 0; |
1182 |
while (enumeration.hasMoreElements() && (i < length)) { |
1183 |
NameClassPair ncPair = enumeration.nextElement(); |
1184 |
String name = ncPair.getName(); |
1185 |
// Ignore non JARs present in the lib folder |
1186 |
if (!name.endsWith(".jar")) |
1187 |
continue; |
1188 |
if (!name.equals(jarNames[i])) { |
1189 |
// Missing JAR |
1190 |
log.info(" Additional JARs have been added : '" |
1191 |
+ name + "'"); |
1192 |
return (true); |
1193 |
} |
1194 |
i++; |
1195 |
} |
1196 |
if (enumeration.hasMoreElements()) { |
1197 |
while (enumeration.hasMoreElements()) { |
1198 |
NameClassPair ncPair = enumeration.nextElement(); |
1199 |
String name = ncPair.getName(); |
1200 |
// Additional non-JAR files are allowed |
1201 |
if (name.endsWith(".jar")) { |
1202 |
// There was more JARs |
1203 |
log.info(" Additional JARs have been added"); |
1204 |
return (true); |
1205 |
} |
1206 |
} |
1207 |
} else if (i < jarNames.length) { |
1208 |
// There was less JARs |
1209 |
log.info(" Additional JARs have been added"); |
1210 |
return (true); |
1211 |
} |
1212 |
} catch (NamingException e) { |
1213 |
if (log.isDebugEnabled()) |
1214 |
log.debug(" Failed tracking modifications of '" |
1215 |
+ getJarPath() + "'"); |
1216 |
} catch (ClassCastException e) { |
1217 |
log.error(" Failed tracking modifications of '" |
1218 |
+ getJarPath() + "' : " + e.getMessage()); |
1219 |
} |
1220 |
|
1221 |
} |
1222 |
|
1223 |
// No classes have been modified |
1224 |
return (false); |
1225 |
|
1226 |
} |
1227 |
|
1228 |
|
1229 |
/** |
1230 |
* Render a String representation of this object. |
1231 |
*/ |
1232 |
@Override |
1233 |
public String toString() { |
1234 |
|
1235 |
StringBuilder sb = new StringBuilder("WebappClassLoader\r\n"); |
1236 |
sb.append(" context: "); |
1237 |
sb.append(contextName); |
1238 |
sb.append("\r\n"); |
1239 |
sb.append(" delegate: "); |
1240 |
sb.append(delegate); |
1241 |
sb.append("\r\n"); |
1242 |
sb.append(" repositories:\r\n"); |
1243 |
if (repositories != null) { |
1244 |
for (int i = 0; i < repositories.length; i++) { |
1245 |
sb.append(" "); |
1246 |
sb.append(repositories[i]); |
1247 |
sb.append("\r\n"); |
1248 |
} |
1249 |
} |
1250 |
if (this.parent != null) { |
1251 |
sb.append("----------> Parent Classloader:\r\n"); |
1252 |
sb.append(this.parent.toString()); |
1253 |
sb.append("\r\n"); |
1254 |
} |
1255 |
if (this.transformers.size() > 0) { |
1256 |
sb.append("----------> Class file transformers:\r\n"); |
1257 |
for (ClassFileTransformer transformer : this.transformers) { |
1258 |
sb.append(transformer).append("\r\n"); |
1259 |
} |
1260 |
} |
1261 |
return (sb.toString()); |
1262 |
|
1263 |
} |
1264 |
|
1265 |
|
1266 |
// ---------------------------------------------------- ClassLoader Methods |
1267 |
|
1268 |
|
1269 |
/** |
1270 |
* Add the specified URL to the classloader. |
1271 |
*/ |
1272 |
@Override |
1273 |
protected void addURL(URL url) { |
1274 |
super.addURL(url); |
1275 |
hasExternalRepositories = true; |
1276 |
repositoryURLs = null; |
1277 |
} |
1278 |
|
1279 |
|
1280 |
/** |
1281 |
* Expose this method for use by the unit tests. |
1282 |
*/ |
1283 |
protected final Class<?> doDefineClass(String name, byte[] b, int off, int len, |
1284 |
ProtectionDomain protectionDomain) { |
1285 |
return super.defineClass(name, b, off, len, protectionDomain); |
1286 |
} |
1287 |
|
1288 |
/** |
1289 |
* Find the specified class in our local repositories, if possible. If |
1290 |
* not found, throw <code>ClassNotFoundException</code>. |
1291 |
* |
1292 |
* @param name Name of the class to be loaded |
1293 |
* |
1294 |
* @exception ClassNotFoundException if the class was not found |
1295 |
*/ |
1296 |
@Override |
1297 |
public Class<?> findClass(String name) throws ClassNotFoundException { |
1298 |
|
1299 |
if (log.isDebugEnabled()) |
1300 |
log.debug(" findClass(" + name + ")"); |
1301 |
|
1302 |
// Cannot load anything from local repositories if class loader is stopped |
1303 |
if (!started) { |
1304 |
throw new ClassNotFoundException(name); |
1305 |
} |
1306 |
|
1307 |
// (1) Permission to define this class when using a SecurityManager |
1308 |
if (securityManager != null) { |
1309 |
int i = name.lastIndexOf('.'); |
1310 |
if (i >= 0) { |
1311 |
try { |
1312 |
if (log.isTraceEnabled()) |
1313 |
log.trace(" securityManager.checkPackageDefinition"); |
1314 |
securityManager.checkPackageDefinition(name.substring(0,i)); |
1315 |
} catch (Exception se) { |
1316 |
if (log.isTraceEnabled()) |
1317 |
log.trace(" -->Exception-->ClassNotFoundException", se); |
1318 |
throw new ClassNotFoundException(name, se); |
1319 |
} |
1320 |
} |
1321 |
} |
1322 |
|
1323 |
// Ask our superclass to locate this class, if possible |
1324 |
// (throws ClassNotFoundException if it is not found) |
1325 |
Class<?> clazz = null; |
1326 |
try { |
1327 |
if (log.isTraceEnabled()) |
1328 |
log.trace(" findClassInternal(" + name + ")"); |
1329 |
if (hasExternalRepositories && searchExternalFirst) { |
1330 |
try { |
1331 |
clazz = super.findClass(name); |
1332 |
} catch(ClassNotFoundException cnfe) { |
1333 |
// Ignore - will search internal repositories next |
1334 |
} catch(AccessControlException ace) { |
1335 |
log.warn("WebappClassLoader.findClassInternal(" + name |
1336 |
+ ") security exception: " + ace.getMessage(), ace); |
1337 |
throw new ClassNotFoundException(name, ace); |
1338 |
} catch (RuntimeException e) { |
1339 |
if (log.isTraceEnabled()) |
1340 |
log.trace(" -->RuntimeException Rethrown", e); |
1341 |
throw e; |
1342 |
} |
1343 |
} |
1344 |
if ((clazz == null)) { |
1345 |
try { |
1346 |
clazz = findClassInternal(name); |
1347 |
} catch(ClassNotFoundException cnfe) { |
1348 |
if (!hasExternalRepositories || searchExternalFirst) { |
1349 |
throw cnfe; |
1350 |
} |
1351 |
} catch(AccessControlException ace) { |
1352 |
log.warn("WebappClassLoader.findClassInternal(" + name |
1353 |
+ ") security exception: " + ace.getMessage(), ace); |
1354 |
throw new ClassNotFoundException(name, ace); |
1355 |
} catch (RuntimeException e) { |
1356 |
if (log.isTraceEnabled()) |
1357 |
log.trace(" -->RuntimeException Rethrown", e); |
1358 |
throw e; |
1359 |
} |
1360 |
} |
1361 |
if ((clazz == null) && hasExternalRepositories && !searchExternalFirst) { |
1362 |
try { |
1363 |
clazz = super.findClass(name); |
1364 |
} catch(AccessControlException ace) { |
1365 |
log.warn("WebappClassLoader.findClassInternal(" + name |
1366 |
+ ") security exception: " + ace.getMessage(), ace); |
1367 |
throw new ClassNotFoundException(name, ace); |
1368 |
} catch (RuntimeException e) { |
1369 |
if (log.isTraceEnabled()) |
1370 |
log.trace(" -->RuntimeException Rethrown", e); |
1371 |
throw e; |
1372 |
} |
1373 |
} |
1374 |
if (clazz == null) { |
1375 |
if (log.isDebugEnabled()) |
1376 |
log.debug(" --> Returning ClassNotFoundException"); |
1377 |
throw new ClassNotFoundException(name); |
1378 |
} |
1379 |
} catch (ClassNotFoundException e) { |
1380 |
if (log.isTraceEnabled()) |
1381 |
log.trace(" --> Passing on ClassNotFoundException"); |
1382 |
throw e; |
1383 |
} |
1384 |
|
1385 |
// Return the class we have located |
1386 |
if (log.isTraceEnabled()) |
1387 |
log.debug(" Returning class " + clazz); |
1388 |
|
1389 |
if (log.isTraceEnabled()) { |
1390 |
ClassLoader cl; |
1391 |
if (Globals.IS_SECURITY_ENABLED){ |
1392 |
cl = AccessController.doPrivileged( |
1393 |
new PrivilegedGetClassLoader(clazz)); |
1394 |
} else { |
1395 |
cl = clazz.getClassLoader(); |
1396 |
} |
1397 |
log.debug(" Loaded by " + cl.toString()); |
1398 |
} |
1399 |
return (clazz); |
1400 |
|
1401 |
} |
1402 |
|
1403 |
|
1404 |
/** |
1405 |
* Find the specified resource in our local repository, and return a |
1406 |
* <code>URL</code> referring to it, or <code>null</code> if this resource |
1407 |
* cannot be found. |
1408 |
* |
1409 |
* @param name Name of the resource to be found |
1410 |
*/ |
1411 |
@Override |
1412 |
public URL findResource(final String name) { |
1413 |
|
1414 |
if (log.isDebugEnabled()) |
1415 |
log.debug(" findResource(" + name + ")"); |
1416 |
|
1417 |
URL url = null; |
1418 |
|
1419 |
if (hasExternalRepositories && searchExternalFirst) |
1420 |
url = super.findResource(name); |
1421 |
|
1422 |
if (url == null) { |
1423 |
ResourceEntry entry = resourceEntries.get(name); |
1424 |
if (entry == null) { |
1425 |
if (securityManager != null) { |
1426 |
PrivilegedAction<ResourceEntry> dp = |
1427 |
new PrivilegedFindResourceByName(name, name, false); |
1428 |
entry = AccessController.doPrivileged(dp); |
1429 |
} else { |
1430 |
entry = findResourceInternal(name, name, false); |
1431 |
} |
1432 |
} |
1433 |
if (entry != null) { |
1434 |
url = entry.source; |
1435 |
} |
1436 |
} |
1437 |
|
1438 |
if ((url == null) && hasExternalRepositories && !searchExternalFirst) |
1439 |
url = super.findResource(name); |
1440 |
|
1441 |
if (log.isDebugEnabled()) { |
1442 |
if (url != null) |
1443 |
log.debug(" --> Returning '" + url.toString() + "'"); |
1444 |
else |
1445 |
log.debug(" --> Resource not found, returning null"); |
1446 |
} |
1447 |
return (url); |
1448 |
|
1449 |
} |
1450 |
|
1451 |
|
1452 |
/** |
1453 |
* Return an enumeration of <code>URLs</code> representing all of the |
1454 |
* resources with the given name. If no resources with this name are |
1455 |
* found, return an empty enumeration. |
1456 |
* |
1457 |
* @param name Name of the resources to be found |
1458 |
* |
1459 |
* @exception IOException if an input/output error occurs |
1460 |
*/ |
1461 |
@Override |
1462 |
public Enumeration<URL> findResources(String name) throws IOException { |
1463 |
|
1464 |
if (log.isDebugEnabled()) |
1465 |
log.debug(" findResources(" + name + ")"); |
1466 |
|
1467 |
//we use a LinkedHashSet instead of a Vector to avoid duplicates with virtualmappings |
1468 |
LinkedHashSet<URL> result = new LinkedHashSet<URL>(); |
1469 |
|
1470 |
int jarFilesLength = jarFiles.length; |
1471 |
int repositoriesLength = repositories.length; |
1472 |
|
1473 |
int i; |
1474 |
|
1475 |
// Adding the results of a call to the superclass |
1476 |
if (hasExternalRepositories && searchExternalFirst) { |
1477 |
|
1478 |
Enumeration<URL> otherResourcePaths = super.findResources(name); |
1479 |
|
1480 |
while (otherResourcePaths.hasMoreElements()) { |
1481 |
result.add(otherResourcePaths.nextElement()); |
1482 |
} |
1483 |
|
1484 |
} |
1485 |
// Looking at the repositories |
1486 |
for (i = 0; i < repositoriesLength; i++) { |
1487 |
try { |
1488 |
String fullPath = repositories[i] + name; |
1489 |
resources.lookup(fullPath); |
1490 |
// Note : Not getting an exception here means the resource was |
1491 |
// found |
1492 |
try { |
1493 |
result.add(getURI(new File(files[i], name))); |
1494 |
} catch (MalformedURLException e) { |
1495 |
// Ignore |
1496 |
} |
1497 |
} catch (NamingException e) { |
1498 |
// Ignore |
1499 |
} |
1500 |
} |
1501 |
|
1502 |
// Looking at the JAR files |
1503 |
synchronized (jarFiles) { |
1504 |
if (openJARs()) { |
1505 |
for (i = 0; i < jarFilesLength; i++) { |
1506 |
JarEntry jarEntry = jarFiles[i].getJarEntry(name); |
1507 |
if (jarEntry != null) { |
1508 |
try { |
1509 |
String jarFakeUrl = getURI(jarRealFiles[i]).toString(); |
1510 |
jarFakeUrl = "jar:" + jarFakeUrl + "!/" + name; |
1511 |
result.add(new URL(jarFakeUrl)); |
1512 |
} catch (MalformedURLException e) { |
1513 |
// Ignore |
1514 |
} |
1515 |
} |
1516 |
} |
1517 |
} |
1518 |
} |
1519 |
|
1520 |
// Adding the results of a call to the superclass |
1521 |
if (hasExternalRepositories && !searchExternalFirst) { |
1522 |
|
1523 |
Enumeration<URL> otherResourcePaths = super.findResources(name); |
1524 |
|
1525 |
while (otherResourcePaths.hasMoreElements()) { |
1526 |
result.add(otherResourcePaths.nextElement()); |
1527 |
} |
1528 |
|
1529 |
} |
1530 |
|
1531 |
return Collections.enumeration(result); |
1532 |
} |
1533 |
|
1534 |
|
1535 |
/** |
1536 |
* Find the resource with the given name. A resource is some data |
1537 |
* (images, audio, text, etc.) that can be accessed by class code in a |
1538 |
* way that is independent of the location of the code. The name of a |
1539 |
* resource is a "/"-separated path name that identifies the resource. |
1540 |
* If the resource cannot be found, return <code>null</code>. |
1541 |
* <p> |
1542 |
* This method searches according to the following algorithm, returning |
1543 |
* as soon as it finds the appropriate URL. If the resource cannot be |
1544 |
* found, returns <code>null</code>. |
1545 |
* <ul> |
1546 |
* <li>If the <code>delegate</code> property is set to <code>true</code>, |
1547 |
* call the <code>getResource()</code> method of the parent class |
1548 |
* loader, if any.</li> |
1549 |
* <li>Call <code>findResource()</code> to find this resource in our |
1550 |
* locally defined repositories.</li> |
1551 |
* <li>Call the <code>getResource()</code> method of the parent class |
1552 |
* loader, if any.</li> |
1553 |
* </ul> |
1554 |
* |
1555 |
* @param name Name of the resource to return a URL for |
1556 |
*/ |
1557 |
@Override |
1558 |
public URL getResource(String name) { |
1559 |
|
1560 |
if (log.isDebugEnabled()) |
1561 |
log.debug("getResource(" + name + ")"); |
1562 |
URL url = null; |
1563 |
|
1564 |
// (1) Delegate to parent if requested |
1565 |
if (delegate) { |
1566 |
if (log.isDebugEnabled()) |
1567 |
log.debug(" Delegating to parent classloader " + parent); |
1568 |
url = parent.getResource(name); |
1569 |
if (url != null) { |
1570 |
if (log.isDebugEnabled()) |
1571 |
log.debug(" --> Returning '" + url.toString() + "'"); |
1572 |
return (url); |
1573 |
} |
1574 |
} |
1575 |
|
1576 |
// (2) Search local repositories |
1577 |
url = findResource(name); |
1578 |
if (url != null) { |
1579 |
// Locating the repository for special handling in the case |
1580 |
// of a JAR |
1581 |
if (antiJARLocking) { |
1582 |
ResourceEntry entry = resourceEntries.get(name); |
1583 |
try { |
1584 |
String repository = entry.codeBase.toString(); |
1585 |
if ((repository.endsWith(".jar")) |
1586 |
&& (!(name.endsWith(CLASS_FILE_SUFFIX)))) { |
1587 |
// Copy binary content to the work directory if not present |
1588 |
File resourceFile = new File(loaderDir, name); |
1589 |
url = getURI(resourceFile); |
1590 |
} |
1591 |
} catch (Exception e) { |
1592 |
// Ignore |
1593 |
} |
1594 |
} |
1595 |
if (log.isDebugEnabled()) |
1596 |
log.debug(" --> Returning '" + url.toString() + "'"); |
1597 |
return (url); |
1598 |
} |
1599 |
|
1600 |
// (3) Delegate to parent unconditionally if not already attempted |
1601 |
if( !delegate ) { |
1602 |
url = parent.getResource(name); |
1603 |
if (url != null) { |
1604 |
if (log.isDebugEnabled()) |
1605 |
log.debug(" --> Returning '" + url.toString() + "'"); |
1606 |
return (url); |
1607 |
} |
1608 |
} |
1609 |
|
1610 |
// (4) Resource was not found |
1611 |
if (log.isDebugEnabled()) |
1612 |
log.debug(" --> Resource not found, returning null"); |
1613 |
return (null); |
1614 |
|
1615 |
} |
1616 |
|
1617 |
|
1618 |
/** |
1619 |
* Find the resource with the given name, and return an input stream |
1620 |
* that can be used for reading it. The search order is as described |
1621 |
* for <code>getResource()</code>, after checking to see if the resource |
1622 |
* data has been previously cached. If the resource cannot be found, |
1623 |
* return <code>null</code>. |
1624 |
* |
1625 |
* @param name Name of the resource to return an input stream for |
1626 |
*/ |
1627 |
@Override |
1628 |
public InputStream getResourceAsStream(String name) { |
1629 |
|
1630 |
if (log.isDebugEnabled()) |
1631 |
log.debug("getResourceAsStream(" + name + ")"); |
1632 |
InputStream stream = null; |
1633 |
|
1634 |
// (0) Check for a cached copy of this resource |
1635 |
stream = findLoadedResource(name); |
1636 |
if (stream != null) { |
1637 |
if (log.isDebugEnabled()) |
1638 |
log.debug(" --> Returning stream from cache"); |
1639 |
return (stream); |
1640 |
} |
1641 |
|
1642 |
// (1) Delegate to parent if requested |
1643 |
if (delegate) { |
1644 |
if (log.isDebugEnabled()) |
1645 |
log.debug(" Delegating to parent classloader " + parent); |
1646 |
stream = parent.getResourceAsStream(name); |
1647 |
if (stream != null) { |
1648 |
// FIXME - cache??? |
1649 |
if (log.isDebugEnabled()) |
1650 |
log.debug(" --> Returning stream from parent"); |
1651 |
return (stream); |
1652 |
} |
1653 |
} |
1654 |
|
1655 |
// (2) Search local repositories |
1656 |
if (log.isDebugEnabled()) |
1657 |
log.debug(" Searching local repositories"); |
1658 |
URL url = findResource(name); |
1659 |
if (url != null) { |
1660 |
// FIXME - cache??? |
1661 |
if (log.isDebugEnabled()) |
1662 |
log.debug(" --> Returning stream from local"); |
1663 |
stream = findLoadedResource(name); |
1664 |
try { |
1665 |
if (hasExternalRepositories && (stream == null)) |
1666 |
stream = url.openStream(); |
1667 |
} catch (IOException e) { |
1668 |
// Ignore |
1669 |
} |
1670 |
if (stream != null) |
1671 |
return (stream); |
1672 |
} |
1673 |
|
1674 |
// (3) Delegate to parent unconditionally |
1675 |
if (!delegate) { |
1676 |
if (log.isDebugEnabled()) |
1677 |
log.debug(" Delegating to parent classloader unconditionally " + parent); |
1678 |
stream = parent.getResourceAsStream(name); |
1679 |
if (stream != null) { |
1680 |
// FIXME - cache??? |
1681 |
if (log.isDebugEnabled()) |
1682 |
log.debug(" --> Returning stream from parent"); |
1683 |
return (stream); |
1684 |
} |
1685 |
} |
1686 |
|
1687 |
// (4) Resource was not found |
1688 |
if (log.isDebugEnabled()) |
1689 |
log.debug(" --> Resource not found, returning null"); |
1690 |
return (null); |
1691 |
|
1692 |
} |
1693 |
|
1694 |
|
1695 |
/** |
1696 |
* Load the class with the specified name. This method searches for |
1697 |
* classes in the same manner as <code>loadClass(String, boolean)</code> |
1698 |
* with <code>false</code> as the second argument. |
1699 |
* |
1700 |
* @param name Name of the class to be loaded |
1701 |
* |
1702 |
* @exception ClassNotFoundException if the class was not found |
1703 |
*/ |
1704 |
@Override |
1705 |
public Class<?> loadClass(String name) throws ClassNotFoundException { |
1706 |
|
1707 |
return (loadClass(name, false)); |
1708 |
|
1709 |
} |
1710 |
|
1711 |
|
1712 |
/** |
1713 |
* Load the class with the specified name, searching using the following |
1714 |
* algorithm until it finds and returns the class. If the class cannot |
1715 |
* be found, returns <code>ClassNotFoundException</code>. |
1716 |
* <ul> |
1717 |
* <li>Call <code>findLoadedClass(String)</code> to check if the |
1718 |
* class has already been loaded. If it has, the same |
1719 |
* <code>Class</code> object is returned.</li> |
1720 |
* <li>If the <code>delegate</code> property is set to <code>true</code>, |
1721 |
* call the <code>loadClass()</code> method of the parent class |
1722 |
* loader, if any.</li> |
1723 |
* <li>Call <code>findClass()</code> to find this class in our locally |
1724 |
* defined repositories.</li> |
1725 |
* <li>Call the <code>loadClass()</code> method of our parent |
1726 |
* class loader, if any.</li> |
1727 |
* </ul> |
1728 |
* If the class was found using the above steps, and the |
1729 |
* <code>resolve</code> flag is <code>true</code>, this method will then |
1730 |
* call <code>resolveClass(Class)</code> on the resulting Class object. |
1731 |
* |
1732 |
* @param name Name of the class to be loaded |
1733 |
* @param resolve If <code>true</code> then resolve the class |
1734 |
* |
1735 |
* @exception ClassNotFoundException if the class was not found |
1736 |
*/ |
1737 |
@Override |
1738 |
public synchronized Class<?> loadClass(String name, boolean resolve) |
1739 |
throws ClassNotFoundException { |
1740 |
|
1741 |
if (log.isDebugEnabled()) |
1742 |
log.debug("loadClass(" + name + ", " + resolve + ")"); |
1743 |
Class<?> clazz = null; |
1744 |
|
1745 |
// Log access to stopped classloader |
1746 |
if (!started) { |
1747 |
try { |
1748 |
throw new IllegalStateException(); |
1749 |
} catch (IllegalStateException e) { |
1750 |
log.info(sm.getString("webappClassLoader.stopped", name), e); |
1751 |
} |
1752 |
} |
1753 |
|
1754 |
// (0) Check our previously loaded local class cache |
1755 |
clazz = findLoadedClass0(name); |
1756 |
if (clazz != null) { |
1757 |
if (log.isDebugEnabled()) |
1758 |
log.debug(" Returning class from cache"); |
1759 |
if (resolve) |
1760 |
resolveClass(clazz); |
1761 |
return (clazz); |
1762 |
} |
1763 |
|
1764 |
// (0.1) Check our previously loaded class cache |
1765 |
clazz = findLoadedClass(name); |
1766 |
if (clazz != null) { |
1767 |
if (log.isDebugEnabled()) |
1768 |
log.debug(" Returning class from cache"); |
1769 |
if (resolve) |
1770 |
resolveClass(clazz); |
1771 |
return (clazz); |
1772 |
} |
1773 |
|
1774 |
// (0.2) Try loading the class with the system class loader, to prevent |
1775 |
// the webapp from overriding J2SE classes |
1776 |
try { |
1777 |
clazz = j2seClassLoader.loadClass(name); |
1778 |
if (clazz != null) { |
1779 |
if (resolve) |
1780 |
resolveClass(clazz); |
1781 |
return (clazz); |
1782 |
} |
1783 |
} catch (ClassNotFoundException e) { |
1784 |
// Ignore |
1785 |
} |
1786 |
|
1787 |
// (0.5) Permission to access this class when using a SecurityManager |
1788 |
if (securityManager != null) { |
1789 |
int i = name.lastIndexOf('.'); |
1790 |
if (i >= 0) { |
1791 |
try { |
1792 |
securityManager.checkPackageAccess(name.substring(0,i)); |
1793 |
} catch (SecurityException se) { |
1794 |
String error = "Security Violation, attempt to use " + |
1795 |
"Restricted Class: " + name; |
1796 |
log.info(error, se); |
1797 |
throw new ClassNotFoundException(error, se); |
1798 |
} |
1799 |
} |
1800 |
} |
1801 |
|
1802 |
boolean delegateLoad = delegate || filter(name); |
1803 |
|
1804 |
// (1) Delegate to our parent if requested |
1805 |
if (delegateLoad) { |
1806 |
if (log.isDebugEnabled()) |
1807 |
log.debug(" Delegating to parent classloader1 " + parent); |
1808 |
try { |
1809 |
clazz = Class.forName(name, false, parent); |
1810 |
if (clazz != null) { |
1811 |
if (log.isDebugEnabled()) |
1812 |
log.debug(" Loading class from parent"); |
1813 |
if (resolve) |
1814 |
resolveClass(clazz); |
1815 |
return (clazz); |
1816 |
} |
1817 |
} catch (ClassNotFoundException e) { |
1818 |
// Ignore |
1819 |
} |
1820 |
} |
1821 |
|
1822 |
// (2) Search local repositories |
1823 |
if (log.isDebugEnabled()) |
1824 |
log.debug(" Searching local repositories"); |
1825 |
try { |
1826 |
clazz = findClass(name); |
1827 |
if (clazz != null) { |
1828 |
if (log.isDebugEnabled()) |
1829 |
log.debug(" Loading class from local repository"); |
1830 |
if (resolve) |
1831 |
resolveClass(clazz); |
1832 |
return (clazz); |
1833 |
} |
1834 |
} catch (ClassNotFoundException e) { |
1835 |
// Ignore |
1836 |
} |
1837 |
|
1838 |
// (3) Delegate to parent unconditionally |
1839 |
if (!delegateLoad) { |
1840 |
if (log.isDebugEnabled()) |
1841 |
log.debug(" Delegating to parent classloader at end: " + parent); |
1842 |
try { |
1843 |
clazz = Class.forName(name, false, parent); |
1844 |
if (clazz != null) { |
1845 |
if (log.isDebugEnabled()) |
1846 |
log.debug(" Loading class from parent"); |
1847 |
if (resolve) |
1848 |
resolveClass(clazz); |
1849 |
return (clazz); |
1850 |
} |
1851 |
} catch (ClassNotFoundException e) { |
1852 |
// Ignore |
1853 |
} |
1854 |
} |
1855 |
|
1856 |
throw new ClassNotFoundException(name); |
1857 |
|
1858 |
} |
1859 |
|
1860 |
|
1861 |
/** |
1862 |
* Get the Permissions for a CodeSource. If this instance |
1863 |
* of WebappClassLoader is for a web application context, |
1864 |
* add read FilePermission or JndiPermissions for the base |
1865 |
* directory (if unpacked), |
1866 |
* the context URL, and jar file resources. |
1867 |
* |
1868 |
* @param codeSource where the code was loaded from |
1869 |
* @return PermissionCollection for CodeSource |
1870 |
*/ |
1871 |
@Override |
1872 |
protected PermissionCollection getPermissions(CodeSource codeSource) { |
1873 |
|
1874 |
String codeUrl = codeSource.getLocation().toString(); |
1875 |
PermissionCollection pc; |
1876 |
if ((pc = loaderPC.get(codeUrl)) == null) { |
1877 |
pc = super.getPermissions(codeSource); |
1878 |
if (pc != null) { |
1879 |
Iterator<Permission> perms = permissionList.iterator(); |
1880 |
while (perms.hasNext()) { |
1881 |
Permission p = perms.next(); |
1882 |
pc.add(p); |
1883 |
} |
1884 |
loaderPC.put(codeUrl,pc); |
1885 |
} |
1886 |
} |
1887 |
return (pc); |
1888 |
|
1889 |
} |
1890 |
|
1891 |
|
1892 |
/** |
1893 |
* Returns the search path of URLs for loading classes and resources. |
1894 |
* This includes the original list of URLs specified to the constructor, |
1895 |
* along with any URLs subsequently appended by the addURL() method. |
1896 |
* @return the search path of URLs for loading classes and resources. |
1897 |
*/ |
1898 |
@Override |
1899 |
public URL[] getURLs() { |
1900 |
|
1901 |
if (repositoryURLs != null) { |
1902 |
return repositoryURLs.clone(); |
1903 |
} |
1904 |
|
1905 |
URL[] external = super.getURLs(); |
1906 |
|
1907 |
int filesLength = files.length; |
1908 |
int jarFilesLength = jarRealFiles.length; |
1909 |
int externalsLength = external.length; |
1910 |
int off = 0; |
1911 |
int i; |
1912 |
|
1913 |
try { |
1914 |
|
1915 |
URL[] urls = new URL[filesLength + jarFilesLength + externalsLength]; |
1916 |
if (searchExternalFirst) { |
1917 |
for (i = 0; i < externalsLength; i++) { |
1918 |
urls[i] = external[i]; |
1919 |
} |
1920 |
off = externalsLength; |
1921 |
} |
1922 |
for (i = 0; i < filesLength; i++) { |
1923 |
urls[off + i] = getURI(files[i]); |
1924 |
} |
1925 |
off += filesLength; |
1926 |
for (i = 0; i < jarFilesLength; i++) { |
1927 |
urls[off + i] = getURI(jarRealFiles[i]); |
1928 |
} |
1929 |
off += jarFilesLength; |
1930 |
if (!searchExternalFirst) { |
1931 |
for (i = 0; i < externalsLength; i++) { |
1932 |
urls[off + i] = external[i]; |
1933 |
} |
1934 |
} |
1935 |
|
1936 |
repositoryURLs = urls; |
1937 |
|
1938 |
} catch (MalformedURLException e) { |
1939 |
repositoryURLs = new URL[0]; |
1940 |
} |
1941 |
|
1942 |
return repositoryURLs.clone(); |
1943 |
|
1944 |
} |
1945 |
|
1946 |
|
1947 |
// ------------------------------------------------------ Lifecycle Methods |
1948 |
|
1949 |
|
1950 |
/** |
1951 |
* Add a lifecycle event listener to this component. |
1952 |
* |
1953 |
* @param listener The listener to add |
1954 |
*/ |
1955 |
@Override |
1956 |
public void addLifecycleListener(LifecycleListener listener) { |
1957 |
// NOOP |
1958 |
} |
1959 |
|
1960 |
|
1961 |
/** |
1962 |
* Get the lifecycle listeners associated with this lifecycle. If this |
1963 |
* Lifecycle has no listeners registered, a zero-length array is returned. |
1964 |
*/ |
1965 |
@Override |
1966 |
public LifecycleListener[] findLifecycleListeners() { |
1967 |
return new LifecycleListener[0]; |
1968 |
} |
1969 |
|
1970 |
|
1971 |
/** |
1972 |
* Remove a lifecycle event listener from this component. |
1973 |
* |
1974 |
* @param listener The listener to remove |
1975 |
*/ |
1976 |
@Override |
1977 |
public void removeLifecycleListener(LifecycleListener listener) { |
1978 |
// NOOP |
1979 |
} |
1980 |
|
1981 |
|
1982 |
/** |
1983 |
* Obtain the current state of the source component. |
1984 |
* |
1985 |
* @return The current state of the source component. |
1986 |
*/ |
1987 |
@Override |
1988 |
public LifecycleState getState() { |
1989 |
return LifecycleState.NEW; |
1990 |
} |
1991 |
|
1992 |
|
1993 |
/** |
1994 |
* {@inheritDoc} |
1995 |
*/ |
1996 |
@Override |
1997 |
public String getStateName() { |
1998 |
return getState().toString(); |
1999 |
} |
2000 |
|
2001 |
|
2002 |
@Override |
2003 |
public void init() { |
2004 |
// NOOP |
2005 |
} |
2006 |
|
2007 |
|
2008 |
/** |
2009 |
* Start the class loader. |
2010 |
* |
2011 |
* @exception LifecycleException if a lifecycle error occurs |
2012 |
*/ |
2013 |
@Override |
2014 |
public void start() throws LifecycleException { |
2015 |
|
2016 |
started = true; |
2017 |
String encoding = null; |
2018 |
try { |
2019 |
encoding = System.getProperty("file.encoding"); |
2020 |
} catch (SecurityException e) { |
2021 |
return; |
2022 |
} |
2023 |
if (encoding.indexOf("EBCDIC")!=-1) { |
2024 |
needConvert = true; |
2025 |
} |
2026 |
|
2027 |
for (int i = 0; i < repositories.length; i++) { |
2028 |
if (repositories[i].equals("/WEB-INF/classes/")) { |
2029 |
try { |
2030 |
webInfClassesCodeBase = files[i].toURI().toURL(); |
2031 |
} catch (MalformedURLException e) { |
2032 |
// Ignore - leave it as null |
2033 |
} |
2034 |
break; |
2035 |
} |
2036 |
} |
2037 |
|
2038 |
} |
2039 |
|
2040 |
|
2041 |
public boolean isStarted() { |
2042 |
return started; |
2043 |
} |
2044 |
|
2045 |
/** |
2046 |
* Stop the class loader. |
2047 |
* |
2048 |
* @exception LifecycleException if a lifecycle error occurs |
2049 |
*/ |
2050 |
@Override |
2051 |
public void stop() throws LifecycleException { |
2052 |
|
2053 |
// Clearing references should be done before setting started to |
2054 |
// false, due to possible side effects |
2055 |
clearReferences(); |
2056 |
|
2057 |
started = false; |
2058 |
|
2059 |
int length = files.length; |
2060 |
for (int i = 0; i < length; i++) { |
2061 |
files[i] = null; |
2062 |
} |
2063 |
|
2064 |
length = jarFiles.length; |
2065 |
for (int i = 0; i < length; i++) { |
2066 |
try { |
2067 |
if (jarFiles[i] != null) { |
2068 |
jarFiles[i].close(); |
2069 |
} |
2070 |
} catch (IOException e) { |
2071 |
// Ignore |
2072 |
} |
2073 |
jarFiles[i] = null; |
2074 |
} |
2075 |
|
2076 |
notFoundResources.clear(); |
2077 |
resourceEntries.clear(); |
2078 |
resources = null; |
2079 |
repositories = null; |
2080 |
repositoryURLs = null; |
2081 |
files = null; |
2082 |
jarFiles = null; |
2083 |
jarRealFiles = null; |
2084 |
jarPath = null; |
2085 |
jarNames = null; |
2086 |
lastModifiedDates = null; |
2087 |
paths = null; |
2088 |
hasExternalRepositories = false; |
2089 |
parent = null; |
2090 |
webInfClassesCodeBase = null; |
2091 |
|
2092 |
permissionList.clear(); |
2093 |
loaderPC.clear(); |
2094 |
|
2095 |
if (loaderDir != null) { |
2096 |
deleteDir(loaderDir); |
2097 |
} |
2098 |
|
2099 |
} |
2100 |
|
2101 |
|
2102 |
@Override |
2103 |
public void destroy() { |
2104 |
// NOOP |
2105 |
} |
2106 |
|
2107 |
|
2108 |
/** |
2109 |
* Used to periodically signal to the classloader to release |
2110 |
* JAR resources. |
2111 |
*/ |
2112 |
public void closeJARs(boolean force) { |
2113 |
if (jarFiles.length > 0) { |
2114 |
synchronized (jarFiles) { |
2115 |
if (force || (System.currentTimeMillis() |
2116 |
> (lastJarAccessed + 90000))) { |
2117 |
for (int i = 0; i < jarFiles.length; i++) { |
2118 |
try { |
2119 |
if (jarFiles[i] != null) { |
2120 |
jarFiles[i].close(); |
2121 |
jarFiles[i] = null; |
2122 |
} |
2123 |
} catch (IOException e) { |
2124 |
if (log.isDebugEnabled()) { |
2125 |
log.debug("Failed to close JAR", e); |
2126 |
} |
2127 |
} |
2128 |
} |
2129 |
} |
2130 |
} |
2131 |
} |
2132 |
} |
2133 |
|
2134 |
|
2135 |
// ------------------------------------------------------ Protected Methods |
2136 |
|
2137 |
protected ClassLoader getJavaseClassLoader() { |
2138 |
return j2seClassLoader; |
2139 |
} |
2140 |
|
2141 |
protected void setJavaseClassLoader(ClassLoader classLoader) { |
2142 |
if (classLoader == null) { |
2143 |
throw new IllegalArgumentException( |
2144 |
sm.getString("webappClassLoader.javaseClassLoaderNull")); |
2145 |
} |
2146 |
j2seClassLoader = classLoader; |
2147 |
} |
2148 |
|
2149 |
/** |
2150 |
* Clear references. |
2151 |
*/ |
2152 |
protected void clearReferences() { |
2153 |
|
2154 |
// De-register any remaining JDBC drivers |
2155 |
clearReferencesJdbc(); |
2156 |
|
2157 |
// Stop any threads the web application started |
2158 |
clearReferencesThreads(); |
2159 |
|
2160 |
// Check for leaks triggered by ThreadLocals loaded by this class loader |
2161 |
checkThreadLocalsForLeaks(); |
2162 |
|
2163 |
// Clear RMI Targets loaded by this class loader |
2164 |
clearReferencesRmiTargets(); |
2165 |
|
2166 |
// Null out any static or final fields from loaded classes, |
2167 |
// as a workaround for apparent garbage collection bugs |
2168 |
if (clearReferencesStatic) { |
2169 |
clearReferencesStaticFinal(); |
2170 |
} |
2171 |
|
2172 |
// Clear the IntrospectionUtils cache. |
2173 |
IntrospectionUtils.clear(); |
2174 |
|
2175 |
// Clear the classloader reference in common-logging |
2176 |
if (clearReferencesLogFactoryRelease) { |
2177 |
org.apache.juli.logging.LogFactory.release(this); |
2178 |
} |
2179 |
|
2180 |
// Clear the resource bundle cache |
2181 |
// This shouldn't be necessary, the cache uses weak references but |
2182 |
// it has caused leaks. Oddly, using the leak detection code in |
2183 |
// standard host allows the class loader to be GC'd. This has been seen |
2184 |
// on Sun but not IBM JREs. Maybe a bug in Sun's GC impl? |
2185 |
clearReferencesResourceBundles(); |
2186 |
|
2187 |
// Clear the classloader reference in the VM's bean introspector |
2188 |
java.beans.Introspector.flushCaches(); |
2189 |
|
2190 |
} |
2191 |
|
2192 |
|
2193 |
/** |
2194 |
* Deregister any JDBC drivers registered by the webapp that the webapp |
2195 |
* forgot. This is made unnecessary complex because a) DriverManager |
2196 |
* checks the class loader of the calling class (it would be much easier |
2197 |
* if it checked the context class loader) b) using reflection would |
2198 |
* create a dependency on the DriverManager implementation which can, |
2199 |
* and has, changed. |
2200 |
* |
2201 |
* We can't just create an instance of JdbcLeakPrevention as it will be |
2202 |
* loaded by the common class loader (since it's .class file is in the |
2203 |
* $CATALINA_HOME/lib directory). This would fail DriverManager's check |
2204 |
* on the class loader of the calling class. So, we load the bytes via |
2205 |
* our parent class loader but define the class with this class loader |
2206 |
* so the JdbcLeakPrevention looks like a webapp class to the |
2207 |
* DriverManager. |
2208 |
* |
2209 |
* If only apps cleaned up after themselves... |
2210 |
*/ |
2211 |
private final void clearReferencesJdbc() { |
2212 |
InputStream is = getResourceAsStream( |
2213 |
"org/apache/catalina/loader/JdbcLeakPrevention.class"); |
2214 |
// We know roughly how big the class will be (~ 1K) so allow 2k as a |
2215 |
// starting point |
2216 |
byte[] classBytes = new byte[2048]; |
2217 |
int offset = 0; |
2218 |
try { |
2219 |
int read = is.read(classBytes, offset, classBytes.length-offset); |
2220 |
while (read > -1) { |
2221 |
offset += read; |
2222 |
if (offset == classBytes.length) { |
2223 |
// Buffer full - double size |
2224 |
byte[] tmp = new byte[classBytes.length * 2]; |
2225 |
System.arraycopy(classBytes, 0, tmp, 0, classBytes.length); |
2226 |
classBytes = tmp; |
2227 |
} |
2228 |
read = is.read(classBytes, offset, classBytes.length-offset); |
2229 |
} |
2230 |
Class<?> lpClass = |
2231 |
defineClass("org.apache.catalina.loader.JdbcLeakPrevention", |
2232 |
classBytes, 0, offset, this.getClass().getProtectionDomain()); |
2233 |
Object obj = lpClass.newInstance(); |
2234 |
@SuppressWarnings("unchecked") // clearJdbcDriverRegistrations() returns List<String> |
2235 |
List<String> driverNames = (List<String>) obj.getClass().getMethod( |
2236 |
"clearJdbcDriverRegistrations").invoke(obj); |
2237 |
for (String name : driverNames) { |
2238 |
log.error(sm.getString("webappClassLoader.clearJdbc", |
2239 |
contextName, name)); |
2240 |
} |
2241 |
} catch (Exception e) { |
2242 |
// So many things to go wrong above... |
2243 |
Throwable t = ExceptionUtils.unwrapInvocationTargetException(e); |
2244 |
ExceptionUtils.handleThrowable(t); |
2245 |
log.warn(sm.getString( |
2246 |
"webappClassLoader.jdbcRemoveFailed", contextName), t); |
2247 |
} finally { |
2248 |
if (is != null) { |
2249 |
try { |
2250 |
is.close(); |
2251 |
} catch (IOException ioe) { |
2252 |
log.warn(sm.getString( |
2253 |
"webappClassLoader.jdbcRemoveStreamError", |
2254 |
contextName), ioe); |
2255 |
} |
2256 |
} |
2257 |
} |
2258 |
} |
2259 |
|
2260 |
|
2261 |
private final void clearReferencesStaticFinal() { |
2262 |
|
2263 |
@SuppressWarnings("unchecked") // resourceEntries is HashMap<String, ResourceEntry> |
2264 |
Collection<ResourceEntry> values = |
2265 |
((HashMap<String,ResourceEntry>) resourceEntries.clone()).values(); |
2266 |
Iterator<ResourceEntry> loadedClasses = values.iterator(); |
2267 |
// |
2268 |
// walk through all loaded class to trigger initialization for |
2269 |
// any uninitialized classes, otherwise initialization of |
2270 |
// one class may call a previously cleared class. |
2271 |
while(loadedClasses.hasNext()) { |
2272 |
ResourceEntry entry = loadedClasses.next(); |
2273 |
if (entry.loadedClass != null) { |
2274 |
Class<?> clazz = entry.loadedClass; |
2275 |
try { |
2276 |
Field[] fields = clazz.getDeclaredFields(); |
2277 |
for (int i = 0; i < fields.length; i++) { |
2278 |
if(Modifier.isStatic(fields[i].getModifiers())) { |
2279 |
fields[i].get(null); |
2280 |
break; |
2281 |
} |
2282 |
} |
2283 |
} catch(Throwable t) { |
2284 |
// Ignore |
2285 |
} |
2286 |
} |
2287 |
} |
2288 |
loadedClasses = values.iterator(); |
2289 |
while (loadedClasses.hasNext()) { |
2290 |
ResourceEntry entry = loadedClasses.next(); |
2291 |
if (entry.loadedClass != null) { |
2292 |
Class<?> clazz = entry.loadedClass; |
2293 |
try { |
2294 |
Field[] fields = clazz.getDeclaredFields(); |
2295 |
for (int i = 0; i < fields.length; i++) { |
2296 |
Field field = fields[i]; |
2297 |
int mods = field.getModifiers(); |
2298 |
if (field.getType().isPrimitive() |
2299 |
|| (field.getName().indexOf("$") != -1)) { |
2300 |
continue; |
2301 |
} |
2302 |
if (Modifier.isStatic(mods)) { |
2303 |
try { |
2304 |
field.setAccessible(true); |
2305 |
if (Modifier.isFinal(mods)) { |
2306 |
if (!((field.getType().getName().startsWith("java.")) |
2307 |
|| (field.getType().getName().startsWith("javax.")))) { |
2308 |
nullInstance(field.get(null)); |
2309 |
} |
2310 |
} else { |
2311 |
field.set(null, null); |
2312 |
if (log.isDebugEnabled()) { |
2313 |
log.debug("Set field " + field.getName() |
2314 |
+ " to null in class " + clazz.getName()); |
2315 |
} |
2316 |
} |
2317 |
} catch (Throwable t) { |
2318 |
ExceptionUtils.handleThrowable(t); |
2319 |
if (log.isDebugEnabled()) { |
2320 |
log.debug("Could not set field " + field.getName() |
2321 |
+ " to null in class " + clazz.getName(), t); |
2322 |
} |
2323 |
} |
2324 |
} |
2325 |
} |
2326 |
} catch (Throwable t) { |
2327 |
ExceptionUtils.handleThrowable(t); |
2328 |
if (log.isDebugEnabled()) { |
2329 |
log.debug("Could not clean fields for class " + clazz.getName(), t); |
2330 |
} |
2331 |
} |
2332 |
} |
2333 |
} |
2334 |
|
2335 |
} |
2336 |
|
2337 |
|
2338 |
private void nullInstance(Object instance) { |
2339 |
if (instance == null) { |
2340 |
return; |
2341 |
} |
2342 |
Field[] fields = instance.getClass().getDeclaredFields(); |
2343 |
for (int i = 0; i < fields.length; i++) { |
2344 |
Field field = fields[i]; |
2345 |
int mods = field.getModifiers(); |
2346 |
if (field.getType().isPrimitive() |
2347 |
|| (field.getName().indexOf("$") != -1)) { |
2348 |
continue; |
2349 |
} |
2350 |
try { |
2351 |
field.setAccessible(true); |
2352 |
if (Modifier.isStatic(mods) && Modifier.isFinal(mods)) { |
2353 |
// Doing something recursively is too risky |
2354 |
continue; |
2355 |
} |
2356 |
Object value = field.get(instance); |
2357 |
if (null != value) { |
2358 |
Class<? extends Object> valueClass = value.getClass(); |
2359 |
if (!loadedByThisOrChild(valueClass)) { |
2360 |
if (log.isDebugEnabled()) { |
2361 |
log.debug("Not setting field " + field.getName() + |
2362 |
" to null in object of class " + |
2363 |
instance.getClass().getName() + |
2364 |
" because the referenced object was of type " + |
2365 |
valueClass.getName() + |
2366 |
" which was not loaded by this WebappClassLoader."); |
2367 |
} |
2368 |
} else { |
2369 |
field.set(instance, null); |
2370 |
if (log.isDebugEnabled()) { |
2371 |
log.debug("Set field " + field.getName() |
2372 |
+ " to null in class " + instance.getClass().getName()); |
2373 |
} |
2374 |
} |
2375 |
} |
2376 |
} catch (Throwable t) { |
2377 |
ExceptionUtils.handleThrowable(t); |
2378 |
if (log.isDebugEnabled()) { |
2379 |
log.debug("Could not set field " + field.getName() |
2380 |
+ " to null in object instance of class " |
2381 |
+ instance.getClass().getName(), t); |
2382 |
} |
2383 |
} |
2384 |
} |
2385 |
} |
2386 |
|
2387 |
|
2388 |
@SuppressWarnings("deprecation") // thread.stop() |
2389 |
private void clearReferencesThreads() { |
2390 |
Thread[] threads = getThreads(); |
2391 |
List<Thread> executorThreadsToStop = new ArrayList<Thread>(); |
2392 |
|
2393 |
// Iterate over the set of threads |
2394 |
for (Thread thread : threads) { |
2395 |
if (thread != null) { |
2396 |
ClassLoader ccl = thread.getContextClassLoader(); |
2397 |
if (ccl == this) { |
2398 |
// Don't warn about this thread |
2399 |
if (thread == Thread.currentThread()) { |
2400 |
continue; |
2401 |
} |
2402 |
|
2403 |
// JVM controlled threads |
2404 |
ThreadGroup tg = thread.getThreadGroup(); |
2405 |
if (tg != null && |
2406 |
JVM_THREAD_GROUP_NAMES.contains(tg.getName())) { |
2407 |
|
2408 |
// HttpClient keep-alive threads |
2409 |
if (clearReferencesHttpClientKeepAliveThread && |
2410 |
thread.getName().equals("Keep-Alive-Timer")) { |
2411 |
thread.setContextClassLoader(parent); |
2412 |
log.debug(sm.getString( |
2413 |
"webappClassLoader.checkThreadsHttpClient")); |
2414 |
} |
2415 |
|
2416 |
// Don't warn about remaining JVM controlled threads |
2417 |
continue; |
2418 |
} |
2419 |
|
2420 |
// Skip threads that have already died |
2421 |
if (!thread.isAlive()) { |
2422 |
continue; |
2423 |
} |
2424 |
|
2425 |
// TimerThread can be stopped safely so treat separately |
2426 |
// "java.util.TimerThread" in Sun/Oracle JDK |
2427 |
// "java.util.Timer$TimerImpl" in Apache Harmony and in IBM JDK |
2428 |
if (thread.getClass().getName().startsWith("java.util.Timer") && |
2429 |
clearReferencesStopTimerThreads) { |
2430 |
clearReferencesStopTimerThread(thread); |
2431 |
continue; |
2432 |
} |
2433 |
|
2434 |
if (isRequestThread(thread)) { |
2435 |
log.error(sm.getString("webappClassLoader.warnRequestThread", |
2436 |
contextName, thread.getName())); |
2437 |
} else { |
2438 |
log.error(sm.getString("webappClassLoader.warnThread", |
2439 |
contextName, thread.getName())); |
2440 |
} |
2441 |
|
2442 |
// Don't try an stop the threads unless explicitly |
2443 |
// configured to do so |
2444 |
if (!clearReferencesStopThreads) { |
2445 |
continue; |
2446 |
} |
2447 |
|
2448 |
// If the thread has been started via an executor, try |
2449 |
// shutting down the executor |
2450 |
boolean usingExecutor = false; |
2451 |
try { |
2452 |
|
2453 |
// Runnable wrapped by Thread |
2454 |
// "target" in Sun/Oracle JDK |
2455 |
// "runnable" in IBM JDK |
2456 |
// "action" in Apache Harmony |
2457 |
Object target = null; |
2458 |
for (String fieldName : new String[] { "target", |
2459 |
"runnable", "action" }) { |
2460 |
try { |
2461 |
Field targetField = thread.getClass() |
2462 |
.getDeclaredField(fieldName); |
2463 |
targetField.setAccessible(true); |
2464 |
target = targetField.get(thread); |
2465 |
break; |
2466 |
} catch (NoSuchFieldException nfe) { |
2467 |
continue; |
2468 |
} |
2469 |
} |
2470 |
|
2471 |
// "java.util.concurrent" code is in public domain, |
2472 |
// so all implementations are similar |
2473 |
if (target != null && |
2474 |
target.getClass().getCanonicalName() != null |
2475 |
&& target.getClass().getCanonicalName().equals( |
2476 |
"java.util.concurrent.ThreadPoolExecutor.Worker")) { |
2477 |
Field executorField = |
2478 |
target.getClass().getDeclaredField("this$0"); |
2479 |
executorField.setAccessible(true); |
2480 |
Object executor = executorField.get(target); |
2481 |
if (executor instanceof ThreadPoolExecutor) { |
2482 |
((ThreadPoolExecutor) executor).shutdownNow(); |
2483 |
usingExecutor = true; |
2484 |
} |
2485 |
} |
2486 |
} catch (SecurityException e) { |
2487 |
log.warn(sm.getString( |
2488 |
"webappClassLoader.stopThreadFail", |
2489 |
thread.getName(), contextName), e); |
2490 |
} catch (NoSuchFieldException e) { |
2491 |
log.warn(sm.getString( |
2492 |
"webappClassLoader.stopThreadFail", |
2493 |
thread.getName(), contextName), e); |
2494 |
} catch (IllegalArgumentException e) { |
2495 |
log.warn(sm.getString( |
2496 |
"webappClassLoader.stopThreadFail", |
2497 |
thread.getName(), contextName), e); |
2498 |
} catch (IllegalAccessException e) { |
2499 |
log.warn(sm.getString( |
2500 |
"webappClassLoader.stopThreadFail", |
2501 |
thread.getName(), contextName), e); |
2502 |
} |
2503 |
|
2504 |
if (usingExecutor) { |
2505 |
// Executor may take a short time to stop all the |
2506 |
// threads. Make a note of threads that should be |
2507 |
// stopped and check them at the end of the method. |
2508 |
executorThreadsToStop.add(thread); |
2509 |
} else { |
2510 |
// This method is deprecated and for good reason. This |
2511 |
// is very risky code but is the only option at this |
2512 |
// point. A *very* good reason for apps to do this |
2513 |
// clean-up themselves. |
2514 |
thread.stop(); |
2515 |
} |
2516 |
} |
2517 |
} |
2518 |
} |
2519 |
|
2520 |
// If thread stopping is enabled, executor threads should have been |
2521 |
// stopped above when the executor was shut down but that depends on the |
2522 |
// thread correctly handling the interrupt. Give all the executor |
2523 |
// threads a few seconds shutdown and if they are still running |
2524 |
// Give threads up to 2 seconds to shutdown |
2525 |
int count = 0; |
2526 |
for (Thread t : executorThreadsToStop) { |
2527 |
while (t.isAlive() && count < 100) { |
2528 |
try { |
2529 |
Thread.sleep(20); |
2530 |
} catch (InterruptedException e) { |
2531 |
// Quit the while loop |
2532 |
break; |
2533 |
} |
2534 |
count++; |
2535 |
} |
2536 |
if (t.isAlive()) { |
2537 |
// This method is deprecated and for good reason. This is |
2538 |
// very risky code but is the only option at this point. |
2539 |
// A *very* good reason for apps to do this clean-up |
2540 |
// themselves. |
2541 |
t.stop(); |
2542 |
} |
2543 |
} |
2544 |
} |
2545 |
|
2546 |
|
2547 |
/* |
2548 |
* Look at a threads stack trace to see if it is a request thread or not. It |
2549 |
* isn't perfect, but it should be good-enough for most cases. |
2550 |
*/ |
2551 |
private boolean isRequestThread(Thread thread) { |
2552 |
|
2553 |
StackTraceElement[] elements = thread.getStackTrace(); |
2554 |
|
2555 |
if (elements == null || elements.length == 0) { |
2556 |
// Must have stopped already. Too late to ignore it. Assume not a |
2557 |
// request processing thread. |
2558 |
return false; |
2559 |
} |
2560 |
|
2561 |
// Step through the methods in reverse order looking for calls to any |
2562 |
// CoyoteAdapter method. All request threads will have this unless |
2563 |
// Tomcat has been heavily modified - in which case there isn't much we |
2564 |
// can do. |
2565 |
for (int i = 0; i < elements.length; i++) { |
2566 |
StackTraceElement element = elements[elements.length - (i+1)]; |
2567 |
if ("org.apache.catalina.connector.CoyoteAdapter".equals( |
2568 |
element.getClassName())) { |
2569 |
return true; |
2570 |
} |
2571 |
} |
2572 |
return false; |
2573 |
} |
2574 |
|
2575 |
|
2576 |
private void clearReferencesStopTimerThread(Thread thread) { |
2577 |
|
2578 |
// Need to get references to: |
2579 |
// in Sun/Oracle JDK: |
2580 |
// - newTasksMayBeScheduled field (in java.util.TimerThread) |
2581 |
// - queue field |
2582 |
// - queue.clear() |
2583 |
// in IBM JDK, Apache Harmony: |
2584 |
// - cancel() method (in java.util.Timer$TimerImpl) |
2585 |
|
2586 |
try { |
2587 |
|
2588 |
try { |
2589 |
Field newTasksMayBeScheduledField = |
2590 |
thread.getClass().getDeclaredField("newTasksMayBeScheduled"); |
2591 |
newTasksMayBeScheduledField.setAccessible(true); |
2592 |
Field queueField = thread.getClass().getDeclaredField("queue"); |
2593 |
queueField.setAccessible(true); |
2594 |
|
2595 |
Object queue = queueField.get(thread); |
2596 |
|
2597 |
Method clearMethod = queue.getClass().getDeclaredMethod("clear"); |
2598 |
clearMethod.setAccessible(true); |
2599 |
|
2600 |
synchronized(queue) { |
2601 |
newTasksMayBeScheduledField.setBoolean(thread, false); |
2602 |
clearMethod.invoke(queue); |
2603 |
queue.notify(); // In case queue was already empty. |
2604 |
} |
2605 |
|
2606 |
}catch (NoSuchFieldException nfe){ |
2607 |
Method cancelMethod = thread.getClass().getDeclaredMethod("cancel"); |
2608 |
synchronized(thread) { |
2609 |
cancelMethod.setAccessible(true); |
2610 |
cancelMethod.invoke(thread); |
2611 |
} |
2612 |
} |
2613 |
|
2614 |
log.error(sm.getString("webappClassLoader.warnTimerThread", |
2615 |
contextName, thread.getName())); |
2616 |
|
2617 |
} catch (Exception e) { |
2618 |
// So many things to go wrong above... |
2619 |
Throwable t = ExceptionUtils.unwrapInvocationTargetException(e); |
2620 |
ExceptionUtils.handleThrowable(t); |
2621 |
log.warn(sm.getString( |
2622 |
"webappClassLoader.stopTimerThreadFail", |
2623 |
thread.getName(), contextName), t); |
2624 |
} |
2625 |
} |
2626 |
|
2627 |
private void checkThreadLocalsForLeaks() { |
2628 |
Thread[] threads = getThreads(); |
2629 |
|
2630 |
try { |
2631 |
// Make the fields in the Thread class that store ThreadLocals |
2632 |
// accessible |
2633 |
Field threadLocalsField = |
2634 |
Thread.class.getDeclaredField("threadLocals"); |
2635 |
threadLocalsField.setAccessible(true); |
2636 |
Field inheritableThreadLocalsField = |
2637 |
Thread.class.getDeclaredField("inheritableThreadLocals"); |
2638 |
inheritableThreadLocalsField.setAccessible(true); |
2639 |
// Make the underlying array of ThreadLoad.ThreadLocalMap.Entry objects |
2640 |
// accessible |
2641 |
Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); |
2642 |
Field tableField = tlmClass.getDeclaredField("table"); |
2643 |
tableField.setAccessible(true); |
2644 |
Method expungeStaleEntriesMethod = tlmClass.getDeclaredMethod("expungeStaleEntries"); |
2645 |
expungeStaleEntriesMethod.setAccessible(true); |
2646 |
|
2647 |
for (int i = 0; i < threads.length; i++) { |
2648 |
Object threadLocalMap; |
2649 |
if (threads[i] != null) { |
2650 |
|
2651 |
// Clear the first map |
2652 |
threadLocalMap = threadLocalsField.get(threads[i]); |
2653 |
if (null != threadLocalMap){ |
2654 |
expungeStaleEntriesMethod.invoke(threadLocalMap); |
2655 |
checkThreadLocalMapForLeaks(threadLocalMap, tableField); |
2656 |
} |
2657 |
|
2658 |
// Clear the second map |
2659 |
threadLocalMap =inheritableThreadLocalsField.get(threads[i]); |
2660 |
if (null != threadLocalMap){ |
2661 |
expungeStaleEntriesMethod.invoke(threadLocalMap); |
2662 |
checkThreadLocalMapForLeaks(threadLocalMap, tableField); |
2663 |
} |
2664 |
} |
2665 |
} |
2666 |
} catch (Throwable t) { |
2667 |
ExceptionUtils.handleThrowable(t); |
2668 |
log.warn(sm.getString( |
2669 |
"webappClassLoader.checkThreadLocalsForLeaksFail", |
2670 |
getContextName()), t); |
2671 |
} |
2672 |
} |
2673 |
|
2674 |
|
2675 |
/** |
2676 |
* Analyzes the given thread local map object. Also pass in the field that |
2677 |
* points to the internal table to save re-calculating it on every |
2678 |
* call to this method. |
2679 |
*/ |
2680 |
private void checkThreadLocalMapForLeaks(Object map, |
2681 |
Field internalTableField) throws IllegalAccessException, |
2682 |
NoSuchFieldException { |
2683 |
if (map != null) { |
2684 |
Object[] table = (Object[]) internalTableField.get(map); |
2685 |
if (table != null) { |
2686 |
for (int j =0; j < table.length; j++) { |
2687 |
Object obj = table[j]; |
2688 |
if (obj != null) { |
2689 |
boolean potentialLeak = false; |
2690 |
// Check the key |
2691 |
Object key = ((Reference<?>) obj).get(); |
2692 |
if (this.equals(key) || loadedByThisOrChild(key)) { |
2693 |
potentialLeak = true; |
2694 |
} |
2695 |
// Check the value |
2696 |
Field valueField = |
2697 |
obj.getClass().getDeclaredField("value"); |
2698 |
valueField.setAccessible(true); |
2699 |
Object value = valueField.get(obj); |
2700 |
if (this.equals(value) || loadedByThisOrChild(value)) { |
2701 |
potentialLeak = true; |
2702 |
} |
2703 |
if (potentialLeak) { |
2704 |
Object[] args = new Object[5]; |
2705 |
args[0] = contextName; |
2706 |
if (key != null) { |
2707 |
args[1] = getPrettyClassName(key.getClass()); |
2708 |
try { |
2709 |
args[2] = key.toString(); |
2710 |
} catch (Exception e) { |
2711 |
log.error(sm.getString( |
2712 |
"webappClassLoader.checkThreadLocalsForLeaks.badKey", |
2713 |
args[1]), e); |
2714 |
args[2] = sm.getString( |
2715 |
"webappClassLoader.checkThreadLocalsForLeaks.unknown"); |
2716 |
} |
2717 |
} |
2718 |
if (value != null) { |
2719 |
args[3] = getPrettyClassName(value.getClass()); |
2720 |
try { |
2721 |
args[4] = value.toString(); |
2722 |
} catch (Exception e) { |
2723 |
log.error(sm.getString( |
2724 |
"webappClassLoader.checkThreadLocalsForLeaks.badValue", |
2725 |
args[3]), e); |
2726 |
args[4] = sm.getString( |
2727 |
"webappClassLoader.checkThreadLocalsForLeaks.unknown"); |
2728 |
} |
2729 |
} |
2730 |
if (value == null) { |
2731 |
if (log.isDebugEnabled()) { |
2732 |
log.debug(sm.getString( |
2733 |
"webappClassLoader.checkThreadLocalsForLeaksDebug", |
2734 |
args)); |
2735 |
} |
2736 |
} else { |
2737 |
log.error(sm.getString( |
2738 |
"webappClassLoader.checkThreadLocalsForLeaks", |
2739 |
args)); |
2740 |
} |
2741 |
} |
2742 |
} |
2743 |
} |
2744 |
} |
2745 |
} |
2746 |
} |
2747 |
|
2748 |
private String getPrettyClassName(Class<?> clazz) { |
2749 |
String name = clazz.getCanonicalName(); |
2750 |
if (name==null){ |
2751 |
name = clazz.getName(); |
2752 |
} |
2753 |
return name; |
2754 |
} |
2755 |
|
2756 |
/** |
2757 |
* @param o object to test, may be null |
2758 |
* @return <code>true</code> if o has been loaded by the current classloader |
2759 |
* or one of its descendants. |
2760 |
*/ |
2761 |
private boolean loadedByThisOrChild(Object o) { |
2762 |
if (o == null) { |
2763 |
return false; |
2764 |
} |
2765 |
|
2766 |
Class<?> clazz; |
2767 |
if (o instanceof Class) { |
2768 |
clazz = (Class<?>) o; |
2769 |
} else { |
2770 |
clazz = o.getClass(); |
2771 |
} |
2772 |
|
2773 |
ClassLoader cl = clazz.getClassLoader(); |
2774 |
while (cl != null) { |
2775 |
if (cl == this) { |
2776 |
return true; |
2777 |
} |
2778 |
cl = cl.getParent(); |
2779 |
} |
2780 |
|
2781 |
if (o instanceof Collection<?>) { |
2782 |
Iterator<?> iter = ((Collection<?>) o).iterator(); |
2783 |
try { |
2784 |
while (iter.hasNext()) { |
2785 |
Object entry = iter.next(); |
2786 |
if (loadedByThisOrChild(entry)) { |
2787 |
return true; |
2788 |
} |
2789 |
} |
2790 |
} catch (ConcurrentModificationException e) { |
2791 |
log.warn(sm.getString( |
2792 |
"webappClassLoader", clazz.getName(), getContextName()), |
2793 |
e); |
2794 |
} |
2795 |
} |
2796 |
return false; |
2797 |
} |
2798 |
|
2799 |
/* |
2800 |
* Get the set of current threads as an array. |
2801 |
*/ |
2802 |
private Thread[] getThreads() { |
2803 |
// Get the current thread group |
2804 |
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
2805 |
// Find the root thread group |
2806 |
try { |
2807 |
while (tg.getParent() != null) { |
2808 |
tg = tg.getParent(); |
2809 |
} |
2810 |
} catch (SecurityException se) { |
2811 |
String msg = sm.getString( |
2812 |
"webappClassLoader.getThreadGroupError", tg.getName()); |
2813 |
if (log.isDebugEnabled()) { |
2814 |
log.debug(msg, se); |
2815 |
} else { |
2816 |
log.warn(msg); |
2817 |
} |
2818 |
} |
2819 |
|
2820 |
int threadCountGuess = tg.activeCount() + 50; |
2821 |
Thread[] threads = new Thread[threadCountGuess]; |
2822 |
int threadCountActual = tg.enumerate(threads); |
2823 |
// Make sure we don't miss any threads |
2824 |
while (threadCountActual == threadCountGuess) { |
2825 |
threadCountGuess *=2; |
2826 |
threads = new Thread[threadCountGuess]; |
2827 |
// Note tg.enumerate(Thread[]) silently ignores any threads that |
2828 |
// can't fit into the array |
2829 |
threadCountActual = tg.enumerate(threads); |
2830 |
} |
2831 |
|
2832 |
return threads; |
2833 |
} |
2834 |
|
2835 |
|
2836 |
/** |
2837 |
* This depends on the internals of the Sun JVM so it does everything by |
2838 |
* reflection. |
2839 |
*/ |
2840 |
private void clearReferencesRmiTargets() { |
2841 |
try { |
2842 |
// Need access to the ccl field of sun.rmi.transport.Target |
2843 |
Class<?> objectTargetClass = |
2844 |
Class.forName("sun.rmi.transport.Target"); |
2845 |
Field cclField = objectTargetClass.getDeclaredField("ccl"); |
2846 |
cclField.setAccessible(true); |
2847 |
|
2848 |
// Clear the objTable map |
2849 |
Class<?> objectTableClass = |
2850 |
Class.forName("sun.rmi.transport.ObjectTable"); |
2851 |
Field objTableField = objectTableClass.getDeclaredField("objTable"); |
2852 |
objTableField.setAccessible(true); |
2853 |
Object objTable = objTableField.get(null); |
2854 |
if (objTable == null) { |
2855 |
return; |
2856 |
} |
2857 |
|
2858 |
// Iterate over the values in the table |
2859 |
if (objTable instanceof Map<?,?>) { |
2860 |
Iterator<?> iter = ((Map<?,?>) objTable).values().iterator(); |
2861 |
while (iter.hasNext()) { |
2862 |
Object obj = iter.next(); |
2863 |
Object cclObject = cclField.get(obj); |
2864 |
if (this == cclObject) { |
2865 |
iter.remove(); |
2866 |
} |
2867 |
} |
2868 |
} |
2869 |
|
2870 |
// Clear the implTable map |
2871 |
Field implTableField = objectTableClass.getDeclaredField("implTable"); |
2872 |
implTableField.setAccessible(true); |
2873 |
Object implTable = implTableField.get(null); |
2874 |
if (implTable == null) { |
2875 |
return; |
2876 |
} |
2877 |
|
2878 |
// Iterate over the values in the table |
2879 |
if (implTable instanceof Map<?,?>) { |
2880 |
Iterator<?> iter = ((Map<?,?>) implTable).values().iterator(); |
2881 |
while (iter.hasNext()) { |
2882 |
Object obj = iter.next(); |
2883 |
Object cclObject = cclField.get(obj); |
2884 |
if (this == cclObject) { |
2885 |
iter.remove(); |
2886 |
} |
2887 |
} |
2888 |
} |
2889 |
} catch (ClassNotFoundException e) { |
2890 |
log.info(sm.getString("webappClassLoader.clearRmiInfo", |
2891 |
contextName), e); |
2892 |
} catch (SecurityException e) { |
2893 |
log.warn(sm.getString("webappClassLoader.clearRmiFail", |
2894 |
contextName), e); |
2895 |
} catch (NoSuchFieldException e) { |
2896 |
log.warn(sm.getString("webappClassLoader.clearRmiFail", |
2897 |
contextName), e); |
2898 |
} catch (IllegalArgumentException e) { |
2899 |
log.warn(sm.getString("webappClassLoader.clearRmiFail", |
2900 |
contextName), e); |
2901 |
} catch (IllegalAccessException e) { |
2902 |
log.warn(sm.getString("webappClassLoader.clearRmiFail", |
2903 |
contextName), e); |
2904 |
} |
2905 |
} |
2906 |
|
2907 |
|
2908 |
/** |
2909 |
* Clear the {@link ResourceBundle} cache of any bundles loaded by this |
2910 |
* class loader or any class loader where this loader is a parent class |
2911 |
* loader. Whilst {@link ResourceBundle#clearCache()} could be used there |
2912 |
* are complications around the |
2913 |
* {@link org.apache.jasper.servlet.JasperLoader} that mean a reflection |
2914 |
* based approach is more likely to be complete. |
2915 |
* |
2916 |
* The ResourceBundle is using WeakReferences so it shouldn't be pinning the |
2917 |
* class loader in memory. However, it is. Therefore clear ou the |
2918 |
* references. |
2919 |
*/ |
2920 |
private void clearReferencesResourceBundles() { |
2921 |
// Get a reference to the cache |
2922 |
try { |
2923 |
Field cacheListField = |
2924 |
ResourceBundle.class.getDeclaredField("cacheList"); |
2925 |
cacheListField.setAccessible(true); |
2926 |
|
2927 |
// Java 6 uses ConcurrentMap |
2928 |
// Java 5 uses SoftCache extends Abstract Map |
2929 |
// So use Map and it *should* work with both |
2930 |
Map<?,?> cacheList = (Map<?,?>) cacheListField.get(null); |
2931 |
|
2932 |
// Get the keys (loader references are in the key) |
2933 |
Set<?> keys = cacheList.keySet(); |
2934 |
|
2935 |
Field loaderRefField = null; |
2936 |
|
2937 |
// Iterate over the keys looking at the loader instances |
2938 |
Iterator<?> keysIter = keys.iterator(); |
2939 |
|
2940 |
int countRemoved = 0; |
2941 |
|
2942 |
while (keysIter.hasNext()) { |
2943 |
Object key = keysIter.next(); |
2944 |
|
2945 |
if (loaderRefField == null) { |
2946 |
loaderRefField = |
2947 |
key.getClass().getDeclaredField("loaderRef"); |
2948 |
loaderRefField.setAccessible(true); |
2949 |
} |
2950 |
WeakReference<?> loaderRef = |
2951 |
(WeakReference<?>) loaderRefField.get(key); |
2952 |
|
2953 |
ClassLoader loader = (ClassLoader) loaderRef.get(); |
2954 |
|
2955 |
while (loader != null && loader != this) { |
2956 |
loader = loader.getParent(); |
2957 |
} |
2958 |
|
2959 |
if (loader != null) { |
2960 |
keysIter.remove(); |
2961 |
countRemoved++; |
2962 |
} |
2963 |
} |
2964 |
|
2965 |
if (countRemoved > 0 && log.isDebugEnabled()) { |
2966 |
log.debug(sm.getString( |
2967 |
"webappClassLoader.clearReferencesResourceBundlesCount", |
2968 |
Integer.valueOf(countRemoved), contextName)); |
2969 |
} |
2970 |
} catch (SecurityException e) { |
2971 |
log.error(sm.getString( |
2972 |
"webappClassLoader.clearReferencesResourceBundlesFail", |
2973 |
contextName), e); |
2974 |
} catch (NoSuchFieldException e) { |
2975 |
if (Globals.IS_ORACLE_JVM) { |
2976 |
log.error(sm.getString( |
2977 |
"webappClassLoader.clearReferencesResourceBundlesFail", |
2978 |
getContextName()), e); |
2979 |
} else { |
2980 |
log.debug(sm.getString( |
2981 |
"webappClassLoader.clearReferencesResourceBundlesFail", |
2982 |
getContextName()), e); |
2983 |
} |
2984 |
} catch (IllegalArgumentException e) { |
2985 |
log.error(sm.getString( |
2986 |
"webappClassLoader.clearReferencesResourceBundlesFail", |
2987 |
contextName), e); |
2988 |
} catch (IllegalAccessException e) { |
2989 |
log.error(sm.getString( |
2990 |
"webappClassLoader.clearReferencesResourceBundlesFail", |
2991 |
contextName), e); |
2992 |
} |
2993 |
} |
2994 |
|
2995 |
|
2996 |
/** |
2997 |
* Used to periodically signal to the classloader to release JAR resources. |
2998 |
*/ |
2999 |
protected boolean openJARs() { |
3000 |
if (started && (jarFiles.length > 0)) { |
3001 |
lastJarAccessed = System.currentTimeMillis(); |
3002 |
if (jarFiles[0] == null) { |
3003 |
for (int i = 0; i < jarFiles.length; i++) { |
3004 |
try { |
3005 |
jarFiles[i] = new JarFile(jarRealFiles[i]); |
3006 |
} catch (IOException e) { |
3007 |
if (log.isDebugEnabled()) { |
3008 |
log.debug("Failed to open JAR", e); |
3009 |
} |
3010 |
return false; |
3011 |
} |
3012 |
} |
3013 |
} |
3014 |
} |
3015 |
return true; |
3016 |
} |
3017 |
|
3018 |
|
3019 |
/** |
3020 |
* Find specified class in local repositories. |
3021 |
* |
3022 |
* @return the loaded class, or null if the class isn't found |
3023 |
*/ |
3024 |
protected Class<?> findClassInternal(String name) |
3025 |
throws ClassNotFoundException { |
3026 |
|
3027 |
if (!validate(name)) |
3028 |
throw new ClassNotFoundException(name); |
3029 |
|
3030 |
String tempPath = name.replace('.', '/'); |
3031 |
String classPath = tempPath + CLASS_FILE_SUFFIX; |
3032 |
|
3033 |
ResourceEntry entry = null; |
3034 |
|
3035 |
if (securityManager != null) { |
3036 |
PrivilegedAction<ResourceEntry> dp = |
3037 |
new PrivilegedFindResourceByName(name, classPath, true); |
3038 |
entry = AccessController.doPrivileged(dp); |
3039 |
} else { |
3040 |
entry = findResourceInternal(name, classPath, true); |
3041 |
} |
3042 |
|
3043 |
if (entry == null) |
3044 |
throw new ClassNotFoundException(name); |
3045 |
|
3046 |
Class<?> clazz = entry.loadedClass; |
3047 |
if (clazz != null) |
3048 |
return clazz; |
3049 |
|
3050 |
synchronized (this) { |
3051 |
clazz = entry.loadedClass; |
3052 |
if (clazz != null) |
3053 |
return clazz; |
3054 |
|
3055 |
if (entry.binaryContent == null) |
3056 |
throw new ClassNotFoundException(name); |
3057 |
|
3058 |
// Looking up the package |
3059 |
String packageName = null; |
3060 |
int pos = name.lastIndexOf('.'); |
3061 |
if (pos != -1) |
3062 |
packageName = name.substring(0, pos); |
3063 |
|
3064 |
Package pkg = null; |
3065 |
|
3066 |
if (packageName != null) { |
3067 |
pkg = getPackage(packageName); |
3068 |
// Define the package (if null) |
3069 |
if (pkg == null) { |
3070 |
try { |
3071 |
if (entry.manifest == null) { |
3072 |
definePackage(packageName, null, null, null, null, |
3073 |
null, null, null); |
3074 |
} else { |
3075 |
definePackage(packageName, entry.manifest, |
3076 |
entry.codeBase); |
3077 |
} |
3078 |
} catch (IllegalArgumentException e) { |
3079 |
// Ignore: normal error due to dual definition of package |
3080 |
} |
3081 |
pkg = getPackage(packageName); |
3082 |
} |
3083 |
} |
3084 |
|
3085 |
if (securityManager != null) { |
3086 |
|
3087 |
// Checking sealing |
3088 |
if (pkg != null) { |
3089 |
boolean sealCheck = true; |
3090 |
if (pkg.isSealed()) { |
3091 |
sealCheck = pkg.isSealed(entry.codeBase); |
3092 |
} else { |
3093 |
sealCheck = (entry.manifest == null) |
3094 |
|| !isPackageSealed(packageName, entry.manifest); |
3095 |
} |
3096 |
if (!sealCheck) |
3097 |
throw new SecurityException |
3098 |
("Sealing violation loading " + name + " : Package " |
3099 |
+ packageName + " is sealed."); |
3100 |
} |
3101 |
|
3102 |
} |
3103 |
|
3104 |
try { |
3105 |
clazz = defineClass(name, entry.binaryContent, 0, |
3106 |
entry.binaryContent.length, |
3107 |
new CodeSource(entry.codeBase, entry.certificates)); |
3108 |
} catch (UnsupportedClassVersionError ucve) { |
3109 |
throw new UnsupportedClassVersionError( |
3110 |
ucve.getLocalizedMessage() + " " + |
3111 |
sm.getString("webappClassLoader.wrongVersion", |
3112 |
name)); |
3113 |
} |
3114 |
entry.loadedClass = clazz; |
3115 |
entry.binaryContent = null; |
3116 |
entry.source = null; |
3117 |
entry.codeBase = null; |
3118 |
entry.manifest = null; |
3119 |
entry.certificates = null; |
3120 |
} |
3121 |
|
3122 |
return clazz; |
3123 |
|
3124 |
} |
3125 |
|
3126 |
/** |
3127 |
* Find specified resource in local repositories. |
3128 |
* |
3129 |
* @return the loaded resource, or null if the resource isn't found |
3130 |
*/ |
3131 |
protected ResourceEntry findResourceInternal(File file, String path){ |
3132 |
ResourceEntry entry = new ResourceEntry(); |
3133 |
try { |
3134 |
entry.source = getURI(new File(file, path)); |
3135 |
String sourceString = entry.source.toString(); |
3136 |
if (sourceString.startsWith(webInfClassesCodeBase.toString()) && |
3137 |
sourceString.endsWith(CLASS_FILE_SUFFIX)) { |
3138 |
entry.codeBase = webInfClassesCodeBase; |
3139 |
} else { |
3140 |
entry.codeBase = entry.source; |
3141 |
} |
3142 |
} catch (MalformedURLException e) { |
3143 |
return null; |
3144 |
} |
3145 |
return entry; |
3146 |
} |
3147 |
|
3148 |
|
3149 |
/** |
3150 |
* Find specified resource in local repositories. |
3151 |
* |
3152 |
* @return the loaded resource, or null if the resource isn't found |
3153 |
*/ |
3154 |
protected ResourceEntry findResourceInternal(final String name, final String path, |
3155 |
final boolean manifestRequired) { |
3156 |
|
3157 |
if (!started) { |
3158 |
log.info(sm.getString("webappClassLoader.stopped", name)); |
3159 |
return null; |
3160 |
} |
3161 |
|
3162 |
if ((name == null) || (path == null)) |
3163 |
return null; |
3164 |
|
3165 |
ResourceEntry entry = resourceEntries.get(name); |
3166 |
if (entry != null) |
3167 |
return entry; |
3168 |
|
3169 |
int contentLength = -1; |
3170 |
InputStream binaryStream = null; |
3171 |
boolean isClassResource = path.endsWith(CLASS_FILE_SUFFIX); |
3172 |
boolean isCacheable = isClassResource; |
3173 |
if (!isCacheable) { |
3174 |
isCacheable = path.startsWith(SERVICES_PREFIX); |
3175 |
} |
3176 |
|
3177 |
int jarFilesLength = jarFiles.length; |
3178 |
int repositoriesLength = repositories.length; |
3179 |
|
3180 |
int i; |
3181 |
|
3182 |
Resource resource = null; |
3183 |
|
3184 |
boolean fileNeedConvert = false; |
3185 |
|
3186 |
for (i = 0; (entry == null) && (i < repositoriesLength); i++) { |
3187 |
try { |
3188 |
|
3189 |
String fullPath = repositories[i] + path; |
3190 |
|
3191 |
Object lookupResult = resources.lookup(fullPath); |
3192 |
if (lookupResult instanceof Resource) { |
3193 |
resource = (Resource) lookupResult; |
3194 |
} |
3195 |
|
3196 |
// Note : Not getting an exception here means the resource was |
3197 |
// found |
3198 |
|
3199 |
ResourceAttributes attributes = |
3200 |
(ResourceAttributes) resources.getAttributes(fullPath); |
3201 |
contentLength = (int) attributes.getContentLength(); |
3202 |
String canonicalPath = attributes.getCanonicalPath(); |
3203 |
if (canonicalPath != null) { |
3204 |
// we create the ResourceEntry based on the information returned |
3205 |
// by the DirContext rather than just using the path to the |
3206 |
// repository. This allows to have smart DirContext implementations |
3207 |
// that "virtualize" the docbase (e.g. Eclipse WTP) |
3208 |
entry = findResourceInternal(new File(canonicalPath), ""); |
3209 |
} else { |
3210 |
// probably a resource not in the filesystem (e.g. in a |
3211 |
// packaged war) |
3212 |
entry = findResourceInternal(files[i], path); |
3213 |
} |
3214 |
entry.lastModified = attributes.getLastModified(); |
3215 |
|
3216 |
if (resource != null) { |
3217 |
|
3218 |
|
3219 |
try { |
3220 |
binaryStream = resource.streamContent(); |
3221 |
} catch (IOException e) { |
3222 |
return null; |
3223 |
} |
3224 |
|
3225 |
if (needConvert) { |
3226 |
if (path.endsWith(".properties")) { |
3227 |
fileNeedConvert = true; |
3228 |
} |
3229 |
} |
3230 |
|
3231 |
// Register the full path for modification checking |
3232 |
// Note: Only syncing on a 'constant' object is needed |
3233 |
synchronized (allPermission) { |
3234 |
|
3235 |
int j; |
3236 |
|
3237 |
long[] result2 = |
3238 |
new long[lastModifiedDates.length + 1]; |
3239 |
for (j = 0; j < lastModifiedDates.length; j++) { |
3240 |
result2[j] = lastModifiedDates[j]; |
3241 |
} |
3242 |
result2[lastModifiedDates.length] = entry.lastModified; |
3243 |
lastModifiedDates = result2; |
3244 |
|
3245 |
String[] result = new String[paths.length + 1]; |
3246 |
for (j = 0; j < paths.length; j++) { |
3247 |
result[j] = paths[j]; |
3248 |
} |
3249 |
result[paths.length] = fullPath; |
3250 |
paths = result; |
3251 |
|
3252 |
} |
3253 |
|
3254 |
} |
3255 |
|
3256 |
} catch (NamingException e) { |
3257 |
// Ignore |
3258 |
} |
3259 |
} |
3260 |
|
3261 |
if ((entry == null) && (notFoundResources.containsKey(name))) |
3262 |
return null; |
3263 |
|
3264 |
JarEntry jarEntry = null; |
3265 |
|
3266 |
synchronized (jarFiles) { |
3267 |
|
3268 |
try { |
3269 |
if (!openJARs()) { |
3270 |
return null; |
3271 |
} |
3272 |
for (i = 0; (entry == null) && (i < jarFilesLength); i++) { |
3273 |
|
3274 |
jarEntry = jarFiles[i].getJarEntry(path); |
3275 |
|
3276 |
if (jarEntry != null) { |
3277 |
|
3278 |
entry = new ResourceEntry(); |
3279 |
try { |
3280 |
entry.codeBase = getURI(jarRealFiles[i]); |
3281 |
String jarFakeUrl = entry.codeBase.toString(); |
3282 |
jarFakeUrl = "jar:" + jarFakeUrl + "!/" + path; |
3283 |
entry.source = new URL(jarFakeUrl); |
3284 |
entry.lastModified = jarRealFiles[i].lastModified(); |
3285 |
} catch (MalformedURLException e) { |
3286 |
return null; |
3287 |
} |
3288 |
contentLength = (int) jarEntry.getSize(); |
3289 |
try { |
3290 |
if (manifestRequired) { |
3291 |
entry.manifest = jarFiles[i].getManifest(); |
3292 |
} |
3293 |
binaryStream = jarFiles[i].getInputStream(jarEntry); |
3294 |
} catch (IOException e) { |
3295 |
return null; |
3296 |
} |
3297 |
|
3298 |
// Extract resources contained in JAR to the workdir |
3299 |
if (antiJARLocking && !(path.endsWith(CLASS_FILE_SUFFIX))) { |
3300 |
byte[] buf = new byte[1024]; |
3301 |
File resourceFile = new File |
3302 |
(loaderDir, jarEntry.getName()); |
3303 |
if (!resourceFile.exists()) { |
3304 |
Enumeration<JarEntry> entries = |
3305 |
jarFiles[i].entries(); |
3306 |
while (entries.hasMoreElements()) { |
3307 |
JarEntry jarEntry2 = entries.nextElement(); |
3308 |
if (!(jarEntry2.isDirectory()) |
3309 |
&& (!jarEntry2.getName().endsWith |
3310 |
(CLASS_FILE_SUFFIX))) { |
3311 |
resourceFile = new File |
3312 |
(loaderDir, jarEntry2.getName()); |
3313 |
try { |
3314 |
if (!resourceFile.getCanonicalPath().startsWith( |
3315 |
canonicalLoaderDir)) { |
3316 |
throw new IllegalArgumentException( |
3317 |
sm.getString("webappClassLoader.illegalJarPath", |
3318 |
jarEntry2.getName())); |
3319 |
} |
3320 |
} catch (IOException ioe) { |
3321 |
throw new IllegalArgumentException( |
3322 |
sm.getString("webappClassLoader.validationErrorJarPath", |
3323 |
jarEntry2.getName()), ioe); |
3324 |
} |
3325 |
File parentFile = resourceFile.getParentFile(); |
3326 |
if (!parentFile.mkdirs() && !parentFile.exists()) { |
3327 |
// Ignore the error (like the IOExceptions below) |
3328 |
} |
3329 |
FileOutputStream os = null; |
3330 |
InputStream is = null; |
3331 |
try { |
3332 |
is = jarFiles[i].getInputStream |
3333 |
(jarEntry2); |
3334 |
os = new FileOutputStream |
3335 |
(resourceFile); |
3336 |
while (true) { |
3337 |
int n = is.read(buf); |
3338 |
if (n <= 0) { |
3339 |
break; |
3340 |
} |
3341 |
os.write(buf, 0, n); |
3342 |
} |
3343 |
resourceFile.setLastModified( |
3344 |
jarEntry2.getTime()); |
3345 |
} catch (IOException e) { |
3346 |
// Ignore |
3347 |
} finally { |
3348 |
try { |
3349 |
if (is != null) { |
3350 |
is.close(); |
3351 |
} |
3352 |
} catch (IOException e) { |
3353 |
// Ignore |
3354 |
} |
3355 |
try { |
3356 |
if (os != null) { |
3357 |
os.close(); |
3358 |
} |
3359 |
} catch (IOException e) { |
3360 |
// Ignore |
3361 |
} |
3362 |
} |
3363 |
} |
3364 |
} |
3365 |
} |
3366 |
} |
3367 |
|
3368 |
} |
3369 |
|
3370 |
} |
3371 |
|
3372 |
if (entry == null) { |
3373 |
synchronized (notFoundResources) { |
3374 |
notFoundResources.put(name, name); |
3375 |
} |
3376 |
return null; |
3377 |
} |
3378 |
|
3379 |
/* Only cache the binary content if there is some content |
3380 |
* available one of the following is true: |
3381 |
* a) It is a class file since the binary content is only cached |
3382 |
* until the class has been loaded |
3383 |
* or |
3384 |
* b) The file needs conversion to address encoding issues (see |
3385 |
* below) |
3386 |
* or |
3387 |
* c) The resource is a service provider configuration file located |
3388 |
* under META=INF/services |
3389 |
* |
3390 |
* In all other cases do not cache the content to prevent |
3391 |
* excessive memory usage if large resources are present (see |
3392 |
* https://bz.apache.org/bugzilla/show_bug.cgi?id=53081). |
3393 |
*/ |
3394 |
if (binaryStream != null && |
3395 |
(isCacheable || fileNeedConvert)) { |
3396 |
|
3397 |
byte[] binaryContent = new byte[contentLength]; |
3398 |
|
3399 |
int pos = 0; |
3400 |
try { |
3401 |
|
3402 |
while (true) { |
3403 |
int n = binaryStream.read(binaryContent, pos, |
3404 |
binaryContent.length - pos); |
3405 |
if (n <= 0) |
3406 |
break; |
3407 |
pos += n; |
3408 |
} |
3409 |
} catch (IOException e) { |
3410 |
log.error(sm.getString("webappClassLoader.readError", name), e); |
3411 |
return null; |
3412 |
} |
3413 |
if (fileNeedConvert) { |
3414 |
// Workaround for certain files on platforms that use |
3415 |
// EBCDIC encoding, when they are read through FileInputStream. |
3416 |
// See commit message of rev.303915 for details |
3417 |
// http://svn.apache.org/viewvc?view=revision&revision=303915 |
3418 |
String str = new String(binaryContent,0,pos); |
3419 |
try { |
3420 |
binaryContent = str.getBytes(CHARSET_UTF8); |
3421 |
} catch (Exception e) { |
3422 |
return null; |
3423 |
} |
3424 |
} |
3425 |
entry.binaryContent = binaryContent; |
3426 |
|
3427 |
// The certificates are only available after the JarEntry |
3428 |
// associated input stream has been fully read |
3429 |
if (jarEntry != null) { |
3430 |
entry.certificates = jarEntry.getCertificates(); |
3431 |
} |
3432 |
|
3433 |
} |
3434 |
} finally { |
3435 |
if (binaryStream != null) { |
3436 |
try { |
3437 |
binaryStream.close(); |
3438 |
} catch (IOException e) { /* Ignore */} |
3439 |
} |
3440 |
} |
3441 |
} |
3442 |
|
3443 |
if (isClassResource && entry.binaryContent != null && |
3444 |
this.transformers.size() > 0) { |
3445 |
// If the resource is a class just being loaded, decorate it |
3446 |
// with any attached transformers |
3447 |
String className = name.endsWith(CLASS_FILE_SUFFIX) ? |
3448 |
name.substring(0, name.length() - CLASS_FILE_SUFFIX.length()) : name; |
3449 |
String internalName = className.replace(".", "/"); |
3450 |
|
3451 |
for (ClassFileTransformer transformer : this.transformers) { |
3452 |
try { |
3453 |
byte[] transformed = transformer.transform( |
3454 |
this, internalName, null, null, entry.binaryContent |
3455 |
); |
3456 |
if (transformed != null) { |
3457 |
entry.binaryContent = transformed; |
3458 |
} |
3459 |
} catch (IllegalClassFormatException e) { |
3460 |
log.error(sm.getString("webappClassLoader.transformError", name), e); |
3461 |
return null; |
3462 |
} |
3463 |
} |
3464 |
} |
3465 |
|
3466 |
// Add the entry in the local resource repository |
3467 |
synchronized (resourceEntries) { |
3468 |
// Ensures that all the threads which may be in a race to load |
3469 |
// a particular class all end up with the same ResourceEntry |
3470 |
// instance |
3471 |
ResourceEntry entry2 = resourceEntries.get(name); |
3472 |
if (entry2 == null) { |
3473 |
resourceEntries.put(name, entry); |
3474 |
} else { |
3475 |
entry = entry2; |
3476 |
} |
3477 |
} |
3478 |
|
3479 |
return entry; |
3480 |
|
3481 |
} |
3482 |
|
3483 |
|
3484 |
/** |
3485 |
* Returns true if the specified package name is sealed according to the |
3486 |
* given manifest. |
3487 |
*/ |
3488 |
protected boolean isPackageSealed(String name, Manifest man) { |
3489 |
|
3490 |
String path = name.replace('.', '/') + '/'; |
3491 |
Attributes attr = man.getAttributes(path); |
3492 |
String sealed = null; |
3493 |
if (attr != null) { |
3494 |
sealed = attr.getValue(Name.SEALED); |
3495 |
} |
3496 |
if (sealed == null) { |
3497 |
if ((attr = man.getMainAttributes()) != null) { |
3498 |
sealed = attr.getValue(Name.SEALED); |
3499 |
} |
3500 |
} |
3501 |
return "true".equalsIgnoreCase(sealed); |
3502 |
|
3503 |
} |
3504 |
|
3505 |
|
3506 |
/** |
3507 |
* Finds the resource with the given name if it has previously been |
3508 |
* loaded and cached by this class loader, and return an input stream |
3509 |
* to the resource data. If this resource has not been cached, return |
3510 |
* <code>null</code>. |
3511 |
* |
3512 |
* @param name Name of the resource to return |
3513 |
*/ |
3514 |
protected InputStream findLoadedResource(String name) { |
3515 |
|
3516 |
ResourceEntry entry = resourceEntries.get(name); |
3517 |
if (entry != null) { |
3518 |
if (entry.binaryContent != null) |
3519 |
return new ByteArrayInputStream(entry.binaryContent); |
3520 |
else { |
3521 |
try { |
3522 |
return entry.source.openStream(); |
3523 |
} catch (IOException ioe) { |
3524 |
// Ignore |
3525 |
} |
3526 |
} |
3527 |
} |
3528 |
return null; |
3529 |
|
3530 |
} |
3531 |
|
3532 |
|
3533 |
/** |
3534 |
* Finds the class with the given name if it has previously been |
3535 |
* loaded and cached by this class loader, and return the Class object. |
3536 |
* If this class has not been cached, return <code>null</code>. |
3537 |
* |
3538 |
* @param name Name of the resource to return |
3539 |
*/ |
3540 |
protected Class<?> findLoadedClass0(String name) { |
3541 |
|
3542 |
ResourceEntry entry = resourceEntries.get(name); |
3543 |
if (entry != null) { |
3544 |
return entry.loadedClass; |
3545 |
} |
3546 |
return (null); // FIXME - findLoadedResource() |
3547 |
|
3548 |
} |
3549 |
|
3550 |
|
3551 |
/** |
3552 |
* Refresh the system policy file, to pick up eventual changes. |
3553 |
*/ |
3554 |
protected void refreshPolicy() { |
3555 |
|
3556 |
try { |
3557 |
// The policy file may have been modified to adjust |
3558 |
// permissions, so we're reloading it when loading or |
3559 |
// reloading a Context |
3560 |
Policy policy = Policy.getPolicy(); |
3561 |
policy.refresh(); |
3562 |
} catch (AccessControlException e) { |
3563 |
// Some policy files may restrict this, even for the core, |
3564 |
// so this exception is ignored |
3565 |
} |
3566 |
|
3567 |
} |
3568 |
|
3569 |
|
3570 |
/** |
3571 |
* Filter classes. |
3572 |
* |
3573 |
* @param name class name |
3574 |
* @return true if the class should be filtered |
3575 |
*/ |
3576 |
protected boolean filter(String name) { |
3577 |
|
3578 |
if (name == null) |
3579 |
return false; |
3580 |
|
3581 |
// Looking up the package |
3582 |
String packageName = null; |
3583 |
int pos = name.lastIndexOf('.'); |
3584 |
if (pos != -1) |
3585 |
packageName = name.substring(0, pos); |
3586 |
else |
3587 |
return false; |
3588 |
|
3589 |
for (int i = 0; i < packageTriggers.length; i++) { |
3590 |
if (packageName.startsWith(packageTriggers[i])) |
3591 |
return true; |
3592 |
} |
3593 |
|
3594 |
return false; |
3595 |
|
3596 |
} |
3597 |
|
3598 |
|
3599 |
/** |
3600 |
* Validate a classname. As per SRV.9.7.2, we must restrict loading of |
3601 |
* classes from J2SE (java.*) and most classes of the servlet API |
3602 |
* (javax.servlet.*). That should enhance robustness and prevent a number |
3603 |
* of user error (where an older version of servlet.jar would be present |
3604 |
* in /WEB-INF/lib). |
3605 |
* |
3606 |
* @param name class name |
3607 |
* @return true if the name is valid |
3608 |
*/ |
3609 |
protected boolean validate(String name) { |
3610 |
|
3611 |
// Need to be careful with order here |
3612 |
if (name == null) { |
3613 |
// Can't load a class without a name |
3614 |
return false; |
3615 |
} |
3616 |
if (name.startsWith("java.")) { |
3617 |
// Must never load java.* classes |
3618 |
return false; |
3619 |
} |
3620 |
if (name.startsWith("javax.servlet.jsp.jstl")) { |
3621 |
// OK for web apps to package JSTL |
3622 |
return true; |
3623 |
} |
3624 |
if (name.startsWith("javax.servlet.")) { |
3625 |
// Web apps should never package any other Servlet or JSP classes |
3626 |
return false; |
3627 |
} |
3628 |
if (name.startsWith("javax.el")) { |
3629 |
// Must never load javax.el.* classes |
3630 |
return false; |
3631 |
} |
3632 |
|
3633 |
// Assume everything else is OK |
3634 |
return true; |
3635 |
|
3636 |
} |
3637 |
|
3638 |
|
3639 |
/** |
3640 |
* Check the specified JAR file, and return <code>true</code> if it does |
3641 |
* not contain any of the trigger classes. |
3642 |
* |
3643 |
* @param file The JAR file to be checked |
3644 |
* |
3645 |
* @exception IOException if an input/output error occurs |
3646 |
*/ |
3647 |
protected boolean validateJarFile(File file) |
3648 |
throws IOException { |
3649 |
|
3650 |
if (triggers == null) |
3651 |
return (true); |
3652 |
|
3653 |
JarFile jarFile = null; |
3654 |
try { |
3655 |
jarFile = new JarFile(file); |
3656 |
for (int i = 0; i < triggers.length; i++) { |
3657 |
Class<?> clazz = null; |
3658 |
try { |
3659 |
if (parent != null) { |
3660 |
clazz = parent.loadClass(triggers[i]); |
3661 |
} else { |
3662 |
clazz = Class.forName(triggers[i]); |
3663 |
} |
3664 |
} catch (Exception e) { |
3665 |
clazz = null; |
3666 |
} |
3667 |
if (clazz == null) |
3668 |
continue; |
3669 |
String name = triggers[i].replace('.', '/') + CLASS_FILE_SUFFIX; |
3670 |
if (log.isDebugEnabled()) |
3671 |
log.debug(" Checking for " + name); |
3672 |
JarEntry jarEntry = jarFile.getJarEntry(name); |
3673 |
if (jarEntry != null) { |
3674 |
log.info("validateJarFile(" + file + |
3675 |
") - jar not loaded. See Servlet Spec 3.0, " |
3676 |
+ "section 10.7.2. Offending class: " + name); |
3677 |
return false; |
3678 |
} |
3679 |
} |
3680 |
return true; |
3681 |
} finally { |
3682 |
if (jarFile != null) { |
3683 |
try { |
3684 |
jarFile.close(); |
3685 |
} catch (IOException ioe) { |
3686 |
// Ignore |
3687 |
} |
3688 |
} |
3689 |
} |
3690 |
} |
3691 |
|
3692 |
|
3693 |
/** |
3694 |
* Get URL. |
3695 |
* @deprecated Use {@link #getURI(File)} instead |
3696 |
*/ |
3697 |
@Deprecated |
3698 |
protected URL getURL(File file, boolean encoded) |
3699 |
throws MalformedURLException { |
3700 |
|
3701 |
File realFile = file; |
3702 |
try { |
3703 |
realFile = realFile.getCanonicalFile(); |
3704 |
} catch (IOException e) { |
3705 |
// Ignore |
3706 |
} |
3707 |
if(encoded) { |
3708 |
return getURI(realFile); |
3709 |
} |
3710 |
|
3711 |
return realFile.toURI().toURL(); |
3712 |
} |
3713 |
|
3714 |
|
3715 |
/** |
3716 |
* Get the URI for the given file. |
3717 |
*/ |
3718 |
protected URL getURI(File file) |
3719 |
throws MalformedURLException { |
3720 |
|
3721 |
|
3722 |
File realFile = file; |
3723 |
try { |
3724 |
realFile = realFile.getCanonicalFile(); |
3725 |
} catch (IOException e) { |
3726 |
// Ignore |
3727 |
} |
3728 |
return realFile.toURI().toURL(); |
3729 |
|
3730 |
} |
3731 |
|
3732 |
|
3733 |
/** |
3734 |
* Delete the specified directory, including all of its contents and |
3735 |
* subdirectories recursively. |
3736 |
* |
3737 |
* @param dir File object representing the directory to be deleted |
3738 |
*/ |
3739 |
protected static void deleteDir(File dir) { |
3740 |
|
3741 |
String files[] = dir.list(); |
3742 |
if (files == null) { |
3743 |
files = new String[0]; |
3744 |
} |
3745 |
for (int i = 0; i < files.length; i++) { |
3746 |
File file = new File(dir, files[i]); |
3747 |
if (file.isDirectory()) { |
3748 |
deleteDir(file); |
3749 |
} else { |
3750 |
file.delete(); |
3751 |
} |
3752 |
} |
3753 |
dir.delete(); |
3754 |
|
3755 |
} |
3756 |
|
3757 |
|
3758 |
} |
68 |
} |