Search in sources :

Example 1 with JavaExceptionClass

use of org.apache.cxf.tools.common.model.JavaExceptionClass in project cxf by apache.

the class FaultGenerator method generate.

public void generate(ToolContext penv) throws ToolException {
    this.env = penv;
    if (passthrough()) {
        return;
    }
    Map<QName, JavaModel> map = CastUtils.cast((Map<?, ?>) penv.get(WSDLToJavaProcessor.MODEL_MAP));
    for (JavaModel javaModel : map.values()) {
        Map<String, JavaExceptionClass> exceptionClasses = javaModel.getExceptionClasses();
        for (Entry<String, JavaExceptionClass> entry : exceptionClasses.entrySet()) {
            JavaExceptionClass expClz = entry.getValue();
            clearAttributes();
            if (penv.containsKey(ToolConstants.CFG_FAULT_SERIAL_VERSION_UID)) {
                String faultSerialVersionUID = penv.get(ToolConstants.CFG_FAULT_SERIAL_VERSION_UID).toString();
                setAttributes("faultSerialVersionUID", faultSerialVersionUID);
                if ("FQCN".equalsIgnoreCase(faultSerialVersionUID)) {
                    setAttributes("suid", generateHashSUID(expClz.getFullClassName()));
                } else if ("TIMESTAMP".equalsIgnoreCase(faultSerialVersionUID)) {
                    setAttributes("suid", generateTimestampSUID());
                } else if ("NONE".equalsIgnoreCase(faultSerialVersionUID)) {
                    // nothing
                    setAttributes("suid", "");
                } else {
                    // do a quick Parse to make sure it looks like a Long
                    try {
                        Long.parseLong(faultSerialVersionUID);
                    } catch (NumberFormatException nfe) {
                        throw new ToolException(nfe);
                    }
                    setAttributes("suid", faultSerialVersionUID);
                }
            } else {
                setAttributes("suid", "");
            }
            setAttributes("expClass", expClz);
            String exceptionSuperclass;
            if (penv.containsKey(ToolConstants.CFG_EXCEPTION_SUPER)) {
                exceptionSuperclass = penv.get(ToolConstants.CFG_EXCEPTION_SUPER).toString();
            } else {
                exceptionSuperclass = "java.lang.Exception";
            }
            String simpleName = exceptionSuperclass.indexOf('.') == -1 ? exceptionSuperclass : exceptionSuperclass.substring(exceptionSuperclass.lastIndexOf('.') + 1);
            String exceptionSuperclassString = simpleName;
            for (JavaField jf : expClz.getFields()) {
                String jfClassName = jf.getClassName();
                if (jfClassName.substring(jfClassName.lastIndexOf(".") + 1).equals(simpleName)) {
                    exceptionSuperclassString = exceptionSuperclass;
                }
                setAttributes("paraName", ProcessorUtil.mangleNameToVariableName(jf.getName()));
            }
            ClassCollector collector = penv.get(ClassCollector.class);
            for (String pkg : collector.getTypesPackages()) {
                if (collector.containTypesClass(pkg, simpleName)) {
                    exceptionSuperclassString = exceptionSuperclass;
                }
            }
            if (expClz.getName().equals(exceptionSuperclassString)) {
                exceptionSuperclassString = exceptionSuperclass;
            }
            setAttributes("exceptionSuperclass", exceptionSuperclassString);
            if (!exceptionSuperclass.startsWith("java.lang.") && !exceptionSuperclassString.equals(exceptionSuperclass)) {
                expClz.addImport(exceptionSuperclass);
            }
            setCommonAttributes();
            doWrite(FAULT_TEMPLATE, parseOutputName(expClz.getPackageName(), expClz.getName()));
        }
    }
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) QName(javax.xml.namespace.QName) JavaModel(org.apache.cxf.tools.common.model.JavaModel) ClassCollector(org.apache.cxf.tools.util.ClassCollector) JavaExceptionClass(org.apache.cxf.tools.common.model.JavaExceptionClass) ToolException(org.apache.cxf.tools.common.ToolException)

Example 2 with JavaExceptionClass

