Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.databinding.impl;
 
 import static org.apache.tuscany.sca.databinding.DataBinding.IDL_FAULT;
 import static org.apache.tuscany.sca.databinding.DataBinding.IDL_OUTPUT;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
 
Default Mediator implementation

Version:
$Rev: 967109 $ $Date: 2010-07-23 15:30:46 +0100 (Fri, 23 Jul 2010) $
Tuscany.spi.extension.asclient:
 
 public class MediatorImpl implements Mediator {
     private static final String TARGET_OPERATION = "target.operation";
     private static final String SOURCE_OPERATION = "source.operation";
     private ExtensionPointRegistry registry;
 
     MediatorImpl(DataBindingExtensionPoint dataBindingsTransformerExtensionPoint transformers) {
         this. = dataBindings;
         this. = transformers;
     }
 
     public MediatorImpl(ExtensionPointRegistry registry) {
         this. = registry;
         this. = registry.getExtensionPoint(DataBindingExtensionPoint.class);
         this. = registry.getExtensionPoint(TransformerExtensionPoint.class);
         UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class);
         this. = utilities.getUtility(InterfaceContractMapper.class);
         this. = utilities.getUtility(FaultExceptionMapper.class);
 
     }
 
     @SuppressWarnings("unchecked")
     public Object mediate(Object sourceDataType sourceDataTypeDataType targetDataTypeMap<StringObjectmetadata) {
         if (sourceDataType == null || sourceDataType.getDataBinding() == null) {
             if (source != null) {
                 Operation operation = (Operation)metadata.get();
                 sourceDataType = .introspectType(sourceoperation);
             }
         }
         if (sourceDataType == null || targetDataType == null) {
             return source;
        } else if (sourceDataType.equals(targetDataType)) {
            return source;
        }
        List<Transformerpath = getTransformerChain(sourceDataTypetargetDataType);
        Object result = source;
        int size = path.size();
        int i = 0;
        while (i < size) {
            Transformer transformer = path.get(i);
            TransformationContext context =
                createTransformationContext(sourceDataTypetargetDataTypesizeitransformermetadata);
            // the source and target type
            if (transformer instanceof PullTransformer) {
                // For intermediate node, set data type to null
                result = ((PullTransformer)transformer).transform(resultcontext);
            } else if (transformer instanceof PushTransformer) {
                DataPipeTransformer dataPipeFactory = (i < size - 1) ? (DataPipeTransformer)path.get(++i) : null;
                DataPipe dataPipe = dataPipeFactory == null ? null : dataPipeFactory.newInstance();
                ((PushTransformer)transformer).transform(resultdataPipe.getSink(), context);
                result = dataPipe.getResult();
            }
            i++;
        }
        return result;
    }
                                                              DataType targetDataType,
                                                              int size,
                                                              int index,
                                                              Transformer transformer,
                                                              Map<StringObjectmetadata) {
        DataType sourceType =
            (index == 0) ? sourceDataType : new DataTypeImpl<Object>(transformer.getSourceDataBinding(), Object.class,
                                                                     sourceDataType.getLogical());
        DataType targetType =
            (index == size - 1) ? targetDataType : new DataTypeImpl<Object>(transformer.getTargetDataBinding(),
                                                                            Object.classtargetDataType.getLogical());
         Map<StringObjectcopy = new HashMap<StringObject>();
        if (metadata != null) {
            copy.putAll(metadata);
        }
        copy.put(ExtensionPointRegistry.class.getName(), );
        TransformationContext context = new TransformationContextImpl(sourceTypetargetTypecopy);
        return context;
    }
    @SuppressWarnings("unchecked")
    public void mediate(Object source,
                        Object target,
                        DataType sourceDataType,
                        DataType targetDataType,
                        Map<StringObjectmetadata) {
        if (source == null) {
            // Shortcut for null value
            return;
        }
        if (sourceDataType == null || sourceDataType.getDataBinding() == null) {
            Operation operation = (Operation)metadata.get();
            sourceDataType = .introspectType(sourceoperation);
        }
        if (sourceDataType == null) {
            return;
        } else if (sourceDataType.equals(targetDataType)) {
            return;
        }
        List<Transformerpath = getTransformerChain(sourceDataTypetargetDataType);
        Object result = source;
        int size = path.size();
        for (int i = 0; i < sizei++) {
            Transformer transformer = path.get(i);
            TransformationContext context =
                createTransformationContext(sourceDataTypetargetDataTypesizeitransformermetadata);
            if (transformer instanceof PullTransformer) {
                result = ((PullTransformer)transformer).transform(resultcontext);
            } else if (transformer instanceof PushTransformer) {
                DataPipeTransformer dataPipeFactory = (i < size - 1) ? (DataPipeTransformer)path.get(++i) : null;
                DataPipe dataPipe = dataPipeFactory == null ? null : dataPipeFactory.newInstance();
                Object sink = dataPipe != null ? dataPipe.getSink() : target;
                ((PushTransformer)transformer).transform(resultsinkcontext);
                result = (dataPipe != null) ? dataPipe.getResult() : null;
            }
        }
    }
    private List<TransformergetTransformerChain(DataType sourceDataTypeDataType targetDataType) {
        String sourceId = sourceDataType.getDataBinding();
        String targetId = targetDataType.getDataBinding();
        List<Transformerpath = .getTransformerChain(sourceIdtargetId);
        if (path == null) {
            TransformationException ex =
                new TransformationException("No path found for the transformation: " + sourceId + "->" + targetId);
            ex.setSourceDataBinding(sourceId);
            ex.setTargetDataBinding(targetId);
            throw ex;
        }
        return path;
    }
        return ;
    }
        return ;
    }

    
