1 /**
2 *
3 * Copyright 2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.ObjectStreamClass;
26 import java.util.Map;
27 import java.lang.reflect.Field;
28
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32 *
33 * @version $Rev: 428082 $ $Date: 2006-08-02 11:35:10 -0700 (Wed, 02 Aug 2006) $
34 */
35 public class ConfigurationClassLoader extends URLClassLoader {
36 private final URI id;
37
38 public ConfigurationClassLoader(URI id, URL[] urls, ClassLoader parent) {
39 super(urls, parent);
40 this.id = id;
41 }
42
43 public URI getID() {
44 return id;
45 }
46
47 public void destroy() {
48 LogFactory.release(this);
49 clearSoftCache(ObjectInputStream.class, "subclassAudits");
50 clearSoftCache(ObjectOutputStream.class, "subclassAudits");
51 clearSoftCache(ObjectStreamClass.class, "localDescs");
52 clearSoftCache(ObjectStreamClass.class, "reflectors");
53 }
54
55 public String toString() {
56 return "[Configuration ClassLoader id=" + id + "]";
57 }
58
59 private static Object lock = new Object();
60 private static boolean clearSoftCacheFailed = false;
61 private static void clearSoftCache(Class clazz, String fieldName) {
62 Map cache = null;
63 try {
64 Field f = clazz.getDeclaredField(fieldName);
65 f.setAccessible(true);
66 cache = (Map) f.get(null);
67 } catch (Throwable 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 }