X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2FSpringComponent.java;h=0e54deaec72ff12b194348704632f9bb8fdf7c4e;hb=b0e1c060d6207c0fc06e4673764a6980da775210;hp=633050cbfecacee89b2cd5a723762ec8d520b970;hpb=d2bdf4e813c6a3964958c87b2ce56eaadf8a1f0a;p=utils diff --git a/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java index 633050cb..0e54deae 100644 --- a/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java +++ b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java @@ -1,45 +1,49 @@ +/* + * Copyright 2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.wamblee.system.spring; import java.util.Map; import java.util.Properties; -import java.util.Set; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.wamblee.system.AbstractComponent; -import org.wamblee.system.CompositeComponent; -import org.wamblee.system.ProvidedInterfaceDescriptor; -import org.wamblee.system.RequiredInterfaceDescriptor; -import org.wamblee.system.Service; -import org.wamblee.system.InterfaceDescriptor; -import org.wamblee.system.ServiceRegistry; -import org.wamblee.system.SystemAssembler; +import org.wamblee.system.ProvidedInterface; +import org.wamblee.system.RequiredInterface; import org.wamblee.system.SystemAssemblyException; /** - * Represents a system configured based on spring. - * The spring config files that are configured should not contain any PropertyPlaceholderConfigurer - * objects. - * + * Represents a system configured based on spring. The spring config files that + * are configured should not contain any PropertyPlaceholderConfigurer objects. + * * @author Erik Brakkee */ public class SpringComponent extends AbstractComponent { - /** - * Singleton access to the service registry. Required while starting up. - */ - static ThreadLocal REGISTRY = new ThreadLocal(); + static final ThreadLocal THIS = new ThreadLocal(); - private Properties _properties; + private Properties _properties; private String[] _configFiles; - private Map _provided; - private Map _required; + private Map _provided; + private Map _required; /** * Parent application context containing required services. */ @@ -66,70 +70,72 @@ public class SpringComponent extends AbstractComponent { * names that the spring config files use for each required * service. */ - public SpringComponent(String aName, ServiceRegistry aRegistry, String[] aConfigFiles, - Map aProvided, - Map aRequired) { - super(aName, aRegistry, aProvided.values().toArray(new InterfaceDescriptor[0]), - aRequired.keySet().toArray(new InterfaceDescriptor[0])); - _properties = new Properties(); + public SpringComponent(String aName, String[] aConfigFiles, + Map aProvided, + Map aRequired) { + super(aName, aProvided.values().toArray(new ProvidedInterface[0]), + aRequired.keySet().toArray(new RequiredInterface[0])); + _properties = new Properties(); _configFiles = aConfigFiles; _provided = aProvided; _required = aRequired; } - + /** - * Must be called to make a property available in the application context. - * @param aKey Property key. - * @param aValue Property value. + * Must be called to make a property available in the application context. + * + * @param aKey + * Property key. + * @param aValue + * Property value. */ - public void setProperty(String aKey, String aValue) { + public void setProperty(String aKey, String aValue) { _properties.put(aKey, aValue); } - - public void addProperties(Properties aProperties) { - for (Object key: aProperties.keySet()) { - setProperty((String)key, aProperties.getProperty((String)key)); + + public void addProperties(Properties aProperties) { + for (Object key : aProperties.keySet()) { + setProperty((String) key, aProperties.getProperty((String) key)); } } @Override - protected void doStart(String aContext, - Service[] aRequiredServices) { - ServiceRegistry oldRegistry = REGISTRY.get(); - try { - REGISTRY.set(getRegistry()); - try { - _parentContext = new GenericApplicationContext(); - - registerRequiredServices(aRequiredServices); - - _parentContext.refresh(); - - parseConfigFiles(); - - _context.addBeanFactoryPostProcessor(new PropertySetter(_properties)); - _context.refresh(); - - exposeProvidedServices(aContext); - } catch (Exception e) { - throw new SystemAssemblyException( - "Failed to assemble spring system", e); - } + protected void doStart() { + + SpringComponent old = THIS.get(); + THIS.set(this); + try { + _parentContext = new GenericApplicationContext(); + + registerRequiredServices(); + + _parentContext.refresh(); + + parseConfigFiles(); + + _context + .addBeanFactoryPostProcessor(new PropertySetter(_properties)); + _context.refresh(); + + exposeProvidedServices(); + } catch (Exception e) { + throw new SystemAssemblyException( + "Failed to assemble spring system", e); } finally { - REGISTRY.set(oldRegistry); + THIS.set(old); } } - private void exposeProvidedServices(String aContext) { + private void exposeProvidedServices() { // Call addService for each provided service. for (String name : _provided.keySet()) { Object svc = _context.getBean(name); if (svc == null) { - throw new IllegalArgumentException(aContext + ": service '" + throw new IllegalArgumentException(getQualifiedName() + ": service '" + name + "' is null"); } - addService(aContext, _provided.get(name), svc); + addService(_provided.get(name), svc); } } @@ -140,27 +146,21 @@ public class SpringComponent extends AbstractComponent { _parentContext); } - private void registerRequiredServices(Service[] aRequiredServices) { + private void registerRequiredServices() { // Register required services in a parent context - - for (Service svc: aRequiredServices) { - String id = svc.getId(); - ProvidedInterfaceDescriptor descriptor = svc.getDescriptor(); - RequiredInterfaceDescriptor[] requiredServices = SystemAssembler.filterRequiredServices(descriptor, - _required.keySet()); - for (RequiredInterfaceDescriptor required: requiredServices) { - String beanName = _required.get(required); - ConstructorArgumentValues cargs = new ConstructorArgumentValues(); - cargs.addGenericArgumentValue(id); - BeanDefinition definition = new RootBeanDefinition(RequiredServiceBean.class, cargs, - new MutablePropertyValues()); - _parentContext.registerBeanDefinition(beanName, definition); - } + for (RequiredInterface required: getRequiredServices()) { + String beanName = _required.get(required); + ConstructorArgumentValues cargs = new ConstructorArgumentValues(); + cargs.addGenericArgumentValue(required.getName()); + BeanDefinition definition = new RootBeanDefinition( + RequiredServiceBean.class, cargs, + new MutablePropertyValues()); + _parentContext.registerBeanDefinition(beanName, definition); } } @Override protected void doStop() { - _context.close(); + _context.close(); } }