You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ri...@apache.org on 2007/02/20 14:32:53 UTC

svn commit: r509554 - in /incubator/tuscany/branches/sca-java-integration: sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/ sca/kernel/core/src/main/resources/org/apache/tuscany/core/ testing/sca/itest/exceptionXbindingTest/src/m...

Author: rineholt
Date: Tue Feb 20 05:32:51 2007
New Revision: 509554

URL: http://svn.apache.org/viewvc?view=rev&rev=509554
Log:
process messages that are business exceptions

Added:
    incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java   (with props)
Modified:
    incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/DataBindingInteceptor.java
    incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/resources/org/apache/tuscany/core/databinding.scdl
    incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/impl/StockTraderSDOImpl.java
    incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/sdohandgen/StockExceptionTest.java
    incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/resources/wsdl/StockExceptionTest.wsdl
    incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/test/java/org/apache/tuscany/sca/test/exceptions/IntraCompositeTestCase.java

Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/DataBindingInteceptor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/DataBindingInteceptor.java?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/DataBindingInteceptor.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/DataBindingInteceptor.java Tue Feb 20 05:32:51 2007
@@ -23,7 +23,10 @@
 import java.util.Map;
 
 import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.databinding.DataBinding;
+import org.apache.tuscany.spi.databinding.ExceptionHandler;
 import org.apache.tuscany.spi.databinding.Mediator;
+import org.apache.tuscany.spi.databinding.TransformationException;
 import org.apache.tuscany.spi.model.DataType;
 import org.apache.tuscany.spi.model.Operation;
 import org.apache.tuscany.spi.wire.Interceptor;
