summaryrefslogtreecommitdiffstats
path: root/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2008-09-09 20:07:50 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2008-09-09 20:07:50 +0000
commit8ff4657da7b5a1da59a21a65b675d26bcf033417 (patch)
tree4f3d2b3e94a35eb6fd22dd8979d55d0eb6dcecb9 /branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly
parentbf5f54e4300a67803d53ac1b47ea2f70c1e2542e (diff)
Copied the android port modules under the modules directory, to match the structure in trunk as it'll help with merges, diffs etc.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@693584 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly')
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ActivationException.java37
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java154
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivatorImpl.java967
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointReferenceImpl.java186
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParameterProcessor.java89
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParametersImpl.java139
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java53
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentImpl.java94
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentReferenceImpl.java122
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentServiceImpl.java153
-rw-r--r--branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java377
11 files changed, 2371 insertions, 0 deletions
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ActivationException.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ActivationException.java
new file mode 100644
index 0000000000..1e68564d1d
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ActivationException.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+
+/**
+ * Denotes an error starting the runtime
+ *
+ * @version $Rev: 568826 $ $Date: 2007-08-22 22:27:34 -0700 (Wed, 22 Aug 2007) $
+ */
+public class ActivationException extends Exception {
+ private static final long serialVersionUID = 8612661660934426123L;
+
+ public ActivationException(String message) {
+ super(message);
+ }
+
+ public ActivationException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java
new file mode 100644
index 0000000000..a96abce839
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivator.java
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.core.context.ComponentContextHelper;
+import org.apache.tuscany.sca.core.conversation.ConversationManager;
+import org.apache.tuscany.sca.core.invocation.ProxyFactory;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+
+/**
+ * Start/stop a composite
+ *
+ * @version $Rev: 575101 $ $Date: 2007-09-12 15:09:19 -0700 (Wed, 12 Sep 2007) $
+ */
+public interface CompositeActivator {
+ /**
+ * Activate a composite
+ * @param composite
+ */
+ void activate(Composite composite) throws ActivationException;
+
+ /**
+ * Activate a component reference
+ * @param component
+ * @param ref
+ */
+ void start(RuntimeComponent component, RuntimeComponentReference ref);
+
+ /**
+ * Activate a component reference
+ * @param component
+ * @param ref
+ */
+ void activate(RuntimeComponent component, RuntimeComponentReference ref);
+
+ /**
+ * Activate a component reference
+ * @param component
+ * @param ref
+ */
+ void activate(RuntimeComponent component, RuntimeComponentService service);
+
+ /**
+ * De-activate a component reference
+ * @param component
+ * @param ref
+ */
+ void deactivate(RuntimeComponent component, RuntimeComponentReference ref);
+
+ /**
+ * De-activate a component reference
+ * @param component
+ * @param ref
+ */
+ void deactivate(RuntimeComponent component, RuntimeComponentService service);
+
+ /**
+ * Stop a composite
+ * @param composite
+ */
+ void deactivate(Composite composite) throws ActivationException;
+
+ /**
+ * Start a component
+ * @param component
+ */
+ void start(Component component) throws ActivationException;
+
+ /**
+ * Stop a component
+ * @param component
+ */
+ void stop(Component component) throws ActivationException;
+
+ /**
+ * Start components in a composite
+ * @param composite
+ */
+ void start(Composite composite) throws ActivationException;
+
+ /**
+ * Stop components in a composite
+ * @param composite
+ */
+ void stop(Composite composite) throws ActivationException;
+
+ /**
+ * Get the component context helper
+ * @return
+ */
+ ComponentContextHelper getComponentContextHelper();
+
+ /**
+ * Get the proxy factory
+ * @return
+ */
+ ProxyFactory getProxyFactory();
+
+ /**
+ * Get the java interface factory
+ * @return
+ */
+ JavaInterfaceFactory getJavaInterfaceFactory();
+
+ ConversationManager getConversationManager();
+
+ /**
+ * Configure the runtime component with component context
+ * @param component
+ */
+ void configureComponentContext(RuntimeComponent component);
+
+ /**
+ * Resolve a component by URI in the domain
+ * @param componentURI
+ * @return
+ */
+ Component resolve(String componentURI);
+
+ /**
+ * Set the domain composite
+ * @param domainComposite
+ */
+ void setDomainComposite(Composite domainComposite);
+
+ /**
+ * Get the domain composite
+ * @return
+ */
+ Composite getDomainComposite();
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivatorImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivatorImpl.java
new file mode 100644
index 0000000000..f730b2ff71
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/CompositeActivatorImpl.java
@@ -0,0 +1,967 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.Implementation;
+import org.apache.tuscany.sca.assembly.OptimizableBinding;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.SCABindingFactory;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.context.RequestContextFactory;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.core.context.ComponentContextHelper;
+import org.apache.tuscany.sca.core.context.ComponentContextImpl;
+import org.apache.tuscany.sca.core.conversation.ConversationManager;
+import org.apache.tuscany.sca.core.invocation.ProxyFactory;
+import org.apache.tuscany.sca.core.scope.ConversationalScopeContainer;
+import org.apache.tuscany.sca.core.scope.Scope;
+import org.apache.tuscany.sca.core.scope.ScopeContainer;
+import org.apache.tuscany.sca.core.scope.ScopeRegistry;
+import org.apache.tuscany.sca.core.scope.ScopedRuntimeComponent;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.invocation.MessageFactory;
+import org.apache.tuscany.sca.provider.BindingProviderFactory;
+import org.apache.tuscany.sca.provider.ImplementationProvider;
+import org.apache.tuscany.sca.provider.ImplementationProviderFactory;
+import org.apache.tuscany.sca.provider.PolicyProvider;
+import org.apache.tuscany.sca.provider.PolicyProviderFactory;
+import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.EndpointReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentContext;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.apache.tuscany.sca.runtime.RuntimeWireProcessor;
+import org.apache.tuscany.sca.work.WorkScheduler;
+
+/**
+ * @version $Rev: 639277 $ $Date: 2008-03-20 06:02:37 -0700 (Thu, 20 Mar 2008) $
+ */
+public class CompositeActivatorImpl implements CompositeActivator {
+ private final static Logger logger = Logger.getLogger(CompositeActivatorImpl.class.getName());
+
+ private final AssemblyFactory assemblyFactory;
+ private final MessageFactory messageFactory;
+ private final InterfaceContractMapper interfaceContractMapper;
+ private final ScopeRegistry scopeRegistry;
+ private final WorkScheduler workScheduler;
+ private final RuntimeWireProcessor wireProcessor;
+ private final ProviderFactoryExtensionPoint providerFactories;
+
+ private final RequestContextFactory requestContextFactory;
+ private final ProxyFactory proxyFactory;
+ private final JavaInterfaceFactory javaInterfaceFactory;
+ private final ConversationManager conversationManager;
+
+ private final ComponentContextHelper componentContextHelper;
+
+ private Composite domainComposite;
+
+ /**
+ * @param assemblyFactory
+ * @param interfaceContractMapper
+ * @param workScheduler
+ * @param conversationManager TODO
+ * @param workContext
+ * @param wirePostProcessorRegistry
+ */
+ public CompositeActivatorImpl(AssemblyFactory assemblyFactory,
+ MessageFactory messageFactory,
+ JavaInterfaceFactory javaInterfaceFactory,
+ SCABindingFactory scaBindingFactory,
+ InterfaceContractMapper interfaceContractMapper,
+ ScopeRegistry scopeRegistry,
+ WorkScheduler workScheduler,
+ RuntimeWireProcessor wireProcessor,
+ RequestContextFactory requestContextFactory,
+ ProxyFactory proxyFactory,
+ ProviderFactoryExtensionPoint providerFactories,
+ StAXArtifactProcessorExtensionPoint processors,
+ ConversationManager conversationManager) {
+ this.assemblyFactory = assemblyFactory;
+ this.messageFactory = messageFactory;
+ this.interfaceContractMapper = interfaceContractMapper;
+ this.scopeRegistry = scopeRegistry;
+ this.workScheduler = workScheduler;
+ this.wireProcessor = wireProcessor;
+ this.providerFactories = providerFactories;
+ this.javaInterfaceFactory = javaInterfaceFactory;
+ this.requestContextFactory = requestContextFactory;
+ this.proxyFactory = proxyFactory;
+ this.conversationManager = conversationManager;
+ this.componentContextHelper = new ComponentContextHelper(assemblyFactory, javaInterfaceFactory, processors);
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.core.assembly.CompositeActivator#activate(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentReference)
+ */
+ public void activate(RuntimeComponent component, RuntimeComponentReference ref) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Activating component reference: " + component.getURI() + "#" + ref.getName());
+ }
+ resolveTargets(ref);
+ for (Binding binding : ref.getBindings()) {
+ addReferenceBindingProvider(component, ref, binding);
+ }
+ }
+
+ public void start(RuntimeComponent component, RuntimeComponentReference ref) {
+ synchronized (ref) {
+ resolveTargets(ref);
+ for (Binding binding : ref.getBindings()) {
+ ReferenceBindingProvider provider = ref.getBindingProvider(binding);
+ if (provider == null) {
+ provider = addReferenceBindingProvider(component, ref, binding);
+ }
+ if (provider != null) {
+ provider.start();
+ }
+ addReferenceWire(component, ref, binding);
+ }
+ }
+ }
+
+ public void stop(Component component, ComponentReference reference)
+ {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting component reference: " + component.getURI() + "#" + reference.getName());
+ }
+ RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference);
+ for (Binding binding : reference.getBindings()) {
+ ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(binding);
+ if (bindingProvider != null) {
+ bindingProvider.stop();
+ }
+ }
+ }
+
+ public void deactivate(RuntimeComponent component, RuntimeComponentReference ref) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Deactivating component reference: " + component.getURI() + "#" + ref.getName());
+ }
+ removeReferenceWires(ref);
+ for (Binding binding : ref.getBindings()) {
+ removeReferenceBindingProvider(component, ref, binding);
+ }
+
+ }
+
+ /**
+ * @param component
+ * @param reference
+ * @param binding
+ */
+ private ReferenceBindingProvider addReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ Binding binding) {
+ BindingProviderFactory providerFactory =
+ (BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
+ if (providerFactory != null) {
+ @SuppressWarnings("unchecked")
+ ReferenceBindingProvider bindingProvider =
+ providerFactory.createReferenceBindingProvider((RuntimeComponent)component,
+ (RuntimeComponentReference)reference,
+ binding);
+ if (bindingProvider != null) {
+ ((RuntimeComponentReference)reference).setBindingProvider(binding, bindingProvider);
+ }
+ for (PolicyProviderFactory f : providerFactories.getPolicyProviderFactories()) {
+ PolicyProvider policyProvider = f.createReferencePolicyProvider(component, reference, binding);
+ if (policyProvider != null) {
+ reference.addPolicyProvider(binding, policyProvider);
+ }
+ }
+
+ return bindingProvider;
+ } else {
+ throw new IllegalStateException("Provider factory not found for class: " + binding.getClass().getName());
+ }
+ }
+
+ /**
+ * @param reference
+ */
+ private void resolveTargets(RuntimeComponentReference reference) {
+
+/* Not used now that domain wires are created using the domain wire builder
+ // go over any targets that have not been resolved yet (as they are running on other nodes)
+ // and try an resolve them remotely
+ // TODO - this should work for any kind of wired binding but the only wireable binding
+ // is currently the SCA binding so we assume that
+ for (ComponentService service : reference.getTargets()) {
+ if (service.isUnresolved()) {
+ for (Binding binding : service.getBindings()) {
+
+
+ //binding.setURI(service.getName());
+
+ // TODO - we should look at all the bindings now associated with the
+ // unresolved target but we assume the SCA binding here as
+ // its currently the only wireable one
+ if (binding instanceof SCABinding) {
+ OptimizableBinding scaBinding = (OptimizableBinding)binding;
+
+ // clone the SCA binding and fill in service details
+ // its cloned as each target
+ SCABinding clonedSCABinding = null;
+ try {
+ clonedSCABinding = (SCABinding)((OptimizableBinding)scaBinding).clone();
+ // TODO - Reusing the name here to store the target name so the
+ // binding can be found again once the URI is set to the real endpoint
+ clonedSCABinding.setName(service.getName());
+ clonedSCABinding.setURI(service.getName());
+ // wireable binding stuff needs to go. SCA binding uses it
+ // currently to get to the service to work out if the service
+ // is resolved.
+ OptimizableBinding endpoint = ((OptimizableBinding)clonedSCABinding);
+ endpoint.setTargetComponentService(service);
+ //endpoint.setTargetComponent(component); - not known for unresolved target
+ //endpoint.setTargetBinding(serviceBinding); - not known for unresolved target
+
+ // add the cloned SCA binding to the reference as it will be used to look up the
+ // provider later
+ reference.getBindings().remove(binding);
+ reference.getBindings().add(clonedSCABinding);
+ } catch (Exception e) {
+ // warning("The binding doesn't support clone: " + binding.getClass().getSimpleName(), binding);
+ }
+ } else {
+
+ * Just leave the binding as it. It will be filled in later
+ * when the node resolves the targets
+ throw new IllegalStateException(
+ "Unable to create a distributed SCA binding provider for reference: " + reference
+ .getName()
+ + " and target: "
+ + service.getName());
+
+ }
+ }
+ }
+ }
+*/
+ }
+
+ /**
+ * Create the runtime wires for a reference binding
+ *
+ * @param component
+ * @param reference
+ * @param binding
+ */
+ private void addReferenceWire(Component component, ComponentReference reference, Binding binding) {
+ if (!(reference instanceof RuntimeComponentReference)) {
+ return;
+ }
+
+ // create wire if binding has an endpoint
+ Component targetComponent = null;
+ ComponentService targetComponentService = null;
+ Binding targetBinding = null;
+ if (binding instanceof OptimizableBinding) {
+ OptimizableBinding endpoint = (OptimizableBinding)binding;
+ targetComponent = endpoint.getTargetComponent();
+ targetComponentService = endpoint.getTargetComponentService();
+ targetBinding = endpoint.getTargetBinding();
+ }
+
+ // create a forward wire, either static or dynamic
+ addReferenceWire(component, reference, binding, targetComponent, targetComponentService, targetBinding);
+
+ /*
+ // if static forward wire (not from self-reference), try to create a static callback wire
+ if (targetComponentService != null && !reference.getName().startsWith("$self$.")) {
+ ComponentReference callbackReference = targetComponentService.getCallbackReference();
+ if (callbackReference != null) {
+ Binding callbackBinding = null;
+ Binding callbackServiceBinding = null;
+ // select a service callback binding that can be wired back to this component
+ for (Binding refBinding : callbackReference.getBindings()) {
+ // first look for a callback binding whose name matches the target binding name
+ if (refBinding.getName().equals(targetBinding.getName())) {
+ callbackBinding = refBinding;
+ break;
+ }
+ }
+ // see if there is a matching reference callback binding
+ if (callbackBinding != null) {
+ callbackServiceBinding = reference.getCallbackService().getBinding(callbackBinding.getClass());
+ }
+ // if there isn't an end-to-end match, try again based on target binding type
+ if (callbackBinding == null || callbackServiceBinding == null) {
+ callbackBinding = callbackReference.getBinding(targetBinding.getClass());
+ if (callbackBinding != null) {
+ callbackServiceBinding = reference.getCallbackService().getBinding(callbackBinding.getClass());
+ }
+ }
+ if (callbackBinding != null && callbackServiceBinding != null) {
+ // end-to-end match, so create a static callback wire as well as the static forward wire
+
+ addReferenceWire(targetComponent, callbackReference, callbackBinding, component, reference
+ .getCallbackService(), callbackServiceBinding);
+ } else {
+ // no end-to-end match, so do not create a static callback wire
+ }
+ }
+ }
+ */
+ }
+
+ /**
+ * Create a reference wire for a forward call or a callback
+ * @param reference
+ * @param service
+ * @param serviceBinding
+ * @param component
+ * @param referenceBinding
+ */
+ private RuntimeWire addReferenceWire(Component refComponent,
+ ComponentReference reference,
+ Binding refBinding,
+ Component serviceComponent,
+ ComponentService service,
+ Binding serviceBinding) {
+ RuntimeComponentReference runtimeRef = (RuntimeComponentReference)reference;
+ InterfaceContract bindingContract = getInterfaceContract(reference, refBinding);
+
+ // Use the interface contract of the reference on the component type
+ Reference componentTypeRef = reference.getReference();
+ InterfaceContract sourceContract =
+ componentTypeRef == null ? reference.getInterfaceContract() : componentTypeRef.getInterfaceContract();
+ sourceContract = sourceContract.makeUnidirectional(false);
+
+ EndpointReference wireSource =
+ new EndpointReferenceImpl((RuntimeComponent)refComponent, reference, refBinding, sourceContract);
+ ComponentService callbackService = reference.getCallbackService();
+ if (callbackService != null) {
+ // select a reference callback binding to pass with invocations on this wire
+ Binding callbackBinding = null;
+ for (Binding binding : callbackService.getBindings()) {
+ // first look for a callback binding whose name matches the reference binding name
+ if (refBinding.getName().startsWith(binding.getName())) {
+ callbackBinding = binding;
+ break;
+ }
+ }
+ // if no callback binding found, try again based on reference binding type
+ if (callbackBinding == null) {
+ callbackBinding = callbackService.getBinding(refBinding.getClass());
+ }
+ InterfaceContract callbackContract = callbackService.getInterfaceContract();
+ EndpointReference callbackEndpoint =
+ new EndpointReferenceImpl((RuntimeComponent)refComponent, callbackService, callbackBinding,
+ callbackContract);
+ wireSource.setCallbackEndpoint(callbackEndpoint);
+ }
+
+ EndpointReference wireTarget =
+ new EndpointReferenceImpl((RuntimeComponent)serviceComponent, service, serviceBinding, bindingContract);
+
+ RuntimeWire wire =
+ new RuntimeWireImpl(wireSource, wireTarget, interfaceContractMapper, workScheduler, wireProcessor,
+ messageFactory, conversationManager);
+ runtimeRef.getRuntimeWires().add(wire);
+
+ return wire;
+ }
+
+ private void addImplementationProvider(RuntimeComponent component, Implementation implementation) {
+ ImplementationProviderFactory providerFactory =
+ (ImplementationProviderFactory)providerFactories.getProviderFactory(implementation.getClass());
+ if (providerFactory != null) {
+ @SuppressWarnings("unchecked")
+ ImplementationProvider implementationProvider =
+ providerFactory.createImplementationProvider(component, implementation);
+ if (implementationProvider != null) {
+ component.setImplementationProvider(implementationProvider);
+ }
+ } else {
+ throw new IllegalStateException("Provider factory not found for class: " + implementation.getClass()
+ .getName());
+ }
+ for (PolicyProviderFactory f : providerFactories.getPolicyProviderFactories()) {
+ PolicyProvider policyProvider = f.createImplementationPolicyProvider(component, implementation);
+ if (policyProvider != null) {
+ component.addPolicyProvider(policyProvider);
+ }
+ }
+
+ }
+
+ private void removeImplementationProvider(RuntimeComponent component) {
+ component.setImplementationProvider(null);
+ component.getPolicyProviders().clear();
+ }
+
+ /**
+ * @param component
+ * @param service
+ * @param binding
+ */
+ private ServiceBindingProvider addServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ Binding binding) {
+ BindingProviderFactory providerFactory =
+ (BindingProviderFactory)providerFactories.getProviderFactory(binding.getClass());
+ if (providerFactory != null) {
+ @SuppressWarnings("unchecked")
+ ServiceBindingProvider bindingProvider =
+ providerFactory.createServiceBindingProvider((RuntimeComponent)component,
+ (RuntimeComponentService)service,
+ binding);
+ if (bindingProvider != null) {
+ ((RuntimeComponentService)service).setBindingProvider(binding, bindingProvider);
+ }
+ for (PolicyProviderFactory f : providerFactories.getPolicyProviderFactories()) {
+ PolicyProvider policyProvider = f.createServicePolicyProvider(component, service, binding);
+ if (policyProvider != null) {
+ service.addPolicyProvider(binding, policyProvider);
+ }
+ }
+ return bindingProvider;
+ } else {
+ throw new IllegalStateException("Provider factory not found for class: " + binding.getClass().getName());
+ }
+ }
+
+ private void removeServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ Binding binding) {
+ service.setBindingProvider(binding, null);
+ for (Binding b : service.getBindings()) {
+ List<PolicyProvider> pps = service.getPolicyProviders(b);
+ if (pps != null) {
+ pps.clear();
+ }
+ }
+ }
+
+ private void removeReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ Binding binding) {
+ reference.setBindingProvider(binding, null);
+ for (Binding b : reference.getBindings()) {
+ List<PolicyProvider> pps = reference.getPolicyProviders(b);
+ if (pps != null) {
+ pps.clear();
+ }
+ }
+ }
+
+ public void start(Composite composite) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting composite: " + composite.getName());
+ }
+ for (Component component : composite.getComponents()) {
+ start(component);
+ }
+ }
+
+ public void stop(Composite composite) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Stopping composite: " + composite.getName());
+ }
+ for (Component component : composite.getComponents()) {
+ stop(component);
+ }
+ }
+
+ public void start(Component component) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting component: " + component.getURI());
+ }
+ RuntimeComponent runtimeComponent = ((RuntimeComponent)component);
+ if(runtimeComponent.isStarted()) {
+ return;
+ }
+
+ configureComponentContext(runtimeComponent);
+
+ for (ComponentReference reference : component.getReferences()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting component reference: " + component.getURI() + "#" + reference.getName());
+ }
+ RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference);
+ runtimeRef.setComponent(runtimeComponent);
+ for (Binding binding : reference.getBindings()) {
+ ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(binding);
+ if (bindingProvider != null) {
+ bindingProvider.start();
+ }
+ }
+ }
+
+ for (ComponentService service : component.getServices()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting component service: " + component.getURI() + "#" + service.getName());
+ }
+ RuntimeComponentService runtimeService = (RuntimeComponentService)service;
+ for (Binding binding : service.getBindings()) {
+ ServiceBindingProvider bindingProvider = runtimeService.getBindingProvider(binding);
+ if (bindingProvider != null) {
+ bindingProvider.start();
+ }
+ }
+ }
+
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ start((Composite)implementation);
+ } else {
+ ImplementationProvider implementationProvider = runtimeComponent.getImplementationProvider();
+ if (implementationProvider != null) {
+ implementationProvider.start();
+ }
+ }
+
+ if (component instanceof ScopedRuntimeComponent) {
+ ScopedRuntimeComponent scopedRuntimeComponent = (ScopedRuntimeComponent)component;
+ if (scopedRuntimeComponent.getScopeContainer() != null) {
+ scopedRuntimeComponent.getScopeContainer().start();
+ }
+ }
+
+ runtimeComponent.setStarted(true);
+ }
+
+ /**
+ * @param runtimeComponent
+ */
+ public void configureComponentContext(RuntimeComponent runtimeComponent) {
+ RuntimeComponentContext componentContext =
+ new ComponentContextImpl(this, assemblyFactory, proxyFactory, interfaceContractMapper,
+ requestContextFactory, javaInterfaceFactory, runtimeComponent);
+ runtimeComponent.setComponentContext(componentContext);
+ }
+
+ /**
+ * Stop a component
+ */
+ public void stop(Component component) {
+ if (!((RuntimeComponent)component).isStarted()) {
+ return;
+ }
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Stopping component: " + component.getURI());
+ }
+ for (ComponentService service : component.getServices()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Stopping component service: " + component.getURI() + "#" + service.getName());
+ }
+ for (Binding binding : service.getBindings()) {
+ ServiceBindingProvider bindingProvider = ((RuntimeComponentService)service).getBindingProvider(binding);
+ if (bindingProvider != null) {
+ bindingProvider.stop();
+ }
+ }
+ }
+ for (ComponentReference reference : component.getReferences()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Starting component reference: " + component.getURI() + "#" + reference.getName());
+ }
+ RuntimeComponentReference runtimeRef = ((RuntimeComponentReference)reference);
+ for (Binding binding : reference.getBindings()) {
+ ReferenceBindingProvider bindingProvider = runtimeRef.getBindingProvider(binding);
+ if (bindingProvider != null) {
+ bindingProvider.stop();
+ }
+ }
+ }
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ stop((Composite)implementation);
+ } else {
+ ImplementationProvider implementationProvider = ((RuntimeComponent)component).getImplementationProvider();
+ if (implementationProvider != null) {
+ implementationProvider.stop();
+ }
+ }
+
+ if (component instanceof ScopedRuntimeComponent) {
+ ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component;
+ if (runtimeComponent.getScopeContainer() != null &&
+ runtimeComponent.getScopeContainer().getLifecycleState() != ScopeContainer.STOPPED) {
+ runtimeComponent.getScopeContainer().stop();
+ }
+ }
+
+ ((RuntimeComponent)component).setStarted(false);
+ }
+
+ /**
+ * Get the effective interface contract for a reference binding
+ *
+ * @param reference
+ * @param binding
+ * @return
+ */
+ private InterfaceContract getInterfaceContract(ComponentReference reference, Binding binding) {
+ InterfaceContract interfaceContract = reference.getInterfaceContract();
+ ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding);
+ if (provider != null) {
+ InterfaceContract bindingContract = provider.getBindingInterfaceContract();
+ if (bindingContract != null) {
+ interfaceContract = bindingContract;
+ }
+ }
+ return interfaceContract.makeUnidirectional(false);
+ }
+
+ /**
+ * Remove the runtime wires for a reference binding
+ * @param reference
+ */
+ private void removeReferenceWires(ComponentReference reference) {
+ if (!(reference instanceof RuntimeComponentReference)) {
+ return;
+ }
+ // [rfeng] Comment out the following statements to avoid the on-demand activation
+ // RuntimeComponentReference runtimeRef = (RuntimeComponentReference)reference;
+ // runtimeRef.getRuntimeWires().clear();
+ }
+
+ /**
+ * Get the effective interface contract for the service binding
+ *
+ * @param service
+ * @param binding
+ * @return
+ */
+ private InterfaceContract getInterfaceContract(ComponentService service, Binding binding) {
+ InterfaceContract interfaceContract = service.getInterfaceContract();
+
+ ServiceBindingProvider provider = ((RuntimeComponentService)service).getBindingProvider(binding);
+ if (provider != null) {
+ InterfaceContract bindingContract = provider.getBindingInterfaceContract();
+ if (bindingContract != null) {
+ interfaceContract = bindingContract;
+ }
+ }
+ return interfaceContract.makeUnidirectional(false);
+ }
+
+ /**
+ * Remove runtime wires for a service binding
+ *
+ * @param component
+ * @param service
+ */
+ private void removeServiceWires(ComponentService service) {
+ if (!(service instanceof RuntimeComponentService)) {
+ return;
+ }
+ RuntimeComponentService runtimeService = (RuntimeComponentService)service;
+ runtimeService.getRuntimeWires().clear();
+ }
+
+ /**
+ * Create a service wire for a forward call or a callback
+ * @param service
+ * @param serviceBinding
+ * @param reference
+ * @param component
+ * @param referenceBinding
+ */
+ private RuntimeWire addServiceWire(Component serviceComponent, ComponentService service, Binding serviceBinding) {
+ if (!(service instanceof RuntimeComponentService)) {
+ return null;
+ }
+ RuntimeComponentService runtimeService = (RuntimeComponentService)service;
+
+ // FIXME: [rfeng] We might need a better way to get the impl interface contract
+ Service targetService = service.getService();
+ if (targetService == null) {
+ targetService = service;
+ }
+ InterfaceContract targetContract = targetService.getInterfaceContract().makeUnidirectional(false);
+
+ InterfaceContract sourceContract = getInterfaceContract(service, serviceBinding);
+
+ EndpointReference wireSource = new EndpointReferenceImpl(null, null, serviceBinding, sourceContract);
+
+ EndpointReference wireTarget =
+ new EndpointReferenceImpl((RuntimeComponent)serviceComponent, (RuntimeComponentService)service,
+ serviceBinding, targetContract);
+
+ RuntimeWire wire =
+ new RuntimeWireImpl(wireSource, wireTarget, interfaceContractMapper, workScheduler, wireProcessor,
+ messageFactory, conversationManager);
+ runtimeService.getRuntimeWires().add(wire);
+
+ return wire;
+ }
+
+ public void activate(RuntimeComponent component, RuntimeComponentService service) {
+ if (service.getService() == null) {
+ if (logger.isLoggable(Level.WARNING)) {
+ logger.warning("Skipping component service not defined in the component type: " + component.getURI()
+ + "#"
+ + service.getName());
+ }
+ return;
+ }
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Activating component service: " + component.getURI() + "#" + service.getName());
+ }
+
+ for (Binding binding : service.getBindings()) {
+ addServiceBindingProvider(component, service, binding);
+ addServiceWire(component, service, binding);
+ }
+ }
+
+ public void deactivate(RuntimeComponent component, RuntimeComponentService service) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Deactivating component service: " + component.getURI() + "#" + service.getName());
+ }
+ removeServiceWires(service);
+ for (Binding binding : service.getBindings()) {
+ removeServiceBindingProvider(component, service, binding);
+ }
+ }
+
+ private void addScopeContainer(Component component) {
+ if (!(component instanceof ScopedRuntimeComponent)) {
+ return;
+ }
+ ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component;
+ ScopeContainer scopeContainer = scopeRegistry.getScopeContainer(runtimeComponent);
+ if (scopeContainer != null && scopeContainer.getScope() == Scope.CONVERSATION) {
+ conversationManager.addListener((ConversationalScopeContainer)scopeContainer);
+ }
+ runtimeComponent.setScopeContainer(scopeContainer);
+ }
+
+ private void removeScopeContainer(Component component) {
+ if (!(component instanceof ScopedRuntimeComponent)) {
+ return;
+ }
+ ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component;
+ ScopeContainer scopeContainer = runtimeComponent.getScopeContainer();
+ if(scopeContainer != null && scopeContainer.getScope() == Scope.CONVERSATION) {
+ conversationManager.removeListener((ConversationalScopeContainer) scopeContainer);
+ }
+ runtimeComponent.setScopeContainer(null);
+ }
+
+ public void activateComponent(Component component)
+ throws ActivationException {
+ try {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Activating component: " + component.getURI());
+ }
+
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ activate((Composite) implementation);
+ } else if (implementation != null) {
+ addImplementationProvider((RuntimeComponent) component,
+ implementation);
+ addScopeContainer(component);
+ }
+
+ for (ComponentService service : component.getServices()) {
+ activate((RuntimeComponent) component,
+ (RuntimeComponentService) service);
+ }
+
+ for (ComponentReference reference : component.getReferences()) {
+ activate((RuntimeComponent) component,
+ (RuntimeComponentReference) reference);
+ }
+ } catch (Exception e) {
+ throw new ActivationException(e);
+ }
+ }
+
+ public void deactivateComponent(Component component)
+ throws ActivationException {
+ try {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Deactivating component: " + component.getURI());
+ }
+ for (ComponentService service : component.getServices()) {
+ deactivate((RuntimeComponent) component,
+ (RuntimeComponentService) service);
+ }
+
+ for (ComponentReference reference : component.getReferences()) {
+ deactivate((RuntimeComponent) component,
+ (RuntimeComponentReference) reference);
+ }
+
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ deactivate((Composite) implementation);
+ } else if (implementation != null) {
+ removeImplementationProvider((RuntimeComponent) component);
+ removeScopeContainer(component);
+ }
+ } catch (Exception e) {
+ throw new ActivationException(e);
+ }
+ }
+
+ public void activate(Composite composite) throws ActivationException {
+ try {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Activating composite: " + composite.getName());
+ }
+ for (Component component : composite.getComponents()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Activating component: " + component.getURI());
+ }
+
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ activate((Composite)implementation);
+ } else if (implementation != null) {
+ addImplementationProvider((RuntimeComponent)component, implementation);
+ addScopeContainer(component);
+ }
+
+ for (ComponentService service : component.getServices()) {
+ activate((RuntimeComponent)component, (RuntimeComponentService)service);
+ }
+
+ for (ComponentReference reference : component.getReferences()) {
+ activate((RuntimeComponent)component, (RuntimeComponentReference)reference);
+ }
+ }
+ } catch (Exception e) {
+ throw new ActivationException(e);
+ }
+ }
+
+ public void deactivate(Composite composite) throws ActivationException {
+ try {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Deactivating composite: " + composite.getName());
+ }
+ for (Component component : composite.getComponents()) {
+ if (logger.isLoggable(Level.FINE)) {
+ logger.fine("Deactivating component: " + component.getURI());
+ }
+ for (ComponentService service : component.getServices()) {
+ deactivate((RuntimeComponent)component, (RuntimeComponentService)service);
+ }
+
+ for (ComponentReference reference : component.getReferences()) {
+ deactivate((RuntimeComponent)component, (RuntimeComponentReference)reference);
+ }
+
+ Implementation implementation = component.getImplementation();
+ if (implementation instanceof Composite) {
+ deactivate((Composite)implementation);
+ } else if (implementation != null) {
+ removeImplementationProvider((RuntimeComponent)component);
+ removeScopeContainer(component);
+ }
+ }
+ } catch (Exception e) {
+ throw new ActivationException(e);
+ }
+ }
+
+ /**
+ * @return the referenceHelper
+ */
+ public ComponentContextHelper getComponentContextHelper() {
+ return componentContextHelper;
+ }
+
+ /**
+ * @return the proxyFactory
+ */
+ public ProxyFactory getProxyFactory() {
+ return proxyFactory;
+ }
+
+ /**
+ * @return the domainComposite
+ */
+ public Composite getDomainComposite() {
+ return domainComposite;
+ }
+
+ /**
+ * @param domainComposite the domainComposite to set
+ */
+ public void setDomainComposite(Composite domainComposite) {
+ this.domainComposite = domainComposite;
+ }
+
+ public Component resolve(String componentURI) {
+ for (Composite composite : domainComposite.getIncludes()) {
+ Component component = resolve(composite, componentURI);
+ if (component != null) {
+ return component;
+ }
+ }
+ return null;
+ }
+
+ public Component resolve(Composite composite, String componentURI) {
+ for (Component component : composite.getComponents()) {
+ String uri = component.getURI();
+ if (uri.equals(componentURI)) {
+ return component;
+ }
+ if (componentURI.startsWith(uri)) {
+ Implementation implementation = component.getImplementation();
+ if (!(implementation instanceof Composite)) {
+ return null;
+ }
+ return resolve((Composite)implementation, componentURI);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @return the javaInterfaceFactory
+ */
+ public JavaInterfaceFactory getJavaInterfaceFactory() {
+ return javaInterfaceFactory;
+ }
+
+ /**
+ * @return the conversationManager
+ */
+ public ConversationManager getConversationManager() {
+ return conversationManager;
+ }
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointReferenceImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointReferenceImpl.java
new file mode 100644
index 0000000000..2d7cfefd16
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/EndpointReferenceImpl.java
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Contract;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.runtime.EndpointReference;
+import org.apache.tuscany.sca.runtime.ReferenceParameters;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+
+/**
+ * @version $Rev: 637621 $ $Date: 2008-03-16 10:59:45 -0700 (Sun, 16 Mar 2008) $
+ */
+public class EndpointReferenceImpl implements EndpointReference {
+ private RuntimeComponent component;
+ private Contract contract;
+ private Binding binding;
+ private InterfaceContract interfaceContract;
+ private String uri;
+ private EndpointReference callbackEndpoint;
+ private ReferenceParameters parameters = new ReferenceParametersImpl();
+
+ /**
+ * @param component
+ * @param contract
+ * @param binding
+ * @param interfaceContract
+ */
+ public EndpointReferenceImpl(RuntimeComponent component,
+ Contract contract,
+ Binding binding,
+ InterfaceContract interfaceContract) {
+ super();
+ this.component = component;
+ this.contract = contract;
+ this.binding = binding;
+ this.interfaceContract = interfaceContract;
+ this.uri = (component != null ? component.getURI() : "") + '/' +
+ (contract != null ? contract.getName() : "");
+ }
+
+ /**
+ * @param uri
+ */
+ public EndpointReferenceImpl(String uri) {
+ super();
+ this.uri = uri;
+ }
+
+ public Binding getBinding() {
+ return binding;
+ }
+
+ public void setBinding(Binding binding) {
+ this.binding = binding;
+ }
+
+ public RuntimeComponent getComponent() {
+ return component;
+ }
+
+ public void setComponent(RuntimeComponent component) {
+ this.component = component;
+ }
+
+ public Contract getContract() {
+ return contract;
+ }
+
+ public void setContract(Contract contract) {
+ this.contract = contract;
+ }
+
+ public InterfaceContract getInterfaceContract() {
+ return interfaceContract;
+ }
+
+ public void setInterfaceContract(InterfaceContract interfaceContract) {
+ this.interfaceContract = interfaceContract;
+ }
+
+ public String getURI() {
+ return uri;
+ }
+
+ public void setURI(String uri) {
+ this.uri = uri;
+ }
+
+ public EndpointReference getCallbackEndpoint() {
+ return callbackEndpoint;
+ }
+
+ public void setCallbackEndpoint(EndpointReference callbackEndpoint) {
+ this.callbackEndpoint = callbackEndpoint;
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ((uri == null) ? 0 : uri.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final EndpointReferenceImpl other = (EndpointReferenceImpl)obj;
+ if (uri == null) {
+ if (other.uri != null) {
+ return false;
+ }
+ } else if (!uri.equals(other.uri)) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ EndpointReferenceImpl copy = (EndpointReferenceImpl)super.clone();
+ /* [nash] no need to copy callback endpoint
+ if (callbackEndpoint != null) {
+ copy.callbackEndpoint = (EndpointReference)callbackEndpoint.clone();
+ }
+ */
+ if (parameters != null) {
+ copy.parameters = (ReferenceParameters)parameters.clone();
+ }
+ return copy;
+ }
+
+ /**
+ * @return the parameters
+ */
+ public ReferenceParameters getReferenceParameters() {
+ return parameters;
+ }
+
+ /**
+ * @param parameters the parameters to set
+ */
+ public void setReferenceParameters(ReferenceParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ public void mergeEndpoint(EndpointReference epr) {
+ this.component = epr.getComponent();
+ this.contract = epr.getContract();
+ this.binding = epr.getBinding();
+ this.interfaceContract = epr.getInterfaceContract();
+ this.uri = epr.getURI();
+ this.callbackEndpoint = epr.getCallbackEndpoint();
+ }
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParameterProcessor.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParameterProcessor.java
new file mode 100644
index 0000000000..7087df2a42
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParameterProcessor.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.service.ContributionReadException;
+import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
+import org.apache.tuscany.sca.runtime.ReferenceParameters;
+
+/**
+ * @version $Rev: 574648 $ $Date: 2007-09-11 10:45:36 -0700 (Tue, 11 Sep 2007) $
+ */
+public class ReferenceParameterProcessor implements StAXArtifactProcessor<ReferenceParameters> {
+ private final static QName REFERENCE_PARAMETERS =
+ new QName("http://tuscany.apache.org/xmlns/sca/1.0", "referenceParameters", "tuscany");
+
+ /**
+ * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#getArtifactType()
+ */
+ public QName getArtifactType() {
+ return REFERENCE_PARAMETERS;
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#read(javax.xml.stream.XMLStreamReader)
+ */
+ public ReferenceParameters read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+ ReferenceParameters parameters = new ReferenceParametersImpl();
+ parameters.setConversationID(reader.getAttributeValue(null, "conversationID"));
+ parameters.setCallbackID(reader.getAttributeValue(null, "callbackID"));
+ return parameters;
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#write(java.lang.Object, javax.xml.stream.XMLStreamWriter)
+ */
+ public void write(ReferenceParameters model, XMLStreamWriter writer) throws ContributionWriteException,
+ XMLStreamException {
+ writer.writeStartElement(REFERENCE_PARAMETERS.getPrefix(),
+ REFERENCE_PARAMETERS.getLocalPart(),
+ REFERENCE_PARAMETERS.getNamespaceURI());
+ writer.writeNamespace(REFERENCE_PARAMETERS.getPrefix(), REFERENCE_PARAMETERS.getNamespaceURI());
+ if (model.getConversationID() != null) {
+ writer.writeAttribute("conversationID", model.getConversationID().toString());
+ }
+ if (model.getCallbackID() != null) {
+ writer.writeAttribute("callbackID", model.getCallbackID().toString());
+ }
+ writer.writeEndElement();
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#getModelType()
+ */
+ public Class<ReferenceParameters> getModelType() {
+ return ReferenceParameters.class;
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#resolve(java.lang.Object, org.apache.tuscany.sca.contribution.resolver.ModelResolver)
+ */
+ public void resolve(ReferenceParameters model, ModelResolver resolver) throws ContributionResolveException {
+ }
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParametersImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParametersImpl.java
new file mode 100644
index 0000000000..e500e8ee20
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/ReferenceParametersImpl.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import org.apache.tuscany.sca.runtime.EndpointReference;
+import org.apache.tuscany.sca.runtime.ReferenceParameters;
+
+/**
+ * @version $Rev: 574648 $ $Date: 2007-09-11 10:45:36 -0700 (Tue, 11 Sep 2007) $
+ */
+public class ReferenceParametersImpl implements ReferenceParameters {
+ private Object callbackID;
+ private Object conversationID;
+ private EndpointReference callbackReference;
+ private Object callbackObjectID;
+
+ /**
+ * @return the callbackID
+ */
+ public Object getCallbackID() {
+ return callbackID;
+ }
+ /**
+ * @param callbackID the callbackID to set
+ */
+ public void setCallbackID(Object callbackID) {
+ this.callbackID = callbackID;
+ }
+ /**
+ * @return the conversationID
+ */
+ public Object getConversationID() {
+ return conversationID;
+ }
+ /**
+ * @param conversationID the conversationID to set
+ */
+ public void setConversationID(Object conversationID) {
+ this.conversationID = conversationID;
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.runtime.ReferenceParameters#getCallbackReference()
+ */
+ public EndpointReference getCallbackReference() {
+ return callbackReference;
+ }
+ /**
+ * @see org.apache.tuscany.sca.runtime.ReferenceParameters#setCallback(java.lang.Object)
+ */
+ public void setCallbackReference(EndpointReference callback) {
+ this.callbackReference = callback;
+ }
+
+ /**
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ return super.clone();
+ }
+
+ /**
+ * @return the callbackObjectID
+ */
+ public Object getCallbackObjectID() {
+ return callbackObjectID;
+ }
+ /**
+ * @param callbackObjectID the callbackObjectID to set
+ */
+ public void setCallbackObjectID(Object callbackObjectID) {
+ this.callbackObjectID = callbackObjectID;
+ }
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((callbackID == null) ? 0 : callbackID.hashCode());
+ result = prime * result + ((callbackObjectID == null) ? 0 : callbackObjectID.hashCode());
+ result = prime * result + ((callbackReference == null) ? 0 : callbackReference.hashCode());
+ result = prime * result + ((conversationID == null) ? 0 : conversationID.hashCode());
+ return result;
+ }
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (!(obj instanceof ReferenceParametersImpl))
+ return false;
+ final ReferenceParametersImpl other = (ReferenceParametersImpl)obj;
+ if (callbackID == null) {
+ if (other.callbackID != null)
+ return false;
+ } else if (!callbackID.equals(other.callbackID))
+ return false;
+ if (callbackObjectID == null) {
+ if (other.callbackObjectID != null)
+ return false;
+ } else if (!callbackObjectID.equals(other.callbackObjectID))
+ return false;
+ if (callbackReference == null) {
+ if (other.callbackReference != null)
+ return false;
+ } else if (!callbackReference.equals(other.callbackReference))
+ return false;
+ if (conversationID == null) {
+ if (other.conversationID != null)
+ return false;
+ } else if (!conversationID.equals(other.conversationID))
+ return false;
+ return true;
+ }
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java
new file mode 100644
index 0000000000..5d7eab26fa
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeAssemblyFactory.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+
+/**
+ * The runtime version of assembly factory
+ * @version $Rev: 571866 $ $Date: 2007-09-01 13:20:37 -0700 (Sat, 01 Sep 2007) $
+ */
+public class RuntimeAssemblyFactory extends DefaultAssemblyFactory implements AssemblyFactory {
+
+ public RuntimeAssemblyFactory() {
+ super();
+ }
+
+ @Override
+ public Component createComponent() {
+ return new RuntimeComponentImpl();
+ }
+
+ @Override
+ public ComponentReference createComponentReference() {
+ return new RuntimeComponentReferenceImpl();
+ }
+
+ @Override
+ public ComponentService createComponentService() {
+ return new RuntimeComponentServiceImpl();
+ }
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentImpl.java
new file mode 100644
index 0000000000..68312e50f4
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentImpl.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.impl.ComponentImpl;
+import org.apache.tuscany.sca.core.scope.ScopeContainer;
+import org.apache.tuscany.sca.core.scope.ScopedRuntimeComponent;
+import org.apache.tuscany.sca.provider.ImplementationProvider;
+import org.apache.tuscany.sca.provider.PolicyProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentContext;
+
+/**
+ * @version $Rev: 634776 $ $Date: 2008-03-07 10:48:00 -0800 (Fri, 07 Mar 2008) $
+ */
+public class RuntimeComponentImpl extends ComponentImpl implements RuntimeComponent, ScopedRuntimeComponent {
+ protected RuntimeComponentContext componentContext;
+ protected ImplementationProvider implementationProvider;
+ protected List<PolicyProvider> policyProviders = new ArrayList<PolicyProvider>();
+ protected ScopeContainer scopeContainer;
+ protected boolean started;
+
+ /**
+ */
+ public RuntimeComponentImpl() {
+ super();
+ }
+
+ public ImplementationProvider getImplementationProvider() {
+ return implementationProvider;
+ }
+
+ public void setImplementationProvider(ImplementationProvider provider) {
+ this.implementationProvider = provider;
+ }
+
+ public ScopeContainer getScopeContainer() {
+ return scopeContainer;
+ }
+
+ public void setScopeContainer(ScopeContainer scopeContainer) {
+ this.scopeContainer = scopeContainer;
+ }
+
+ public boolean isStarted() {
+ return started;
+ }
+
+ public void setStarted(boolean started) {
+ this.started = started;
+ }
+
+ /**
+ * @return the componentContext
+ */
+ public RuntimeComponentContext getComponentContext() {
+ return componentContext;
+ }
+
+ /**
+ * @param componentContext the componentContext to set
+ */
+ public void setComponentContext(RuntimeComponentContext componentContext) {
+ this.componentContext = componentContext;
+ }
+
+ public void addPolicyProvider(PolicyProvider policyProvider) {
+ policyProviders.add(policyProvider);
+ }
+
+ public List<PolicyProvider> getPolicyProviders() {
+ return policyProviders;
+ }
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentReferenceImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentReferenceImpl.java
new file mode 100644
index 0000000000..f8c80135c8
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentReferenceImpl.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.impl.ComponentReferenceImpl;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.InvocationChain;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.provider.PolicyProvider;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+
+public class RuntimeComponentReferenceImpl extends ComponentReferenceImpl implements RuntimeComponentReference {
+ private ArrayList<RuntimeWire> wires;
+ private HashMap<Binding, ReferenceBindingProvider> bindingProviders =
+ new HashMap<Binding, ReferenceBindingProvider>();
+ private HashMap<Binding, List<PolicyProvider>> policyProviders = new HashMap<Binding, List<PolicyProvider>>();
+
+ private RuntimeComponent component;
+
+ public RuntimeComponentReferenceImpl() {
+ super();
+ }
+
+ public synchronized List<RuntimeWire> getRuntimeWires() {
+ if (wires == null) {
+ wires = new ArrayList<RuntimeWire>();
+ component.getComponentContext().start(this);
+ }
+ return wires;
+ }
+
+ public RuntimeWire getRuntimeWire(Binding binding) {
+ for (RuntimeWire wire : getRuntimeWires()) {
+ if (wire.getSource().getBinding() == binding) {
+ return wire;
+ }
+ }
+ return null;
+ }
+
+ public ReferenceBindingProvider getBindingProvider(Binding binding) {
+ return bindingProviders.get(binding);
+ }
+
+ public void setBindingProvider(Binding binding, ReferenceBindingProvider bindingProvider) {
+ bindingProviders.put(binding, bindingProvider);
+ }
+
+ public Invoker getInvoker(Binding binding, Operation operation) {
+ RuntimeWire wire = getRuntimeWire(binding);
+ if (wire == null) {
+ return null;
+ }
+ InvocationChain chain = wire.getInvocationChain(operation);
+ return chain == null ? null : chain.getHeadInvoker();
+ }
+
+ /**
+ * @return the component
+ */
+ public RuntimeComponent getComponent() {
+ return component;
+ }
+
+ /**
+ * @param component the component to set
+ */
+ public void setComponent(RuntimeComponent component) {
+ this.component = component;
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.assembly.impl.ComponentReferenceImpl#clone()
+ */
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ RuntimeComponentReferenceImpl ref = (RuntimeComponentReferenceImpl)super.clone();
+ ref.wires = null;
+ ref.bindingProviders = new HashMap<Binding, ReferenceBindingProvider>();
+ ref.policyProviders = new HashMap<Binding, List<PolicyProvider>>();
+ return ref;
+ }
+
+ public void addPolicyProvider(Binding binding, PolicyProvider policyProvider) {
+ List<PolicyProvider> providers = policyProviders.get(binding);
+ if (providers == null) {
+ providers = new ArrayList<PolicyProvider>();
+ policyProviders.put(binding, providers);
+ }
+ providers.add(policyProvider);
+ }
+
+ public List<PolicyProvider> getPolicyProviders(Binding binding) {
+ return policyProviders.get(binding);
+ }
+
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentServiceImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentServiceImpl.java
new file mode 100644
index 0000000000..0393d31d61
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeComponentServiceImpl.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.impl.ComponentServiceImpl;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.InvocationChain;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.provider.PolicyProvider;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.osoa.sca.ServiceRuntimeException;
+
+public class RuntimeComponentServiceImpl extends ComponentServiceImpl implements RuntimeComponentService {
+ private ArrayList<RuntimeWire> wires = new ArrayList<RuntimeWire>();
+ private ArrayList<RuntimeWire> callbackWires = new ArrayList<RuntimeWire>();
+ private HashMap<Binding, ServiceBindingProvider> bindingProviders = new HashMap<Binding, ServiceBindingProvider>();
+ private HashMap<Binding, List<PolicyProvider>> policyProviders = new HashMap<Binding, List<PolicyProvider>>();
+
+ public RuntimeComponentServiceImpl() {
+ super();
+ }
+
+ public List<RuntimeWire> getRuntimeWires() {
+ return wires;
+ }
+
+ public RuntimeWire getRuntimeWire(Binding binding) {
+ for (RuntimeWire wire : wires) {
+ if (wire.getTarget().getBinding() == binding) {
+ return wire;
+ }
+ }
+ return null;
+ }
+
+ public RuntimeWire getRuntimeWire(Binding binding, InterfaceContract interfaceContract) {
+ RuntimeWire wire = getRuntimeWire(binding);
+ if (wire == null) {
+ return null;
+ }
+ if (interfaceContract != null && interfaceContract != wire.getSource().getInterfaceContract()) {
+ try {
+ // FIXME: [rfeng] We could avoid clone() using a better comparison of the two interface contracts
+ wire = (RuntimeWire)wire.clone();
+ wire.getSource().setInterfaceContract(interfaceContract);
+ wire.rebuild();
+ } catch (CloneNotSupportedException e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+ return wire;
+ }
+
+ public List<RuntimeWire> getCallbackWires() {
+ return callbackWires;
+ }
+
+ public ServiceBindingProvider getBindingProvider(Binding binding) {
+ return bindingProviders.get(binding);
+ }
+
+ public void setBindingProvider(Binding binding, ServiceBindingProvider bindingProvider) {
+ bindingProviders.put(binding, bindingProvider);
+ }
+
+ public Invoker getInvoker(Binding binding, Operation operation) {
+ return getInvoker(binding, null, operation);
+ }
+
+ public Invoker getInvoker(Binding binding, InterfaceContract interfaceContract, Operation operation) {
+ InvocationChain chain = getInvocationChain(binding, interfaceContract, operation);
+ if (chain != null) {
+ return chain.getHeadInvoker();
+ } else {
+ return null;
+ }
+ }
+
+ public InvocationChain getInvocationChain(Binding binding, InterfaceContract interfaceContract, Operation operation) {
+ RuntimeWire wire = getRuntimeWire(binding);
+ if (wire == null) {
+ return null;
+ }
+ if (interfaceContract != null && interfaceContract != wire.getSource().getInterfaceContract()) {
+ try {
+ // FIXME: [rfeng] We could avoid clone() using a better comparison of the two interface contracts
+ wire = (RuntimeWire)wire.clone();
+ wire.getSource().setInterfaceContract(interfaceContract);
+ wire.rebuild();
+ } catch (CloneNotSupportedException e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+ return wire.getInvocationChain(operation);
+ }
+
+ public InvocationChain getInvocationChain(Binding binding, Operation operation) {
+ return getInvocationChain(binding, null, operation);
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.assembly.impl.ComponentServiceImpl#clone()
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ RuntimeComponentServiceImpl clone = (RuntimeComponentServiceImpl)super.clone();
+ clone.bindingProviders = (HashMap<Binding, ServiceBindingProvider>)bindingProviders.clone();
+ clone.wires = (ArrayList<RuntimeWire>)wires.clone();
+ clone.callbackWires = (ArrayList<RuntimeWire>)callbackWires.clone();
+ clone.policyProviders = (HashMap<Binding, List<PolicyProvider>>)policyProviders.clone();
+ return clone;
+ }
+
+ public void addPolicyProvider(Binding binding, PolicyProvider policyProvider) {
+ List<PolicyProvider> providers = policyProviders.get(binding);
+ if (providers == null) {
+ providers = new ArrayList<PolicyProvider>();
+ policyProviders.put(binding, providers);
+ }
+ providers.add(policyProvider);
+ }
+
+ public List<PolicyProvider> getPolicyProviders(Binding binding) {
+ return policyProviders.get(binding);
+ }
+}
diff --git a/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java
new file mode 100644
index 0000000000..bd8f5e0aa1
--- /dev/null
+++ b/branches/sca-android/modules/core-android/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java
@@ -0,0 +1,377 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.core.assembly;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.Contract;
+import org.apache.tuscany.sca.core.conversation.ConversationManager;
+import org.apache.tuscany.sca.core.invocation.InvocationChainImpl;
+import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor;
+import org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Interceptor;
+import org.apache.tuscany.sca.invocation.InvocationChain;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.invocation.Message;
+import org.apache.tuscany.sca.invocation.MessageFactory;
+import org.apache.tuscany.sca.invocation.Phase;
+import org.apache.tuscany.sca.provider.ImplementationProvider;
+import org.apache.tuscany.sca.provider.PolicyProvider;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.EndpointReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.apache.tuscany.sca.runtime.RuntimeWireProcessor;
+import org.apache.tuscany.sca.work.WorkScheduler;
+import org.osoa.sca.ServiceRuntimeException;
+
+/**
+ * @version $Rev: 634776 $ $Date: 2008-03-07 10:48:00 -0800 (Fri, 07 Mar 2008) $
+ */
+public class RuntimeWireImpl implements RuntimeWire {
+ private EndpointReference wireSource;
+ private EndpointReference wireTarget;
+
+ private transient RuntimeWireProcessor wireProcessor;
+ private transient InterfaceContractMapper interfaceContractMapper;
+ private transient WorkScheduler workScheduler;
+ private transient MessageFactory messageFactory;
+ private transient ConversationManager conversationManager;
+ private transient RuntimeWireInvoker invoker;
+
+ // the following is a very simple cache that avoids re-cloning a wire
+ // when consecutive callbacks to the same endpoint are made
+ private EndpointReference lastCallback;
+ private RuntimeWire cachedWire;
+ private boolean wireReserved;
+ private RuntimeWireImpl clonedFrom;
+
+ private List<InvocationChain> chains;
+
+ /**
+ * @param source
+ * @param target
+ * @param interfaceContractMapper
+ * @param workScheduler
+ * @param wireProcessor
+ * @param messageFactory
+ * @param conversationManager
+ */
+ public RuntimeWireImpl(EndpointReference source,
+ EndpointReference target,
+ InterfaceContractMapper interfaceContractMapper,
+ WorkScheduler workScheduler,
+ RuntimeWireProcessor wireProcessor,
+ MessageFactory messageFactory,
+ ConversationManager conversationManager) {
+ super();
+ this.wireSource = source;
+ this.wireTarget = target;
+ this.interfaceContractMapper = interfaceContractMapper;
+ this.workScheduler = workScheduler;
+ this.wireProcessor = wireProcessor;
+ this.messageFactory = messageFactory;
+ this.conversationManager = conversationManager;
+ this.invoker = new RuntimeWireInvoker(this.messageFactory, this.conversationManager, this);
+ }
+
+ public synchronized List<InvocationChain> getInvocationChains() {
+ if (chains == null) {
+ initInvocationChains();
+ }
+ return chains;
+ }
+
+ public InvocationChain getInvocationChain(Operation operation) {
+ for (InvocationChain chain : getInvocationChains()) {
+ Operation op = null;
+ if (wireSource.getContract() != null) {
+ // Reference chain
+ op = chain.getSourceOperation();
+ } else {
+ // Service chain
+ op = chain.getTargetOperation();
+ }
+ if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) {
+ return chain;
+ }
+ }
+ return null;
+ }
+
+ public Object invoke(Operation operation, Object[] args) throws InvocationTargetException {
+ Message msg = messageFactory.createMessage();
+ msg.setBody(args);
+ return invoker.invoke(operation, msg);
+ }
+
+ public Object invoke(Operation operation, Message msg) throws InvocationTargetException {
+ return invoker.invoke(operation, msg);
+ }
+
+ /**
+ * Initialize the invocation chains
+ */
+ private void initInvocationChains() {
+ chains = new ArrayList<InvocationChain>();
+ InterfaceContract sourceContract = wireSource.getInterfaceContract();
+ InterfaceContract targetContract = wireTarget.getInterfaceContract();
+
+ Contract source = wireSource.getContract();
+ if (source instanceof RuntimeComponentReference) {
+ // It's the reference wire
+ RuntimeComponentReference reference = (RuntimeComponentReference)wireSource.getContract();
+ Binding refBinding = wireSource.getBinding();
+ for (Operation operation : sourceContract.getInterface().getOperations()) {
+ Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation);
+ if (targetOperation == null) {
+ throw new ServiceRuntimeException("No matching operation for " + operation.getName()
+ + " is found in reference "
+ + wireSource.getComponent().getURI()
+ + "#"
+ + reference.getName());
+ }
+ InvocationChain chain = new InvocationChainImpl(operation, targetOperation, true);
+ if (operation.isNonBlocking()) {
+ addNonBlockingInterceptor(reference, refBinding, chain);
+ }
+ addReferenceBindingInterceptor(reference, refBinding, chain, operation);
+ chains.add(chain);
+ }
+ } else {
+ // It's the service wire
+ RuntimeComponentService service = (RuntimeComponentService)wireTarget.getContract();
+ RuntimeComponent serviceComponent = wireTarget.getComponent();
+ Binding serviceBinding = wireTarget.getBinding();
+ for (Operation operation : sourceContract.getInterface().getOperations()) {
+ Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation);
+ if (targetOperation == null) {
+ throw new ServiceRuntimeException("No matching operation for " + operation.getName()
+ + " is found in service "
+ + serviceComponent.getURI()
+ + "#"
+ + service.getName());
+ }
+ InvocationChain chain = new InvocationChainImpl(operation, targetOperation, false);
+ if (operation.isNonBlocking()) {
+ addNonBlockingInterceptor(service, serviceBinding, chain);
+ }
+ addImplementationInterceptor(serviceComponent, service, chain, targetOperation);
+ chains.add(chain);
+ }
+ }
+ wireProcessor.process(this);
+ }
+
+ public EndpointReference getSource() {
+ return wireSource;
+ }
+
+ public EndpointReference getTarget() {
+ return wireTarget;
+ }
+
+ public void setTarget(EndpointReference target) {
+ if (this.wireTarget != target) {
+ rebuild();
+ }
+ this.wireTarget = target;
+ }
+
+ public void rebuild() {
+ this.chains = null;
+ }
+
+ /**
+ * Add the interceptor for a reference binding
+ *
+ * @param reference
+ * @param binding
+ * @param chain
+ * @param operation
+ */
+ private void addReferenceBindingInterceptor(ComponentReference reference,
+ Binding binding,
+ InvocationChain chain,
+ Operation operation) {
+ ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding);
+ if (provider != null) {
+ Invoker invoker = provider.createInvoker(operation);
+ if (invoker != null) {
+ chain.addInvoker(invoker);
+ }
+ }
+ List<PolicyProvider> pps = ((RuntimeComponentReference)reference).getPolicyProviders(binding);
+ if (pps != null) {
+ for (PolicyProvider p : pps) {
+ Interceptor interceptor = p.createInterceptor(operation);
+ if (interceptor != null) {
+ chain.addInterceptor(p.getPhase(), p.createInterceptor(operation));
+ }
+ }
+ }
+ }
+
+ /**
+ * Add the interceptor for a binding
+ *
+ * @param reference
+ * @param binding
+ * @param chain
+ * @param operation
+ */
+ private void addServiceBindingInterceptor(ComponentReference service,
+ Binding binding,
+ InvocationChain chain,
+ Operation operation) {
+ List<PolicyProvider> pps = ((RuntimeComponentService)service).getPolicyProviders(binding);
+ if (pps != null) {
+ for (PolicyProvider p : pps) {
+ Interceptor interceptor = p.createInterceptor(operation);
+ if (interceptor != null) {
+ chain.addInterceptor(p.getPhase(), p.createInterceptor(operation));
+ }
+ }
+ }
+ }
+
+ /**
+ * Add a non-blocking interceptor if the reference binding needs it
+ *
+ * @param reference
+ * @param binding
+ * @param chain
+ */
+ private void addNonBlockingInterceptor(ComponentReference reference, Binding binding, InvocationChain chain) {
+ ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding);
+ if (provider != null) {
+ boolean supportsOneWayInvocation = provider.supportsOneWayInvocation();
+ if (!supportsOneWayInvocation) {
+ chain.addInterceptor(Phase.REFERENCE, new NonBlockingInterceptor(workScheduler));
+ }
+ }
+ }
+
+ /**
+ * Add a non-blocking interceptor if the service binding needs it
+ *
+ * @param service
+ * @param binding
+ * @param chain
+ */
+ private void addNonBlockingInterceptor(ComponentService service, Binding binding, InvocationChain chain) {
+ ServiceBindingProvider provider = ((RuntimeComponentService)service).getBindingProvider(binding);
+ if (provider != null) {
+ if (!provider.supportsOneWayInvocation()) {
+ chain.addInterceptor(Phase.SERVICE_BINDING, new NonBlockingInterceptor(workScheduler));
+ }
+ }
+ }
+
+ /**
+ * Add the interceptor for a component implementation
+ *
+ * @param component
+ * @param service
+ * @param chain
+ * @param operation
+ */
+ private void addImplementationInterceptor(Component component,
+ ComponentService service,
+ InvocationChain chain,
+ Operation operation) {
+ ImplementationProvider provider = ((RuntimeComponent)component).getImplementationProvider();
+ if (provider != null) {
+ Invoker invoker = null;
+ invoker = provider.createInvoker((RuntimeComponentService)service, operation);
+ chain.addInvoker(invoker);
+ }
+ List<PolicyProvider> pps = ((RuntimeComponent)component).getPolicyProviders();
+ if (pps != null) {
+ for (PolicyProvider p : pps) {
+ Interceptor interceptor = p.createInterceptor(operation);
+ if (interceptor != null) {
+ chain.addInterceptor(p.getPhase(), p.createInterceptor(operation));
+ }
+ }
+ }
+ }
+
+ /**
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ RuntimeWireImpl copy = (RuntimeWireImpl)super.clone();
+ copy.wireSource = (EndpointReference)wireSource.clone();
+ copy.wireTarget = (EndpointReference)wireTarget.clone();
+ copy.invoker = new RuntimeWireInvoker(copy.messageFactory, copy.conversationManager, copy);
+ return copy;
+ }
+
+ /**
+ * @return the conversationManager
+ */
+ public ConversationManager getConversationManager() {
+ return conversationManager;
+ }
+
+ public synchronized RuntimeWire lookupCache(EndpointReference callback) {
+ if (lastCallback != null && callback.getURI().equals(lastCallback.getURI()) && !wireReserved) {
+ wireReserved = true;
+ return cachedWire;
+ } else {
+ return null;
+ }
+ }
+
+ public synchronized void addToCache(EndpointReference callback, RuntimeWire clonedWire) {
+ ((RuntimeWireImpl)clonedWire).setClonedFrom(this);
+ lastCallback = callback;
+ cachedWire = clonedWire;
+ wireReserved = true;
+ }
+
+ public synchronized void releaseClonedWire(RuntimeWire wire) {
+ if (cachedWire == wire) {
+ wireReserved = false;
+ }
+ }
+
+ public synchronized void releaseWire() {
+ clonedFrom.releaseClonedWire(this);
+ }
+
+ private void setClonedFrom(RuntimeWireImpl wire) {
+ clonedFrom = wire;
+ }
+}