Search in sources :

Example 1 with WSDLToJava

use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.

the class WSDL2JavaMojo method generate.

@Override
protected Bus generate(GenericWsdlOption genericWsdlOption, Bus bus, Set<URI> classPath) throws MojoExecutionException {
    WsdlOption wsdlOption = (WsdlOption) genericWsdlOption;
    File outputDirFile = wsdlOption.getOutputDir();
    outputDirFile.mkdirs();
    URI basedir = project.getBasedir().toURI();
    URI wsdlURI = wsdlOption.getWsdlURI(basedir);
    File doneFile = getDoneFile(basedir, wsdlURI, "java");
    if (!shouldRun(wsdlOption, doneFile, wsdlURI)) {
        return bus;
    }
    doneFile.delete();
    try {
        File file = new File(getBaseFileURI(wsdlURI));
        if (file.exists()) {
            buildContext.removeMessages(file);
        }
    } catch (Throwable t) {
    // ignore
    }
    if (wsdlOption.getDependencies() != null) {
        for (URI dependency : wsdlOption.getDependencyURIs(project.getBasedir().toURI())) {
            URI baseDependency = getBaseFileURI(dependency);
            if ("file".equals(baseDependency.getScheme())) {
                buildContext.removeMessages(new File(baseDependency));
            }
        }
    }
    List<String> list = wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog().isDebugEnabled());
    if (encoding != null) {
        list.add(0, "-encoding");
        list.add(1, encoding);
    }
    String[] args = list.toArray(new String[0]);
    getLog().debug("Calling wsdl2java with args: " + Arrays.toString(args));
    if (!"false".equals(fork)) {
        Set<URI> artifactsPath = new LinkedHashSet<>();
        for (Artifact a : pluginArtifacts) {
            File file = a.getFile();
            if (file == null) {
                throw new MojoExecutionException("Unable to find (null) file for artifact " + a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion());
            }
            artifactsPath.add(file.toURI());
        }
        addPluginArtifact(artifactsPath);
        artifactsPath.addAll(classPath);
        runForked(artifactsPath, WSDLToJava.class.getName(), args);
    } else {
        if (bus == null) {
            bus = BusFactory.newInstance().createBus();
            BusFactory.setThreadDefaultBus(bus);
        }
        try {
            ToolContext ctx = new ToolContext();
            final List<File> files = new ArrayList<>();
            final List<File> errorfiles = new ArrayList<>();
            ctx.put(OutputStreamCreator.class, new OutputStreamCreator() {

                public OutputStream createOutputStream(File file) throws IOException {
                    files.add(file);
                    return buildContext.newFileOutputStream(file);
                }
            });
            ctx.setErrorListener(new MavenToolErrorListener(errorfiles));
            new WSDLToJava(args).run(ctx);
            List<File> oldFiles = CastUtils.cast((List<?>) buildContext.getValue("cxf.file.list." + doneFile.getName()));
            if (oldFiles != null) {
                for (File f : oldFiles) {
                    if (!files.contains(f)) {
                        f.delete();
                        buildContext.refresh(f);
                    }
                }
            }
            buildContext.setValue("cxf.file.list." + doneFile.getName(), files);
        } catch (Throwable e) {
            buildContext.setValue("cxf.file.list." + doneFile.getName(), null);
            getLog().debug(e);
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    try {
        createMarkerFile(wsdlOption, doneFile, wsdlURI);
        buildContext.refresh(doneFile);
    } catch (Throwable e) {
        getLog().warn("Could not create marker file " + doneFile.getAbsolutePath());
        getLog().debug(e);
        throw new MojoExecutionException("Failed to create marker file " + doneFile.getAbsolutePath());
    }
    if (project != null && getGeneratedSourceRoot() != null && getGeneratedSourceRoot().exists()) {
        project.addCompileSourceRoot(getGeneratedSourceRoot().getAbsolutePath());
        buildContext.refresh(getGeneratedSourceRoot().getAbsoluteFile());
    }
    if (project != null && getGeneratedTestRoot() != null && getGeneratedTestRoot().exists()) {
        project.addTestCompileSourceRoot(getGeneratedTestRoot().getAbsolutePath());
        buildContext.refresh(getGeneratedTestRoot().getAbsoluteFile());
    }
    return bus;
}
Also used : GenericWsdlOption(org.apache.cxf.maven_plugin.GenericWsdlOption) LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ToolContext(org.apache.cxf.tools.common.ToolContext) IOException(java.io.IOException) URI(java.net.URI) Artifact(org.apache.maven.artifact.Artifact) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) File(java.io.File)

Example 2 with WSDLToJava

use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.

the class CodeGenBugTest method testExcludeNSWithoutPackageName.

