Search in sources :

Example 1 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JavaScriptContainer method execute.

@SuppressWarnings("unchecked")
public void execute() throws ToolException {
    if (hasInfoOption()) {
        return;
    }
    buildToolContext();
    validate(context);
    WSDLConstants.WSDLVersion version = getWSDLVersion();
    String wsdlURL = (String) context.get(ToolConstants.CFG_WSDLURL);
    List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
    if (serviceList == null) {
        serviceList = new ArrayList<>();
        PluginLoader pluginLoader = PluginLoader.newInstance();
        // for JavaScript generation, we always use JAX-WS.
        FrontEndProfile frontend = pluginLoader.getFrontEndProfile("jaxws");
        // Build the ServiceModel from the WSDLModel
        if (version == WSDLConstants.WSDLVersion.WSDL11) {
            AbstractWSDLBuilder builder = frontend.getWSDLBuilder();
            builder.setContext(context);
            builder.setBus(getBus());
            context.put(Bus.class, getBus());
            builder.build(URIParserUtil.getAbsoluteURI(wsdlURL));
            builder.customize();
            Definition definition = builder.getWSDLModel();
            context.put(Definition.class, definition);
            builder.validate(definition);
            WSDLServiceBuilder serviceBuilder = new WSDLServiceBuilder(getBus());
            String serviceName = (String) context.get(ToolConstants.CFG_SERVICENAME);
            if (serviceName != null) {
                List<ServiceInfo> services = serviceBuilder.buildServices(definition, getServiceQName(definition));
                serviceList.addAll(services);
            } else if (definition.getServices().size() > 0) {
                serviceList = serviceBuilder.buildServices(definition);
            } else {
                serviceList = serviceBuilder.buildMockServices(definition);
            }
        } else {
            // TODO: wsdl2.0 support
            throw new ToolException("Only WSDL 1.1 supported");
        }
    }
    if (serviceList.isEmpty()) {
        throw new ToolException("Did not find any services in WSDL");
    }
    Map<String, InterfaceInfo> interfaces = new LinkedHashMap<>();
    ServiceInfo service0 = serviceList.get(0);
    SchemaCollection schemaCollection = service0.getXmlSchemaCollection();
    context.put(ToolConstants.XML_SCHEMA_COLLECTION, schemaCollection);
    context.put(ToolConstants.PORTTYPE_MAP, interfaces);
    context.put(ClassCollector.class, new ClassCollector());
    WSDLToJavaScriptProcessor processor = new WSDLToJavaScriptProcessor();
    for (ServiceInfo service : serviceList) {
        context.put(ServiceInfo.class, service);
        validate(service);
        processor.setEnvironment(context);
        processor.process();
    }
}
Also used : AbstractWSDLBuilder(org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder) ClassCollector(org.apache.cxf.tools.util.ClassCollector) Definition(javax.wsdl.Definition) FrontEndProfile(org.apache.cxf.tools.wsdlto.core.FrontEndProfile) LinkedHashMap(java.util.LinkedHashMap) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) WSDLConstants(org.apache.cxf.wsdl.WSDLConstants) WSDLServiceBuilder(org.apache.cxf.wsdl11.WSDLServiceBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ToolException(org.apache.cxf.tools.common.ToolException) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) PluginLoader(org.apache.cxf.tools.wsdlto.core.PluginLoader)

Example 2 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class WSDLToJavaContainer method processClientJar.

private void processClientJar(ToolContext context) {
    ClassCollector oldCollector = context.get(ClassCollector.class);
    ClassCollector newCollector = new ClassCollector();
    String oldClassDir = (String) context.get(ToolConstants.CFG_CLASSDIR);
    File tmpDir = FileUtils.createTmpDir();
    context.put(ToolConstants.CFG_CLASSDIR, tmpDir.getAbsolutePath());
    newCollector.setTypesClassNames(oldCollector.getTypesClassNames());
    newCollector.setSeiClassNames(oldCollector.getSeiClassNames());
    newCollector.setExceptionClassNames(oldCollector.getExceptionClassNames());
    newCollector.setServiceClassNames(oldCollector.getServiceClassNames());
    context.put(ClassCollector.class, newCollector);
    new ClassUtils().compile(context);
    generateLocalWSDL(context);
    File clientJarFile = new File((String) context.get(ToolConstants.CFG_OUTPUTDIR), (String) context.get(ToolConstants.CFG_CLIENT_JAR));
    try (JarOutputStream jarout = new JarOutputStream(Files.newOutputStream(clientJarFile.toPath()), new Manifest())) {
        createClientJar(tmpDir, jarout);
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "FAILED_TO_CREAT_CLIENTJAR", e);
        Message msg = new Message("FAILED_TO_CREAT_CLIENTJAR", LOG);
        throw new ToolException(msg, e);
    }
    context.put(ToolConstants.CFG_CLASSDIR, oldClassDir);
    context.put(ClassCollector.class, oldCollector);
}
Also used : Message(org.apache.cxf.common.i18n.Message) ClassCollector(org.apache.cxf.tools.util.ClassCollector) JarOutputStream(java.util.jar.JarOutputStream) ToolException(org.apache.cxf.tools.common.ToolException) Manifest(java.util.jar.Manifest) File(java.io.File) ClassUtils(org.apache.cxf.tools.common.ClassUtils) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 3 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class ImplGenerator method mapClassName.

