From 5559ef5edbf8d3616f7a4b497b2a459b0ee4082b Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:07:07 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835122 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/databinding/sdo/SDOExceptionHandler.java | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-1.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOExceptionHandler.java (limited to 'sca-java-1.x/branches/sca-java-1.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOExceptionHandler.java') diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOExceptionHandler.java b/sca-java-1.x/branches/sca-java-1.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOExceptionHandler.java new file mode 100644 index 0000000000..5c23240b19 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOExceptionHandler.java @@ -0,0 +1,124 @@ +/* + * 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.sdo; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.ExceptionHandler; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.impl.HelperProvider; + +/** + * SDO implementation of ExceptionHandler + * + * @version $Rev$ $Date$ + */ +public class SDOExceptionHandler implements ExceptionHandler { + private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + + // FIXME: Need a way to pass in the HelperContext + private HelperContext helperContext = HelperProvider.getDefaultContext(); + + /** + * + */ + public Exception createException(DataType exceptionType, String message, Object faultInfo, Throwable cause) { + Class exceptionClass = exceptionType.getPhysical(); + DataType faultBeanType = exceptionType.getLogical(); + Class faultBeanClass = faultBeanType.getPhysical(); + try { + Constructor constructor = + exceptionClass.getConstructor(new Class[] {String.class, faultBeanClass, Throwable.class}); + return (Exception)constructor.newInstance(new Object[] {message, faultInfo, cause}); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + public Object getFaultInfo(Exception exception) { + if (exception == null) { + return null; + } + try { + Method method = exception.getClass().getMethod("getFaultInfo", EMPTY_CLASS_ARRAY); + return method.invoke(exception, (Object[])null); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + public DataType getFaultType(DataType exceptionDataType) { + Class exceptionType = exceptionDataType.getPhysical(); + Class faultBeanClass = null; + try { + Method method = exceptionType.getMethod("getFaultInfo", EMPTY_CLASS_ARRAY); + faultBeanClass = method.getReturnType(); + } catch (NoSuchMethodException e) { + faultBeanClass = null; + } + if (faultBeanClass == null) { + return null; + } + + QName faultElement = null; + try { + Field field = exceptionType.getField("FAULT_ELEMENT"); + faultElement = (QName)field.get(null); + } catch (NoSuchFieldException e) { + // Fall back to type inspection + Type type = helperContext.getTypeHelper().getType(faultBeanClass); + if (type != null && !type.isDataType()) { + String ns = type.getURI(); + String name = helperContext.getXSDHelper().getLocalName(type); + faultElement = new QName(ns, name); + } + } catch (Throwable e) { + // Ignore + } + if (faultElement == null) { + return null; + } + DataType faultType = + new DataTypeImpl(SDODataBinding.NAME, faultBeanClass, new XMLType(faultElement, null)); + return faultType; + + } + +} -- cgit v1.2.3