X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2FSpringComponent.java;h=88746a5ecc4a94f50a0af17653a49a05bce7f51d;hb=32a8562695029cf13d915bc7941a61fe07ff0005;hp=8d50bf280d53b6d09f562519cbecdc20c9526e11;hpb=8a6ee427cf3de42d9dd9d8fea09ee9fd059ee53e;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 8d50bf28..88746a5e 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 @@ -15,6 +15,7 @@ */ package org.wamblee.system.spring; +import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -26,8 +27,10 @@ import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.wamblee.system.core.AbstractComponent; +import org.wamblee.system.core.DefaultScope; import org.wamblee.system.core.ProvidedInterface; import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.core.Scope; import org.wamblee.system.core.SystemAssemblyException; /** @@ -36,23 +39,18 @@ import org.wamblee.system.core.SystemAssemblyException; * * @author Erik Brakkee */ -public class SpringComponent extends AbstractComponent { +public class SpringComponent extends AbstractComponent { + + private static final String CONTEXT_KEY = "context"; static final ThreadLocal THIS = new ThreadLocal(); + static final ThreadLocal SCOPE = new ThreadLocal(); private Properties _properties; private String[] _configFiles; private Map _provided; private Map _required; - /** - * Parent application context containing required services. - */ - private GenericApplicationContext _parentContext; - - /** - * Application context containing parsed objects. - */ - private AbstractApplicationContext _context; + private Map _propertyObjects; /** * Constructs a spring system. @@ -79,6 +77,8 @@ public class SpringComponent extends AbstractComponent { _configFiles = aConfigFiles; _provided = aProvided; _required = aRequired; + _propertyObjects = new HashMap(); + } /** @@ -98,72 +98,105 @@ public class SpringComponent extends AbstractComponent { setProperty((String) key, aProperties.getProperty((String) key)); } } + + public void addProperties(String aBeanname, Properties aProperties) { + _propertyObjects.put(aBeanname, aProperties); + } + + public Properties getProperties(String aBeanname) { + return _propertyObjects.get(aBeanname); + } @Override - protected void doStart() { + protected Scope doStart(Scope aExternalScope) { SpringComponent old = THIS.get(); + Scope oldScope = SCOPE.get(); THIS.set(this); + Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope); + SCOPE.set(scope); try { - _parentContext = new GenericApplicationContext(); - - registerRequiredServices(); + GenericApplicationContext parentContext = new GenericApplicationContext(); - _parentContext.refresh(); + registerRequiredServices(parentContext); + registerPropertyObjects(parentContext); - parseConfigFiles(); + parentContext.refresh(); + + System.out.println("Parent context " + parentContext); + + AbstractApplicationContext context = parseConfigFiles(parentContext); - _context + context .addBeanFactoryPostProcessor(new PropertySetter(_properties)); - _context.refresh(); + context.refresh(); - exposeProvidedServices(); + exposeProvidedServices(context, aExternalScope); + + scope.put(CONTEXT_KEY, context); + return scope; } catch (Exception e) { throw new SystemAssemblyException( - "Failed to assemble spring system", e); + "Failed to assemble spring system " + getName(), e); } finally { THIS.set(old); + SCOPE.set(oldScope); } } - private void exposeProvidedServices() { + private void exposeProvidedServices(AbstractApplicationContext aContext, Scope aScope) { // Call addService for each provided service. for (String name : _provided.keySet()) { - Object svc = _context.getBean(name); + Object svc = aContext.getBean(name); if (svc == null) { throw new IllegalArgumentException(getQualifiedName() + ": service '" + name + "' is null"); } - addInterface(_provided.get(name), svc); + addInterface(_provided.get(name), svc, aScope); + System.out.println("addService " + _provided.get(name) + " " + svc); } } - private void parseConfigFiles() { + private AbstractApplicationContext parseConfigFiles(GenericApplicationContext aParentContext) { // Parse spring config files - _context = new ClassPathXmlApplicationContext((String[]) _configFiles, - _parentContext); + return new ClassPathXmlApplicationContext((String[]) _configFiles, + false, aParentContext); } - private void registerRequiredServices() { + private void registerRequiredServices(GenericApplicationContext aParentContext) { // Register required services in a parent context for (RequiredInterface required: getRequiredInterfaces()) { 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); + if ( beanName != null && beanName.length() > 0) { + ConstructorArgumentValues cargs = new ConstructorArgumentValues(); + cargs.addGenericArgumentValue(required.getName()); + BeanDefinition definition = new RootBeanDefinition( + RequiredServiceBean.class, cargs, + new MutablePropertyValues()); + aParentContext.registerBeanDefinition(beanName, definition); + } else { + // The required interface is not required by the spring config but by the sub-class directly. + } } } + + private void registerPropertyObjects(GenericApplicationContext aParentContext) { + for (String beanName: _propertyObjects.keySet()) { + ConstructorArgumentValues cargs = new ConstructorArgumentValues(); + cargs.addGenericArgumentValue(PropertySetter.createPropertyFile(_propertyObjects.get(beanName))); + BeanDefinition definition = new RootBeanDefinition( + ConfiguredProperties.class, cargs, + new MutablePropertyValues()); + aParentContext.registerBeanDefinition(beanName, definition); + } + } + @Override - protected void doStop() { - _context.close(); - for (ProvidedInterface provided: getProvidedInterfaces()) { - removeInterface(provided); - } + protected void doStop(Scope aRuntime) { + AbstractApplicationContext context = (AbstractApplicationContext)aRuntime.get(CONTEXT_KEY); + context.close(); } }