Author: rfeng
Date: Sat May 21 04:35:44 2011
New Revision: 1125636

URL: http://svn.apache.org/viewvc?rev=1125636&view=rev
Log:
Allows pass -by-reference for self references

Modified:
    
tuscany/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/RuntimeSCAReferenceBindingProvider.java
    
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java
    
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorld.java
    
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorldImpl.java
    
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java

Modified: 
tuscany/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/RuntimeSCAReferenceBindingProvider.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/RuntimeSCAReferenceBindingProvider.java?rev=1125636&r1=1125635&r2=1125636&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/RuntimeSCAReferenceBindingProvider.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/RuntimeSCAReferenceBindingProvider.java
 Sat May 21 04:35:44 2011
@@ -148,7 +148,9 @@ public class RuntimeSCAReferenceBindingP
                 } 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,

Modified: 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java?rev=1125636&r1=1125635&r2=1125636&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java
 Sat May 21 04:35:44 2011
@@ -106,17 +106,18 @@ public class ComponentContextImpl implem
     }
 
     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 implem
                 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 implem
             }
         }
         //TUSCANY-3543
-        if(returnEp == null) {
+        if (returnEp == null) {
             returnEp = eps.get(0);
         }
-        
+
         return returnEp;
     }
 
@@ -213,7 +215,7 @@ public class ComponentContextImpl implem
     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 implem
         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 implem
             }
             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 implem
             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 implem
      */
     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 implem
             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 implem
             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 implem
         
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 implem
                 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 implem
                         
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 implem
         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 implem
             }
             throw new IllegalArgumentException("Reference not found: " + 
referenceName);
         } catch (IllegalArgumentException iae) {
-               throw iae;
+            throw iae;
         } catch (ServiceRuntimeException e) {
             throw e;
         } catch (Exception e) {

Modified: 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorld.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorld.java?rev=1125636&r1=1125635&r2=1125636&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorld.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorld.java
 Sat May 21 04:35:44 2011
@@ -27,4 +27,11 @@ import org.oasisopen.sca.annotation.Remo
 @Remotable
 public interface HelloWorld {
     String hello(String name);
+
+    Message echo(Message msg);
+
+    public class Message {
+        public String name;
+        public String message;
+    }
 }

Modified: 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorldImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorldImpl.java?rev=1125636&r1=1125635&r2=1125636&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorldImpl.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/hello/HelloWorldImpl.java
 Sat May 21 04:35:44 2011
@@ -19,6 +19,8 @@
 
 package hello;
 
+import org.oasisopen.sca.annotation.AllowsPassByReference;
+
 /**
  * HelloWorldImpl
  */
@@ -27,4 +29,10 @@ public class HelloWorldImpl implements H
         System.out.println("Hello: " + name);
         return "Hello, " + name;
     }
+
+    @Override
+    @AllowsPassByReference
+    public Message echo(Message msg) {
+        return msg;
+    }
 }

Modified: 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java?rev=1125636&r1=1125635&r2=1125636&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
 Sat May 21 04:35:44 2011
@@ -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();
     }
 


Reply via email to