Search in sources :

Example 1 with JAnnotation

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

the class WrapperBeanFieldAnnotator method annotate.

public void annotate(final JavaAnnotatable field) {
    final JavaField jField;
    if (field instanceof JavaField) {
        jField = (JavaField) field;
    } else {
        throw new RuntimeException("WrapperBeanFiledAnnotator expect JavaField as input");
    }
    String rawName = jField.getRawName();
    boolean hasEl = false;
    for (Annotation ann : jField.getJaxbAnnotaions()) {
        if (ann instanceof XmlMimeType) {
            JAnnotation mimeAnno = new JAnnotation(XmlMimeType.class);
            mimeAnno.addElement(new JAnnotationElement("value", ((XmlMimeType) ann).value()));
            jField.addAnnotation(mimeAnno);
        } else if (ann instanceof XmlJavaTypeAdapter) {
            JAnnotation jaxbAnn = new JAnnotation(XmlJavaTypeAdapter.class);
            jaxbAnn.addElement(new JAnnotationElement("value", ((XmlJavaTypeAdapter) ann).value()));
            jaxbAnn.addElement(new JAnnotationElement("type", ((XmlJavaTypeAdapter) ann).type()));
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlAttachmentRef) {
            JAnnotation jaxbAnn = new JAnnotation(XmlAttachmentRef.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlList) {
            JAnnotation jaxbAnn = new JAnnotation(XmlList.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlElement) {
            hasEl = true;
            XmlElement el = (XmlElement) ann;
            JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
            xmlElementAnnotation.addElement(new JAnnotationElement("name", el.name()));
            if (!StringUtils.isEmpty(el.namespace())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("namespace", el.namespace()));
            }
            if (el.nillable()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("nillable", el.nillable(), true));
            }
            if (el.required()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("required", el.required(), true));
            }
            if (!StringUtils.isEmpty(el.defaultValue())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("defaultValue", el.defaultValue()));
            }
            jField.addAnnotation(xmlElementAnnotation);
        }
    }
    if (!hasEl) {
        JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
        xmlElementAnnotation.addElement(new JAnnotationElement("name", rawName));
        if (!StringUtils.isEmpty(jField.getTargetNamespace())) {
            xmlElementAnnotation.addElement(new JAnnotationElement("namespace", jField.getTargetNamespace()));
        }
        jField.addAnnotation(xmlElementAnnotation);
    }
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) XmlMimeType(javax.xml.bind.annotation.XmlMimeType) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElement(javax.xml.bind.annotation.XmlElement) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Example 2 with JAnnotation

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

the class WrapperBeanAnnotatorTest method testAnnotate.

@Test
public void testAnnotate() {
    String pkgName = "org.apache.cxf.tools.fortest.withannotation.doc.jaxws";
    WrapperBeanClass clz = new WrapperBeanClass();
    clz.setFullClassName(pkgName + ".SayHi");
    clz.setElementName(new QName("http://doc.withannotation.fortest.tools.cxf.apache.org/", "sayHi"));
    clz.annotate(new WrapperBeanAnnotator());
    List<JAnnotation> annotations = clz.getAnnotations();
    String expectedNamespace = "http://doc.withannotation.fortest.tools.cxf.apache.org/";
    JAnnotation rootElementAnnotation = annotations.get(0);
    assertEquals("@XmlRootElement(name = \"sayHi\", " + "namespace = \"" + expectedNamespace + "\")", rootElementAnnotation.toString());
    JAnnotation xmlTypeAnnotation = annotations.get(2);
    assertEquals("@XmlType(name = \"sayHi\", " + "namespace = \"" + expectedNamespace + "\")", xmlTypeAnnotation.toString());
    JAnnotation accessorTypeAnnotation = annotations.get(1);
    assertEquals("@XmlAccessorType(XmlAccessType.FIELD)", accessorTypeAnnotation.toString());
    WrapperBeanClass resWrapperClass = new WrapperBeanClass();
    resWrapperClass.setFullClassName(pkgName + ".SayHiResponse");
    resWrapperClass.setElementName(new QName(expectedNamespace, "sayHiResponse"));
    resWrapperClass.annotate(new WrapperBeanAnnotator());
    annotations = resWrapperClass.getAnnotations();
    rootElementAnnotation = annotations.get(0);
    assertEquals("@XmlRootElement(name = \"sayHiResponse\", " + "namespace = \"" + expectedNamespace + "\")", rootElementAnnotation.toString());
    accessorTypeAnnotation = annotations.get(1);
    assertEquals("@XmlAccessorType(XmlAccessType.FIELD)", accessorTypeAnnotation.toString());
    xmlTypeAnnotation = annotations.get(2);
    assertEquals("@XmlType(name = \"sayHiResponse\", " + "namespace = \"" + expectedNamespace + "\")", xmlTypeAnnotation.toString());
}
Also used : JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) QName(javax.xml.namespace.QName) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) Test(org.junit.Test)

