Allows pass -by-reference for self references

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1125636 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2011-05-21 04:35:44 +00:00
parent c34f78773a
commit 09fdc02e7a
5 changed files with 78 additions and 45 deletions
sca-java-2.x/trunk/modules
binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider
core/src/main/java/org/apache/tuscany/sca/core/context/impl
node-impl/src/test/java
hello
org/apache/tuscany/sca/node/impl

View file

@ -148,7 +148,9 @@ public class RuntimeSCAReferenceBindingProvider implements EndpointReferenceAsyn
} else {
Reference ref = epr.getReference().getReference();
// The spec says both ref and service needs to allowsPassByReference
boolean allowsPBR = (ref != null && ref.isAllowsPassByReference()) && chain.allowsPassByReference();
boolean allowsPBR =
(epr.getReference().isAllowsPassByReference() || (ref != null && ref.isAllowsPassByReference())) && chain
.allowsPassByReference();
if (allowsPBR && interfaceContractMapper.isCompatibleByReference(operation,
targetOp,

View file

@ -106,17 +106,18 @@ public class ComponentContextImpl implements RuntimeComponentContext {
}
public <B> B getService(Class<B> businessInterface, String referenceName) throws IllegalArgumentException {
B service = null;
ServiceReference<B> serviceRef = getServiceReference(businessInterface, referenceName);
if(serviceRef != null) {
B service = null;
ServiceReference<B> serviceRef = getServiceReference(businessInterface, referenceName);
if (serviceRef != null) {
service = serviceRef.getService();
}
}
return service;
}
public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) throws IllegalArgumentException {
public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName)
throws IllegalArgumentException {
for (ComponentReference ref : component.getReferences()) {
if (referenceName.equals(ref.getName())) {
@ -129,7 +130,8 @@ public class ComponentContextImpl implements RuntimeComponentContext {
if (ref.getEndpointReferences().size() < 1) {
return null;
}
ServiceReference<B> sr = getServiceReference(businessInterface, (RuntimeEndpointReference)getEndpointReference(ref));
ServiceReference<B> sr =
getServiceReference(businessInterface, (RuntimeEndpointReference)getEndpointReference(ref));
if (sr == null) {
throw new IllegalArgumentException("Reference " + referenceName + " is null");
}
@ -181,10 +183,10 @@ public class ComponentContextImpl implements RuntimeComponentContext {
}
}
//TUSCANY-3543
if(returnEp == null) {
if (returnEp == null) {
returnEp = eps.get(0);
}
return returnEp;
}
@ -213,7 +215,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
public <B> B getProperty(Class<B> type, String propertyName) {
for (ComponentProperty p : component.getProperties()) {
if (propertyName.equals(p.getName())) {
return propertyFactory.createPropertyValue(p, type);
return propertyFactory.createPropertyValue(p, type);
}
}
throw new IllegalArgumentException("Property not found: " + propertyName);
@ -251,7 +253,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
try {
return createSelfReference(businessInterface, service);
} catch (IllegalArgumentException iae) {
throw iae;
throw iae;
} catch (Exception e) {
throw new ServiceRuntimeException(e.getMessage(), e);
}
@ -279,7 +281,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
}
throw new IllegalArgumentException("Service not found: " + serviceName);
} catch (IllegalArgumentException iae) {
throw iae;
throw iae;
} catch (ServiceRuntimeException e) {
throw e;
} catch (Exception e) {
@ -300,7 +302,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
ref.setComponent(component);
return getServiceReference(businessInterface, ref);
} catch (IllegalArgumentException iae) {
throw iae;
throw iae;
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
@ -323,8 +325,8 @@ public class ComponentContextImpl implements RuntimeComponentContext {
*/
public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface,
RuntimeEndpointReference endpointReference) {
ServiceReference<B> result = null;
ServiceReference<B> result = null;
try {
InterfaceContract interfaceContract = endpointReference.getComponentTypeReferenceInterfaceContract();
if (businessInterface == null) {
@ -333,24 +335,26 @@ public class ComponentContextImpl implements RuntimeComponentContext {
RuntimeComponentReference ref = (RuntimeComponentReference)endpointReference.getReference();
InterfaceContract refInterfaceContract = getInterfaceContract(interfaceContract, businessInterface);
if (refInterfaceContract != null) {
if (refInterfaceContract != interfaceContract) {
ref = (RuntimeComponentReference)ref.clone();
if (interfaceContract != null) {
ref.setInterfaceContract(interfaceContract);
} else {
ref.setInterfaceContract(refInterfaceContract);
}
}
ref.setComponent(component);
result = new ServiceReferenceImpl<B>(businessInterface, endpointReference, component.getComponentContext().getCompositeContext());
if (refInterfaceContract != interfaceContract) {
ref = (RuntimeComponentReference)ref.clone();
if (interfaceContract != null) {
ref.setInterfaceContract(interfaceContract);
} else {
ref.setInterfaceContract(refInterfaceContract);
}
}
ref.setComponent(component);
result =
new ServiceReferenceImpl<B>(businessInterface, endpointReference, component.getComponentContext()
.getCompositeContext());
}
} catch (IllegalArgumentException iae ) {
throw iae;
} catch (IllegalArgumentException iae) {
throw iae;
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
return result;
}
@ -365,7 +369,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
ref.setComponent(component);
return new ServiceReferenceImpl<B>(businessInterface, ref, compositeContext);
} catch (IllegalArgumentException iae) {
throw iae;
throw iae;
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
@ -401,6 +405,9 @@ public class ComponentContextImpl implements RuntimeComponentContext {
componentReference.getRequiredIntents().addAll(service.getRequiredIntents());
componentReference.getBindings().add(endpoint.getBinding());
// For the self-reference, allows pass by reference
componentReference.setAllowsPassByReference(true);
InterfaceContract interfaceContract = service.getInterfaceContract();
Service componentTypeService = service.getService();
if (componentTypeService != null && componentTypeService.getInterfaceContract() != null) {
@ -453,7 +460,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
if (businessInterface.isAssignableFrom(cls)) {
compatible = true;
}
if(!compatible) {
if (!compatible) {
InterfaceContract biContract = javaInterfaceFactory.createJavaInterfaceContract();
JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(businessInterface);
biContract.setInterface(callInterface);
@ -461,16 +468,18 @@ public class ComponentContextImpl implements RuntimeComponentContext {
biContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(callInterface
.getCallbackClass()));
}
InterfaceContractMapper ifcm = registry.getExtensionPoint(InterfaceContractMapper.class);
compatible = ifcm.isCompatibleSubset(biContract , interfaceContract);
InterfaceContractMapper ifcm = registry.getExtensionPoint(InterfaceContractMapper.class);
compatible = ifcm.isCompatibleSubset(biContract, interfaceContract);
}
}
}
if(!compatible) {
// JCA-9011
throw new IllegalArgumentException("Business interface " + businessInterface.getName() + " is not compatible with " + interfaceContract.getInterface());
if (!compatible) {
// JCA-9011
throw new IllegalArgumentException("Business interface " + businessInterface.getName()
+ " is not compatible with "
+ interfaceContract.getInterface());
}
return interfaceContract;
@ -497,11 +506,13 @@ public class ComponentContextImpl implements RuntimeComponentContext {
try {
for (ComponentReference ref : component.getReferences()) {
if (referenceName.equals(ref.getName())) {
if ( ref.getMultiplicity() == Multiplicity.ONE_ONE )
throw new IllegalArgumentException("Reference " + referenceName + " is not a valid argument for getServiceReferences because it has a multiplicity of 1..1");
if (ref.getMultiplicity() == Multiplicity.ZERO_ONE)
throw new IllegalArgumentException("Reference " + referenceName + " is not a valid argument for getServiceReferences because it has a multiplicity of 0..1");
if (ref.getMultiplicity() == Multiplicity.ONE_ONE)
throw new IllegalArgumentException("Reference " + referenceName
+ " is not a valid argument for getServiceReferences because it has a multiplicity of 1..1");
if (ref.getMultiplicity() == Multiplicity.ZERO_ONE)
throw new IllegalArgumentException("Reference " + referenceName
+ " is not a valid argument for getServiceReferences because it has a multiplicity of 0..1");
ArrayList<ServiceReference<B>> serviceRefs = new ArrayList<ServiceReference<B>>();
for (EndpointReference endpointReference : ref.getEndpointReferences()) {
RuntimeEndpointReference epr = (RuntimeEndpointReference)endpointReference;
@ -512,7 +523,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
}
throw new IllegalArgumentException("Reference not found: " + referenceName);
} catch (IllegalArgumentException iae) {
throw iae;
throw iae;
} catch (ServiceRuntimeException e) {
throw e;
} catch (Exception e) {

View file

@ -27,4 +27,11 @@ import org.oasisopen.sca.annotation.Remotable;
@Remotable
public interface HelloWorld {
String hello(String name);
Message echo(Message msg);
public class Message {
public String name;
public String message;
}
}

View file

@ -19,6 +19,8 @@
package hello;
import org.oasisopen.sca.annotation.AllowsPassByReference;
/**
* HelloWorldImpl
*/
@ -27,4 +29,10 @@ public class HelloWorldImpl implements HelloWorld {
System.out.println("Hello: " + name);
return "Hello, " + name;
}
@Override
@AllowsPassByReference
public Message echo(Message msg) {
return msg;
}
}

View file

@ -125,7 +125,12 @@ public class NodeImplTestCase {
address = node.getEndpointAddress("HelloWorld/HelloWorld/HelloWorld");
Assert.assertNotNull(address);
address = node.getEndpointAddress("HelloWorld/HelloWorld1");
Assert.assertNull(address);
Assert.assertNull(address);
HelloWorld.Message msg = new HelloWorld.Message();
msg.name = "John";
msg.message = "Hi";
Assert.assertSame(msg, hw.echo(msg));
node.stop();
}