1 package org.kohsuke.stapler; 2 3 import org.apache.commons.jelly.JellyContext; 4 import org.apache.commons.jelly.Script; 5 import org.apache.commons.jelly.Tag; 6 7 import java.lang.ref.WeakReference ; 8 import java.net.URL ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 17 public abstract class AbstractTearOff<CLT,S,E extends Exception > { 18 protected final MetaClass owner; 19 protected final CLT classLoader; 20 21 protected AbstractTearOff(MetaClass owner, Class <CLT> cltClass) { 22 this.owner = owner; 23 if(owner.classLoader!=null) 24 classLoader = owner.classLoader.loadTearOff(cltClass); 25 else 26 classLoader = null; 27 } 28 29 42 public S findScript(String name) throws E { 43 S script; 44 45 synchronized(this) { 46 script = getScripts().get(name); 47 if(script==null || MetaClass.NO_CACHE) { 48 ClassLoader cl = owner.clazz.getClassLoader(); 49 if(cl!=null) { 50 51 URL res = findResource(name, cl); 52 if(res!=null) { 53 script = parseScript(res); 54 getScripts().put(name,script); 55 } 56 } 57 } 58 } 59 if(script!=null) 60 return script; 61 62 if(owner.baseClass!=null) 64 return ((AbstractTearOff<CLT,S,E>)owner.baseClass.loadTearOff(getClass())).findScript(name); 65 66 return null; 67 } 68 69 72 protected abstract S parseScript(URL res) throws E; 73 74 85 private volatile WeakReference <Map <String ,S>> scripts; 86 87 private Map <String ,S> getScripts() { 88 Map <String ,S> r=null; 89 if(scripts!=null) 90 r = scripts.get(); 91 92 if(r!=null) 93 return r; 94 95 r = new HashMap <String ,S>(); 96 scripts = new WeakReference <Map <String ,S>>(r); 97 return r; 98 } 99 100 protected final URL findResource(String name, ClassLoader cl) { 101 URL res = null; 102 if (MetaClassLoader.debugLoader != null) 103 res = getResource(name, MetaClassLoader.debugLoader.loader); 104 if (res == null) 105 res = getResource(name, cl); 106 return res; 107 } 108 109 private URL getResource(String name, ClassLoader cl) { 110 URL res; 111 if(name.startsWith("/")) { 112 res = cl.getResource(name.substring(1)); 114 } else { 115 res = cl.getResource(owner.clazz.getName().replace('.','/').replace('$','/')+'/'+name); 117 } 118 return res; 119 } 120 } 121 | Popular Tags |