Example 3 with JAnnotation

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

the class HandlerConfigGenerator method generate.

public void generate(ToolContext penv) throws ToolException {
    this.env = penv;
    if (passthrough()) {
        return;
    }
    Element e = this.intf.getHandlerChains();
    List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(e, ToolConstants.HANDLER_CHAINS_URI, ToolConstants.HANDLER_CHAIN);
    if (!elemList.isEmpty()) {
        String fName = ProcessorUtil.getHandlerConfigFileName(this.intf.getName());
        handlerChainAnnotation = new JAnnotation(HandlerChain.class);
        handlerChainAnnotation.addElement(new JAnnotationElement("name", HANDLER_CHAIN_NAME));
        handlerChainAnnotation.addElement(new JAnnotationElement("file", fName + ".xml"));
        generateHandlerChainFile(e, parseOutputName(this.intf.getPackageName(), fName, ".xml"));
    }
}
Also used : HandlerChain(javax.jws.HandlerChain) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) Element(org.w3c.dom.Element) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement)

Example 4 with JAnnotation

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

the class SEIGenerator 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, JavaInterface> interfaces = javaModel.getInterfaces();
        if (interfaces.isEmpty()) {
            ServiceInfo serviceInfo = env.get(ServiceInfo.class);
            String wsdl = serviceInfo.getDescription().getBaseURI();
            Message msg = new Message("CAN_NOT_GEN_SEI", LOG, wsdl);
            if (penv.isVerbose()) {
                System.out.println(msg.toString());
            }
            continue;
        }
        for (JavaInterface intf : interfaces.values()) {
            if (hasHandlerConfig(intf)) {
                HandlerConfigGenerator handlerGen = new HandlerConfigGenerator();
                // REVISIT: find a better way to handle Handler gen, should not
                // pass JavaInterface around.
                handlerGen.setJavaInterface(intf);
                handlerGen.generate(getEnvironment());
                JAnnotation annot = handlerGen.getHandlerAnnotation();
                if (handlerGen.getHandlerAnnotation() != null) {
                    boolean existHandlerAnno = false;
                    for (JAnnotation jann : intf.getAnnotations()) {
                        if (jann.getType() == HandlerChain.class) {
                            existHandlerAnno = true;
                        }
                    }
                    if (!existHandlerAnno) {
                        intf.addAnnotation(annot);
                        intf.addImport("javax.jws.HandlerChain");
                    }
                }
            }
            if (penv.containsKey(ToolConstants.RUNTIME_DATABINDING_CLASS)) {
                JAnnotation ann = new JAnnotation(DataBinding.class);
                JAnnotationElement el = new JAnnotationElement(null, penv.get(ToolConstants.RUNTIME_DATABINDING_CLASS), true);
                ann.addElement(el);
                intf.addAnnotation(ann);
            }
            clearAttributes();
            setAttributes("intf", intf);
            String seiSc = "";
            for (String s : intf.getSuperInterfaces()) {
                if (!seiSc.isEmpty()) {
                    seiSc += ", ";
                } else {
                    seiSc = "extends ";
                }
                seiSc += s;
            }
            if (!StringUtils.isEmpty(seiSc)) {
                seiSc += " ";
            }
            setAttributes("seiSuperinterfaceString", seiSc);
            setCommonAttributes();
            doWrite(SEI_TEMPLATE, parseOutputName(intf.getPackageName(), intf.getName()));
        }
    }
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) Message(org.apache.cxf.common.i18n.Message) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) QName(javax.xml.namespace.QName) JavaModel(org.apache.cxf.tools.common.model.JavaModel) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement)

Example 5 with JAnnotation

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

