001 /**
002 *
003 * Copyright 2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.kernel.config;
019
020 import java.net.URI;
021 import java.net.URL;
022 import java.net.URLClassLoader;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.io.ObjectStreamClass;
026 import java.util.Map;
027 import java.lang.reflect.Field;
028
029 import org.apache.commons.logging.LogFactory;
030
031 /**
032 *
033 * @version $Rev: 428082 $ $Date: 2006-08-02 11:35:10 -0700 (Wed, 02 Aug 2006) $
034 */
035 public class ConfigurationClassLoader extends URLClassLoader {
036 private final URI id;
037
038 public ConfigurationClassLoader(URI id, URL[] urls, ClassLoader parent) {
039 super(urls, parent);
040 this.id = id;
041 }
042
043 public URI getID() {
044 return id;
045 }
046
047 public void destroy() {
048 LogFactory.release(this);
049 clearSoftCache(ObjectInputStream.class, "subclassAudits");
050 clearSoftCache(ObjectOutputStream.class, "subclassAudits");
051 clearSoftCache(ObjectStreamClass.class, "localDescs");
052 clearSoftCache(ObjectStreamClass.class, "reflectors");
053 }
054
055 public String toString() {
056 return "[Configuration ClassLoader id=" + id + "]";
057 }
058
059 private static Object lock = new Object();
060 private static boolean clearSoftCacheFailed = false;
061 private static void clearSoftCache(Class clazz, String fieldName) {
062 Map cache = null;
063 try {
064 Field f = clazz.getDeclaredField(fieldName);
065 f.setAccessible(true);
066 cache = (Map) f.get(null);
067 } catch (Throwable e) {
068 synchronized (lock) {
069 if (!clearSoftCacheFailed) {
070 clearSoftCacheFailed = true;
071 LogFactory.getLog(ConfigurationClassLoader.class).debug("Unable to clear SoftCache field " + fieldName + " in class " + clazz);
072 }
073 }
074 }
075
076 if (cache != null) {
077 synchronized (cache) {
078 cache.clear();
079 }
080 }
081 }
082 }