Author: jboynes
Date: Sat Apr 15 17:31:28 2006
New Revision: 394407
URL: http://svn.apache.org/viewcvs?rev=394407&view=rev
Log:
merge autowire and configuration context handling into common superclass
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/AbstractCompositeContext.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/CompositeContextImpl.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/system/context/SystemCompositeContextImpl.java
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/AbstractCompositeContext.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/AbstractCompositeContext.java?rev=394407&r1=394406&r2=394407&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/AbstractCompositeContext.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/AbstractCompositeContext.java
Sat Apr 15 17:31:28 2006
@@ -22,6 +22,7 @@
import org.apache.tuscany.core.context.MissingContextFactoryException;
import org.apache.tuscany.core.context.ProxyConfigurationException;
import org.apache.tuscany.core.context.MissingScopeException;
+import org.apache.tuscany.core.context.AutowireResolutionException;
import org.apache.tuscany.core.context.event.RequestEnd;
import org.apache.tuscany.core.context.event.Event;
import org.apache.tuscany.core.context.event.SessionBound;
@@ -33,6 +34,7 @@
import org.apache.tuscany.core.wire.ProxyInitializationException;
import org.apache.tuscany.core.system.annotation.Autowire;
import org.apache.tuscany.core.system.annotation.ParentContext;
+import org.apache.tuscany.core.system.assembly.SystemBinding;
import org.apache.tuscany.model.assembly.Composite;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.Implementation;
@@ -41,6 +43,10 @@
import org.apache.tuscany.model.assembly.ExternalService;
import org.apache.tuscany.model.assembly.Module;
import org.apache.tuscany.model.assembly.Scope;
+import org.apache.tuscany.model.assembly.Binding;
+import org.apache.tuscany.model.assembly.ModuleComponent;
+import org.apache.tuscany.model.assembly.Service;
+import org.apache.tuscany.model.assembly.AssemblyObject;
import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl;
import org.apache.tuscany.common.TuscanyRuntimeException;
@@ -50,6 +56,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.EnumMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -59,7 +66,8 @@
*
* @version $Rev$ $Date$
*/
-public abstract class AbstractCompositeContext extends AbstractContext
implements AutowireContext, ScopeAwareContext {
[EMAIL PROTECTED]({"FieldAccessedSynchronizedAndUnsynchronized",
"RawUseOfParameterizedType", "NonPrivateFieldAccessedInSynchronizedContext"})
+public abstract class AbstractCompositeContext extends AbstractContext
implements AutowireContext, ScopeAwareContext, ConfigurationContext {
public static final int DEFAULT_WAIT = 1000 * 60;
@@ -97,6 +105,12 @@
// Indicates whether the module context has been initialized
protected boolean initialized;
+ // a mapping of service type to component name
+ protected Map<Class, NameToScope> autowireIndex = new
ConcurrentHashMap<Class, NameToScope>();
+
+ @Autowire(required = false)
+ private AutowireContext autowireContext;
+
public AbstractCompositeContext() {
scopeIndex = new ConcurrentHashMap<String, ScopeContext>();
// FIXME the factory should be injected
@@ -127,7 +141,7 @@
lifecycleState = INITIALIZING;
initializeScopes();
- Map<Scope, List<ContextFactory<Context>>>
configurationsByScope = new HashMap<Scope, List<ContextFactory<Context>>>();
+ Map<Scope, List<ContextFactory<Context>>>
configurationsByScope = new EnumMap<Scope,
List<ContextFactory<Context>>>(Scope.class);
if (configurations != null) {
for (ContextFactory<Context> contextFactory :
configurations.values()) {
// FIXME scopes are defined at the interface level
@@ -194,7 +208,7 @@
initialized = false;
if (scopeContexts != null) {
for (ScopeContext scopeContext : scopeContexts.values()) {
- if (scopeContext.getLifecycleState() == ScopeContext.RUNNING) {
+ if (scopeContext.getLifecycleState() == Context.RUNNING) {
scopeContext.stop();
}
}
@@ -488,13 +502,6 @@
}
/**
- * Registers a model object as autowirable
- *
- * @throws org.apache.tuscany.core.context.ContextInitException
- */
- protected abstract void registerAutowire(Extensible model) throws
ConfigurationException;
-
- /**
* Blocks until the module context has been initialized
*/
protected void checkInit() {
@@ -608,4 +615,184 @@
public Composite getComposite() {
return module;
}
-}
\ No newline at end of file
+
+ public void setAutowireContext(AutowireContext context) {
+ autowireContext = context;
+ }
+
+ public <T> T resolveInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
+ if (ConfigurationContext.class.equals(instanceInterface)) {
+ return instanceInterface.cast(this);
+ } else if (AutowireContext.class.equals(instanceInterface)) {
+ return instanceInterface.cast(this);
+ }
+
+ NameToScope nts = autowireIndex.get(instanceInterface);
+ if (nts != null) {
+ try {
+ return
instanceInterface.cast(nts.getScopeContext().getInstance(nts.getName()));
+ } catch (TargetException e) {
+ AutowireResolutionException ae = new
AutowireResolutionException("Autowire instance not found", e);
+ ae.addContextName(getName());
+ throw ae;
+ }
+ }
+ if (autowireContext != null) {
+ try {
+ // resolve to parent
+ return autowireContext.resolveInstance(instanceInterface);
+ } catch (AutowireResolutionException e) {
+ e.addContextName(getName());
+ throw e;
+ }
+ } else {
+ return null;
+ }
+ }
+
+ public <T> T resolveExternalInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
+ NameToScope nts = autowireIndex.get(instanceInterface);
+ if (nts != null && nts.isVisible()) {
+ return
instanceInterface.cast(nts.getScopeContext().getInstance(nts.getName()));
+ } else {
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void registerAutowire(Extensible model) throws
ConfigurationException {
+ if (lifecycleState == INITIALIZING || lifecycleState == INITIALIZED ||
lifecycleState == RUNNING) {
+ if (model instanceof EntryPoint) {
+ EntryPoint ep = (EntryPoint) model;
+ for (Binding binding : ep.getBindings()) {
+ if (binding instanceof SystemBinding) {
+ Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
+ NameToScope nts = autowireIndex.get(interfaze);
+ if (nts == null || !nts.isEntryPoint()) { // handle
special case where two entry points with
+ // same interface register: first wins
+ ScopeContext scope =
scopeContexts.get(((ContextFactory) ep.getContextFactory()).getScope());
+ if (scope == null) {
+ ConfigurationException ce = new
MissingScopeException("Scope not found for entry point");
+ ce.setIdentifier(ep.getName());
+ ce.addContextName(getName());
+ throw ce;
+ }
+ // only register if an impl has not already been
registered
+ NameToScope mapping = new NameToScope(new
QualifiedName(ep.getName()), scope, true, true);
+ autowireIndex.put(interfaze, mapping);
+ }
+ }
+ }
+ } else if (model instanceof ModuleComponent) {
+ ModuleComponent component = (ModuleComponent) model;
+ for (EntryPoint ep :
component.getImplementation().getEntryPoints()) {
+ for (Binding binding : ep.getBindings()) {
+ if (binding instanceof SystemBinding) {
+ Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
+ if (autowireIndex.get(interfaze) == null) {
+ ScopeContext scope =
scopeContexts.get(Scope.AGGREGATE);
+ // only register if an impl has not already
been registered, ensuring it is not visible outside the containment
+ NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()
+ + QualifiedName.NAME_SEPARATOR +
ep.getName()), scope, false, false);
+ autowireIndex.put(interfaze, mapping);
+ }
+ }
+ }
+ }
+ } else if (model instanceof Component) {
+ Component component = (Component) model;
+ for (Service service :
component.getImplementation().getComponentInfo().getServices()) {
+ Class interfaze =
service.getServiceContract().getInterface();
+ if (autowireIndex.get(interfaze) == null) {
+ // only register if an impl has not already been
registered
+ ScopeContext scopeCtx =
scopeContexts.get(service.getServiceContract().getScope());
+ NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()), scopeCtx, false, false);
+ autowireIndex.put(interfaze, mapping);
+ }
+ }
+ }
+ }
+ }
+
+ protected static class NameToScope {
+
+ private final QualifiedName qName;
+
+ private final ScopeContext scope;
+
+ private final boolean visible;
+
+ private final boolean entryPoint;
+
+ public NameToScope(QualifiedName name, ScopeContext scope, boolean
visible, boolean entryPoint) {
+ this.qName = name;
+ this.scope = scope;
+ this.visible = visible;
+ this.entryPoint = entryPoint;
+ }
+
+ public QualifiedName getName() {
+ return qName;
+ }
+
+ public ScopeContext getScopeContext() {
+ return scope;
+ }
+
+ public boolean isVisible() {
+ return visible;
+ }
+
+ public boolean isEntryPoint() {
+ return entryPoint;
+ }
+
+ }
+
+
+ public void configure(Extensible model) throws ConfigurationException {
+ if (configurationContext != null) {
+ try {
+ configurationContext.configure(model);
+ } catch (ConfigurationException e) {
+ e.addContextName(getName());
+ throw e;
+ }
+ }
+ }
+
+ public void build(AssemblyObject model) throws BuilderConfigException {
+ if (configurationContext != null) {
+ try {
+ configurationContext.build(model);
+ } catch (BuilderConfigException e) {
+ e.addContextName(getName());
+ throw e;
+ }
+ }
+ }
+
+ public void connect(ProxyFactory sourceFactory, ProxyFactory
targetFactory, Class targetType, boolean downScope,
+ ScopeContext targetScopeContext) throws BuilderConfigException {
+ if (configurationContext != null) {
+ try {
+ configurationContext.connect(sourceFactory, targetFactory,
targetType, downScope, targetScopeContext);
+ } catch (BuilderConfigException e) {
+ e.addContextName(getName());
+ throw e;
+ }
+ }
+ }
+
+ public void completeTargetChain(ProxyFactory targetFactory, Class
targetType, ScopeContext targetScopeContext)
+ throws BuilderConfigException {
+ if (configurationContext != null) {
+ try {
+ configurationContext.completeTargetChain(targetFactory,
targetType, targetScopeContext);
+ } catch (BuilderConfigException e) {
+ e.addContextName(getName());
+ throw e;
+ }
+ }
+ }
+}
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/CompositeContextImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/CompositeContextImpl.java?rev=394407&r1=394406&r2=394407&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/CompositeContextImpl.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/impl/CompositeContextImpl.java
Sat Apr 15 17:31:28 2006
@@ -16,39 +16,23 @@
*/
package org.apache.tuscany.core.context.impl;
-import org.apache.tuscany.core.builder.BuilderConfigException;
-import org.apache.tuscany.core.builder.ContextFactory;
-import org.apache.tuscany.core.config.ConfigurationException;
-import org.apache.tuscany.core.context.ConfigurationContext;
-import org.apache.tuscany.core.context.EventContext;
+import org.osoa.sca.ModuleContext;
+import org.osoa.sca.RequestContext;
+import org.osoa.sca.ServiceReference;
+import org.osoa.sca.ServiceUnavailableException;
+
import org.apache.tuscany.core.context.AutowireContext;
+import org.apache.tuscany.core.context.AutowireResolutionException;
import org.apache.tuscany.core.context.CompositeContext;
-import org.apache.tuscany.core.context.ScopeStrategy;
+import org.apache.tuscany.core.context.ConfigurationContext;
+import org.apache.tuscany.core.context.Context;
+import org.apache.tuscany.core.context.EventContext;
import org.apache.tuscany.core.context.QualifiedName;
-import org.apache.tuscany.core.context.ServiceNotFoundException;
import org.apache.tuscany.core.context.ScopeContext;
-import org.apache.tuscany.core.context.Context;
+import org.apache.tuscany.core.context.ScopeStrategy;
+import org.apache.tuscany.core.context.ServiceNotFoundException;
import org.apache.tuscany.core.context.TargetException;
-import org.apache.tuscany.core.context.AutowireResolutionException;
-import org.apache.tuscany.core.context.MissingScopeException;
-import org.apache.tuscany.core.wire.ProxyFactory;
import org.apache.tuscany.core.system.annotation.Autowire;
-import org.apache.tuscany.core.system.assembly.SystemBinding;
-import org.apache.tuscany.model.assembly.AssemblyObject;
-import org.apache.tuscany.model.assembly.Binding;
-import org.apache.tuscany.model.assembly.Component;
-import org.apache.tuscany.model.assembly.EntryPoint;
-import org.apache.tuscany.model.assembly.Extensible;
-import org.apache.tuscany.model.assembly.ModuleComponent;
-import org.apache.tuscany.model.assembly.Scope;
-import org.apache.tuscany.model.assembly.Service;
-import org.osoa.sca.ModuleContext;
-import org.osoa.sca.RequestContext;
-import org.osoa.sca.ServiceReference;
-import org.osoa.sca.ServiceUnavailableException;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
/**
* The standard implementation of an composite context. Autowiring is
performed by delegating to the parent context.
@@ -57,12 +41,6 @@
*/
public class CompositeContextImpl extends AbstractCompositeContext implements
ConfigurationContext, ModuleContext {
- // a mapping of service type to component name
- private Map<Class, NameToScope> autowireIndex = new
ConcurrentHashMap<Class, NameToScope>();
-
- @Autowire(required = false)
- private AutowireContext autowireContext;
-
@Autowire(required = false)
public void setScopeStrategy(ScopeStrategy scopeStrategy) {
if (scopeStrategy != null) {
@@ -83,7 +61,7 @@
public CompositeContextImpl(String name, CompositeContext parent,
AutowireContext autowireContext, ScopeStrategy strategy,
EventContext ctx, ConfigurationContext
configCtx) {
super(name, parent, strategy, ctx, configCtx);
- this.autowireContext = autowireContext;
+ setAutowireContext(autowireContext);
}
// ----------------------------------
@@ -143,175 +121,4 @@
public ServiceReference newSession(String serviceName, Object sessionId) {
throw new UnsupportedOperationException();
}
-
- public <T> T resolveExternalInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
- NameToScope nts = autowireIndex.get(instanceInterface);
- if (nts != null && nts.isVisible()) {
- return
instanceInterface.cast(nts.getScopeContext().getInstance(nts.getName()));
- // } else if (autowireContext != null) {
- // return
instanceInterface.cast(autowireContext.resolveExternalInstance(instanceInterface));
- } else {
- return null;
- }
- }
-
- public <T> T resolveInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
- if (ConfigurationContext.class.equals(instanceInterface)) {
- return instanceInterface.cast(this);
- } else if (AutowireContext.class.equals(instanceInterface)) {
- return instanceInterface.cast(this);
- }
- NameToScope nts = autowireIndex.get(instanceInterface);
- if (nts != null) {
- return
instanceInterface.cast(nts.getScopeContext().getInstance(nts.getName()));
- }
- if (autowireContext != null) {
- try {
- // resolve to parent
- return
instanceInterface.cast(autowireContext.resolveInstance(instanceInterface));
- } catch (AutowireResolutionException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- return null;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- protected void registerAutowire(Extensible model) throws
ConfigurationException {
- if (lifecycleState == INITIALIZING || lifecycleState == INITIALIZED ||
lifecycleState == RUNNING) {
- if (model instanceof EntryPoint) {
- EntryPoint ep = (EntryPoint) model;
- for (Binding binding : ep.getBindings()) {
- if (binding instanceof SystemBinding) {
- Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
- NameToScope nts = autowireIndex.get(interfaze);
- if (nts == null || !nts.isEntryPoint()) { // handle
special case where two entry points with
- // same interface register: first wins
- ScopeContext scope =
scopeContexts.get(((ContextFactory) ep.getContextFactory()).getScope());
- if (scope == null) {
- ConfigurationException ce = new
MissingScopeException("Scope not found for entry point");
- ce.setIdentifier(ep.getName());
- ce.addContextName(getName());
- throw ce;
- }
- // only register if an impl has not already been
registered
- NameToScope mapping = new NameToScope(new
QualifiedName(ep.getName()), scope, true, true);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- } else if (model instanceof ModuleComponent) {
- ModuleComponent component = (ModuleComponent) model;
- for (EntryPoint ep :
component.getImplementation().getEntryPoints()) {
- for (Binding binding : ep.getBindings()) {
- if (binding instanceof SystemBinding) {
- Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
- if (autowireIndex.get(interfaze) == null) {
- ScopeContext scope =
scopeContexts.get(Scope.AGGREGATE);
- // only register if an impl has not already
been registered, ensuring it is not visible outside the containment
- NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()
- + QualifiedName.NAME_SEPARATOR +
ep.getName()), scope, false, false);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- }
- } else if (model instanceof Component) {
- Component component = (Component) model;
- for (Service service :
component.getImplementation().getComponentInfo().getServices()) {
- Class interfaze =
service.getServiceContract().getInterface();
- if (autowireIndex.get(interfaze) == null) {
- // only register if an impl has not already been
registered
- ScopeContext scopeCtx =
scopeContexts.get(service.getServiceContract().getScope());
- NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()), scopeCtx, false, false);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- }
- }
-
- public void configure(Extensible model) throws ConfigurationException {
- if (configurationContext != null) {
- try {
- configurationContext.configure(model);
- } catch (ConfigurationException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- public void build(AssemblyObject model) throws BuilderConfigException {
- if (configurationContext != null) {
- try {
- configurationContext.build(model);
- } catch (BuilderConfigException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- public void connect(ProxyFactory sourceFactory, ProxyFactory
targetFactory, Class targetType, boolean downScope,
- ScopeContext targetScopeContext) throws BuilderConfigException {
- if (configurationContext != null) {
- try {
- configurationContext.connect(sourceFactory, targetFactory,
targetType, downScope, targetScopeContext);
- } catch (BuilderConfigException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- public void completeTargetChain(ProxyFactory targetFactory, Class
targetType, ScopeContext targetScopeContext)
- throws BuilderConfigException {
- if (configurationContext != null) {
- try {
- configurationContext.completeTargetChain(targetFactory,
targetType, targetScopeContext);
- } catch (BuilderConfigException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- private class NameToScope {
-
- private QualifiedName qName;
-
- private ScopeContext scope;
-
- private boolean visible;
-
- private boolean entryPoint;
-
- public NameToScope(QualifiedName name, ScopeContext scope, boolean
visible, boolean entryPoint) {
- this.qName = name;
- this.scope = scope;
- this.visible = visible;
- this.entryPoint = entryPoint;
- }
-
- public QualifiedName getName() {
- return qName;
- }
-
- public ScopeContext getScopeContext() {
- return scope;
- }
-
- public boolean isVisible() {
- return visible;
- }
-
- public boolean isEntryPoint() {
- return entryPoint;
- }
-
- }
-
}
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/system/context/SystemCompositeContextImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/system/context/SystemCompositeContextImpl.java?rev=394407&r1=394406&r2=394407&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/system/context/SystemCompositeContextImpl.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/system/context/SystemCompositeContextImpl.java
Sat Apr 15 17:31:28 2006
@@ -16,42 +16,25 @@
*/
package org.apache.tuscany.core.system.context;
-import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import org.apache.tuscany.core.builder.BuilderConfigException;
-import org.apache.tuscany.core.builder.ContextFactory;
import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.context.AutowireContext;
import org.apache.tuscany.core.context.AutowireResolutionException;
import org.apache.tuscany.core.context.CompositeContext;
import org.apache.tuscany.core.context.ConfigurationContext;
import org.apache.tuscany.core.context.EventContext;
-import org.apache.tuscany.core.context.MissingScopeException;
import org.apache.tuscany.core.context.QualifiedName;
import org.apache.tuscany.core.context.ScopeContext;
import org.apache.tuscany.core.context.ScopeStrategy;
import org.apache.tuscany.core.context.SystemCompositeContext;
-import org.apache.tuscany.core.context.TargetException;
import org.apache.tuscany.core.context.impl.AbstractCompositeContext;
import org.apache.tuscany.core.context.impl.EventContextImpl;
import org.apache.tuscany.core.message.MessageFactory;
import org.apache.tuscany.core.message.impl.MessageFactoryImpl;
-import org.apache.tuscany.core.runtime.RuntimeContext;
-import org.apache.tuscany.core.system.annotation.Autowire;
-import org.apache.tuscany.core.system.assembly.SystemBinding;
import org.apache.tuscany.core.system.config.SystemObjectContextFactory;
-import org.apache.tuscany.core.wire.ProxyFactory;
import org.apache.tuscany.core.wire.ProxyFactoryFactory;
import org.apache.tuscany.core.wire.jdk.JDKProxyFactoryFactory;
-import org.apache.tuscany.model.assembly.AssemblyObject;
-import org.apache.tuscany.model.assembly.Binding;
-import org.apache.tuscany.model.assembly.Component;
-import org.apache.tuscany.model.assembly.EntryPoint;
-import org.apache.tuscany.model.assembly.Extensible;
-import org.apache.tuscany.model.assembly.ModuleComponent;
-import org.apache.tuscany.model.assembly.Scope;
-import org.apache.tuscany.model.assembly.Service;
/**
@@ -65,12 +48,6 @@
* @version $Rev$ $Date$
*/
public class SystemCompositeContextImpl extends AbstractCompositeContext
implements SystemCompositeContext {
- // a mapping of service type to component name
- private Map<Class, NameToScope> autowireIndex = new
ConcurrentHashMap<Class, NameToScope>();
-
- @Autowire(required = false)
- private AutowireContext autowireContext;
-
public SystemCompositeContextImpl() {
super();
eventContext = new EventContextImpl();
@@ -85,14 +62,10 @@
ConfigurationContext configCtx
) {
super(name, parent, strategy, ctx, configCtx);
- this.autowireContext = autowire;
+ setAutowireContext(autowire);
scopeIndex = new ConcurrentHashMap<String, ScopeContext>();
}
- public void setAutowireContext(AutowireContext context) {
- autowireContext = context;
- }
-
public void registerJavaObject(String componentName, Class<?> service,
Object instance) throws ConfigurationException {
SystemObjectContextFactory configuration = new
SystemObjectContextFactory(componentName, instance);
registerConfiguration(configuration);
@@ -107,179 +80,14 @@
private static final ProxyFactoryFactory proxyFactoryFactory = new
JDKProxyFactoryFactory();
public <T> T resolveInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
- if (RuntimeContext.class.equals(instanceInterface)) {
- return autowireContext.resolveInstance(instanceInterface);
- } else if (ConfigurationContext.class.equals(instanceInterface)) {
- return instanceInterface.cast(this);
- } else if (CompositeContext.class.equals(instanceInterface)) {
- return instanceInterface.cast(this);
- } else if (AutowireContext.class.equals(instanceInterface)) {
+ if (CompositeContext.class.equals(instanceInterface)) {
return instanceInterface.cast(this);
} else if (MessageFactory.class.equals(instanceInterface)) {
return instanceInterface.cast(messageFactory);
} else if (ProxyFactoryFactory.class.equals(instanceInterface)) {
return instanceInterface.cast(proxyFactoryFactory);
- }
-
- NameToScope mapping = autowireIndex.get(instanceInterface);
- if (mapping != null) {
- try {
- return
instanceInterface.cast(mapping.getScopeContext().getInstance(mapping.getName()));
- } catch (TargetException e) {
- AutowireResolutionException ae = new
AutowireResolutionException("Autowire instance not found", e);
- ae.addContextName(getName());
- throw ae;
- }
- }
- if (autowireContext != null) {
- return autowireContext.resolveInstance(instanceInterface);
- } else {
- return null;
- }
- }
-
- public <T> T resolveExternalInstance(Class<T> instanceInterface) throws
AutowireResolutionException {
- NameToScope nts = autowireIndex.get(instanceInterface);
- if (nts != null && nts.isVisible()) {
- return
instanceInterface.cast(nts.getScopeContext().getInstance(nts.getName()));
} else {
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- protected void registerAutowire(Extensible model) throws
ConfigurationException {
- if (lifecycleState == INITIALIZING || lifecycleState == INITIALIZED ||
lifecycleState == RUNNING) {
- if (model instanceof EntryPoint) {
- EntryPoint ep = (EntryPoint) model;
- for (Binding binding : ep.getBindings()) {
- if (binding instanceof SystemBinding) {
- Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
- NameToScope nts = autowireIndex.get(interfaze);
- if (nts == null || !nts.isEntryPoint()) { // handle
special case where two entry points with
- // same interface register: first wins
- ScopeContext scope =
scopeContexts.get(((ContextFactory) ep.getContextFactory()).getScope());
- if (scope == null) {
- ConfigurationException ce = new
MissingScopeException("Scope not found for entry point");
- ce.setIdentifier(ep.getName());
- ce.addContextName(getName());
- throw ce;
- }
- // only register if an impl has not already been
registered
- NameToScope mapping = new NameToScope(new
QualifiedName(ep.getName()), scope, true, true);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- } else if (model instanceof ModuleComponent) {
- ModuleComponent component = (ModuleComponent) model;
- for (EntryPoint ep :
component.getImplementation().getEntryPoints()) {
- for (Binding binding : ep.getBindings()) {
- if (binding instanceof SystemBinding) {
- Class interfaze =
ep.getConfiguredService().getPort().getServiceContract().getInterface();
- if (autowireIndex.get(interfaze) == null) {
- ScopeContext scope =
scopeContexts.get(Scope.AGGREGATE);
- // only register if an impl has not already
been registered, ensuring it is not visible outside the containment
- NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()
- + QualifiedName.NAME_SEPARATOR +
ep.getName()), scope, false, false);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- }
- } else if (model instanceof Component) {
- Component component = (Component) model;
- for (Service service :
component.getImplementation().getComponentInfo().getServices()) {
- Class interfaze =
service.getServiceContract().getInterface();
- if (autowireIndex.get(interfaze) == null) {
- // only register if an impl has not already been
registered
- ScopeContext scopeCtx =
scopeContexts.get(service.getServiceContract().getScope());
- NameToScope mapping = new NameToScope(new
QualifiedName(component.getName()), scopeCtx, false, false);
- autowireIndex.put(interfaze, mapping);
- }
- }
- }
- }
- }
-
- public void configure(Extensible model) throws ConfigurationException {
- if (configurationContext != null) {
- configurationContext.configure(model);
- }
- }
-
- public void build(AssemblyObject model) throws BuilderConfigException {
- if (configurationContext != null) {
- configurationContext.build(model);
+ return super.resolveInstance(instanceInterface);
}
}
-
- public void connect(ProxyFactory sourceFactory, ProxyFactory
targetFactory, Class targetType, boolean downScope,
- ScopeContext targetScopeContext) throws
BuilderConfigException {
- if (configurationContext != null) {
- try {
- configurationContext.connect(sourceFactory, targetFactory,
targetType, downScope, targetScopeContext);
- } catch (BuilderConfigException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- public void completeTargetChain(ProxyFactory targetFactory, Class
targetType, ScopeContext targetScopeContext)
- throws BuilderConfigException {
- if (configurationContext != null) {
- try {
- configurationContext.completeTargetChain(targetFactory,
targetType, targetScopeContext);
- } catch (BuilderConfigException e) {
- e.addContextName(getName());
- throw e;
- }
- }
- }
-
- // ----------------------------------
- // Inner classes
- // ----------------------------------
-
- /**
- * Maps a context name to a scope
- * <p/>
- * TODO this is a duplicate of composite context
- */
- private class NameToScope {
-
- private QualifiedName qName;
-
- private ScopeContext scope;
-
- private boolean visible;
-
- private boolean entryPoint;
-
- public NameToScope(QualifiedName name, ScopeContext scope, boolean
visible, boolean entryPoint) {
- this.qName = name;
- this.scope = scope;
- this.visible = visible;
- this.entryPoint = entryPoint;
- }
-
- public QualifiedName getName() {
- return qName;
- }
-
- public ScopeContext getScopeContext() {
- return scope;
- }
-
- public boolean isVisible() {
- return visible;
- }
-
- public boolean isEntryPoint() {
- return entryPoint;
- }
-
- }
-
}