KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > ConfigurationClassLoader


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

17
18 package org.apache.geronimo.kernel.config;
19
20 import java.net.URI JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.ObjectStreamClass JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class ConfigurationClassLoader extends URLClassLoader JavaDoc {
36     private final URI JavaDoc id;
37     
38     public ConfigurationClassLoader(URI JavaDoc id, URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
39         super(urls, parent);
40         this.id = id;
41     }
42     
43     public URI JavaDoc getID() {
44         return id;
45     }
46
47     public void destroy() {
48         LogFactory.release(this);
49         clearSoftCache(ObjectInputStream JavaDoc.class, "subclassAudits");
50         clearSoftCache(ObjectOutputStream JavaDoc.class, "subclassAudits");
51         clearSoftCache(ObjectStreamClass JavaDoc.class, "localDescs");
52         clearSoftCache(ObjectStreamClass JavaDoc.class, "reflectors");
53     }
54
55     public String JavaDoc toString() {
56         return "[Configuration ClassLoader id=" + id + "]";
57     }
58
59     private static Object JavaDoc lock = new Object JavaDoc();
60     private static boolean clearSoftCacheFailed = false;
61     private static void clearSoftCache(Class JavaDoc clazz, String JavaDoc fieldName) {
62         Map JavaDoc cache = null;
63         try {
64             Field JavaDoc f = clazz.getDeclaredField(fieldName);
65             f.setAccessible(true);
66             cache = (Map JavaDoc) f.get(null);
67         } catch (Throwable JavaDoc e) {
68             synchronized (lock) {
69                 if (!clearSoftCacheFailed) {
70                     clearSoftCacheFailed = true;
71                     LogFactory.getLog(ConfigurationClassLoader.class).debug("Unable to clear SoftCache field " + fieldName + " in class " + clazz);
72                 }
73             }
74         }
75
76         if (cache != null) {
77             synchronized (cache) {
78                 cache.clear();
79             }
80         }
81     }
82 }
83
Popular Tags