updated coding rules.
[utils] / system / spring / src / main / java / org / wamblee / system / spring / SpringComponent.java
index aea9b15aa1a548eacfe9e7c1a39667f684606003..0e4c62eb1fe26ae2ccb858342053d818e9af0749 100644 (file)
@@ -15,6 +15,7 @@
  */ 
 package org.wamblee.system.spring;
 
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
@@ -45,10 +46,11 @@ public class SpringComponent extends AbstractComponent<Scope> {
        static final ThreadLocal<SpringComponent> THIS = new ThreadLocal<SpringComponent>();
        static final ThreadLocal<Scope> SCOPE = new ThreadLocal<Scope>();
 
-       private Properties _properties;
-       private String[] _configFiles;
-       private Map<String, ProvidedInterface> _provided;
-       private Map<RequiredInterface, String> _required;
+       private Properties properties;
+       private String[] configFiles;
+       private Map<String, ProvidedInterface> provided;
+       private Map<RequiredInterface, String> required;
+       private Map<String, Properties> propertyObjects;
 
        /**
         * Constructs a spring system.
@@ -71,10 +73,12 @@ public class SpringComponent extends AbstractComponent<Scope> {
                        Map<RequiredInterface, String> aRequired) {
                super(aName, aProvided.values().toArray(new ProvidedInterface[0]),
                                aRequired.keySet().toArray(new RequiredInterface[0]));
-               _properties = new Properties();
-               _configFiles = aConfigFiles;
-               _provided = aProvided;
-               _required = aRequired;
+               properties = new Properties();
+               configFiles = aConfigFiles;
+               provided = aProvided;
+               required = aRequired;
+               propertyObjects = new HashMap<String, Properties>(); 
+               
        }
 
        /**
@@ -86,7 +90,7 @@ public class SpringComponent extends AbstractComponent<Scope> {
         *            Property value.
         */
        public void setProperty(String aKey, String aValue) {
-               _properties.put(aKey, aValue);
+               properties.put(aKey, aValue);
        }
 
        public void addProperties(Properties aProperties) {
@@ -95,8 +99,12 @@ public class SpringComponent extends AbstractComponent<Scope> {
                }
        }
        
-       public Scope start() { 
-               return super.start(new DefaultScope(new ProvidedInterface[0]));
+       public void addProperties(String aBeanname, Properties aProperties) { 
+           propertyObjects.put(aBeanname, aProperties);
+       }
+       
+       public Properties getProperties(String aBeanname) { 
+           return propertyObjects.get(aBeanname);
        }
 
        @Override
@@ -105,28 +113,31 @@ 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();
-
+                       
+                       System.out.println("Parent context " + parentContext);
+                   
                        AbstractApplicationContext context = parseConfigFiles(parentContext);
 
                        context
-                                       .addBeanFactoryPostProcessor(new PropertySetter(_properties));
+                                       .addBeanFactoryPostProcessor(new PropertySetter(properties));
                        context.refresh();
 
-                       exposeProvidedServices(context, scope);
+                       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);
@@ -136,37 +147,52 @@ public class SpringComponent extends AbstractComponent<Scope> {
        private void exposeProvidedServices(AbstractApplicationContext aContext, Scope aScope) {
                // Call addService for each provided service.
 
-               for (String name : _provided.keySet()) {
+               for (String name : provided.keySet()) {
                        Object svc = aContext.getBean(name);
                        if (svc == null) {
                                throw new IllegalArgumentException(getQualifiedName() + ": service '"
                                                + name + "' is null");
                        }
-                       addInterface(_provided.get(name), svc, aScope);
-                       System.out.println("addService " + _provided.get(name) + " " + svc);
-                       aScope.publishInterface(_provided.get(name), svc);
+                       addInterface(provided.get(name), svc, aScope);
+                       System.out.println("addService " + provided.get(name) + " " + svc);
                }
        }
 
        private AbstractApplicationContext parseConfigFiles(GenericApplicationContext aParentContext) {
                // Parse spring config files
 
-               return new ClassPathXmlApplicationContext((String[]) _configFiles,
-                               aParentContext);
+               return new ClassPathXmlApplicationContext((String[]) configFiles,
+                               false, aParentContext);
        }
 
        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());
-                       aParentContext.registerBeanDefinition(beanName, definition);
+               for (RequiredInterface requiredIntf: getRequiredInterfaces()) { 
+                       String beanName = required.get(requiredIntf);
+                       if ( beanName != null && beanName.length() > 0) { 
+                           ConstructorArgumentValues cargs = new ConstructorArgumentValues();
+                   cargs.addGenericArgumentValue(requiredIntf.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) {