Search in sources :

Example 1 with JType

use of org.apache.cxf.common.jaxb.JAXBUtils.JType in project cxf by apache.

the class TypeClassInitializer method begin.

@Override
public void begin(MessagePartInfo part) {
    OperationInfo op = part.getMessageInfo().getOperation();
    if (!isFault && !allowWrapperOperations && op.isUnwrappedCapable() && !op.isUnwrapped()) {
        return;
    }
    QName name;
    if (part.isElement()) {
        name = part.getElementQName();
    } else {
        name = part.getTypeQName();
    }
    Mapping mapping = model.get(name);
    // String clsName = null;
    JType jType = null;
    if (mapping != null) {
        jType = mapping.getType().getTypeClass();
    }
    if (jType == null) {
        TypeAndAnnotation typeAndAnnotation = model.getJavaType(part.getTypeQName());
        if (typeAndAnnotation != null) {
            jType = typeAndAnnotation.getTypeClass();
        }
    }
    if (jType == null && part.isElement() && part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getSchemaTypeName() == null) {
        // anonymous inner thing.....
        UnwrappedOperationInfo oInfo = (UnwrappedOperationInfo) op;
        op = oInfo.getWrappedOperation();
        if (part.getMessageInfo() == oInfo.getInput()) {
            mapping = model.get(op.getInput().getFirstMessagePart().getElementQName());
        } else {
            mapping = model.get(op.getOutput().getFirstMessagePart().getElementQName());
        }
        if (mapping != null) {
            jType = mapping.getType().getTypeClass();
            try {
                Iterator<JType> i = jType.classes();
                while (i.hasNext()) {
                    JType jt = i.next();
                    if (jt.name().equalsIgnoreCase(part.getElementQName().getLocalPart())) {
                        jType = jt;
                    }
                }
            } catch (Throwable t) {
            // ignore, JType is a type that doesn't have a classes method
            }
        }
    }
    if (jType == null) {
        throw new ServiceConstructionException(new Message("NO_JAXB_CLASSMapping", LOG, name));
    }
    Class<?> cls;
    try {
        int arrayCount = 0;
        JType rootType = jType;
        while (rootType.isArray()) {
            rootType = rootType.elementType();
            arrayCount++;
        }
        if (arrayCount == 0 && part.isElement() && part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMaxOccurs() > 1) {
            arrayCount = 1;
        }
        cls = getClassByName(rootType);
        // an array object on the way.
        if (arrayCount > 0) {
            int[] dimensions = new int[arrayCount];
            while (arrayCount > 0) {
                arrayCount--;
                dimensions[arrayCount] = 0;
            }
            Object emptyArray = Array.newInstance(cls, dimensions);
            cls = emptyArray.getClass();
        }
    } catch (ClassNotFoundException e) {
        throw new ServiceConstructionException(e);
    }
    part.setTypeClass(cls);
    if (isFault) {
        // need to create an Exception class for this
        try {
            part.getMessageInfo().setProperty(Class.class.getName(), createFaultClass(cls));
        } catch (Throwable t) {
        // ignore - probably no asm
        }
    }
    super.begin(part);
}
Also used : UnwrappedOperationInfo(org.apache.cxf.service.model.UnwrappedOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) UnwrappedOperationInfo(org.apache.cxf.service.model.UnwrappedOperationInfo) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Mapping(org.apache.cxf.common.jaxb.JAXBUtils.Mapping) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) TypeAndAnnotation(org.apache.cxf.common.jaxb.JAXBUtils.TypeAndAnnotation) JType(org.apache.cxf.common.jaxb.JAXBUtils.JType)

Aggregations

QName (javax.xml.namespace.QName)1 Message (org.apache.cxf.common.i18n.Message)1 JType (org.apache.cxf.common.jaxb.JAXBUtils.JType)1 Mapping (org.apache.cxf.common.jaxb.JAXBUtils.Mapping)1 TypeAndAnnotation (org.apache.cxf.common.jaxb.JAXBUtils.TypeAndAnnotation)1 ServiceConstructionException (org.apache.cxf.service.factory.ServiceConstructionException)1 OperationInfo (org.apache.cxf.service.model.OperationInfo)1 UnwrappedOperationInfo (org.apache.cxf.service.model.UnwrappedOperationInfo)1 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)1