View Javadoc

1   /*
2    * $Id: SpringRegistry.java 12287 2008-07-10 21:16:52Z dfeist $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.agent.Agent;
15  import org.mule.api.config.MuleConfiguration;
16  import org.mule.api.endpoint.EndpointBuilder;
17  import org.mule.api.endpoint.ImmutableEndpoint;
18  import org.mule.api.lifecycle.Disposable;
19  import org.mule.api.lifecycle.Initialisable;
20  import org.mule.api.lifecycle.LifecycleManager;
21  import org.mule.api.model.Model;
22  import org.mule.api.registry.RegistrationException;
23  import org.mule.api.registry.ServiceDescriptor;
24  import org.mule.api.registry.ServiceDescriptorFactory;
25  import org.mule.api.registry.ServiceException;
26  import org.mule.api.service.Service;
27  import org.mule.api.transformer.Transformer;
28  import org.mule.api.transport.Connector;
29  import org.mule.config.i18n.CoreMessages;
30  import org.mule.config.i18n.MessageFactory;
31  import org.mule.lifecycle.ContainerManagedLifecyclePhase;
32  import org.mule.lifecycle.GenericLifecycleManager;
33  import org.mule.registry.AbstractRegistry;
34  import org.mule.util.SpiUtils;
35  import org.mule.util.StringUtils;
36  
37  import java.util.Collection;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  import javax.transaction.TransactionManager;
42  
43  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
44  import org.springframework.context.ConfigurableApplicationContext;
45  
46  public class SpringRegistry extends AbstractRegistry
47  {
48      public static final String REGISTRY_ID = "org.mule.Registry.Spring";
49  
50      /**
51       * Key used to lookup Spring Application Context from SpringRegistry via Mule's
52       * Registry interface.
53       **/
54      public static final String SPRING_APPLICATION_CONTEXT = "springApplicationContext";
55      
56      protected ConfigurableApplicationContext applicationContext;
57  
58      public SpringRegistry()
59      {
60          super(REGISTRY_ID);
61      }
62  
63      public SpringRegistry(String id)
64      {
65          super(id);
66      }
67  
68      public SpringRegistry(ConfigurableApplicationContext applicationContext)
69      {
70          super(REGISTRY_ID);
71          this.applicationContext = applicationContext;
72      }
73  
74      public SpringRegistry(String id, ConfigurableApplicationContext applicationContext)
75      {
76          super(id);
77          this.applicationContext = applicationContext;
78      }
79  
80      protected LifecycleManager createLifecycleManager()
81      {
82          GenericLifecycleManager lcm = new GenericLifecycleManager();
83          lcm.registerLifecycle(new ContainerManagedLifecyclePhase(Initialisable.PHASE_NAME,
84                  Initialisable.class, Disposable.PHASE_NAME));
85          lcm.registerLifecycle(new ContainerManagedLifecyclePhase(Disposable.PHASE_NAME, Disposable.class,
86                  Initialisable.PHASE_NAME));
87          return lcm;
88      }
89  
90      protected Object doLookupObject(String key)
91      {
92          if (StringUtils.isBlank(key))
93          {
94              logger.warn(
95                      MessageFactory.createStaticMessage("Detected a lookup attempt with an empty or null key"),
96                      new Throwable().fillInStackTrace());
97              return null;
98          }
99  
100         if (key.equals(SPRING_APPLICATION_CONTEXT) && applicationContext != null)
101         {
102             return applicationContext;
103         }
104         else
105         {
106             try
107             {
108                 return applicationContext.getBean(key);
109             }
110             catch (NoSuchBeanDefinitionException e)
111             {
112                 logger.debug(e);
113                 return null;
114             }
115         }
116     }
117 
118     protected Collection doLookupObjects(Class type)
119     {
120         Map map = applicationContext.getBeansOfType(type);
121         // MULE-2762
122         //if (logger.isDebugEnabled())
123         //{
124         //    MapUtils.debugPrint(System.out, "Beans of type " + type, map);
125         //}
126         return map.values();
127     }
128 
129     public ServiceDescriptor lookupServiceDescriptor(String type, String name, Properties overrides)
130             throws ServiceException
131     {
132         Properties props = SpiUtils.findServiceDescriptor(type, name);
133         if (props == null)
134         {
135             throw new ServiceException(CoreMessages.failedToLoad(type + " " + name));
136         }
137         return ServiceDescriptorFactory.create(type, name, props, overrides, this);
138     }
139 
140     /** {@inheritDoc} */
141     public TransactionManager getTransactionManager()
142     {
143         try
144         {
145             return (TransactionManager) lookupObject(TransactionManager.class);
146         }
147         catch (RegistrationException e)
148         {
149             throw new RuntimeException(e);
150         }
151     }
152 
153     public Collection getModels()
154     {
155         return applicationContext.getBeansOfType(Model.class).values();
156     }
157 
158     /** {@inheritDoc} */
159     public Collection getConnectors()
160     {
161         return applicationContext.getBeansOfType(Connector.class).values();
162     }
163 
164     public Collection getAgents()
165     {
166         return applicationContext.getBeansOfType(Agent.class).values();
167     }
168 
169     /** {@inheritDoc} */
170     public Collection getEndpoints()
171     {
172         return applicationContext.getBeansOfType(ImmutableEndpoint.class).values();
173     }
174 
175     /** {@inheritDoc} */
176     public Collection getTransformers()
177     {
178         return applicationContext.getBeansOfType(Transformer.class).values();
179     }
180 
181     public boolean isReadOnly()
182     {
183         return true;
184     }
185 
186     public boolean isRemote()
187     {
188         return false;
189     }
190 
191     public void registerConnector(Connector connector)
192             throws MuleException
193     {
194         unsupportedOperation("registerConnector", connector);
195     }
196 
197     public void unregisterConnector(String connectorName) throws MuleException
198     {
199         unsupportedOperation("unregisterConnector", connectorName);
200     }
201 
202     public void registerEndpoint(ImmutableEndpoint endpoint)
203             throws MuleException
204     {
205         unsupportedOperation("registerEndpoint", endpoint);
206     }
207 
208     public void unregisterEndpoint(String endpointName)
209     {
210         unsupportedOperation("unregisterEndpoint", endpointName);
211     }
212 
213     protected void doRegisterTransformer(Transformer transformer) throws MuleException
214     {
215         unsupportedOperation("registerTransformer", transformer);
216     }
217 
218     public void unregisterTransformer(String transformerName)
219     {
220         unsupportedOperation("unregistertransformer", transformerName);
221     }
222 
223     /** {@inheritDoc} */
224     public void registerService(Service service)
225             throws MuleException
226     {
227         unsupportedOperation("registerService", service);
228     }
229 
230     public void unregisterService(String serviceName)
231     {
232         unsupportedOperation("unregisterService", serviceName);
233     }
234 
235     public void registerModel(Model model) throws MuleException
236     {
237         unsupportedOperation("registerModel", model);
238     }
239 
240     public void unregisterModel(String modelName)
241     {
242         unsupportedOperation("unregisterModel", modelName);
243     }
244 
245     public void registerAgent(Agent agent) throws MuleException
246     {
247         unsupportedOperation("registerAgent", agent);
248     }
249 
250     public void unregisterAgent(String agentName) throws MuleException
251     {
252         unsupportedOperation("unregisterAgent", agentName);
253     }
254 
255     protected void doRegisterObject(String key,
256                                     Object value,
257                                     Object metadata) throws RegistrationException
258     {
259         unsupportedOperation("doRegisterObject", key);
260     }
261 
262     public void unregisterObject(String key)
263     {
264         unsupportedOperation("unregisterObject", key);
265     }
266 
267     public void registerObjects(Map objects) throws RegistrationException
268     {
269         unsupportedOperation("registryObjects", objects);
270     }
271 
272     public void setConfiguration(MuleConfiguration config)
273     {
274         unsupportedOperation("setConfiguration", config);
275     }
276 
277     public void registerEndpointBuilder(String name,
278                                         EndpointBuilder builder) throws MuleException
279     {
280         unsupportedOperation("registerEndpointBuilder", builder);
281     }
282     
283     protected void doDispose()
284     {
285         super.doDispose();
286         applicationContext.close();
287     }
288 }