1 9 package org.nanocontainer.script; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.io.Reader ; 15 import java.net.URL ; 16 17 import org.nanocontainer.integrationkit.LifecycleContainerBuilder; 18 import org.picocontainer.MutablePicoContainer; 19 import org.picocontainer.PicoContainer; 20 21 29 public abstract class ScriptedContainerBuilder extends LifecycleContainerBuilder { 30 private final Reader scriptReader; 31 private final URL scriptURL; 32 private final ClassLoader classLoader; 33 34 public ScriptedContainerBuilder(Reader script, ClassLoader classLoader) { 35 this.scriptReader = script; 36 if (script == null) { 37 throw new NullPointerException ("script"); 38 } 39 this.scriptURL = null; 40 this.classLoader = classLoader; 41 if ( classLoader == null) { 42 throw new NullPointerException ("classLoader"); 43 } 44 } 45 46 public ScriptedContainerBuilder(URL script, ClassLoader classLoader) { 47 this.scriptReader = null; 48 this.scriptURL = script; 49 if (script == null) { 50 throw new NullPointerException ("script"); 51 } 52 this.classLoader = classLoader; 53 if ( classLoader == null) { 54 throw new NullPointerException ("classLoader"); 55 } 56 } 57 58 protected final PicoContainer createContainer(PicoContainer parentContainer, Object assemblyScope) { 59 try { 60 return createContainerFromScript(parentContainer, assemblyScope); 61 } finally { 62 try { 63 Reader reader = getScriptReader(); 64 if (reader != null) { 65 reader.close(); 66 } 67 } catch (IOException e) { 68 } 70 } 71 } 72 73 protected final ClassLoader getClassLoader() { 74 return classLoader; 75 } 76 77 protected final InputStream getScriptInputStream() throws IOException { 78 if ( scriptReader != null ){ 79 return new InputStream () { 80 public int read() throws IOException { 81 return scriptReader.read(); 82 } 83 }; 84 } 85 return scriptURL.openStream(); 86 } 87 88 protected final Reader getScriptReader() throws IOException { 89 if ( scriptReader != null ){ 90 return scriptReader; 91 } 92 return new InputStreamReader (scriptURL.openStream()); 93 } 94 95 protected abstract PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope); 97 98 protected void composeContainer(MutablePicoContainer container, Object assemblyScope) { 99 } 101 } | Popular Tags |