Find the fault data type behind the exception data type

Parameters:
exceptionType The exception data type
Returns:
The fault data type
    private DataType getFaultType(DataType exceptionType) {
        return exceptionType == null ? null : (DataType)exceptionType.getLogical();
    }

    

Parameters:
qn1
qn2
    private boolean matches(QName qn1QName qn2) {
        if (qn1 == qn2) {
            return true;
        }
        if (qn1 == null || qn2 == null) {
            return false;
        }
        String ns1 = qn1.getNamespaceURI();
        String ns2 = qn2.getNamespaceURI();
        String e1 = qn1.getLocalPart();
        String e2 = qn2.getLocalPart();
        if (e1.equals(e2) && (ns1.equals(ns2) || ns1.equals(ns2 + "/") || ns2.equals(ns1 + "/"))) {
            // Tolerating the trailing / which is required by JAX-WS java package --> xml ns mapping
            return true;
        }
        return false;
    }

    

Parameters:
source The source exception
sourceExType The data type for the source exception
targetExType The data type for the target exception
sourceType The fault type for the source
targetType The fault type for the target
Returns:
    private Object transformException(Object source,
                                      DataType sourceExType,
                                      DataType targetExType,
                                      DataType sourceType,
                                      DataType targetType,
                                      Map<StringObjectmetadata) {
        if (sourceType == targetType || (sourceType != null && sourceType.equals(targetType))) {
            return source;
        }
        DataType<DataTypeeSourceDataType =
            new DataTypeImpl<DataType>(sourceExType.getPhysical(), sourceType);
        DataType<DataTypeeTargetDataType =
            new DataTypeImpl<DataType>(targetExType.getPhysical(), targetType);
        return mediate(sourceeSourceDataTypeeTargetDataTypemetadata);
    }
    //
    // Assumes we're going from target->source, knowing that we're throwing BACK an exception, rather than the more
    // obvious source->target
    //
    public Object mediateFault(Object result,
                               Operation sourceOperation,
                               Operation targetOperation,
                               Map<StringObjectmetadata) {
        // FIXME: How to match fault data to a fault type for the
        // operation?
        // If the result is from an InvocationTargetException look at
        // the actual cause.
        if (result instanceof InvocationTargetException) {
            result = ((InvocationTargetException)result).getCause();
        }
        
        DataType targetDataType = findFaultDataType(targetOperationresult);
        DataType targetFaultType = getFaultType(targetDataType);
        if (targetFaultType == null) {
            // No matching fault type, it's a system exception
            Throwable cause = (Throwable)result;
            throw new ServiceRuntimeException(cause);
        }
        // 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 && typesMatch(targetFaultType.getLogical(), faultType.getLogical())) {
                sourceDataType = exType;
                sourceFaultType = faultType;
                break;
            }
        }
        if (sourceFaultType == null) {
            // No matching fault type, it's a system exception
            Throwable cause = (Throwable)result;
            throw new ServiceRuntimeException(cause);
        }
        Map<StringObjectcontext = new HashMap<StringObject>();
        if (metadata != null) {
            context.putAll(metadata);
        }
        if (targetOperation != null) {
            context.put(targetOperation);
        }
        if (sourceOperation != null) {
            context.put(sourceOperation);
        }
        Object newResult =
            transformException(resulttargetDataTypesourceDataTypetargetFaultTypesourceFaultTypecontext);
        return newResult;
    }

    
