updated coding rules.
[utils] / system / spring / src / main / java / org / wamblee / system / spring / SpringComponent.java
index f7177759aeabe7865ed1f14f72c10d488efd277f..0e4c62eb1fe26ae2ccb858342053d818e9af0749 100644 (file)
@@ -1,5 +1,21 @@
+/*
+ * Copyright 2007 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
 package org.wamblee.system.spring;
 
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
@@ -10,10 +26,12 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
-import org.wamblee.system.AbstractComponent;
-import org.wamblee.system.ProvidedInterface;
-import org.wamblee.system.RequiredInterface;
-import org.wamblee.system.SystemAssemblyException;
+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;
 
 /**
  * Represents a system configured based on spring. The spring config files that
@@ -21,23 +39,18 @@ import org.wamblee.system.SystemAssemblyException;
  * 
  * @author Erik Brakkee
  */
-public class SpringComponent extends AbstractComponent {
+public class SpringComponent extends AbstractComponent<Scope> {
 
-       static final ThreadLocal<SpringComponent> THIS = new ThreadLocal<SpringComponent>();
+       private static final String CONTEXT_KEY = "context";
 
-       private Properties _properties;
-       private String[] _configFiles;
-       private Map<String, ProvidedInterface> _provided;
-       private Map<RequiredInterface, String> _required;
-       /**
-        * Parent application context containing required services.
-        */
-       private GenericApplicationContext _parentContext;
+       static final ThreadLocal<SpringComponent> THIS = new ThreadLocal<SpringComponent>();
+       static final ThreadLocal<Scope> SCOPE = new ThreadLocal<Scope>();
 
-       /**
-        * Application context containing parsed objects.
-        */
-       private AbstractApplicationContext _context;
+       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.
@@ -60,10 +73,12 @@ public class SpringComponent extends AbstractComponent {
                        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>(); 
+               
        }
 
        /**
@@ -75,7 +90,7 @@ public class SpringComponent extends AbstractComponent {
         *            Property value.
         */
        public void setProperty(String aKey, String aValue) {
-               _properties.put(aKey, aValue);
+               properties.put(aKey, aValue);
        }
 
        public void addProperties(Properties aProperties) {
@@ -83,69 +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(String aContext) {
+       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();
+                       GenericApplicationContext parentContext = new GenericApplicationContext();
 
-                       registerRequiredServices();
+                       registerRequiredServices(parentContext);
+                       registerPropertyObjects(parentContext);
 
-                       _parentContext.refresh();
+                       parentContext.refresh();
+                       
+                       System.out.println("Parent context " + parentContext);
+                   
+                       AbstractApplicationContext context = parseConfigFiles(parentContext);
 
-                       parseConfigFiles();
+                       context
+                                       .addBeanFactoryPostProcessor(new PropertySetter(properties));
+                       context.refresh();
 
-                       _context
-                                       .addBeanFactoryPostProcessor(new PropertySetter(_properties));
-                       _context.refresh();
-
-                       exposeProvidedServices(aContext);
+                       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(String aContext) {
+       private void exposeProvidedServices(AbstractApplicationContext aContext, Scope aScope) {
                // Call addService for each provided service.
 
-               for (String name : _provided.keySet()) {
-                       Object svc = _context.getBean(name);
+               for (String name : provided.keySet()) {
+                       Object svc = aContext.getBean(name);
                        if (svc == null) {
-                               throw new IllegalArgumentException(aContext + ": service '"
+                               throw new IllegalArgumentException(getQualifiedName() + ": service '"
                                                + name + "' is null");
                        }
-                       addService(aContext, _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: getRequiredServices()) { 
-                       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);
+               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() {
-               _context.close();
+       protected void doStop(Scope aRuntime) {
+               AbstractApplicationContext context = (AbstractApplicationContext)aRuntime.get(CONTEXT_KEY);
+               context.close();
        }
 }