X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fspring%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fspring%2FSpringComponent.java;h=aea9b15aa1a548eacfe9e7c1a39667f684606003;hb=fc10e15e031cf409146b04cbb5e98b649c6ccddc;hp=f7177759aeabe7865ed1f14f72c10d488efd277f;hpb=6110f13b29c47ba0fe8039194ee2c4a992911e3b;p=utils diff --git a/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java index f7177759..aea9b15a 100644 --- a/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java +++ b/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java @@ -1,3 +1,18 @@ +/* + * 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.Map; @@ -10,10 +25,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 +38,17 @@ import org.wamblee.system.SystemAssemblyException; * * @author Erik Brakkee */ -public class SpringComponent extends AbstractComponent { +public class SpringComponent extends AbstractComponent { + + private static final String CONTEXT_KEY = "context"; static final ThreadLocal THIS = new ThreadLocal(); + static final ThreadLocal SCOPE = new ThreadLocal(); private Properties _properties; private String[] _configFiles; private Map _provided; private Map _required; - /** - * Parent application context containing required services. - */ - private GenericApplicationContext _parentContext; - - /** - * Application context containing parsed objects. - */ - private AbstractApplicationContext _context; /** * Constructs a spring system. @@ -83,69 +94,83 @@ public class SpringComponent extends AbstractComponent { setProperty((String) key, aProperties.getProperty((String) key)); } } + + public Scope start() { + return super.start(new DefaultScope(new ProvidedInterface[0])); + } @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(), aExternalScope); + SCOPE.set(scope); try { - _parentContext = new GenericApplicationContext(); + GenericApplicationContext parentContext = new GenericApplicationContext(); - registerRequiredServices(); + registerRequiredServices(parentContext); - _parentContext.refresh(); + parentContext.refresh(); - parseConfigFiles(); + AbstractApplicationContext context = parseConfigFiles(parentContext); - _context + context .addBeanFactoryPostProcessor(new PropertySetter(_properties)); - _context.refresh(); + context.refresh(); - exposeProvidedServices(aContext); + exposeProvidedServices(context, scope); + + scope.put(CONTEXT_KEY, context); + return scope; } catch (Exception e) { throw new SystemAssemblyException( "Failed to assemble spring system", 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); + 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); + aScope.publishInterface(_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, + 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(); } }