1 9 10 package org.nanocontainer.script; 11 12 import java.io.File ; 13 import java.io.FileNotFoundException ; 14 import java.io.FileReader ; 15 import java.io.IOException ; 16 import java.io.Reader ; 17 import java.net.URL ; 18 import org.nanocontainer.DefaultNanoContainer; 19 import org.picocontainer.ComponentAdapter; 20 import org.picocontainer.defaults.DefaultPicoContainer; 21 22 40 public class ScriptedContainerBuilderFactory { 41 42 46 public static final String GROOVY = ".groovy"; 47 48 52 public static final String BEANSHELL = ".bsh"; 53 54 58 public static final String JAVASCRIPT = ".js"; 59 60 64 public static final String JYTHON = ".py"; 65 66 70 public static final String XML = ".xml"; 71 72 73 private ScriptedContainerBuilder containerBuilder; 74 75 76 77 public ScriptedContainerBuilderFactory(File compositionFile, ClassLoader classLoader) throws IOException , ClassNotFoundException { 78 this(compositionFile, classLoader, new ScriptBuilderResolver()); 79 } 80 81 96 public ScriptedContainerBuilderFactory(File compositionFile, ClassLoader classLoader, ScriptBuilderResolver builderClassResolver) throws IOException , ClassNotFoundException ,UnsupportedScriptTypeException { 97 this(new FileReader (fileExists(compositionFile)), builderClassResolver.getBuilderClassName(compositionFile), classLoader); 98 } 99 100 101 public ScriptedContainerBuilderFactory(File compositionFile) throws IOException , ClassNotFoundException { 102 this(compositionFile, Thread.currentThread().getContextClassLoader()); 103 } 104 105 public ScriptedContainerBuilderFactory(URL compositionURL) throws ClassNotFoundException { 106 this(compositionURL, Thread.currentThread().getContextClassLoader(),new ScriptBuilderResolver()); 107 } 108 109 124 public ScriptedContainerBuilderFactory(URL compositionURL, ClassLoader classLoader, ScriptBuilderResolver builderClassResolver) throws ClassNotFoundException ,UnsupportedScriptTypeException { 125 this(compositionURL, builderClassResolver.getBuilderClassName(compositionURL), classLoader); 126 } 127 128 129 public ScriptedContainerBuilderFactory(URL compositionURL, String builderClassName, ClassLoader contextClassLoader) throws ClassNotFoundException { 130 createContainerBuilder(compositionURL, contextClassLoader, builderClassName); 131 } 132 133 134 public ScriptedContainerBuilderFactory(Reader composition, String builderClass) throws ClassNotFoundException { 135 this(composition, builderClass, Thread.currentThread().getContextClassLoader()); 136 } 137 138 145 public ScriptedContainerBuilderFactory(Reader composition, String builderClass, ClassLoader classLoader) throws ClassNotFoundException { 146 createContainerBuilder(composition, classLoader, builderClass); 147 } 148 149 150 158 private void createContainerBuilder(Object composition, ClassLoader classLoader, String builderClass) throws ClassNotFoundException { 159 DefaultNanoContainer defaultNanoContainer; 160 { 161 DefaultPicoContainer factory = new DefaultPicoContainer(); 163 if(composition == null) { 164 throw new NullPointerException ("composition can't be null"); 165 } 166 factory.registerComponentInstance(composition); 167 168 if(classLoader == null) { 169 classLoader = getClass().getClassLoader(); 175 } 176 factory.registerComponentInstance(classLoader); 177 178 defaultNanoContainer = new DefaultNanoContainer(classLoader,factory); 184 } 185 ComponentAdapter componentAdapter = defaultNanoContainer.registerComponentImplementation(builderClass); 186 containerBuilder = (ScriptedContainerBuilder) componentAdapter.getComponentInstance(defaultNanoContainer.getPico()); 187 } 188 189 private static File fileExists(final File file) throws FileNotFoundException { 190 if (file.exists()) { 191 return file; 192 } else { 193 throw new FileNotFoundException ("File " + file.getAbsolutePath() + " does not exist."); 195 } 196 } 197 198 207 public static String getBuilderClassName(final String extension) { 208 return new ScriptBuilderResolver().getBuilderClassName(extension); 209 } 210 211 212 216 public ScriptedContainerBuilder getContainerBuilder() { 217 return containerBuilder; 218 } 219 220 } 221 | Popular Tags |