KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > impl > ServletContextCleaner


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.logging.impl;
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23 import javax.servlet.ServletContextEvent JavaDoc;
24 import javax.servlet.ServletContextListener JavaDoc;
25
26 import org.apache.commons.logging.LogFactory;
27
28
29 /**
30  * This class is capable of receiving notifications about the undeployment of
31  * a webapp, and responds by ensuring that commons-logging releases all
32  * memory associated with the undeployed webapp.
33  * <p>
34  * In general, the WeakHashtable support added in commons-logging release 1.1
35  * ensures that logging classes do not hold references that prevent an
36  * undeployed webapp's memory from being garbage-collected even when multiple
37  * copies of commons-logging are deployed via multiple classloaders (a
38  * situation that earlier versions had problems with). However there are
39  * some rare cases where the WeakHashtable approach does not work; in these
40  * situations specifying this class as a listener for the web application will
41  * ensure that all references held by commons-logging are fully released.
42  * <p>
43  * To use this class, configure the webapp deployment descriptor to call
44  * this class on webapp undeploy; the contextDestroyed method will tell
45  * every accessable LogFactory class that the entry in its map for the
46  * current webapp's context classloader should be cleared.
47  *
48  * @since 1.1
49  */

50
51 public class ServletContextCleaner implements ServletContextListener JavaDoc {
52
53     private Class JavaDoc[] RELEASE_SIGNATURE = {ClassLoader JavaDoc.class};
54     
55     /**
56      * Invoked when a webapp is undeployed, this tells the LogFactory
57      * class to release any logging information related to the current
58      * contextClassloader.
59      */

60     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
61         ClassLoader JavaDoc tccl = Thread.currentThread().getContextClassLoader();
62
63         Object JavaDoc[] params = new Object JavaDoc[1];
64         params[0] = tccl;
65
66         // Walk up the tree of classloaders, finding all the available
67
// LogFactory classes and releasing any objects associated with
68
// the tccl (ie the webapp).
69
//
70
// When there is only one LogFactory in the classpath, and it
71
// is within the webapp being undeployed then there is no problem;
72
// garbage collection works fine.
73
//
74
// When there are multiple LogFactory classes in the classpath but
75
// parent-first classloading is used everywhere, this loop is really
76
// short. The first instance of LogFactory found will
77
// be the highest in the classpath, and then no more will be found.
78
// This is ok, as with this setup this will be the only LogFactory
79
// holding any data associated with the tccl being released.
80
//
81
// When there are multiple LogFactory classes in the classpath and
82
// child-first classloading is used in any classloader, then multiple
83
// LogFactory instances may hold info about this TCCL; whenever the
84
// webapp makes a call into a class loaded via an ancestor classloader
85
// and that class calls LogFactory the tccl gets registered in
86
// the LogFactory instance that is visible from the ancestor
87
// classloader. However the concrete logging library it points
88
// to is expected to have been loaded via the TCCL, so the
89
// underlying logging lib is only initialised/configured once.
90
// These references from ancestor LogFactory classes down to
91
// TCCL classloaders are held via weak references and so should
92
// be released but there are circumstances where they may not.
93
// Walking up the classloader ancestry ladder releasing
94
// the current tccl at each level tree, though, will definitely
95
// clear any problem references.
96
ClassLoader JavaDoc loader = tccl;
97         while (loader != null) {
98             // Load via the current loader. Note that if the class is not accessable
99
// via this loader, but is accessable via some ancestor then that class
100
// will be returned.
101
try {
102                 Class JavaDoc logFactoryClass = loader.loadClass("org.apache.commons.logging.LogFactory");
103                 Method JavaDoc releaseMethod = logFactoryClass.getMethod("release", RELEASE_SIGNATURE);
104                 releaseMethod.invoke(null, params);
105                 loader = logFactoryClass.getClassLoader().getParent();
106             } catch(ClassNotFoundException JavaDoc ex) {
107                 // Neither the current classloader nor any of its ancestors could find
108
// the LogFactory class, so we can stop now.
109
loader = null;
110             } catch(NoSuchMethodException JavaDoc ex) {
111                 // This is not expected; every version of JCL has this method
112
System.err.println("LogFactory instance found which does not support release method!");
113                 loader = null;
114             } catch(IllegalAccessException JavaDoc ex) {
115                 // This is not expected; every ancestor class should be accessable
116
System.err.println("LogFactory instance found which is not accessable!");
117                 loader = null;
118             } catch(InvocationTargetException JavaDoc ex) {
119                 // This is not expected
120
System.err.println("LogFactory instance release method failed!");
121                 loader = null;
122             }
123         }
124         
125         // Just to be sure, invoke release on the LogFactory that is visible from
126
// this ServletContextCleaner class too. This should already have been caught
127
// by the above loop but just in case...
128
LogFactory.release(tccl);
129     }
130     
131     /**
132      * Invoked when a webapp is deployed. Nothing needs to be done here.
133      */

134     public void contextInitialized(ServletContextEvent JavaDoc sce) {
135         // do nothing
136
}
137 }
138
Popular Tags