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