From 04ef75e0a7e0e5928a90ca53a686665c2c634573 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 29 Sep 2008 00:55:58 +0000 Subject: Pulled from trunk. TUSCANY-2281 provide a api to allow service references to be retrieved for references where the multiplicity >1. Thanks to Daniel Stucky for the patch. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@699935 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/context/ComponentContextImpl.java | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'branches/sca-equinox/modules/core/src/main/java/org/apache') diff --git a/branches/sca-equinox/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextImpl.java b/branches/sca-equinox/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextImpl.java index ed01a509d2..2bcf13f679 100644 --- a/branches/sca-equinox/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextImpl.java +++ b/branches/sca-equinox/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ComponentContextImpl.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.core.context; import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.util.Collection; +import java.util.ArrayList; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Binding; @@ -107,6 +109,14 @@ public class ComponentContextImpl implements RuntimeComponentContext { try { for (ComponentReference ref : component.getReferences()) { if (referenceName.equals(ref.getName())) { + /* ******************** Contribution for issue TUSCANY-2281 ******************** */ + Multiplicity multiplicity = ref.getMultiplicity(); + if( multiplicity == Multiplicity.ZERO_N || multiplicity == Multiplicity.ONE_N) + { + throw new IllegalArgumentException("Reference " + referenceName + " has multiplicity " + multiplicity); + } + /* ******************** Contribution for issue TUSCANY-2281 ******************** */ + return getServiceReference(businessInterface, (RuntimeComponentReference)ref, null); } } @@ -400,4 +410,43 @@ public class ComponentContextImpl implements RuntimeComponentContext { public void write(RuntimeComponentReference reference, Writer writer) throws IOException { compositeActivator.getComponentContextHelper().write(component, reference, writer); } + + /* ******************** Contribution for issue TUSCANY-2281 ******************** */ + + /** + * @see ComponentContext#getServices(Class, String) + */ + public Collection getServices(Class businessInterface, String referenceName) { + ArrayList services = new ArrayList(); + Collection> serviceRefs = getServiceReferences(businessInterface, referenceName); + for (ServiceReference serviceRef : serviceRefs) { + services.add(serviceRef.getService()); + } + return services; + } + + /** + * @see ComponentContext#getServiceReferences(Class, String) + */ + public Collection> getServiceReferences(Class businessInterface, String referenceName) { + try { + for (ComponentReference ref : component.getReferences()) { + if (referenceName.equals(ref.getName())) { + ArrayList> serviceRefs = new ArrayList>(); + for(Binding binding : ref.getBindings()) + { + serviceRefs.add( getServiceReference(businessInterface, (RuntimeComponentReference) ref, binding) ); + } + return serviceRefs; + } + } + throw new ServiceRuntimeException("Reference not found: " + referenceName); + } catch (ServiceRuntimeException e) { + throw e; + } catch (Exception e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + /* ******************** Contribution for issue TUSCANY-2281 ******************** */ + } -- cgit v1.2.3