@@ -32,7 +35,7 @@
 
 /**
  * An interceptor to transform data accross databindings on the wire
- *
+ * 
  * @version $Rev$ $Date$
  */
 public class DataBindingInteceptor implements Interceptor {
@@ -65,35 +68,90 @@
     /**
      * @see org.apache.tuscany.spi.wire.Interceptor#invoke(org.apache.tuscany.spi.wire.Message)
      */
+    /* (non-Javadoc)
+     * @see org.apache.tuscany.spi.wire.Interceptor#invoke(org.apache.tuscany.spi.wire.Message)
+     */
     public Message invoke(Message msg) {
         Object input = transform(msg.getBody(), sourceOperation.getInputType(), targetOperation.getInputType());
         msg.setBody(input);
         Message resultMsg = next.invoke(msg);
         Object result = resultMsg.getBody();
-        // FIXME: How to deal with faults?
+        if (sourceOperation.isNonBlocking()) {
+            // Not to reset the message body
+            return resultMsg;
+        }
+
+        // FIXME: Should we fix the Operation model so that getOutputType
+        // returns DataType<DataType<T>>?
+        DataType<DataType> targetType =
+            new DataType<DataType>(DataBinding.IDL_OUTPUT, Object.class, targetOperation.getOutputType());
+
+        targetType.setOperation(targetOperation.getOutputType().getOperation());
+        DataType<DataType> sourceType =
+            new DataType<DataType>(DataBinding.IDL_OUTPUT, Object.class, sourceOperation.getOutputType());
+        sourceType.setOperation(sourceOperation.getOutputType().getOperation());
+
         if (resultMsg.isFault()) {
-            // We need to figure out what fault type it is and then transform it back the source fault type
+
+            // FIXME: We need to figure out what fault type it is and then transform it
+            // back the source fault type
             // throw new InvocationRuntimeException((Throwable) result);
-            return resultMsg;
-        } else {
-            if (sourceOperation.isNonBlocking()) {
-                // Not to reset the message body
-                return resultMsg;
+
+            if ((result instanceof Exception) && !(result instanceof RuntimeException)) {
+                // FIXME: How to match fault data to a fault type for the
+                // operation?
+                DataType targetDataType = null;
+                for (DataType exType : targetOperation.getFaultTypes()) {
+                    if (((Class)exType.getPhysical()).isInstance(result)) {
+                        targetDataType = exType;
+                        break;
+                    }
+                }
+                
+                if (targetDataType == null) {
+                    // Not a business exception
+                    return resultMsg;
+                }
+
+                DataType targetFaultType = getFaultType(targetDataType);
+                if (targetFaultType == null) {
+                    throw new TransformationException("Target fault type cannot be resolved");
+                }
+
+                // FIXME: How to match a source fault type to a target fault
+                // type?
+                DataType sourceDataType = null;
+                DataType sourceFaultType = null;
+                for (DataType exType : sourceOperation.getFaultTypes()) {
+                    DataType faultType = getFaultType(exType);
+                    // Match by the QName (XSD element) of the fault type
+                    if (faultType != null && targetFaultType.getLogical().equals(faultType.getLogical())) {
+                        sourceDataType = exType;
+                        sourceFaultType = faultType;
+                        break;
+                    }
+                }
+
+                if (sourceFaultType == null) {
+                    throw new TransformationException("No matching source fault type is found");
+                }
+
+                Object newResult =
+                    transformException(result, targetDataType, sourceDataType, targetFaultType, sourceFaultType);
+                if (newResult != result) {
+                    resultMsg.setBodyWithFault(newResult);
+                }
             }
-            // FIXME: Should we fix the Operation model so that getOutputType returns DataType<DataType<T>>?
-            DataType<DataType> targetType =
-                new DataType<DataType>("idl:output", Object.class, targetOperation.getOutputType());
-
-            targetType.setOperation(targetOperation.getOutputType().getOperation());
-            DataType<DataType> sourceType =
-                new DataType<DataType>("idl:output", Object.class, sourceOperation.getOutputType());
-            sourceType.setOperation(sourceOperation.getOutputType().getOperation());
+
+        } else {
+            assert !(result instanceof Throwable) : "Expected messages that are not throwable " + result;
 
             Object newResult = transform(result, targetType, sourceType);
             if (newResult != result) {
                 resultMsg.setBody(newResult);
             }
         }
+
         return resultMsg;
     }
 
@@ -104,6 +162,46 @@
         Map<Class<?>, Object> metadata = new HashMap<Class<?>, Object>();
         metadata.put(CompositeComponent.class, compositeComponent);
         return mediator.mediate(source, sourceType, targetType, metadata);
+    }
+
+    private DataType getFaultType(DataType exceptionType) {
+        DataBinding targetDataBinding =
+            mediator.getDataBindingRegistry().getDataBinding(exceptionType.getDataBinding());
+        if (targetDataBinding == null) {
+            return null;
+        }
+        ExceptionHandler targetHandler = targetDataBinding.getExceptionHandler();
+        if (targetHandler == null) {
+            return null;
+        }
+        return targetHandler.getFaultType((Class)exceptionType.getPhysical());
+    }
+
+    /**
+     * @param source The source exception
+     * @param sourceExType The data type for the source exception
+     * @param targetExType The data type for the target exception
+     * @param sourceType The fault type for the source
+     * @param targetType The fault type for the target
+     * @return
+     */
+    private Object transformException(Object source,
+                                      DataType sourceExType,
+                                      DataType targetExType,
+                                      DataType sourceType,
+                                      DataType targetType) {
+        if (sourceType == targetType || (sourceType != null && sourceType.equals(targetType))) {
+            return source;
+        }
+        Map<Class<?>, Object> metadata = new HashMap<Class<?>, Object>();
+        metadata.put(CompositeComponent.class, compositeComponent);
+
+        DataType<DataType<?>> eSourceDataType =
+            new DataType<DataType<?>>("idl:fault", sourceExType.getPhysical(), sourceType);
+        DataType<DataType<?>> eTargetDataType =
+            new DataType<DataType<?>>("idl:fault", targetExType.getPhysical(), targetType);
+
+        return mediator.mediate(source, eSourceDataType, eTargetDataType, metadata);
     }
 
     /**

Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java?view=auto&rev=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java Tue Feb 20 05:32:51 2007
@@ -0,0 +1,126 @@
+/*
+ * 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.core.databinding.impl;
+
+import org.apache.tuscany.spi.annotation.Autowire;
+import org.apache.tuscany.spi.databinding.DataBinding;
+import org.apache.tuscany.spi.databinding.ExceptionHandler;
+import org.apache.tuscany.spi.databinding.Mediator;
+import org.apache.tuscany.spi.databinding.PullTransformer;
+import org.apache.tuscany.spi.databinding.TransformationContext;
+import org.apache.tuscany.spi.databinding.Transformer;
+import org.apache.tuscany.spi.databinding.extension.TransformerExtension;
+import org.apache.tuscany.spi.model.DataType;
+import org.osoa.sca.annotations.Service;
+
+/**
+ * This is a special transformer to transform the exception from one IDL to the
+ * other one
+ */
+@Service(Transformer.class)
+public class Exception2ExceptionTransformer extends TransformerExtension<Object[], Object[]> implements
+    PullTransformer<Exception, Exception> {
+
+    protected Mediator mediator;
+
+    public Exception2ExceptionTransformer() {
+        super();
+    }
+
+    @Override
+    public String getSourceDataBinding() {
+        return DataBinding.IDL_FAULT;
+    }
+
+    @Override
+    public String getTargetDataBinding() {
+        return DataBinding.IDL_FAULT;
+    }
+
+    /**
+     * @param mediator the mediator to set
+     */
+    @Autowire
+    public void setMediator(Mediator mediator) {
+        this.mediator = mediator;
+    }
+
+    /**
+     * @see org.apache.tuscany.spi.databinding.extension.TransformerExtension#getSourceType()
+     */
+    @Override
+    protected Class getSourceType() {
+        return Exception.class;
+    }
+
+    /**
+     * @see org.apache.tuscany.spi.databinding.extension.TransformerExtension#getTargetType()
+     */
+    @Override
+    protected Class getTargetType() {
+        return Exception.class;
+    }
+
+    /**
+     * @see org.apache.tuscany.spi.databinding.Transformer#getWeight()
+     */
+    public int getWeight() {
+        return 10000;
+    }
+
+    @SuppressWarnings("unchecked")
+    public Exception transform(Exception source, TransformationContext context) {
+        DataType<DataType> sourceType = context.getSourceDataType();
+
+        DataType<DataType> targetType = context.getTargetDataType();
+
+        ExceptionHandler exceptionHandler = getExceptionHandler(sourceType);
+        if (exceptionHandler == null) {
+            return source;
+        }
+
+        Object sourceFaultInfo = exceptionHandler.getFaultInfo(source);
+        Object targetFaultInfo =
+            mediator.mediate(sourceFaultInfo, sourceType.getLogical(), targetType.getLogical(), context.getMetadata());
+
+        ExceptionHandler targetHandler = getExceptionHandler(targetType);
+
+        if (targetHandler != null) {
+            Exception targetException =
+                targetHandler.createException(targetType, source.getMessage(), targetFaultInfo, source.getCause());
+            return targetException;
+        }
+
+        // FIXME
+        return source;
+
+    }
+
+    private ExceptionHandler getExceptionHandler(DataType<DataType> targetType) {
+        DataType targetFaultType = (DataType)targetType.getLogical();
+        DataBinding targetDataBinding =
+            mediator.getDataBindingRegistry().getDataBinding(targetFaultType.getDataBinding());
+        if (targetDataBinding == null) {
+            return null;
+        }
+        ExceptionHandler targetHandler = targetDataBinding.getExceptionHandler();
+        return targetHandler;
+    }
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/impl/Exception2ExceptionTransformer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/resources/org/apache/tuscany/core/databinding.scdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/resources/org/apache/tuscany/core/databinding.scdl?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/resources/org/apache/tuscany/core/databinding.scdl (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/resources/org/apache/tuscany/core/databinding.scdl Tue Feb 20 05:32:51 2007
@@ -67,6 +67,11 @@
         <system:implementation.system class="org.apache.tuscany.core.databinding.impl.Input2InputTransformer" />
     </component>
 
+    <component name="transformer.Exception2ExceptionTransformer">
+        <system:implementation.system class="org.apache.tuscany.core.databinding.impl.Exception2ExceptionTransformer" />
+    </component>
+
+
     <component name="transformer.Output2OutputTransformer">
         <system:implementation.system class="org.apache.tuscany.core.databinding.impl.Output2OutputTransformer" />
     </component>

Modified: incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/impl/StockTraderSDOImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/impl/StockTraderSDOImpl.java?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/impl/StockTraderSDOImpl.java (original)
+++ incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/impl/StockTraderSDOImpl.java Tue Feb 20 05:32:51 2007
@@ -72,7 +72,8 @@
         StockOffer stockOffer = ScatesttoolFactory.INSTANCE.createStockOffer();
         // set up for a InvalidSymbolSDOException
         stockOffer.setName("");
-        stockOffer.setSymbol("");
+        stockOffer.setSymbol("IBM0");
+
         stockOffer.setPrice(11.0F); // offer to buy at max $100.00
         exchangeJaxb.stockQuoteOffer(stockOffer);
             

Modified: incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/sdohandgen/StockExceptionTest.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/sdohandgen/StockExceptionTest.java?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/sdohandgen/StockExceptionTest.java (original)
+++ incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/java/org/apache/tuscany/sca/test/exceptions/sdohandgen/StockExceptionTest.java Tue Feb 20 05:32:51 2007
@@ -29,6 +29,8 @@
 
 import stockexceptiontestservice.scatesttool.StockOffer;
 
+import org.apache.tuscany.sca.test.exceptions.sdohandgen.MarketClosedSDOException;
+
 /*
  * StockExceptionTest java interface
  */
@@ -42,7 +44,7 @@
      * 
      * @param param0
      */
-    stockexceptiontestservice.scatesttool.StockOffer stockQuoteOffer(StockOffer param0)
+    StockOffer stockQuoteOffer(StockOffer param0)
         throws java.rmi.RemoteException, InvalidSymbolSDOException, MarketClosedSDOException;
 
 }

Modified: incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/resources/wsdl/StockExceptionTest.wsdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/resources/wsdl/StockExceptionTest.wsdl?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/resources/wsdl/StockExceptionTest.wsdl (original)
+++ incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/main/resources/wsdl/StockExceptionTest.wsdl Tue Feb 20 05:32:51 2007
@@ -57,7 +57,29 @@
                 </complexType>
             </element>
             
-            <element name="hoursTillopen"   type="xsd:int" />
+            <element name="MarketClosedFault"   type="xsd:int" />
+           
+           <!-- Fault wrapper -->
+           <element name="detail" type="tns:detail" minOccurs="0"/>
+            
+           <element name="ServiceWrapperFault">
+                <complexType>
+                    <sequence>
+                        <element name="faultreason"  type="xsd:string" />
+                        <element name="detail" minOccurs="1" type="tns:detail" />
+                    </sequence>
+                </complexType>
+            </element>
+            xs:complexType name="detail">
+
+
+			<complexType name="detail">
+			<sequence>
+			  <any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="1" /> 
+			  </sequence>
+			  <anyAttribute namespace="##other" processContents="lax" /> 
+			</complexType>
+     
         </schema>
     </wsdl:types>
 
@@ -75,7 +97,7 @@
     </wsdl:message>
 
     <wsdl:message name="MarketClosedFault">
-        <wsdl:part element="tns:hoursTillopen" name="fault" />
+        <wsdl:part element="tns:MarketClosedFault" name="fault" />
     </wsdl:message>
 
 

Modified: incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/test/java/org/apache/tuscany/sca/test/exceptions/IntraCompositeTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/test/java/org/apache/tuscany/sca/test/exceptions/IntraCompositeTestCase.java?view=diff&rev=509554&r1=509553&r2=509554
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/test/java/org/apache/tuscany/sca/test/exceptions/IntraCompositeTestCase.java (original)
+++ incubator/tuscany/branches/sca-java-integration/testing/sca/itest/exceptionXbindingTest/src/test/java/org/apache/tuscany/sca/test/exceptions/IntraCompositeTestCase.java Tue Feb 20 05:32:51 2007
@@ -43,13 +43,13 @@
         } catch (Exception e) {
 
             e.printStackTrace();
-            throw new AssertionFailedError(e + "");
+            fail(e + "");
 
         }
 
     }
 
-    public void badtestInvalidSymbolSDOException() {
+    public void _testInvalidSymbolSDOException() {
         try {
             stockTrader.testInvalidSymbolSDOException();
             throw new AssertionFailedError("Expected InvalidSymbolSDOException");
@@ -58,13 +58,13 @@
 
             assertNotNull(isf);
             StockOffer sp = isf.getOffer();
-            assertEquals(99.00F, sp.getPrice());
-            assertEquals("IBM", sp.getSymbol());
+            assertEquals(11.00F, sp.getPrice());
+            assertEquals("IBM0", sp.getSymbol());
 
         } catch (Exception e) {
             e.printStackTrace();
-            throw new AssertionFailedError("Expected InvalidSymbolSDOException" + e);
-
+            fail("Expected InvalidSymbolSDOException" + e);
+            
         }
 
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org