use of org.apache.cxf.tools.common.model.JavaExceptionClass in project cxf by apache.

the class FaultProcessor method processFault.

private void processFault(JavaMethod method, FaultInfo faultMessage) throws ToolException {
    JavaModel model = method.getInterface().getJavaModel();
    String name = NameUtil.mangleNameToClassName(faultMessage.getName().getLocalPart());
    String namespace = faultMessage.getName().getNamespaceURI();
    String packageName = ProcessorUtil.parsePackageName(namespace, context.mapPackageName(namespace));
    if (namespace.equals(method.getInterface().getNamespace())) {
        packageName = method.getInterface().getPackageName();
    }
    JAXWSBinding jaxwsBinding = faultMessage.getExtensor(JAXWSBinding.class);
    if (jaxwsBinding != null) {
        if (jaxwsBinding.getPackage() != null) {
            packageName = jaxwsBinding.getPackage();
        }
        if (jaxwsBinding.getJaxwsClass() != null && jaxwsBinding.getJaxwsClass().getClassName() != null) {
            name = jaxwsBinding.getJaxwsClass().getClassName();
            if (name.contains(".")) {
                packageName = name.substring(0, name.lastIndexOf('.'));
                name = name.substring(name.lastIndexOf('.') + 1);
            }
        }
    }
    while (isNameCollision(packageName, name)) {
        name = name + "_Exception";
    }
    String fullClassName = packageName + "." + name;
    collector.addExceptionClassName(packageName, name, fullClassName);
    boolean samePackage = method.getInterface().getPackageName().equals(packageName);
    method.addException(new JavaException(faultMessage.getName().getLocalPart(), samePackage ? name : fullClassName, namespace));
    List<MessagePartInfo> faultParts = faultMessage.getMessageParts();
    JavaExceptionClass expClass = new JavaExceptionClass(model);
    expClass.setName(name);
    expClass.setNamespace(namespace);
    expClass.setPackageName(packageName);
    for (MessagePartInfo part : faultParts) {
        String fName = null;
        String fNamespace = null;
        if (part.getElementQName() != null) {
            fNamespace = part.getElementQName().getNamespaceURI();
            // fNamespace = part.getConcreteName().getNamespaceURI();
            fName = part.getConcreteName().getLocalPart();
        } else {
            fNamespace = part.getTypeQName().getNamespaceURI();
            fName = part.getConcreteName().getLocalPart();
        }
        if (StringUtils.isEmpty(fNamespace)) {
            fNamespace = namespace;
        }
        String fType = ProcessorUtil.getType(part, context, false);
        // REVISIT - custom JAXB package names
        String fPackageName = method.getInterface().getPackageName();
        JavaField fField = new JavaField(fName, fType, fNamespace);
        fField.setQName(ProcessorUtil.getElementName(part));
        if (!method.getInterface().getPackageName().equals(fPackageName)) {
            fField.setClassName(ProcessorUtil.getFullClzName(part, context, false));
        }
        if (!fType.equals(ProcessorUtil.resolvePartType(part))) {
            fField.setClassName(ProcessorUtil.getType(part, context, true));
        }
        expClass.addField(fField);
    }
    model.addExceptionClass(packageName + "." + name, expClass);
    expClass.setClassJavaDoc(faultMessage.getDocumentation());
}
Also used : JavaException(org.apache.cxf.tools.common.model.JavaException) JavaField(org.apache.cxf.tools.common.model.JavaField) JavaModel(org.apache.cxf.tools.common.model.JavaModel) JavaExceptionClass(org.apache.cxf.tools.common.model.JavaExceptionClass) JAXWSBinding(org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Aggregations

JavaExceptionClass (org.apache.cxf.tools.common.model.JavaExceptionClass)2 JavaField (org.apache.cxf.tools.common.model.JavaField)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 QName (javax.xml.namespace.QName)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 ToolException (org.apache.cxf.tools.common.ToolException)1 JavaException (org.apache.cxf.tools.common.model.JavaException)1 ClassCollector (org.apache.cxf.tools.util.ClassCollector)1 JAXWSBinding (org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding)1