X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2FSpringComponent.java;fp=system%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2FSpringSystem.java;h=633050cbfecacee89b2cd5a723762ec8d520b970;hb=d2bdf4e813c6a3964958c87b2ce56eaadf8a1f0a;hp=05731af829aa0b7ae24aafa2d79243af3fa247f2;hpb=2a885e74d3f518def3887df98920f632177ed0b9;p=utils diff --git a/system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java similarity index 77% rename from system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java rename to system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java index 05731af8..633050cb 100644 --- a/system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java +++ b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java @@ -1,6 +1,8 @@ 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; @@ -22,16 +24,19 @@ 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. * * @author Erik Brakkee */ -public class SpringSystem extends AbstractComponent { +public class SpringComponent extends AbstractComponent { /** * Singleton access to the service registry. Required while starting up. */ static ThreadLocal REGISTRY = new ThreadLocal(); + private Properties _properties; private String[] _configFiles; private Map _provided; private Map _required; @@ -46,11 +51,11 @@ public class SpringSystem extends AbstractComponent { private AbstractApplicationContext _context; /** - * Construcst a spring system. + * Constructs a spring system. * * @param aName * Name of the system. - * @param aConfigFiles + * @param aConfigFil * Spring config files to read. * @param aProvided * Map of bean name to service descriptor describing the bean @@ -61,27 +66,49 @@ public class SpringSystem extends AbstractComponent { * names that the spring config files use for each required * service. */ - public SpringSystem(String aName, ServiceRegistry aRegistry, String[] aConfigFiles, + 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(); _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. + */ + 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)); + } + } @Override protected void doStart(String aContext, Service[] aRequiredServices) { - ServiceRegistry old = REGISTRY.get(); + ServiceRegistry oldRegistry = REGISTRY.get(); try { - REGISTRY.set(getRegistry()); + REGISTRY.set(getRegistry()); try { + _parentContext = new GenericApplicationContext(); registerRequiredServices(aRequiredServices); + + _parentContext.refresh(); parseConfigFiles(); + + _context.addBeanFactoryPostProcessor(new PropertySetter(_properties)); + _context.refresh(); exposeProvidedServices(aContext); } catch (Exception e) { @@ -89,7 +116,7 @@ public class SpringSystem extends AbstractComponent { "Failed to assemble spring system", e); } } finally { - REGISTRY.set(old); + REGISTRY.set(oldRegistry); } } @@ -116,9 +143,6 @@ public class SpringSystem extends AbstractComponent { private void registerRequiredServices(Service[] aRequiredServices) { // Register required services in a parent context - // Register the Hibernate mapping files as a bean. - _parentContext = new GenericApplicationContext(); - for (Service svc: aRequiredServices) { String id = svc.getId(); ProvidedInterfaceDescriptor descriptor = svc.getDescriptor(); @@ -128,12 +152,11 @@ public class SpringSystem extends AbstractComponent { String beanName = _required.get(required); ConstructorArgumentValues cargs = new ConstructorArgumentValues(); cargs.addGenericArgumentValue(id); - BeanDefinition definition = new RootBeanDefinition(ProvidedServiceBean.class, cargs, + BeanDefinition definition = new RootBeanDefinition(RequiredServiceBean.class, cargs, new MutablePropertyValues()); _parentContext.registerBeanDefinition(beanName, definition); } } - _parentContext.refresh(); } @Override