rlm@10: /** rlm@10: * Copyright (c) Rich Hickey. All rights reserved. rlm@10: * The use and distribution terms for this software are covered by the rlm@10: * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: * which can be found in the file epl-v10.html at the root of this distribution. rlm@10: * By using this software in any fashion, you are agreeing to be bound by rlm@10: * the terms of this license. rlm@10: * You must not remove this notice, or any other, from this software. rlm@10: **/ rlm@10: rlm@10: /* rich Aug 21, 2007 */ rlm@10: rlm@10: package clojure.lang; rlm@10: rlm@10: import java.util.HashMap; rlm@10: import java.util.Map; rlm@10: import java.util.concurrent.ConcurrentHashMap; rlm@10: import java.net.URLClassLoader; rlm@10: import java.net.URL; rlm@10: import java.lang.ref.ReferenceQueue; rlm@10: import java.lang.ref.SoftReference; rlm@10: rlm@10: public class DynamicClassLoader extends URLClassLoader{ rlm@10: HashMap constantVals = new HashMap(); rlm@10: static ConcurrentHashMap>classCache = rlm@10: new ConcurrentHashMap >(); rlm@10: rlm@10: static final URL[] EMPTY_URLS = new URL[]{}; rlm@10: rlm@10: static final ReferenceQueue rq = new ReferenceQueue(); rlm@10: rlm@10: public DynamicClassLoader(){ rlm@10: //pseudo test in lieu of hasContextClassLoader() rlm@10: super(EMPTY_URLS,(Thread.currentThread().getContextClassLoader() == null || rlm@10: Thread.currentThread().getContextClassLoader() == ClassLoader.getSystemClassLoader())? rlm@10: Compiler.class.getClassLoader():Thread.currentThread().getContextClassLoader()); rlm@10: } rlm@10: rlm@10: public DynamicClassLoader(ClassLoader parent){ rlm@10: super(EMPTY_URLS,parent); rlm@10: } rlm@10: rlm@10: public Class defineClass(String name, byte[] bytes, Object srcForm){ rlm@10: Util.clearCache(rq, classCache); rlm@10: Class c = defineClass(name, bytes, 0, bytes.length); rlm@10: classCache.put(name, new SoftReference(c,rq)); rlm@10: return c; rlm@10: } rlm@10: rlm@10: protected Class findClass(String name) throws ClassNotFoundException{ rlm@10: SoftReference cr = classCache.get(name); rlm@10: if(cr != null) rlm@10: { rlm@10: Class c = cr.get(); rlm@10: if(c != null) rlm@10: return c; rlm@10: } rlm@10: return super.findClass(name); rlm@10: } rlm@10: rlm@10: public void registerConstants(int id, Object[] val){ rlm@10: constantVals.put(id, val); rlm@10: } rlm@10: rlm@10: public Object[] getConstants(int id){ rlm@10: return constantVals.get(id); rlm@10: } rlm@10: rlm@10: public void addURL(URL url){ rlm@10: super.addURL(url); rlm@10: } rlm@10: rlm@10: }