Search in sources :

Example 1 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class AbstractCodeGeneratorMojo method callCodeGenerator.

protected Bus callCodeGenerator(WadlOption option, Bus bus, Set<URI> classPath) throws MojoExecutionException {
    File outputDirFile = option.getOutputDir();
    outputDirFile.mkdirs();
    URI basedir = project.getBasedir().toURI();
    for (URI wadlURI : option.getWadlURIs(basedir, getResourceLoader())) {
        File doneFile = getDoneFile(basedir, wadlURI);
        if (!shouldRun(option, doneFile, wadlURI)) {
            return bus;
        }
        doneFile.delete();
        List<String> list = option.generateCommandLine(outputDirFile, basedir, wadlURI, getLog().isDebugEnabled());
        String[] args = list.toArray(new String[list.size()]);
        getLog().debug("Calling wadl2java with args: " + Arrays.toString(args));
        if (!"false".equals(fork)) {
            Set<URI> artifactsPath = new LinkedHashSet<URI>();
            for (Artifact a : pluginArtifacts) {
                File file = a.getFile();
                if (file == null) {
                    throw new MojoExecutionException("Unable to find file for artifact " + a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion());
                }
                artifactsPath.add(file.toURI());
            }
            addPluginArtifact(artifactsPath);
            artifactsPath.addAll(classPath);
            runForked(artifactsPath, WADLToJava.class, args);
        } else {
            if (bus == null) {
                bus = BusFactory.newInstance().createBus();
                BusFactory.setThreadDefaultBus(bus);
            }
            try {
                new WADLToJava(args).run(new ToolContext());
            } catch (Throwable e) {
                getLog().debug(e);
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
        try {
            doneFile.createNewFile();
        } catch (Throwable e) {
            getLog().warn("Could not create marker file " + doneFile.getAbsolutePath());
            getLog().debug(e);
        }
    }
    return bus;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WADLToJava(org.apache.cxf.tools.wadlto.WADLToJava) ToolContext(org.apache.cxf.tools.common.ToolContext) File(java.io.File) URI(java.net.URI) DocumentArtifact(org.apache.cxf.maven_plugin.common.DocumentArtifact) Artifact(org.apache.maven.artifact.Artifact)

Example 2 with ToolContext

use of org.apache.cxf.tools.common.ToolContext 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[list.size()]);
    getLog().debug("Calling wsdl2java with args: " + Arrays.toString(args));
    if (!"false".equals(fork)) {
        Set<URI> artifactsPath = new LinkedHashSet<URI>();
        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 3 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class ForkOnceWSDL2Javascript method main.

public static void main(String[] args) throws Exception {
    File file = new File(args[0]);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    while (line != null) {
        int i = Integer.parseInt(line);
        if (i == -1) {
            reader.close();
            return;
        }
        String[] wargs = new String[i];
        for (int x = 0; x < i; x++) {
            wargs[x] = reader.readLine();
        }
        new WSDLToJavaScript(wargs).run(new ToolContext());
        line = reader.readLine();
    }
    reader.close();
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ToolContext(org.apache.cxf.tools.common.ToolContext) File(java.io.File) WSDLToJavaScript(org.apache.cxf.tools.wsdlto.javascript.WSDLToJavaScript)

Example 4 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class WSDL2JavaScriptMojo 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;
    try {
        wsdlURI = new URI(wsdlOption.getUri());
    } catch (URISyntaxException e) {
        throw new MojoExecutionException("Failed to get URI for wsdl " + wsdlOption.getUri(), e);
    }
    File doneFile = getDoneFile(basedir, wsdlURI, "js");
    if (!shouldRun(wsdlOption, doneFile, wsdlURI)) {
        return bus;
    }
    doneFile.delete();
    List<String> list = wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog().isDebugEnabled());
    String[] args = list.toArray(new String[list.size()]);
    getLog().debug("Calling wsdl2js with args: " + Arrays.toString(args));
    if (!"false".equals(fork)) {
        Set<URI> artifactsPath = new LinkedHashSet<URI>();
        for (Artifact a : pluginArtifacts) {
            File file = a.getFile();
            if (file == null) {
                throw new MojoExecutionException("Unable to find file for artifact " + a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion());
            }
            artifactsPath.add(file.toURI());
        }
        addPluginArtifact(artifactsPath);
        artifactsPath.addAll(classPath);
        runForked(artifactsPath, WSDLToJavaScript.class.getName(), args);
    } else {
        if (bus == null) {
            bus = BusFactory.newInstance().createBus();
            BusFactory.setThreadDefaultBus(bus);
        }
        try {
            new WSDLToJavaScript(args).run(new ToolContext());
        } catch (Throwable e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    try {
        createMarkerFile(wsdlOption, doneFile, wsdlURI);
    } 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());
    }
    if (project != null && getGeneratedTestRoot() != null && getGeneratedTestRoot().exists()) {
        project.addTestCompileSourceRoot(getGeneratedTestRoot().getAbsolutePath());
    }
    return bus;
}
Also used : GenericWsdlOption(org.apache.cxf.maven_plugin.GenericWsdlOption) LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ToolContext(org.apache.cxf.tools.common.ToolContext) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Artifact(org.apache.maven.artifact.Artifact) File(java.io.File) WSDLToJavaScript(org.apache.cxf.tools.wsdlto.javascript.WSDLToJavaScript)

Example 5 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class WSDLToJavaScript method main.

public static void main(String[] pargs) {
    CommandInterfaceUtils.commandCommonMain();
    WSDLToJavaScript w2j = new WSDLToJavaScript(pargs);
    try {
        w2j.run(new ToolContext());
    } catch (ToolException ex) {
        System.err.println();
        System.err.println("WSDLToJS Error : " + ex.getMessage());
        System.err.println();
        if (w2j.isVerbose()) {
            ex.printStackTrace();
        }
        if (w2j.isExitOnFinish()) {
            System.exit(1);
        }
    } catch (Exception ex) {
        System.err.println("WSDLToJS Error : " + ex.getMessage());
        System.err.println();
        if (w2j.isVerbose()) {
            ex.printStackTrace();
        }
        if (w2j.isExitOnFinish()) {
            System.exit(1);
        }
    }
    if (w2j.isExitOnFinish()) {
        System.exit(0);
    }
}
Also used : ToolContext(org.apache.cxf.tools.common.ToolContext) ToolException(org.apache.cxf.tools.common.ToolException) ToolException(org.apache.cxf.tools.common.ToolException)

Aggregations

ToolContext (org.apache.cxf.tools.common.ToolContext)69 Test (org.junit.Test)45 URISyntaxException (java.net.URISyntaxException)35 File (java.io.File)23 ToolException (org.apache.cxf.tools.common.ToolException)13 BadUsageException (org.apache.cxf.tools.common.toolspec.parser.BadUsageException)7 WADLToJava (org.apache.cxf.tools.wadlto.WADLToJava)5 BufferedReader (java.io.BufferedReader)4 Method (java.lang.reflect.Method)4 URLClassLoader (java.net.URLClassLoader)4 QName (javax.xml.namespace.QName)4 Before (org.junit.Before)4 FileReader (java.io.FileReader)3 URI (java.net.URI)3 LinkedHashSet (java.util.LinkedHashSet)3 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)3 Artifact (org.apache.maven.artifact.Artifact)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 IOException (java.io.IOException)2 Writer (java.io.Writer)2