the class ServiceGenerator 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()) {
        ClassCollector collector = penv.get(ClassCollector.class);
        Map<String, JavaServiceClass> serviceClasses = javaModel.getServiceClasses();
        if (serviceClasses.isEmpty()) {
            ServiceInfo serviceInfo = env.get(ServiceInfo.class);
            String wsdl = serviceInfo.getDescription().getBaseURI();
            Message msg = new Message("CAN_NOT_GEN_SERVICE", LOG, wsdl);
            if (penv.isVerbose()) {
                System.out.println(msg.toString());
            }
            return;
        }
        for (JavaServiceClass js : serviceClasses.values()) {
            if (js.getHandlerChains() != null) {
                HandlerConfigGenerator handlerGen = new HandlerConfigGenerator();
                handlerGen.setJavaInterface(js);
                handlerGen.generate(getEnvironment());
                JAnnotation annot = handlerGen.getHandlerAnnotation();
                if (handlerGen.getHandlerAnnotation() != null) {
                    boolean existHandlerAnno = false;
                    for (JAnnotation jann : js.getAnnotations()) {
                        if (jann.getType() == HandlerChain.class) {
                            existHandlerAnno = true;
                        }
                    }
                    if (!existHandlerAnno) {
                        js.addAnnotation(annot);
                        js.addImport("javax.jws.HandlerChain");
                    }
                }
            }
            Set<String> portNames = new HashSet<>();
            for (JavaPort port : js.getPorts()) {
                if (!port.getPackageName().equals(js.getPackageName()) && !port.getInterfaceClass().equals(js.getName())) {
                    js.addImport(port.getFullClassName());
                }
                portNames.add(port.getInterfaceClass());
            }
            if ("cxf".equals(env.get(ToolConstants.CFG_FRONTEND))) {
                js.addImport(BindingProvider.class.getName());
                js.addImport(Client.class.getName());
            }
            String url = (String) env.get(ToolConstants.CFG_WSDLURL);
            String location = (String) env.get(ToolConstants.CFG_WSDLLOCATION);
            if (location == null) {
                location = url;
            }
            String serviceSuperclass = (String) env.get("service.superclass");
            String simpleServiceName = serviceSuperclass.substring(serviceSuperclass.lastIndexOf('.') + 1);
            for (String s : collector.getGeneratedFileInfo()) {
                if (s.equals(js.getPackageName() + "." + simpleServiceName)) {
                    simpleServiceName = serviceSuperclass;
                    break;
                }
            }
            clearAttributes();
            boolean useGetResource = false;
            try {
                new URL(location);
            } catch (MalformedURLException e) {
                useGetResource = true;
            }
            setAttributes("cxfPortClassnames", portNames.toArray(new String[0]));
            setAttributes("service", js);
            setAttributes("wsdlLocation", location);
            setAttributes("useGetResource", useGetResource);
            setAttributes("serviceSuperclass", simpleServiceName);
            if (!simpleServiceName.equals(serviceSuperclass)) {
                js.addImport(serviceSuperclass);
            }
            setAttributes("wsdlUrl", url);
            setCommonAttributes();
            String target = (String) env.get("service.target");
            setAttributes("serviceTarget", target);
            if ("jaxws22".equals(target)) {
                setAttributes("jaxws22", true);
            }
            doWrite(SERVICE_TEMPLATE, parseOutputName(js.getPackageName(), js.getName()));
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Message(org.apache.cxf.common.i18n.Message) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) JavaServiceClass(org.apache.cxf.tools.common.model.JavaServiceClass) QName(javax.xml.namespace.QName) ClassCollector(org.apache.cxf.tools.util.ClassCollector) BindingProvider(javax.xml.ws.BindingProvider) URL(java.net.URL) JavaPort(org.apache.cxf.tools.common.model.JavaPort) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) JavaModel(org.apache.cxf.tools.common.model.JavaModel) Client(org.apache.cxf.endpoint.Client) HashSet(java.util.HashSet)

Aggregations

JAnnotation (org.apache.cxf.tools.common.model.JAnnotation)27 JAnnotationElement (org.apache.cxf.tools.common.model.JAnnotationElement)20 JavaMethod (org.apache.cxf.tools.common.model.JavaMethod)14 Test (org.junit.Test)7 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)6 JavaParameter (org.apache.cxf.tools.common.model.JavaParameter)5 ArrayList (java.util.ArrayList)4 QName (javax.xml.namespace.QName)3 Message (org.apache.cxf.common.i18n.Message)3 JavaField (org.apache.cxf.tools.common.model.JavaField)3 XmlList (javax.xml.bind.annotation.XmlList)2 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 JavaReturn (org.apache.cxf.tools.common.model.JavaReturn)2 WrapperBeanClass (org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass)2 WebMethodAnnotator (org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.annotator.WebMethodAnnotator)2 Annotation (java.lang.annotation.Annotation)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1