001: /*
002: * $Id: SpringRegistry.java 11371 2008-03-15 03:12:09Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.agent.Agent;
015: import org.mule.api.config.MuleConfiguration;
016: import org.mule.api.endpoint.EndpointBuilder;
017: import org.mule.api.endpoint.ImmutableEndpoint;
018: import org.mule.api.lifecycle.Disposable;
019: import org.mule.api.lifecycle.Initialisable;
020: import org.mule.api.lifecycle.LifecycleManager;
021: import org.mule.api.model.Model;
022: import org.mule.api.registry.RegistrationException;
023: import org.mule.api.registry.ServiceDescriptor;
024: import org.mule.api.registry.ServiceDescriptorFactory;
025: import org.mule.api.registry.ServiceException;
026: import org.mule.api.service.Service;
027: import org.mule.api.transformer.Transformer;
028: import org.mule.api.transport.Connector;
029: import org.mule.config.i18n.CoreMessages;
030: import org.mule.config.i18n.MessageFactory;
031: import org.mule.lifecycle.ContainerManagedLifecyclePhase;
032: import org.mule.lifecycle.GenericLifecycleManager;
033: import org.mule.registry.AbstractRegistry;
034: import org.mule.util.SpiUtils;
035: import org.mule.util.StringUtils;
036:
037: import java.util.Collection;
038: import java.util.Map;
039: import java.util.Properties;
040:
041: import javax.transaction.TransactionManager;
042:
043: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
044: import org.springframework.context.ConfigurableApplicationContext;
045:
046: public class SpringRegistry extends AbstractRegistry {
047: public static final String REGISTRY_ID = "org.mule.Registry.Spring";
048:
049: protected ConfigurableApplicationContext applicationContext;
050:
051: public SpringRegistry() {
052: super (REGISTRY_ID);
053: }
054:
055: public SpringRegistry(String id) {
056: super (id);
057: }
058:
059: public SpringRegistry(
060: ConfigurableApplicationContext applicationContext) {
061: super (REGISTRY_ID);
062: this .applicationContext = applicationContext;
063: }
064:
065: public SpringRegistry(String id,
066: ConfigurableApplicationContext applicationContext) {
067: super (id);
068: this .applicationContext = applicationContext;
069: }
070:
071: protected LifecycleManager createLifecycleManager() {
072: GenericLifecycleManager lcm = new GenericLifecycleManager();
073: lcm.registerLifecycle(new ContainerManagedLifecyclePhase(
074: Initialisable.PHASE_NAME, Initialisable.class,
075: Disposable.PHASE_NAME));
076: lcm.registerLifecycle(new ContainerManagedLifecyclePhase(
077: Disposable.PHASE_NAME, Disposable.class,
078: Initialisable.PHASE_NAME));
079: return lcm;
080: }
081:
082: protected Object doLookupObject(String key) {
083: if (StringUtils.isBlank(key)) {
084: logger
085: .warn(
086: MessageFactory
087: .createStaticMessage("Detected a lookup attempt with an empty or null key"),
088: new Throwable().fillInStackTrace());
089: return null;
090: }
091:
092: try {
093: return applicationContext.getBean(key);
094: } catch (NoSuchBeanDefinitionException e) {
095: logger.debug(e);
096: return null;
097: }
098: }
099:
100: protected Collection doLookupObjects(Class type) {
101: Map map = applicationContext.getBeansOfType(type);
102: // MULE-2762
103: //if (logger.isDebugEnabled())
104: //{
105: // MapUtils.debugPrint(System.out, "Beans of type " + type, map);
106: //}
107: return map.values();
108: }
109:
110: public ServiceDescriptor lookupServiceDescriptor(String type,
111: String name, Properties overrides) throws ServiceException {
112: Properties props = SpiUtils.findServiceDescriptor(type, name);
113: if (props == null) {
114: throw new ServiceException(CoreMessages.failedToLoad(type
115: + " " + name));
116: }
117: return ServiceDescriptorFactory.create(type, name, props,
118: overrides, this );
119: }
120:
121: /** {@inheritDoc} */
122: public TransactionManager getTransactionManager() {
123: try {
124: return (TransactionManager) lookupObject(TransactionManager.class);
125: } catch (RegistrationException e) {
126: throw new RuntimeException(e);
127: }
128: }
129:
130: public Collection getModels() {
131: return applicationContext.getBeansOfType(Model.class).values();
132: }
133:
134: /** {@inheritDoc} */
135: public Collection getConnectors() {
136: return applicationContext.getBeansOfType(Connector.class)
137: .values();
138: }
139:
140: public Collection getAgents() {
141: return applicationContext.getBeansOfType(Agent.class).values();
142: }
143:
144: /** {@inheritDoc} */
145: public Collection getEndpoints() {
146: return applicationContext.getBeansOfType(
147: ImmutableEndpoint.class).values();
148: }
149:
150: /** {@inheritDoc} */
151: public Collection getTransformers() {
152: return applicationContext.getBeansOfType(Transformer.class)
153: .values();
154: }
155:
156: public boolean isReadOnly() {
157: return true;
158: }
159:
160: public boolean isRemote() {
161: return false;
162: }
163:
164: public void registerConnector(Connector connector)
165: throws MuleException {
166: unsupportedOperation("registerConnector", connector);
167: }
168:
169: public void unregisterConnector(String connectorName)
170: throws MuleException {
171: unsupportedOperation("unregisterConnector", connectorName);
172: }
173:
174: public void registerEndpoint(ImmutableEndpoint endpoint)
175: throws MuleException {
176: unsupportedOperation("registerEndpoint", endpoint);
177: }
178:
179: public void unregisterEndpoint(String endpointName) {
180: unsupportedOperation("unregisterEndpoint", endpointName);
181: }
182:
183: protected void doRegisterTransformer(Transformer transformer)
184: throws MuleException {
185: unsupportedOperation("registerTransformer", transformer);
186: }
187:
188: public void unregisterTransformer(String transformerName) {
189: unsupportedOperation("unregistertransformer", transformerName);
190: }
191:
192: /** {@inheritDoc} */
193: public void registerService(Service service) throws MuleException {
194: unsupportedOperation("registerService", service);
195: }
196:
197: public void unregisterService(String serviceName) {
198: unsupportedOperation("unregisterService", serviceName);
199: }
200:
201: public void registerModel(Model model) throws MuleException {
202: unsupportedOperation("registerModel", model);
203: }
204:
205: public void unregisterModel(String modelName) {
206: unsupportedOperation("unregisterModel", modelName);
207: }
208:
209: public void registerAgent(Agent agent) throws MuleException {
210: unsupportedOperation("registerAgent", agent);
211: }
212:
213: public void unregisterAgent(String agentName) throws MuleException {
214: unsupportedOperation("unregisterAgent", agentName);
215: }
216:
217: protected void doRegisterObject(String key, Object value,
218: Object metadata) throws RegistrationException {
219: unsupportedOperation("doRegisterObject", key);
220: }
221:
222: public void unregisterObject(String key) {
223: unsupportedOperation("unregisterObject", key);
224: }
225:
226: public void registerObjects(Map objects)
227: throws RegistrationException {
228: unsupportedOperation("registryObjects", objects);
229: }
230:
231: public void setConfiguration(MuleConfiguration config) {
232: unsupportedOperation("setConfiguration", config);
233: }
234:
235: public void registerEndpointBuilder(String name,
236: EndpointBuilder builder) throws MuleException {
237: unsupportedOperation("registerEndpointBuilder", builder);
238: }
239:
240: protected void doDispose() {
241: super.doDispose();
242: applicationContext.close();
243: }
244: }
|