1 10 11 package org.mule.extras.picocontainer; 12 13 import java.io.Reader ; 14 import java.io.StringReader ; 15 16 import org.mule.config.i18n.Message; 17 import org.mule.config.i18n.Messages; 18 import org.mule.impl.container.AbstractContainerContext; 19 import org.mule.impl.container.ContainerKeyPair; 20 import org.mule.umo.lifecycle.InitialisationException; 21 import org.mule.umo.lifecycle.RecoverableException; 22 import org.mule.umo.manager.ContainerException; 23 import org.mule.umo.manager.ObjectNotFoundException; 24 import org.mule.util.ClassUtils; 25 import org.mule.util.IOUtils; 26 import org.nanocontainer.integrationkit.ContainerBuilder; 27 import org.nanocontainer.integrationkit.PicoCompositionException; 28 import org.nanocontainer.script.ScriptedContainerBuilderFactory; 29 import org.picocontainer.MutablePicoContainer; 30 import org.picocontainer.defaults.SimpleReference; 31 32 36 public class PicoContainerContext extends AbstractContainerContext 37 { 38 public static final String CONFIGEXTENSION = "CONFIG"; 39 40 private String extension = ScriptedContainerBuilderFactory.XML; 41 42 45 protected String configFile; 46 47 50 private MutablePicoContainer container; 51 52 public PicoContainerContext() 53 { 54 super("pico"); 55 } 56 57 public String getExtension() 58 { 59 return extension; 60 } 61 62 public void setExtension(String extension) 63 { 64 this.extension = extension; 65 } 66 67 72 public Object getComponent(Object key) throws ObjectNotFoundException 73 { 74 if (container == null) 75 { 76 throw new IllegalStateException ("Pico container has not been set"); 77 } 78 if (key == null) 79 { 80 throw new ObjectNotFoundException("Component not found for null key"); 81 } 82 83 if (key instanceof ContainerKeyPair) 84 { 85 key = ((ContainerKeyPair)key).getKey(); 86 } 87 88 Object component = null; 89 if (key instanceof String ) 90 { 91 try 92 { 93 Class keyClass = ClassUtils.loadClass((String )key, getClass()); 94 component = container.getComponentInstance(keyClass); 95 } 96 catch (ClassNotFoundException e) 97 { 98 component = container.getComponentInstance(key); 99 } 100 } 101 else 102 { 103 component = container.getComponentInstance(key); 104 } 105 106 if (component == null) 107 { 108 throw new ObjectNotFoundException("Component not found for key: " + key.toString()); 109 } 110 return component; 111 } 112 113 116 public MutablePicoContainer getContainer() 117 { 118 return container; 119 } 120 121 124 public void setContainer(MutablePicoContainer container) 125 { 126 this.container = container; 127 } 128 129 134 public void setConfigFile(String configFile) throws PicoCompositionException 135 { 136 this.configFile = configFile; 137 138 } 139 140 public void configure(Reader configuration) throws ContainerException 141 { 142 String className = ScriptedContainerBuilderFactory.getBuilderClassName(extension); 143 doConfigure(configuration, className); 144 } 145 146 protected void doConfigure(Reader configReader, String builderClassName) throws ContainerException 147 { 148 org.picocontainer.defaults.ObjectReference containerRef = new SimpleReference(); 149 org.picocontainer.defaults.ObjectReference parentContainerRef = new SimpleReference(); 150 ScriptedContainerBuilderFactory scriptedContainerBuilderFactory = null; 151 try 152 { 153 scriptedContainerBuilderFactory = new ScriptedContainerBuilderFactory(configReader, 154 builderClassName, Thread.currentThread().getContextClassLoader()); 155 } 156 catch (ClassNotFoundException e) 157 { 158 throw new ContainerException(new Message(Messages.FAILED_TO_CONFIGURE_CONTAINER), e); 159 } 160 161 ContainerBuilder builder = scriptedContainerBuilderFactory.getContainerBuilder(); 162 builder.buildContainer(containerRef, parentContainerRef, null, false); 163 setContainer((MutablePicoContainer)containerRef.get()); 164 } 165 166 private String getBuilderClassName(String scriptName) 167 { 168 String extension = scriptName.substring(scriptName.lastIndexOf('.')); 169 return ScriptedContainerBuilderFactory.getBuilderClassName(extension); 170 } 171 172 public void initialise() throws InitialisationException, RecoverableException 173 { 174 if (configFile == null) 175 { 176 return; 177 } 178 try 179 { 180 String builderClassName = getBuilderClassName(configFile); 181 String configString = IOUtils.getResourceAsString(configFile, getClass()); 182 StringReader configReader = new StringReader (configString); 183 doConfigure(configReader, builderClassName); 184 185 } 186 catch (Exception e) 187 { 188 throw new PicoCompositionException(e); 189 } 190 } 191 192 public void dispose() 193 { 194 if (container != null) 195 { 196 container.dispose(); 197 } 198 } 199 } 200 | Popular Tags |