![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper Maven / Gradle / Ivy
The newest version!
/*
* 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.jaxb;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import javax.xml.namespace.QName;
import org.apache.tuscany.sca.common.java.collection.LRUCache;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.databinding.SimpleTypeMapper;
import org.apache.tuscany.sca.databinding.TransformationContext;
import org.apache.tuscany.sca.databinding.TransformationException;
import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Interface;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.interfacedef.util.WrapperInfo;
import org.apache.tuscany.sca.interfacedef.util.XMLType;
/**
*
* @version $Rev: 1296845 $ $Date: 2012-03-04 09:48:55 -0800 (Sun, 04 Mar 2012) $
*/
public final class JAXBContextHelper {
private final JAXBContextCache cache;
private final static SimpleTypeMapper SIMPLE_TYPE_MAPPER = new SimpleTypeMapperImpl();
public JAXBContextHelper(ExtensionPointRegistry registry) {
cache = new JAXBContextCache(registry);
}
public static JAXBContextHelper getInstance(ExtensionPointRegistry registry) {
UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class);
return utilityExtensionPoint.getUtility(JAXBContextHelper.class);
}
/**
* Create a JAXBContext for a given class
* @param cls
* @return
* @throws JAXBException
*/
public JAXBContext createJAXBContext(Class> cls) throws JAXBException {
return cache.getJAXBContext(cls);
}
public JAXBContext createJAXBContext(TransformationContext tContext, boolean source) throws JAXBException {
if (tContext == null)
throw new TransformationException("JAXB context is not set for the transformation.");
// TODO: [rfeng] Need to figure out what's the best granularity to create the JAXBContext
// per interface, operation or parameter
Operation op = source ? tContext.getSourceOperation() : tContext.getTargetOperation();
if (op != null) {
synchronized (op) {
JAXBContext context = op.getInputType().getMetaData(JAXBContext.class);
if (context == null) {
context = createJAXBContext(getDataTypes(op, true));
op.getInputType().setMetaData(JAXBContext.class, context);
}
return context;
}
}
// For property transformation, the operation can be null
DataType> dataType = source ? tContext.getSourceDataType() : tContext.getTargetDataType();
return createJAXBContext(dataType);
}
private static Class>[] getSeeAlso(Class> interfaze) {
if (interfaze == null) {
return null;
}
XmlSeeAlso seeAlso = interfaze.getAnnotation(XmlSeeAlso.class);
if (seeAlso == null) {
return null;
} else {
return seeAlso.value();
}
}
public JAXBContext createJAXBContext(DataType dataType) throws JAXBException {
return createJAXBContext(findClasses(dataType));
}
public Unmarshaller getUnmarshaller(JAXBContext context) throws JAXBException {
return cache.getUnmarshaller(context);
}
public void releaseJAXBUnmarshaller(JAXBContext context, Unmarshaller unmarshaller) {
cache.releaseJAXBUnmarshaller(context, unmarshaller);
}
public Marshaller getMarshaller(JAXBContext context) throws JAXBException {
return cache.getMarshaller(context);
}
public void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
cache.releaseJAXBMarshaller(context, marshaller);
}
@SuppressWarnings("unchecked")
public static Object createJAXBElement(JAXBContext context, DataType dataType, Object value) {
Class> type = dataType == null ? value.getClass() : dataType.getPhysical();
type = getValueType(type);
QName name = JAXBDataBinding.ROOT_ELEMENT;
if (context != null) {
Object logical = dataType == null ? null : dataType.getLogical();
if (logical instanceof XMLType) {
XMLType xmlType = (XMLType)logical;
if (xmlType.isElement()) {
name = xmlType.getElementName();
} else {
/**
* Set the declared type to Object.class so that xsi:type
* will be produced
*/
type = Object.class;
}
} else {
type = Object.class;
}
}
JAXBIntrospector introspector = context.createJAXBIntrospector();
Object element = null;
if (value != null && introspector.isElement(value)) {
// NOTE: [rfeng] We cannot wrap an element in a JAXBElement
element = value;
}
if (element == null) {
// For local elements, we still have to produce xsi:type
element = new JAXBElement(name, Object.class, value);
}
return element;
}
@SuppressWarnings("unchecked")
public static Object createReturnValue(JAXBContext context, DataType dataType, Object value) {
Class> cls = getJavaType(dataType);
if (cls == JAXBElement.class) {
return createJAXBElement(context, dataType, value);
} else {
if (value instanceof JAXBElement) {
Object returnValue = ((JAXBElement)value).getValue();
if (returnValue == null) {
// TUSCANY-3530
// something went wrong in the transformation that
// generated the JAXBElement. Have seen this when trying
// to convert a value to a simple type with an incompatible
// value.
throw new TransformationException("Null returned when trying to convert value to: " + cls.getName());
}
return returnValue;
} else {
return value;
}
}
}
/**
* Create a JAXContext for an array of classes
* @param classes
* @return
* @throws JAXBException
*/
public JAXBContext createJAXBContext(Class>[] classes) throws JAXBException {
return cache.getJAXBContext(classes);
}
public JAXBContext createJAXBContext(Set> classes) throws JAXBException {
return cache.getJAXBContext(classes);
}
/**
* Create a JAXBContext for a given java interface
* @param intf
* @return
* @throws JAXBException
*/
public JAXBContext createJAXBContext(Interface intf, boolean useWrapper) throws JAXBException {
synchronized (cache) {
LRUCache
© 2015 - 2025 Weber Informatics LLC | Privacy Policy