@Test
public void testExcludeNSWithoutPackageName() throws Exception {
    String[] args = new String[] { "-d", output.getCanonicalPath(), "-nexclude", "http://apache.org/test/types", getLocation("/wsdl2java_wsdl/hello_world_exclude.wsdl") };
    CommandInterfaceUtils.commandCommonMain();
    WSDLToJava w2j = new WSDLToJava(args);
    w2j.run(new ToolContext());
    assertNotNull(output);
    File com = new File(output, "test");
    assertFalse("Generated file has been excluded", com.exists());
}
Also used : WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) ToolContext(org.apache.cxf.tools.common.ToolContext) File(java.io.File) AbstractCodeGenTest(org.apache.cxf.tools.wsdlto.AbstractCodeGenTest) Test(org.junit.Test)

Example 3 with WSDLToJava

use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.

the class CodeGenBugTest method testMultiXjcArgs.

@Test
public void testMultiXjcArgs() throws Exception {
    String[] args = new String[] { "-d", output.getCanonicalPath(), "-xjc-Xlocator", "-xjc-Xsync-methods", getLocation("/wsdl2java_wsdl/hello_world.wsdl") };
    CommandInterfaceUtils.commandCommonMain();
    WSDLToJava w2j = new WSDLToJava(args);
    w2j.run(new ToolContext());
    File file = new File(output, "org/apache/cxf/w2j/hello_world_soap_http/types/SayHi.java");
    assertTrue(file.exists());
    String str = TestFileUtils.getStringFromFile(file);
    assertTrue(str.contains("@XmlLocation"));
    assertTrue(str.contains("synchronized"));
}
Also used : WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) ToolContext(org.apache.cxf.tools.common.ToolContext) File(java.io.File) AbstractCodeGenTest(org.apache.cxf.tools.wsdlto.AbstractCodeGenTest) Test(org.junit.Test)

Example 4 with WSDLToJava

use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.

the class CodeGenBugTest method testBug305924ForExternalBinding.

@Test
public void testBug305924ForExternalBinding() {
    try {
        String[] args = new String[] { "-all", "-compile", "-classdir", output.getCanonicalPath() + "/classes", "-d", output.getCanonicalPath(), "-b", getLocation("/wsdl2java_wsdl/bug305924/binding1.xml"), getLocation("/wsdl2java_wsdl/bug305924/hello_world.wsdl") };
        CommandInterfaceUtils.commandCommonMain();
        WSDLToJava w2j = new WSDLToJava(args);
        w2j.run(new ToolContext());
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    try {
        Class<?> clz = classLoader.loadClass("org.apache.cxf.w2j.hello_world_soap_http.types.CreateProcess$MyProcess");
        assertNotNull("Customization binding code should be generated", clz);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) ToolContext(org.apache.cxf.tools.common.ToolContext) WSDLRuntimeException(org.apache.cxf.wsdl11.WSDLRuntimeException) ToolException(org.apache.cxf.tools.common.ToolException) AbstractCodeGenTest(org.apache.cxf.tools.wsdlto.AbstractCodeGenTest) Test(org.junit.Test)

Example 5 with WSDLToJava

use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.

the class CodeGenBugTest method testBug305924ForNestedBinding.

@Test
public void testBug305924ForNestedBinding() {
    try {
        String[] args = new String[] { "-all", "-compile", "-classdir", output.getCanonicalPath() + "/classes", "-d", output.getCanonicalPath(), "-b", getLocation("/wsdl2java_wsdl/bug305924/binding2.xml"), getLocation("/wsdl2java_wsdl/bug305924/hello_world.wsdl") };
        CommandInterfaceUtils.commandCommonMain();
        WSDLToJava w2j = new WSDLToJava(args);
        w2j.run(new ToolContext());
    } catch (Exception e) {
    }
    try {
        Class<?> clz = classLoader.loadClass("org.apache.cxf.w2j.hello_world_soap_http.types.CreateProcess$MyProcess");
        assertNotNull("Customization binding code should be generated", clz);
    } catch (ClassNotFoundException e) {
        fail("Can not load the inner class MyProcess, the customization failed: \n" + e.getMessage());
    }
}
Also used : WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) ToolContext(org.apache.cxf.tools.common.ToolContext) WSDLRuntimeException(org.apache.cxf.wsdl11.WSDLRuntimeException) ToolException(org.apache.cxf.tools.common.ToolException) AbstractCodeGenTest(org.apache.cxf.tools.wsdlto.AbstractCodeGenTest) Test(org.junit.Test)

Aggregations

ToolContext (org.apache.cxf.tools.common.ToolContext)17 WSDLToJava (org.apache.cxf.tools.wsdlto.WSDLToJava)17 File (java.io.File)14 AbstractCodeGenTest (org.apache.cxf.tools.wsdlto.AbstractCodeGenTest)14 Test (org.junit.Test)14 ToolException (org.apache.cxf.tools.common.ToolException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 WSDLRuntimeException (org.apache.cxf.wsdl11.WSDLRuntimeException)2 BufferedReader (java.io.BufferedReader)1 DataOutputStream (java.io.DataOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 URI (java.net.URI)1