Look up the fault data type that matches the fault or exception instance

Parameters:
operation The operation
faultOrException The fault or exception
Returns:
The matching fault data type
    private DataType findFaultDataType(Operation operationObject 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 firstObject second) {
        if (first.equals(second)) {
            return true;
        }
        if (first instanceof XMLType && second instanceof XMLType) {
            XMLType t1 = (XMLType)first;
            XMLType t2 = (XMLType)second;
            // TUSCANY-2113, we should compare element names only
            return matches(t1.getElementName(), t2.getElementName());
        }
        return false;
    }

    
Assumes we're going from target-to-source, knowing that we're sending BACK an output response, rather than the more obvious source-to-target.

Parameters:
output
sourceOperation
targetOperation
Returns:
    public Object mediateOutput(Object output,
                                Operation sourceOperation,
                                Operation targetOperation,
                                Map<StringObjectmetadata) {
        // Create a data type to represent the ouput produced by the target operation
        DataType<DataTypetargetType =
            new DataTypeImpl<DataType>(Object.classtargetOperation.getOutputType());
        // Create a data type to represent the ouput expected by the source operation
        DataType<DataTypesourceType =
            new DataTypeImpl<DataType>(Object.classsourceOperation.getOutputType());
        if (sourceType == targetType || (sourceType != null && sourceType.equals(targetType))) {
            return output;
        }
        Map<StringObjectcontext = new HashMap<StringObject>();
        if (metadata != null) {
            context.putAll(metadata);
        }
        if (targetOperation != null) {
            context.put(targetOperation);
        }
        if (sourceOperation != null) {
            context.put(sourceOperation);
        }
        return mediate(outputtargetTypesourceTypecontext);
    }
    public Object mediateInput(Object input,
                               Operation sourceOperation,
                               Operation targetOperation,
                               Map<StringObjectmetadata) {
        // Get the data type to represent the input passed in by the source operation
        DataType sourceType = sourceOperation.getInputType();
        // Get the data type to represent the input expected by the target operation
        DataType targetType = targetOperation.getInputType();
        if (sourceType == targetType || (sourceType != null && sourceType.equals(targetType))) {
            return input;
        }
        Map<StringObjectcontext = new HashMap<StringObject>();
        if (metadata != null) {
            context.putAll(metadata);
        }
        if (sourceOperation != null) {
            context.put(sourceOperation);
        }
        if (targetOperation != null) {
            context.put(targetOperation);
        }
        return mediate(inputsourceTypetargetTypecontext);
    }
        return new TransformationContextImpl();
    }
                                                             DataType targetDataType,
                                                             Map<StringObjectmetadata) {
        return new TransformationContextImpl(sourceDataTypetargetDataTypemetadata);
    }
    public Object copy(Object dataDataType dataType) {
        return copy(datadataTypedataTypenullnull);
    }
    public Object copy(Object dataDataType sourceDataTypeDataType targetDataType) {
        return copy(datasourceDataTypetargetDataTypenullnull);
    }

    
Copy data using the specified databinding.

