001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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    import org.apache.geronimo.kernel.util.ClassLoaderRegistry;
032    
033    /**
034     *
035     * @version $Rev: 496422 $ $Date: 2007-01-15 13:13:20 -0500 (Mon, 15 Jan 2007) $
036     */
037    public class ConfigurationClassLoader extends URLClassLoader {
038        private final URI id;
039        
040        public ConfigurationClassLoader(URI id, URL[] urls, ClassLoader parent) {
041            super(urls, parent);
042            ClassLoaderRegistry.add(this);
043            this.id = id;
044        }
045        
046        public URI getID() {
047            return id;
048        }
049    
050        public void destroy() {
051            LogFactory.release(this);
052            clearSoftCache(ObjectInputStream.class, "subclassAudits");
053            clearSoftCache(ObjectOutputStream.class, "subclassAudits");
054            clearSoftCache(ObjectStreamClass.class, "localDescs");
055            clearSoftCache(ObjectStreamClass.class, "reflectors");
056            ClassLoaderRegistry.remove(this);    
057        }
058    
059        public String toString() {
060            return "[Configuration ClassLoader id=" + id + "]";
061        }
062    
063        private static Object lock = new Object();
064        private static boolean clearSoftCacheFailed = false;
065        private static void clearSoftCache(Class clazz, String fieldName) {
066            Map cache = null;
067            try {
068                Field f = clazz.getDeclaredField(fieldName);
069                f.setAccessible(true);
070                cache = (Map) f.get(null);
071            } catch (Throwable e) {
072                synchronized (lock) {
073                    if (!clearSoftCacheFailed) {
074                        clearSoftCacheFailed = true;
075                        LogFactory.getLog(ConfigurationClassLoader.class).debug("Unable to clear SoftCache field " + fieldName + " in class " + clazz);
076                    }
077                }
078            }
079    
080            if (cache != null) {
081                synchronized (cache) {
082                    cache.clear();
083                }
084            }
085        }
086        protected void finalize(){
087            ClassLoaderRegistry.remove(this);
088        }
089    }