(no commit message)
[utils] / system / spring / src / main / java / org / wamblee / system / spring / SpringComponent.java
index 657d11e4f8bd31bfb54341bc7462d4c285c5c62d..88746a5ecc4a94f50a0af17653a49a05bce7f51d 100644 (file)
@@ -15,6 +15,7 @@
  */ 
 package org.wamblee.system.spring;
 
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
@@ -49,6 +50,7 @@ public class SpringComponent extends AbstractComponent<Scope> {
        private String[] _configFiles;
        private Map<String, ProvidedInterface> _provided;
        private Map<RequiredInterface, String> _required;
+       private Map<String, Properties> _propertyObjects;
 
        /**
         * Constructs a spring system.
@@ -75,6 +77,8 @@ public class SpringComponent extends AbstractComponent<Scope> {
                _configFiles = aConfigFiles;
                _provided = aProvided;
                _required = aRequired;
+               _propertyObjects = new HashMap<String, Properties>(); 
+               
        }
 
        /**
@@ -94,6 +98,14 @@ public class SpringComponent extends AbstractComponent<Scope> {
                        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 Scope doStart(Scope aExternalScope) {
@@ -101,12 +113,13 @@ public class SpringComponent extends AbstractComponent<Scope> {
                SpringComponent old = THIS.get();
                Scope oldScope = SCOPE.get();
                THIS.set(this);
-               Scope scope = new DefaultScope(getProvidedInterfaces(), aExternalScope);
+               Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
                SCOPE.set(scope);
                try {
                        GenericApplicationContext parentContext = new GenericApplicationContext();
 
                        registerRequiredServices(parentContext);
+                       registerPropertyObjects(parentContext);
 
                        parentContext.refresh();
                        
@@ -124,7 +137,7 @@ public class SpringComponent extends AbstractComponent<Scope> {
                        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);
@@ -156,14 +169,30 @@ public class SpringComponent extends AbstractComponent<Scope> {
                // 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());
-                       aParentContext.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(Scope aRuntime) {