private String mapClassName(String packageName, String name, ToolContext context) {
    ClassCollector collector = context.get(ClassCollector.class);
    int count = 0;
    String checkName = name;
    while (collector.containImplClass(packageName, checkName)) {
        checkName = name + (++count);
    }
    collector.addImplClassName(packageName, checkName, packageName + "." + checkName);
    return checkName;
}
Also used : ClassCollector(org.apache.cxf.tools.util.ClassCollector)

Example 4 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JAXRSContainerTest method testOnewayMethod.

@Test
public void testOnewayMethod() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);
    final String onewayMethod = "deleteRepository";
    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");
    context.put(WadlToolConstants.CFG_ONEWAY, onewayMethod);
    container.setContext(context);
    container.execute();
    assertNotNull(output.list());
    ClassCollector cc = context.get(ClassCollector.class);
    assertEquals(1, cc.getServiceClassNames().size());
    try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
        final Class<?> generatedClass = loader.loadClass(cc.getServiceClassNames().values().iterator().next());
        Method m = generatedClass.getMethod(onewayMethod, String.class);
        assertNotNull(m.getAnnotation(Oneway.class));
    }
}
Also used : Oneway(org.apache.cxf.jaxrs.ext.Oneway) ClassCollector(org.apache.cxf.tools.util.ClassCollector) URLClassLoader(java.net.URLClassLoader) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JAXRSContainerTest method testThrows.

@Test
public void testThrows() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);
    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, Boolean.TRUE);
    context.put(WadlToolConstants.CFG_INTERFACE, Boolean.TRUE);
    context.put(WadlToolConstants.CFG_IMPL, Boolean.TRUE);
    context.put(WadlToolConstants.CFG_CREATE_JAVA_DOCS, Boolean.TRUE);
    container.setContext(context);
    container.execute();
    assertNotNull(output.list());
    List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(2, javaFiles.size());
    for (File f : javaFiles) {
        if (!f.getName().endsWith("Impl.java")) {
            assertTrue(Files.readAllLines(f.toPath()).contains("     * @throws IOException if something going wrong"));
        }
    }
    ClassCollector cc = context.get(ClassCollector.class);
    assertEquals(2, cc.getServiceClassNames().size());
    final Map<String, Class<?>[]> methods = new HashMap<>();
    methods.put("listRepositories", new Class<?>[] {});
    methods.put("createRepository", new Class<?>[] { java.io.IOException.class });
    methods.put("deleteRepository", new Class<?>[] { javax.ws.rs.NotFoundException.class, java.io.IOException.class });
    methods.put("postThename", new Class<?>[] { java.io.IOException.class, java.lang.NoSuchMethodException.class });
    try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
        for (String className : cc.getServiceClassNames().values()) {
            final Class<?> generatedClass = loader.loadClass(className);
            for (Map.Entry<String, Class<?>[]> entry : methods.entrySet()) {
                Method m;
                try {
                    m = generatedClass.getMethod(entry.getKey(), String.class);
                } catch (NoSuchMethodException e) {
                    m = generatedClass.getMethod(entry.getKey(), String.class, String.class);
                }
                assertArrayEquals(entry.getValue(), m.getExceptionTypes());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ClassCollector(org.apache.cxf.tools.util.ClassCollector) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ClassCollector (org.apache.cxf.tools.util.ClassCollector)20 ToolException (org.apache.cxf.tools.common.ToolException)6 File (java.io.File)5 IOException (java.io.IOException)4 Message (org.apache.cxf.common.i18n.Message)4 ArrayList (java.util.ArrayList)3 QName (javax.xml.namespace.QName)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 HashSet (java.util.HashSet)2 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 ClassUtils (org.apache.cxf.tools.common.ClassUtils)2 ToolContext (org.apache.cxf.tools.common.ToolContext)2 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 JavaPort (org.apache.cxf.tools.common.model.JavaPort)2 JavaServiceClass (org.apache.cxf.tools.common.model.JavaServiceClass)2