Search in sources :

Example 1 with JavaToWS

use of org.apache.cxf.tools.java2ws.JavaToWS in project cxf by apache.

the class Java2WSMojo method processJavaClass.

private void processJavaClass(List<String> args) throws MojoExecutionException {
    if (!fork) {
        try {
            CommandInterfaceUtils.commandCommonMain();
            JavaToWS j2w = new JavaToWS(args.toArray(new String[args.size()]));
            j2w.run();
        } catch (OutOfMemoryError e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder();
            msg.append(e.getMessage()).append('\n');
            msg.append("Try to run this goal using the <fork>true</fork> and " + "<additionalJvmArgs>-Xms128m -Xmx128m</additionalJvmArgs> parameters.");
            throw new MojoExecutionException(msg.toString(), e);
        } catch (Throwable e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    } else {
        getLog().info("Running java2ws in fork mode...");
        Commandline cmd = new Commandline();
        // for JVM args
        cmd.getShell().setQuotedArgumentsEnabled(true);
        cmd.setWorkingDirectory(project.getBuild().getDirectory());
        try {
            cmd.setExecutable(getJavaExecutable().getAbsolutePath());
        } catch (IOException e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
        cmd.addArguments(args.toArray(new String[args.size()]));
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
        int exitCode;
        try {
            exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
        } catch (CommandLineException e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
        String output = StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim();
        String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
        if (exitCode != 0) {
            if (StringUtils.isNotEmpty(output)) {
                getLog().info(output);
            }
            StringBuilder msg = new StringBuilder("\nExit code: ");
            msg.append(exitCode);
            if (StringUtils.isNotEmpty(err.getOutput())) {
                msg.append(" - ").append(err.getOutput());
            }
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
            throw new MojoExecutionException(msg.toString());
        }
        if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("JavaToWS Error")) {
            StringBuilder msg = new StringBuilder();
            msg.append(err.getOutput());
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
            throw new MojoExecutionException(msg.toString());
        }
    }
    // with the enclosing project
    if (attachWsdl && outputFile != null) {
        File wsdlFile = new File(outputFile);
        if (wsdlFile.exists()) {
            if (classifier != null) {
                projectHelper.attachArtifact(project, wsdlFile.getName(), classifier, wsdlFile);
            } else {
                projectHelper.attachArtifact(project, wsdlFile.getName(), wsdlFile);
            }
            boolean hasWsdlAttached = false;
            for (Artifact a : project.getAttachedArtifacts()) {
                if ("wsdl".equals(a.getType())) {
                    hasWsdlAttached = true;
                }
            }
            if (!hasWsdlAttached) {
                if (classifier != null) {
                    projectHelper.attachArtifact(project, "wsdl", classifier, wsdlFile);
                } else {
                    projectHelper.attachArtifact(project, "wsdl", wsdlFile);
                }
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Commandline(org.codehaus.plexus.util.cli.Commandline) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) Artifact(org.apache.maven.artifact.Artifact) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) File(java.io.File)

Example 2 with JavaToWS

use of org.apache.cxf.tools.java2ws.JavaToWS in project jbossws-cxf by jbossws.

the class CXFProviderImpl method provide.

public void provide(Class<?> endpointClass) {
    // Swap the context classloader
    // The '--classpath' switch might provide an URLClassLoader
    ClassLoader oldLoader = SecurityActions.getContextClassLoader();
    if (loader != null)
        SecurityActions.setContextClassLoader(loader);
    try {
        List<String> args = new ArrayList<String>();
        // Use the output directory as the default
        File resourceDir = (this.resourceDir != null) ? this.resourceDir : outputDir;
        File sourceDir = (this.sourceDir != null) ? this.sourceDir : outputDir;
        if (generateSource) {
        }
        if (sourceDir != null) {
            if (!sourceDir.exists() && !sourceDir.mkdirs())
                throw MESSAGES.couldNotMakeDirectory(sourceDir.getName());
            args.add("-s");
            args.add(sourceDir.getAbsolutePath());
        }
        if (!outputDir.exists() && !outputDir.mkdirs())
            throw MESSAGES.couldNotMakeDirectory(outputDir.getName());
        args.add("-classdir");
        args.add(outputDir.getAbsolutePath());
        if (resourceDir != null) {
            if (!resourceDir.exists() && !resourceDir.mkdirs())
                throw MESSAGES.couldNotMakeDirectory(resourceDir.getName());
            args.add("-d");
            args.add(resourceDir.getAbsolutePath());
        }
        PrintStream stream = messageStream;
        if (stream != null) {
            // TODO: There is no need to set verbose to cxf java2ws tool ?
            args.add("-verbose");
        } else {
            stream = NullPrintStream.getInstance();
        }
        // -wsdl[:protocol]
        if (generateWsdl) {
            args.add("-wsdl");
            if (extension)
                args.add("-soap12");
            if (portAddress != null) {
                args.add("-address");
                args.add(portAddress);
            }
        }
        String cp = buildClasspathString(loader);
        if (cp != null) {
            args.add("-cp");
            args.add(cp);
        }
        args.add("-wrapperbean");
        args.add("-createxsdimports");
        // the SEI
        args.add(endpointClass.getCanonicalName());
        JavaToWS j2w = new JavaToWS(args.toArray(new String[0]));
        j2w.run(stream);
    } catch (Throwable t) {
        if (messageStream != null) {
            messageStream.println(MESSAGES.failedToInvoke(JavaToWS.class.getName()));
            t.printStackTrace(messageStream);
        } else {
            t.printStackTrace();
        }
    } finally {
        SecurityActions.setContextClassLoader(oldLoader);
    }
}
Also used : PrintStream(java.io.PrintStream) NullPrintStream(org.jboss.ws.common.utils.NullPrintStream) JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 3 with JavaToWS

use of org.apache.cxf.tools.java2ws.JavaToWS in project cxf by apache.

the class Java2WSMojo method initArgs.

private List<String> initArgs(String cp) {
    List<String> args = new ArrayList<>();
    if (fork) {
        String[] split = additionalJvmArgs.split("\\s+");
        for (String each : split) {
            args.add(each);
        }
        // @see JavaToWS#isExitOnFinish()
        args.add("-DexitOnFinish=true");
    }
    if (!StringUtils.isEmpty(cp)) {
        // classpath arg
        args.add("-cp");
        args.add(cp);
    }
    if (fork) {
        args.add(JavaToWS.class.getCanonicalName());
    }
    // outputfile arg
    if (outputFile == null && project != null) {
        // Put the wsdl in target/generated/wsdl
        int i = className.lastIndexOf('.');
        // Prone to OoBE, but then it's wrong anyway
        String name = className.substring(i + 1);
        outputFile = (project.getBuild().getDirectory() + "/generated/wsdl/" + name + ".wsdl").replace("/", File.separator);
    }
    if (outputFile != null) {
        // JavaToWSDL freaks out if the directory of the outputfile doesn't exist, so lets
        // create it since there's no easy way for the user to create it beforehand in maven
        FileUtils.mkDir(new File(outputFile).getParentFile());
        args.add("-o");
        args.add(outputFile);
        if (project != null) {
            project.addCompileSourceRoot(new File(outputFile).getParentFile().getAbsolutePath());
        }
    }
    if (!StringUtils.isEmpty(frontend)) {
        args.add("-frontend");
        args.add(frontend);
    }
    if (!StringUtils.isEmpty(databinding)) {
        args.add("-databinding");
        args.add(databinding);
    }
    if (genWrapperbean) {
        args.add("-wrapperbean");
    }
    if (genWsdl) {
        args.add("-wsdl");
    }
    if (genServer) {
        args.add("-server");
    }
    if (genClient) {
        args.add("-client");
    }
    // soap12 arg
    if (soap12 != null && soap12.booleanValue()) {
        args.add("-soap12");
    }
    // target namespace arg
    if (!StringUtils.isEmpty(targetNamespace)) {
        args.add("-t");
        args.add(targetNamespace);
    }
    // servicename arg
    if (!StringUtils.isEmpty(serviceName)) {
        args.add("-servicename");
        args.add(serviceName);
    }
    // verbose arg
    if (verbose != null && verbose.booleanValue()) {
        args.add("-verbose");
    }
    // quiet arg
    if (quiet != null && quiet.booleanValue()) {
        args.add("-quiet");
    }
    // address arg
    if (!StringUtils.isEmpty(address)) {
        args.add("-address");
        args.add(address);
    }
    // portname arg
    if (!StringUtils.isEmpty(portName)) {
        args.add("-portname");
        args.add(portName);
    }
    if (argline != null) {
        StringTokenizer stoken = new StringTokenizer(argline, " ");
        while (stoken.hasMoreTokens()) {
            args.add(stoken.nextToken());
        }
    }
    // classname arg
    args.add(className);
    return args;
}
Also used : StringTokenizer(java.util.StringTokenizer) JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) ArrayList(java.util.ArrayList) File(java.io.File)

Example 4 with JavaToWS

use of org.apache.cxf.tools.java2ws.JavaToWS in project cxf by apache.

the class Java2WSMojo method processJavaClass.

private void processJavaClass(List<String> args, String cp) throws MojoExecutionException {
    if (!fork) {
        try {
            CommandInterfaceUtils.commandCommonMain();
            JavaToWS j2w = new JavaToWS(args.toArray(new String[0]));
            j2w.run();
        } catch (OutOfMemoryError e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder(128);
            msg.append(e.getMessage()).append('\n');
            msg.append("Try to run this goal using the <fork>true</fork> and " + "<additionalJvmArgs>-Xms128m -Xmx128m</additionalJvmArgs> parameters.");
            throw new MojoExecutionException(msg.toString(), e);
        } catch (Throwable e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    } else {
        getLog().info("Running java2ws in fork mode...");
        Commandline cmd = new Commandline();
        // for JVM args
        cmd.getShell().setQuotedArgumentsEnabled(true);
        cmd.setWorkingDirectory(project.getBuild().getDirectory());
        try {
            cmd.setExecutable(getJavaExecutable().getAbsolutePath());
        } catch (IOException e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
        cmd.addArguments(args.toArray(new String[0]));
        if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
            cmd.addEnvironment("CLASSPATH", cp);
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
        String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
        int exitCode;
        try {
            exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
        } catch (CommandLineException e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder(e.getMessage());
            if (!(fork && classpathAsEnvVar)) {
                msg.append('\n');
                msg.append("Try to run this goal using <fork>true</fork> and " + "<classpathAsEnvVar>true</classpathAsEnvVar>.");
            }
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString(), e);
        }
        String output = StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim();
        if (exitCode != 0) {
            if (StringUtils.isNotEmpty(output)) {
                getLog().info(output);
            }
            StringBuilder msg = new StringBuilder("\nExit code: ");
            msg.append(exitCode);
            if (StringUtils.isNotEmpty(err.getOutput())) {
                msg.append(" - ").append(err.getOutput());
            }
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString());
        }
        if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("JavaToWS Error")) {
            StringBuilder msg = new StringBuilder();
            msg.append(err.getOutput());
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString());
        }
    }
    // with the enclosing project
    if (attachWsdl && outputFile != null) {
        File wsdlFile = new File(outputFile);
        if (wsdlFile.exists()) {
            boolean hasWsdlAttached = false;
            for (Artifact a : project.getAttachedArtifacts()) {
                if ("wsdl".equals(a.getType()) && classifier != null && classifier.equals(a.getClassifier())) {
                    hasWsdlAttached = true;
                }
            }
            if (!hasWsdlAttached) {
                if (classifier != null) {
                    projectHelper.attachArtifact(project, wsdlFile.getName(), classifier, wsdlFile);
                } else {
                    projectHelper.attachArtifact(project, wsdlFile.getName(), wsdlFile);
                }
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Commandline(org.codehaus.plexus.util.cli.Commandline) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) Artifact(org.apache.maven.artifact.Artifact) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) File(java.io.File)

Example 5 with JavaToWS

use of org.apache.cxf.tools.java2ws.JavaToWS in project cxf by apache.

the class JavaToWSFlagTest method testValidArgs.

@Test
public void testValidArgs() {
    String[] args = new String[] { "a.ww" };
    CommandInterfaceUtils.commandCommonMain();
    JavaToWS j2w = new JavaToWS(args);
    try {
        j2w.run();
    } catch (Throwable ex) {
        System.err.println("JavaToWS Error: " + ex.toString());
        System.err.println();
    }
    assertNotNull(getStdOut());
}
Also used : JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) Test(org.junit.Test)

Aggregations

JavaToWS (org.apache.cxf.tools.java2ws.JavaToWS)11 Test (org.junit.Test)7 File (java.io.File)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Artifact (org.apache.maven.artifact.Artifact)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)2 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)2 Commandline (org.codehaus.plexus.util.cli.Commandline)2 PrintStream (java.io.PrintStream)1 URLClassLoader (java.net.URLClassLoader)1 StringTokenizer (java.util.StringTokenizer)1 NullPrintStream (org.jboss.ws.common.utils.NullPrintStream)1