KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > picocontainer > PicoContainerContext


1 /*
2  * $Id: PicoContainerContext.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.picocontainer;
12
13 import java.io.Reader JavaDoc;
14 import java.io.StringReader JavaDoc;
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 /**
33  * <code>PicoContainerContext</code> is a Pico Context that can expose pico-managed
34  * components for use in the Mule framework.
35  */

36 public class PicoContainerContext extends AbstractContainerContext
37 {
38     public static final String JavaDoc CONFIGEXTENSION = "CONFIG";
39
40     private String JavaDoc extension = ScriptedContainerBuilderFactory.XML;
41
42     /**
43      * The url of the config file to use
44      */

45     protected String JavaDoc configFile;
46
47     /**
48      * the pico container that manages the components
49      */

50     private MutablePicoContainer container;
51
52     public PicoContainerContext()
53     {
54         super("pico");
55     }
56
57     public String JavaDoc getExtension()
58     {
59         return extension;
60     }
61
62     public void setExtension(String JavaDoc extension)
63     {
64         this.extension = extension;
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.mule.model.UMOContainerContext#getComponent(java.lang.Object)
71      */

72     public Object JavaDoc getComponent(Object JavaDoc key) throws ObjectNotFoundException
73     {
74         if (container == null)
75         {
76             throw new IllegalStateException JavaDoc("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 JavaDoc component = null;
89         if (key instanceof String JavaDoc)
90         {
91             try
92             {
93                 Class JavaDoc keyClass = ClassUtils.loadClass((String JavaDoc)key, getClass());
94                 component = container.getComponentInstance(keyClass);
95             }
96             catch (ClassNotFoundException JavaDoc 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     /**
114      * @return Returns the container.
115      */

116     public MutablePicoContainer getContainer()
117     {
118         return container;
119     }
120
121     /**
122      * @param container The container to set.
123      */

124     public void setContainer(MutablePicoContainer container)
125     {
126         this.container = container;
127     }
128
129     /**
130      * The config file can be a resource on the classpath on a file system.
131      *
132      * @param configFile The configFile to set.
133      */

134     public void setConfigFile(String JavaDoc configFile) throws PicoCompositionException
135     {
136         this.configFile = configFile;
137
138     }
139
140     public void configure(Reader JavaDoc configuration) throws ContainerException
141     {
142         String JavaDoc className = ScriptedContainerBuilderFactory.getBuilderClassName(extension);
143         doConfigure(configuration, className);
144     }
145
146     protected void doConfigure(Reader JavaDoc configReader, String JavaDoc 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 JavaDoc 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 JavaDoc getBuilderClassName(String JavaDoc scriptName)
167     {
168         String JavaDoc 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 JavaDoc builderClassName = getBuilderClassName(configFile);
181             String JavaDoc configString = IOUtils.getResourceAsString(configFile, getClass());
182             StringReader JavaDoc configReader = new StringReader JavaDoc(configString);
183             doConfigure(configReader, builderClassName);
184
185         }
186         catch (Exception JavaDoc 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