Parameters:
data input data
sourceDataType
Returns:
a copy of the data
    public Object copy(Object data,
                       DataType sourceDataType,
                       DataType targetDataType,
                       Operation sourceOperation,
                       Operation targetOperation) {
        if (data == null) {
            return null;
        }
        Class<?> clazz = data.getClass();
        if (String.class == clazz || clazz.isPrimitive()
            || Number.class.isAssignableFrom(clazz)
            || Boolean.class.isAssignableFrom(clazz)
            || Character.class.isAssignableFrom(clazz)
            || Byte.class.isAssignableFrom(clazz)
            || URI.class == clazz
            || UUID.class == clazz
            || QName.class == clazz) {
            // Immutable classes
            return data;
        }
        DataBinding javaBeansDataBinding = .getDataBinding(.);
        // FIXME: The JAXB databinding is hard-coded here
        DataBinding jaxbDataBinding = .getDataBinding("javax.xml.bind.JAXBElement");
        DataBinding dataBinding = .getDataBinding(sourceDataType.getDataBinding());
        // If no databinding was specified, introspect the given arg to
        // determine its databinding
        if (dataBinding == null) {
            if (!"java:array".equals(sourceDataType.getDataBinding())) {
                sourceDataType = .introspectType(datasourceOperation);
                if (sourceDataType != null) {
                    String db = sourceDataType.getDataBinding();
                    dataBinding = .getDataBinding(db);
                    if (dataBinding == null && db != null) {
                        return data;
                    }
                }
            }
            if (dataBinding == null) {
                // Default to the JavaBean databinding
                dataBinding = .getDataBinding(.);
            }
        }
        // Use the JAXB databinding to copy non-Serializable data
        if (dataBinding == javaBeansDataBinding) {
            // If the input data is an array containing non Serializable elements
            // use JAXB
            clazz = data.getClass();
            if (clazz.isArray()) {
                if (Array.getLength(data) != 0) {
                    Object element = Array.get(data, 0);
                    if (element != null && !(element instanceof Serializable)) {
                        dataBinding = jaxbDataBinding;
                    }
                }
            } else {
                // If the input data is not Serializable use JAXB
                if (!((data instanceof Serializable) || (data instanceof Cloneable))) {
                    dataBinding = jaxbDataBinding;
                }
            }
        }
        if (dataBinding == null) {
            return data;
        }
        return dataBinding.copy(datasourceDataTypetargetDataTypesourceOperationtargetOperation);
    }

    
Copy an array of data objects passed to an operation

Parameters:
data array of objects to copy
Returns:
the copy
    public Object copyInput(Object inputOperation operation) {
        return copyInput(inputoperationoperation);
    }
    public Object copyInput(Object inputOperation sourceOperationOperation targetOperation) {
        if (input == null) {
            return null;
        }
        Object[] data = (input instanceof Object[]) ? (Object[])input : new Object[] {input};
        List<DataTypeinputTypes = sourceOperation.getInputType().getLogical();
        List<DataTypeinputTypesTarget = targetOperation == null ? null : targetOperation.getInputType().getLogical();
        Object[] copy = new Object[data.length];
        Map<ObjectObjectmap = new IdentityHashMap<ObjectObject>();
        for (int i = 0, size = inputTypes.size(); i < sizei++) {
            Object arg = data[i];
            if (arg == null) {
                copy[i] = null;
            } else {
                Object copiedArg = map.get(arg);
                if (copiedArg != null) {
                    copy[i] = copiedArg;
                } else {
                    copiedArg =
                        copy(arg,
                             inputTypes.get(i),
                             inputTypesTarget == null ? null : inputTypesTarget.get(i),
                             sourceOperation,
                             targetOperation);
                    map.put(argcopiedArg);
                    copy[i] = copiedArg;
                }
            }
        }
        return copy;
    }
    public Object copyOutput(Object dataOperation operation) {
        return copyOutput(dataoperationoperation);
    }
    
    public Object copyOutput(Object dataOperation sourceOperationOperation targetOperation) {
        return copy(datatargetOperation.getOutputType(), sourceOperation.getOutputType(), targetOperationsourceOperation);
    }
    public Object copyFault(Object faultOperation operation) {
        return copyFault(faultoperationoperation);
    }
    
    public Object copyFault(Object faultOperation sourceOperationOperation targetOperation) {
        if ( == null) {
            return fault;
        }
        List<DataTypefts = 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<DataTypeexType = findFaultDataType(targetOperationfault);
                DataType faultType = getFaultType(exType);
                Object faultInfo = .getFaultInfo(exfaultType.getPhysical(), targetOperation);
                DataType targetExType = findSourceFaultDataType(sourceOperationexType);
                DataType targetFaultType = getFaultType(targetExType);
                faultInfo = copy(faultInfofaultTypetargetFaultTypetargetOperationsourceOperation);
                fault = .wrapFaultInfo(targetExTypeex.getMessage(), faultInfoex.getCause(), sourceOperation);
                return fault;
            }
        }
        return fault;
    }
    
    
Lookup a fault data type from the source operation which matches the target fault data type

Parameters:
sourceOperation The source operation
targetExceptionType The target fault data type
Returns:
The matching source target fault type
    private DataType findSourceFaultDataType(Operation sourceOperationDataType targetExceptionType) {
        boolean remotable = sourceOperation.getInterface().isRemotable();
        DataType targetFaultType = getFaultType(targetExceptionType);
        for (DataType dt : sourceOperation.getFaultTypes()) {
            DataType sourceFaultType = getFaultType(dt);
            if (.isCompatible(targetFaultTypesourceFaultTyperemotable)) {
                return dt;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X