Added a method to set properties on the spring component.
[utils] / 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 05731af829aa0b7ae24aafa2d79243af3fa247f2..633050cbfecacee89b2cd5a723762ec8d520b970 100644 (file)
@@ -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<ServiceRegistry> REGISTRY = new ThreadLocal<ServiceRegistry>();
 
+       private Properties _properties; 
        private String[] _configFiles;
        private Map<String, ProvidedInterfaceDescriptor> _provided;
        private Map<RequiredInterfaceDescriptor, String> _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<String, ProvidedInterfaceDescriptor> aProvided,
                        Map<RequiredInterfaceDescriptor, String> 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