- 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;
- private Map<String, Properties> _propertyObjects;
-
- /**
- * Constructs a spring system.
- *
- * @param aName
- * Name of the system.
- * @param aConfigFil
- * Spring config files to read.
- * @param aProvided
- * Map of bean name to service descriptor describing the bean
- * names that the spring config files use for each required
- * service.
- * @param aRequired
- * Map of bean name to service descriptor describing the bean
- * names that the spring config files use for each required
- * service.
- */
- public SpringComponent(String aName, String[] aConfigFiles,
- Map<String, ProvidedInterface> aProvided,
- 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;
- _propertyObjects = new HashMap<String, Properties>();
-
- }
-
- /**
- * 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) {
- _properties.put(aKey, aValue);
- }
-
- public void addProperties(Properties aProperties) {
- for (Object key : aProperties.keySet()) {
- 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 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 {
- 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));
- context.refresh();
-
- exposeProvidedServices(context, aExternalScope);
-
- scope.put(CONTEXT_KEY, context);
- return scope;
- } catch (Exception e) {
- throw new SystemAssemblyException(
- "Failed to assemble spring system " + getName(), e);
- } finally {
- THIS.set(old);
- SCOPE.set(oldScope);
- }
- }
-
- private void exposeProvidedServices(AbstractApplicationContext aContext, Scope aScope) {
- // Call addService for each provided service.
-
- 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);
- }
- }
-
- private AbstractApplicationContext parseConfigFiles(GenericApplicationContext aParentContext) {
- // Parse spring config files
-
- 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);
- }
- }
-
- private void registerPropertyObjects(GenericApplicationContext aParentContext) {
- for (String beanName: _propertyObjects.keySet()) {
+ System.out.println("Parent context " + parentContext);
+
+ AbstractApplicationContext context = parseConfigFiles(parentContext);
+
+ context.addBeanFactoryPostProcessor(new PropertySetter(properties));
+ context.refresh();
+
+ exposeProvidedServices(context, aExternalScope);
+
+ scope.put(CONTEXT_KEY, context);
+
+ return scope;
+ } catch (Exception e) {
+ throw new SystemAssemblyException(
+ "Failed to assemble spring system " + getName(), e);
+ } finally {
+ THIS.set(old);
+ SCOPE.set(oldScope);
+ }
+ }
+
+ private void exposeProvidedServices(AbstractApplicationContext aContext,
+ Scope aScope) {
+ // Call addService for each provided service.
+ 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);
+ }
+ }
+
+ private AbstractApplicationContext parseConfigFiles(
+ GenericApplicationContext aParentContext) {
+ // Parse spring config files
+ return new ClassPathXmlApplicationContext((String[]) configFiles,
+ false, aParentContext);
+ }
+
+ private void registerRequiredServices(
+ GenericApplicationContext aParentContext) {
+ // Register required services in a parent context
+ 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()) {