Now the HibernateUserTest is uses components to connect the different parts together...
[utils] / system / spring / src / main / java / org / wamblee / system / spring / SpringComponent.java
index 36b50e78f38db31b22993386daf00e9024a91f81..1da30a324ed53c4c9812306669e2adc287b5b6a8 100644 (file)
@@ -26,8 +26,10 @@ import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
 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;
 
 /**
@@ -36,23 +38,17 @@ import org.wamblee.system.core.SystemAssemblyException;
  * 
  * @author Erik Brakkee
  */
-public class SpringComponent extends AbstractComponent {
+public class SpringComponent extends AbstractComponent<Scope> {
+
+       private static final String CONTEXT_KEY = "context";
 
        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;
-       /**
-        * Parent application context containing required services.
-        */
-       private GenericApplicationContext _parentContext;
-
-       /**
-        * Application context containing parsed objects.
-        */
-       private AbstractApplicationContext _context;
 
        /**
         * Constructs a spring system.
@@ -100,67 +96,78 @@ public class SpringComponent extends AbstractComponent {
        }
 
        @Override
-       protected void doStart() {
+       protected Scope doStart(Scope aExternalScope) {
 
                SpringComponent old = THIS.get();
+               Scope oldScope = SCOPE.get();
                THIS.set(this);
+               Scope scope = new DefaultScope(getProvidedInterfaces(), aExternalScope);
+               SCOPE.set(scope);
                try {
-                       _parentContext = new GenericApplicationContext();
-
-                       registerRequiredServices();
+                       GenericApplicationContext parentContext = new GenericApplicationContext();
 
-                       _parentContext.refresh();
+                       registerRequiredServices(parentContext);
 
-                       parseConfigFiles();
+                       parentContext.refresh();
+                       
+                       System.out.println("Parent context " + parentContext);
+                   
+                       AbstractApplicationContext context = parseConfigFiles(parentContext);
 
-                       _context
+                       context
                                        .addBeanFactoryPostProcessor(new PropertySetter(_properties));
-                       _context.refresh();
+                       context.refresh();
 
-                       exposeProvidedServices();
+                       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() {
+       private void exposeProvidedServices(AbstractApplicationContext aContext, Scope aScope) {
                // Call addService for each provided service.
 
                for (String name : _provided.keySet()) {
-                       Object svc = _context.getBean(name);
+                       Object svc = aContext.getBean(name);
                        if (svc == null) {
                                throw new IllegalArgumentException(getQualifiedName() + ": service '"
                                                + name + "' is null");
                        }
-                       addService(_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()) { 
+               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());
-                       _parentContext.registerBeanDefinition(beanName, definition);
+                       aParentContext.registerBeanDefinition(beanName, definition);
                }
        }
 
        @Override
-       protected void doStop() {
-               _context.close();
+       protected void doStop(Scope aRuntime) {
+               AbstractApplicationContext context = (AbstractApplicationContext)aRuntime.get(CONTEXT_KEY);
+               context.close();
        }
 }