diff options
Diffstat (limited to 'sca-java-2.x/trunk')
10 files changed, 187 insertions, 64 deletions
diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/ClientImpl.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/ClientImpl.java index 30ae3cc0fb..96a0f2dadd 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/ClientImpl.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/ClientImpl.java @@ -34,7 +34,12 @@ public class ClientImpl implements Client { protected Remote remote; public String getName(String id) { - Customer customer = remote.getCustomer(id); + Customer customer = null; + try { + customer = remote.getCustomer(id); + } catch (CustomerNotFoundException e) { + return null; + } customer.dump("Client.getName()"); return local.getName(customer); } diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/CustomerNotFoundException.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/CustomerNotFoundException.java new file mode 100644 index 0000000000..a6e80c9eca --- /dev/null +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/CustomerNotFoundException.java @@ -0,0 +1,64 @@ +/* + * 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.itest.bindingsca; + +/** + * + */ +public class CustomerNotFoundException extends Exception { + private String customerId; + + /** + * + */ + public CustomerNotFoundException() { + } + + /** + * @param message + */ + public CustomerNotFoundException(String message) { + super(message); + } + + /** + * @param cause + */ + public CustomerNotFoundException(Throwable cause) { + super(cause); + } + + /** + * @param message + * @param cause + */ + public CustomerNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } + +} diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/Remote.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/Remote.java index eaa2298c49..050d35007b 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/Remote.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/Remote.java @@ -30,7 +30,7 @@ public interface Remote { String getId(Customer customer); - Customer getCustomer(String id); + Customer getCustomer(String id) throws CustomerNotFoundException; Customer createCustomer(String id, String name); } diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/RemoteServiceImpl.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/RemoteServiceImpl.java index baf32ad959..6c432a500a 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/RemoteServiceImpl.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/RemoteServiceImpl.java @@ -45,8 +45,13 @@ public class RemoteServiceImpl implements Remote { return customer.getId(); } - public Customer getCustomer(String id) { + public Customer getCustomer(String id) throws CustomerNotFoundException { Customer customer = customers.get(id); + if (customer == null) { + CustomerNotFoundException ex = new CustomerNotFoundException("Customer not found"); + ex.setCustomerId(id); + throw ex; + } customer.dump("Remote.getCustomer()"); return customer; } diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/SCAClientImpl.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/SCAClientImpl.java index eb7fbf06e0..3573eea978 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/SCAClientImpl.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/main/java/org/apache/tuscany/sca/itest/bindingsca/SCAClientImpl.java @@ -37,7 +37,12 @@ public class SCAClientImpl implements Client { } public String getName(String id) { - Customer customer = remote.getCustomer(id); + Customer customer = null; + try { + customer = remote.getCustomer(id); + } catch (CustomerNotFoundException e) { + return null; + } customer.dump("Client.getName()"); return local.getName(customer); } diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/BindingSCATestCase.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/BindingSCATestCase.java index 3ad98ba1f5..05a8c3455c 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/BindingSCATestCase.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/BindingSCATestCase.java @@ -134,6 +134,11 @@ public class BindingSCATestCase { String id = client.create("Ray"); Assert.assertEquals("Ray", client.getName(id)); } + + static void runClientNotFound(Client client) { + String id = "not-there"; + Assert.assertNull(client.getName(id)); + } static String createCustomer(Node node) { Client client = node.getService(Client.class, "ClientComponent/Client"); diff --git a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/ClientSharedLocalTestCase.java b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/ClientSharedLocalTestCase.java index 034136ca53..633e1a767c 100644 --- a/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/ClientSharedLocalTestCase.java +++ b/sca-java-2.x/trunk/itest/nodes/binding-sca-tribes/src/test/java/org/apache/tuscany/sca/itest/bindingsca/ClientSharedLocalTestCase.java @@ -36,7 +36,7 @@ public class ClientSharedLocalTestCase { public static void setUpBeforeClass() throws Exception { runner = new TestCaseRunner(ServiceNode.class, Remote.class.getName(), RemoteServiceImpl.class.getName(), - Customer.class.getName()); + Customer.class.getName(), CustomerNotFoundException.class.getName()); runner.beforeClass(); client = new SCAClientImpl(BindingSCATestCase.DOMAIN_URI); Thread.sleep(1000); @@ -46,6 +46,12 @@ public class ClientSharedLocalTestCase { public void testClient() throws Exception { BindingSCATestCase.runClient(client); } + + @Test + public void testClientNotFound() throws Exception { + BindingSCATestCase.runClientNotFound(client); + } + @AfterClass public static void tearDownAfterClass() throws Exception { diff --git a/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/SCABindingInvoker.java b/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/SCABindingInvoker.java index cbe836954d..a4a5f8c655 100644 --- a/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/SCABindingInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-sca-runtime/src/main/java/org/apache/tuscany/sca/binding/sca/provider/SCABindingInvoker.java @@ -78,10 +78,10 @@ public class SCABindingInvoker implements Interceptor, DataExchangeSemantics { if (passByValue) { // Note source and target operation swapped so result is in source class loader if (resultMsg.isFault()) { - resultMsg.setFaultBody(mediator.copyFault(resultMsg.getBody(), targetOperation, sourceOperation)); + resultMsg.setFaultBody(mediator.copyFault(resultMsg.getBody(), sourceOperation, targetOperation)); } else { if (sourceOperation.getOutputType() != null) { - resultMsg.setBody(mediator.copyOutput(resultMsg.getBody(), targetOperation, sourceOperation)); + resultMsg.setBody(mediator.copyOutput(resultMsg.getBody(), sourceOperation, targetOperation)); } } } diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/Mediator.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/Mediator.java index d33862ea5e..1d0efc3208 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/Mediator.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/Mediator.java @@ -132,10 +132,11 @@ public interface Mediator { /** * Copy an array of data objects passed to an operation - * @param data array of objects to copy + * @param sourceOperation The source operation + * @param targetOperation The target operation * @return the copy */ - public Object copyInput(Object input, Operation operation, Operation targetOperation); + public Object copyInput(Object input, Operation sourceOperation, Operation targetOperation); /** * Copy the output data @@ -146,13 +147,13 @@ public interface Mediator { Object copyOutput(Object data, Operation operation); /** - * Copy the output data + * Copy the output data from target operation into source operation * @param data The orginal output - * @param operation The operation + * @param sourceOperation The operation * @param targetOperation The target operation * @return The copy */ - Object copyOutput(Object data, Operation operation, Operation targetOperation); + Object copyOutput(Object data, Operation sourceOperation, Operation targetOperation); /** * Copy the fault data @@ -163,13 +164,13 @@ public interface Mediator { Object copyFault(Object fault, Operation operation); /** - * Copy the fault data + * Copy the fault data from target operation into source operation * @param fault The orginal fault data - * @param operation The operation + * @param sourceOperation The operation * @param targetOperation The target operation * @return The copy */ - Object copyFault(Object fault, Operation operation, Operation targetOperation); + Object copyFault(Object fault, Operation sourceOperation, Operation targetOperation); /** * Get the DataBindings used by this mediator. diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/MediatorImpl.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/MediatorImpl.java index a5e101b097..c96fe0ae32 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/MediatorImpl.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/MediatorImpl.java @@ -49,6 +49,7 @@ import org.apache.tuscany.sca.databinding.TransformerExtensionPoint; import org.apache.tuscany.sca.databinding.javabeans.JavaBeansDataBinding; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.FaultExceptionMapper; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; import org.apache.tuscany.sca.interfacedef.util.FaultException; @@ -67,6 +68,7 @@ public class MediatorImpl implements Mediator { private ExtensionPointRegistry registry; private DataBindingExtensionPoint dataBindings; private TransformerExtensionPoint transformers; + private InterfaceContractMapper interfaceContractMapper; private FaultExceptionMapper faultExceptionMapper; MediatorImpl(DataBindingExtensionPoint dataBindings, TransformerExtensionPoint transformers) { @@ -78,8 +80,9 @@ public class MediatorImpl implements Mediator { this.registry = registry; this.dataBindings = registry.getExtensionPoint(DataBindingExtensionPoint.class); this.transformers = registry.getExtensionPoint(TransformerExtensionPoint.class); - this.faultExceptionMapper = - registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(FaultExceptionMapper.class); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + this.faultExceptionMapper = utilities.getUtility(FaultExceptionMapper.class); } @@ -207,6 +210,11 @@ public class MediatorImpl implements Mediator { return transformers; } + /** + * Find the fault data type behind the exception data type + * @param exceptionType The exception data type + * @return The fault data type + */ private DataType getFaultType(DataType exceptionType) { return exceptionType == null ? null : (DataType)exceptionType.getLogical(); } @@ -277,30 +285,11 @@ public class MediatorImpl implements Mediator { if (result instanceof InvocationTargetException) { result = ((InvocationTargetException)result).getCause(); } - DataType targetDataType = null; - for (DataType exType : targetOperation.getFaultTypes()) { - if (((Class)exType.getPhysical()).isInstance(result)) { - if (result instanceof FaultException) { - DataType faultType = (DataType)exType.getLogical(); - if (((FaultException)result).isMatchingType(faultType.getLogical())) { - targetDataType = exType; - break; - } - } else { - targetDataType = exType; - break; - } - } - } + + DataType targetDataType = findFaultDataType(targetOperation, result); + DataType targetFaultType = getFaultType(targetDataType); - /* - if (targetDataType == null) { - // Not a business exception - return resultMsg; - } - */ - DataType targetFaultType = getFaultType(targetDataType); if (targetFaultType == null) { // No matching fault type, it's a system exception Throwable cause = (Throwable)result; @@ -345,6 +334,31 @@ public class MediatorImpl implements Mediator { } + /** + * Look up the fault data type that matches the fault or exception instance + * @param operation The operation + * @param faultOrException The fault or exception + * @return The matching fault data type + */ + private DataType findFaultDataType(Operation operation, Object faultOrException) { + DataType targetDataType = null; + for (DataType exType : operation.getFaultTypes()) { + if (((Class)exType.getPhysical()).isInstance(faultOrException)) { + if (faultOrException instanceof FaultException) { + DataType faultType = (DataType)exType.getLogical(); + if (((FaultException)faultOrException).isMatchingType(faultType.getLogical())) { + targetDataType = exType; + break; + } + } else { + targetDataType = exType; + break; + } + } + } + return targetDataType; + } + private boolean typesMatch(Object first, Object second) { if (first.equals(second)) { return true; @@ -525,14 +539,14 @@ public class MediatorImpl implements Mediator { * @return the copy */ public Object copyInput(Object input, Operation operation) { - return copyInput(input, operation, null); + return copyInput(input, operation, operation); } - public Object copyInput(Object input, Operation operation, Operation targetOperation) { + public Object copyInput(Object input, Operation sourceOperation, Operation targetOperation) { if (input == null) { return null; } Object[] data = (input instanceof Object[]) ? (Object[])input : new Object[] {input}; - List<DataType> inputTypes = operation.getInputType().getLogical(); + List<DataType> inputTypes = sourceOperation.getInputType().getLogical(); List<DataType> inputTypesTarget = targetOperation == null ? null : targetOperation.getInputType().getLogical(); Object[] copy = new Object[data.length]; Map<Object, Object> map = new IdentityHashMap<Object, Object>(); @@ -545,7 +559,12 @@ public class MediatorImpl implements Mediator { if (copiedArg != null) { copy[i] = copiedArg; } else { - copiedArg = copy(arg, inputTypes.get(i), inputTypesTarget == null ? null : inputTypesTarget.get(i)); + copiedArg = + copy(arg, + inputTypes.get(i), + inputTypesTarget == null ? null : inputTypesTarget.get(i), + sourceOperation, + targetOperation); map.put(arg, copiedArg); copy[i] = copiedArg; } @@ -555,42 +574,55 @@ public class MediatorImpl implements Mediator { } public Object copyOutput(Object data, Operation operation) { - return copyOutput(data, operation, null); + return copyOutput(data, operation, operation); } - public Object copyOutput(Object data, Operation operation, Operation targetOperation) { - return copy(data, operation.getOutputType(), targetOperation.getOutputType(), operation, targetOperation); + + public Object copyOutput(Object data, Operation sourceOperation, Operation targetOperation) { + return copy(data, targetOperation.getOutputType(), sourceOperation.getOutputType(), targetOperation, sourceOperation); } public Object copyFault(Object fault, Operation operation) { - return copyFault(fault, operation, null); + return copyFault(fault, operation, operation); } - public Object copyFault(Object fault, Operation operation, Operation targetOperation) { + + public Object copyFault(Object fault, Operation sourceOperation, Operation targetOperation) { if (faultExceptionMapper == null) { return fault; } - List<DataType> fts = operation.getFaultTypes(); - for (int i=0; i<fts.size(); i++) { + List<DataType> fts = targetOperation.getFaultTypes(); + for (int i = 0; i < fts.size(); i++) { DataType et = fts.get(i); if (et.getPhysical().isInstance(fault)) { Throwable ex = (Throwable)fault; - DataType<DataType> exType = - new DataTypeImpl<DataType>(ex.getClass(), new DataTypeImpl<XMLType>(ex.getClass(), XMLType.UNKNOWN)); - faultExceptionMapper.introspectFaultDataType(exType, operation, false); - DataType faultType = exType.getLogical(); - Object faultInfo = faultExceptionMapper.getFaultInfo(ex, faultType.getPhysical(), operation); - DataType targetFaultType; - try { - targetFaultType = (DataType)faultType.clone(); - } catch (CloneNotSupportedException e) { - throw new IllegalStateException(e); - } - targetFaultType.setPhysical(targetOperation.getFaultTypes().get(i).getPhysical()); - faultInfo = copy(faultInfo, faultType, targetFaultType); - fault = faultExceptionMapper.wrapFaultInfo(exType, ex.getMessage(), faultInfo, ex.getCause(), operation); + DataType<DataType> exType = findFaultDataType(targetOperation, fault); + DataType faultType = getFaultType(exType); + Object faultInfo = faultExceptionMapper.getFaultInfo(ex, faultType.getPhysical(), targetOperation); + DataType targetExType = findSourceFaultDataType(sourceOperation, exType); + DataType targetFaultType = getFaultType(targetExType); + faultInfo = copy(faultInfo, faultType, targetFaultType, targetOperation, sourceOperation); + fault = faultExceptionMapper.wrapFaultInfo(targetExType, ex.getMessage(), faultInfo, ex.getCause(), sourceOperation); return fault; } } return fault; } + + /** + * Lookup a fault data type from the source operation which matches the target fault data type + * @param sourceOperation The source operation + * @param targetExceptionType The target fault data type + * @return The matching source target fault type + */ + private DataType findSourceFaultDataType(Operation sourceOperation, DataType targetExceptionType) { + boolean remotable = sourceOperation.getInterface().isRemotable(); + DataType targetFaultType = getFaultType(targetExceptionType); + for (DataType dt : sourceOperation.getFaultTypes()) { + DataType sourceFaultType = getFaultType(dt); + if (interfaceContractMapper.isCompatible(targetFaultType, sourceFaultType, remotable)) { + return dt; + } + } + return null; + } } |