some major refactoring.
[utils] / system / spring / src / main / java / org / wamblee / system / spring / SpringComponent.java
index b9a2648c7ca706838795538637e0c0d584aa3271..0b0921e6dcd6abbb410ee56effb32e6c4f916b03 100644 (file)
@@ -14,25 +14,20 @@ import org.wamblee.system.AbstractComponent;
 import org.wamblee.system.ProvidedInterface;
 import org.wamblee.system.RequiredInterface;
 import org.wamblee.system.Service;
-import org.wamblee.system.ServiceRegistry;
 import org.wamblee.system.SystemAssembler;
 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. 
- *
+ * 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 SpringComponent extends AbstractComponent {
 
-       /**
-        * Singleton access to the service registry. Required while starting up.
-        */
-       static ThreadLocal<ServiceRegistry> REGISTRY = new ThreadLocal<ServiceRegistry>();
+       static final ThreadLocal<SpringComponent> THIS = new ThreadLocal<SpringComponent>();
 
-       private Properties _properties; 
+       private Properties _properties;
        private String[] _configFiles;
        private Map<String, ProvidedInterface> _provided;
        private Map<RequiredInterface, String> _required;
@@ -62,57 +57,59 @@ public class SpringComponent extends AbstractComponent {
         *            names that the spring config files use for each required
         *            service.
         */
-       public SpringComponent(String aName, ServiceRegistry aRegistry, String[] aConfigFiles,
+       public SpringComponent(String aName, String[] aConfigFiles,
                        Map<String, ProvidedInterface> aProvided,
                        Map<RequiredInterface, String> aRequired) {
-               super(aName, aRegistry, aProvided.values().toArray(new ProvidedInterface[0]),
+               super(aName, aProvided.values().toArray(new ProvidedInterface[0]),
                                aRequired.keySet().toArray(new RequiredInterface[0]));
-               _properties = new Properties(); 
+               _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. 
+        * 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) { 
+       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));
+
+       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 oldRegistry = REGISTRY.get();
-               try {   
-                       REGISTRY.set(getRegistry());
-                       try {
-                               _parentContext = new GenericApplicationContext();
-
-                               registerRequiredServices(aRequiredServices);
-                               
-                               _parentContext.refresh();
-
-                               parseConfigFiles();
-                               
-                               _context.addBeanFactoryPostProcessor(new PropertySetter(_properties));
-                               _context.refresh(); 
-
-                               exposeProvidedServices(aContext);
-                       } catch (Exception e) {
-                               throw new SystemAssemblyException(
-                                               "Failed to assemble spring system", e);
-                       }
+       protected void doStart(String aContext) {
+
+               SpringComponent old = THIS.get();
+               THIS.set(this);
+               try {
+                       _parentContext = new GenericApplicationContext();
+
+                       registerRequiredServices();
+
+                       _parentContext.refresh();
+
+                       parseConfigFiles();
+
+                       _context
+                                       .addBeanFactoryPostProcessor(new PropertySetter(_properties));
+                       _context.refresh();
+
+                       exposeProvidedServices(aContext);
+               } catch (Exception e) {
+                       throw new SystemAssemblyException(
+                                       "Failed to assemble spring system", e);
                } finally {
-                       REGISTRY.set(oldRegistry);
+                       THIS.set(old);
                }
        }
 
@@ -136,27 +133,21 @@ public class SpringComponent extends AbstractComponent {
                                _parentContext);
        }
 
-       private void registerRequiredServices(Service[] aRequiredServices) {
+       private void registerRequiredServices() {
                // Register required services in a parent context
-
-               for (Service svc: aRequiredServices) { 
-                       String id = svc.getId();
-                       ProvidedInterface descriptor = svc.getDescriptor();
-                       RequiredInterface[] requiredServices = SystemAssembler.filterRequiredServices(descriptor,
-                                       _required.keySet()); 
-                       for (RequiredInterface required: requiredServices) { 
-                               String beanName = _required.get(required);
-                               ConstructorArgumentValues cargs = new ConstructorArgumentValues();
-                               cargs.addGenericArgumentValue(id); 
-                               BeanDefinition definition = new RootBeanDefinition(RequiredServiceBean.class, cargs,
-                                               new MutablePropertyValues());
-                               _parentContext.registerBeanDefinition(beanName, definition);
-                       }
+               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);
                }
        }
 
        @Override
        protected void doStop() {
-           _context.close();
+               _context.close();
        }
 }