cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r1213072 - in /cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin: AbstractCodegenMoho.java WSDL2JavaMojo.java
Date Sun, 11 Dec 2011 21:11:21 GMT
Author: bimargulies
Date: Sun Dec 11 21:11:20 2011
New Revision: 1213072

URL: http://svn.apache.org/viewvc?rev=1213072&view=rev
Log:
CXF-3972: refactor wsdl2java mojo to make room for js.

Added:
    cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractCodegenMoho.java
      - copied, changed from r1213024, cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java
Modified:
    cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java

Copied: cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractCodegenMoho.java (from r1213024, cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractCodegenMoho.java?p2=cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractCodegenMoho.java&p1=cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java&r1=1213024&r2=1213072&rev=1213072&view=diff
==============================================================================
--- cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java (original)
+++ cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractCodegenMoho.java Sun Dec 11 21:11:20 2011
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.cxf.maven_plugin;
 
 import java.io.File;
@@ -35,10 +34,7 @@ import java.util.Set;
 
 import org.apache.commons.lang.SystemUtils;
 import org.apache.cxf.Bus;
-import org.apache.cxf.BusFactory;
 import org.apache.cxf.helpers.FileUtils;
-import org.apache.cxf.tools.common.ToolContext;
-import org.apache.cxf.tools.wsdlto.WSDLToJava;
 import org.apache.maven.ProjectDependenciesResolver;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -57,112 +53,103 @@ import org.codehaus.plexus.util.cli.Comm
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.codehaus.plexus.util.cli.Commandline;
 
-/**
- * @goal wsdl2java
- * @phase generate-sources
- * @description CXF WSDL To Java Tool
- * @requiresDependencyResolution test
- * @threadSafe
- */
-public class WSDL2JavaMojo extends AbstractMojo {
-
-    /**
-     * @parameter expression="${cxf.testSourceRoot}"
-     */
-    File testSourceRoot;
-
-    /**
-     * Path where the generated sources should be placed
-     * 
-     * @parameter expression="${cxf.sourceRoot}"
-     *            default-value="${project.build.directory}/generated-sources/cxf"
-     * @required
-     */
-    File sourceRoot;
+public abstract class AbstractCodegenMoho extends AbstractMojo {
 
     /**
      * @parameter expression="${project.build.outputDirectory}"
+     * @readonly
      * @required
      */
-    String classesDirectory;
-
-    /**
-     * @parameter expression="${project}"
-     * @required
-     */
-    MavenProject project;
-
+    protected String classesDirectory;
     /**
      * Default options to be used when a wsdl has not had it's options explicitly specified.
      * 
      * @parameter
      */
-    Option defaultOptions = new Option();
-
+    protected Option defaultOptions = new Option();
     /**
-     * @parameter
+     * By default all maven dependencies of type "wsdl" are added to the effective wsdlOptions. Setting this
+     * parameter to true disables this functionality
+     * 
+     * @parameter expression="${cxf.disableDependencyScan}" default-value="false"
      */
-    WsdlOption wsdlOptions[];
-
+    protected boolean disableDependencyScan;
     /**
-     * @parameter expression="${cxf.wsdlRoot}" default-value="${basedir}/src/main/resources/wsdl"
+     * Disables the scanning of the wsdlRoot/testWsdlRoot directories configured above.
+     * By default, we scan for *.wsdl (see include/exclude params as well) in the wsdlRoot
+     * directories and run wsdl2java on all the wsdl's we find.    This disables that scan
+     * and requires an explicit wsdlOption to be set for each wsdl that needs to be processed.
+     * @parameter expression="${cxf.disableDirectoryScan}" default-value="false"
      */
-    File wsdlRoot;
-
+    protected boolean disableDirectoryScan;
     /**
-     * @parameter expression="${cxf.testWsdlRoot}" default-value="${basedir}/src/test/resources/wsdl"
+     * Allows running the JavaToWs in a separate process. Valid values are "false", "always", and "once" The
+     * value of "true" is equal to "once"
+     * 
+     * @parameter default-value="false"
+     * @since 2.4
      */
-    File testWsdlRoot;
-
+    protected String fork;
+    /**
+     * A list of wsdl files to include. Can contain ant-style wildcards and double wildcards. Defaults to
+     * *.wsdl
+     * 
+     * @parameter
+     */
+    protected String includes[];
     /**
      * Directory in which the "DONE" markers are saved that
      * 
      * @parameter expression="${cxf.markerDirectory}"
      *            default-value="${project.build.directory}/cxf-codegen-plugin-markers"
      */
-    File markerDirectory;
-
+    protected File markerDirectory;
+    /**
+     * The plugin dependencies, needed for the fork mode.
+     * 
+     * @parameter expression="${plugin.artifacts}"
+     * @required
+     * @readonly
+     */
+    protected List<Artifact> pluginArtifacts;
+    /**
+     * @parameter expression="${project}"
+     * @required
+     */
+    protected MavenProject project;
     /**
      * Use the compile classpath rather than the test classpath for execution useful if the test dependencies
      * clash with those of wsdl2java
      * 
      * @parameter expression="${cxf.useCompileClasspath}" default-value="false"
      */
-    boolean useCompileClasspath;
-    
-    
+    protected boolean useCompileClasspath;
     /**
-     * Disables the scanning of the wsdlRoot/testWsdlRoot directories configured above.
-     * By default, we scan for *.wsdl (see include/exclude params as well) in the wsdlRoot
-     * directories and run wsdl2java on all the wsdl's we find.    This disables that scan
-     * and requires an explicit wsdlOption to be set for each wsdl that needs to be processed.
-     * @parameter expression="${cxf.disableDirectoryScan}" default-value="false"
+     * A list of wsdl files to exclude. Can contain ant-style wildcards and double wildcards.
+     * 
+     * @parameter
      */
-    boolean disableDirectoryScan;
-
+    String excludes[];
     /**
-     * By default all maven dependencies of type "wsdl" are added to the effective wsdlOptions. Setting this
-     * parameter to true disables this functionality
-     * 
-     * @parameter expression="${cxf.disableDependencyScan}" default-value="false"
+     * @parameter expression="${cxf.testWsdlRoot}" default-value="${basedir}/src/test/resources/wsdl"
      */
-    boolean disableDependencyScan;
-
+    File testWsdlRoot;
     /**
-     * A list of wsdl files to include. Can contain ant-style wildcards and double wildcards. Defaults to
-     * *.wsdl
-     * 
      * @parameter
      */
-    String includes[];
-
+    WsdlOption wsdlOptions[];
     /**
-     * A list of wsdl files to exclude. Can contain ant-style wildcards and double wildcards.
+     * @parameter expression="${cxf.wsdlRoot}" default-value="${basedir}/src/main/resources/wsdl"
+     */
+    File wsdlRoot;
+    /**
+     * Sets the JVM arguments (i.e. <code>-Xms128m -Xmx128m</code>) if fork is set to <code>true</code>.
      * 
      * @parameter
+     * @since 2.4
      */
-    String excludes[];
-
+    private String additionalJvmArgs;
+    
     /**
      * Artifact factory, needed to create artifacts.
      * 
@@ -173,6 +160,13 @@ public class WSDL2JavaMojo extends Abstr
     private ArtifactFactory artifactFactory;
 
     /**
+     * Sets the Java executable to use when fork parameter is <code>true</code>.
+     * 
+     * @parameter default-value="${java.home}/bin/java"
+     * @since 2.4
+     */
+    private String javaExecutable;
+    /**
      * The Maven session.
      * 
      * @parameter expression="${session}"
@@ -180,7 +174,6 @@ public class WSDL2JavaMojo extends Abstr
      * @required
      */
     private MavenSession mavenSession;
-    
     /**
      * @component
      * @readonly
@@ -188,189 +181,23 @@ public class WSDL2JavaMojo extends Abstr
      */
     private ProjectDependenciesResolver projectDependencyResolver;
 
-    /**
-     * The plugin dependencies, needed for the fork mode.
-     *
-     * @parameter expression="${plugin.artifacts}"
-     * @required
-     * @readonly
-     */
-    private List<Artifact> pluginArtifacts;
-    /**
-     * Allows running the JavaToWs in a separate process.
-     * Valid values are "false", "always", and "once"
-     * The value of "true" is equal to "once"
-     *
-     * @parameter default-value="false"
-     * @since 2.4
-     */
-    private String fork;
-
-    /**
-     * Sets the Java executable to use when fork parameter is <code>true</code>.
-     *
-     * @parameter default-value="${java.home}/bin/java"
-     * @since 2.4
-     */
-    private String javaExecutable;
-
-    /**
-     * Sets the JVM arguments (i.e. <code>-Xms128m -Xmx128m</code>) if fork is set to <code>true</code>.
-     *
-     * @parameter
-     * @since 2.4
-     */
-    private String additionalJvmArgs;
-    
-    /**
-     * Merge WsdlOptions that point to the same file by adding the extraargs to the first option and deleting
-     * the second from the options list
-     * 
-     * @param options
-     */
-    private void mergeOptions(List<WsdlOption> effectiveWsdlOptions) {
-        if (wsdlOptions == null) {
-            return;
-        }
-        File outputDirFile = testSourceRoot == null ? sourceRoot : testSourceRoot;
-        for (WsdlOption o : wsdlOptions) {
-            if (defaultOptions != null) {
-                o.merge(defaultOptions);
-            }
-            if (o.getOutputDir() == null) {
-                o.setOutputDir(outputDirFile);
-            }
-
-            File file = o.getWsdlFile(project.getBasedir());
-            if (file != null && file.exists()) {
-                file = file.getAbsoluteFile();
-                for (WsdlOption o2 : effectiveWsdlOptions) {
-                    File file2 = o2.getWsdlFile(project.getBasedir());
-                    if (file2 != null && file2.exists() && file2.getAbsoluteFile().equals(file)) {
-                        o.getExtraargs().addAll(0, o2.getExtraargs());
-                        effectiveWsdlOptions.remove(o2);
-                        break;
-                    }
-                }
-            }
-            effectiveWsdlOptions.add(o);
-        }
-    }
-
-    /**
-     * @return effective WsdlOptions
-     * @throws MojoExecutionException
-     */
-    private List<WsdlOption> createWsdlOptionsFromScansAndExplicitWsdlOptions() 
-        throws MojoExecutionException {
-        List<WsdlOption> effectiveWsdlOptions = new ArrayList<WsdlOption>();
-        List<WsdlOption> temp;
-        if (wsdlRoot != null && wsdlRoot.exists() && !disableDirectoryScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(wsdlRoot, includes, excludes, defaultOptions,
-                                                             sourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        if (testWsdlRoot != null && testWsdlRoot.exists() && !disableDirectoryScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(testWsdlRoot, includes, excludes,
-                                                             defaultOptions, testSourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        if (!disableDependencyScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromDependencies(project, defaultOptions, sourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        mergeOptions(effectiveWsdlOptions);
-        downloadRemoteWsdls(effectiveWsdlOptions);
-//        String buildDir = project.getBuild().getDirectory();
-//        File tempBindingDir = new File(buildDir, TEMPBINDINGS_DIR);
-//        for (WsdlOption o : effectiveWsdlOptions) {
-//            BindingFileHelper.setWsdlLocationInBindingsIfNotSet(project.getBasedir(), tempBindingDir, o,
-//                                                                getLog());
-//        }
-        return effectiveWsdlOptions;
-    }
-    
-    private Artifact resolveRemoteWsdlArtifact(Artifact artifact)
-        throws MojoExecutionException {
-        
-        Collection<String> scopes = new ArrayList<String>();
-        scopes.add(artifact.getScope());
-        Set<Artifact> resolvedArtifacts = null;
-        try {
-            resolvedArtifacts = projectDependencyResolver.resolve(project, scopes, mavenSession);
-        } catch (ArtifactResolutionException e) {
-            throw new MojoExecutionException("Error downloading wsdl artifact.", e);
-        } catch (ArtifactNotFoundException e) {
-            throw new MojoExecutionException("Resource can not be found.", e);
-        } 
-        
-        if (resolvedArtifacts != null && resolvedArtifacts.size() != 0) {
-            for (Artifact pArtifact : resolvedArtifacts) {
-                if ("wsdl".equals(pArtifact.getType())) {
-                    getLog().info(String.format("%s resolved to %s", 
-                                                pArtifact.toString(), pArtifact.getFile().getAbsolutePath()));
-                    return pArtifact;
-                }
-            }
-        }
-        throw new MojoExecutionException(String.format("Failed to resolve WSDL artifact %s", 
-                                                       artifact.toString()));
-    }
-
-    private void downloadRemoteWsdls(List<WsdlOption> effectiveWsdlOptions) throws MojoExecutionException {
-        
-        for (WsdlOption wsdlOption : effectiveWsdlOptions) {
-            WsdlArtifact wsdlA = wsdlOption.getWsdlArtifact();
-            if (wsdlA == null) {
-                return;
-            }
-            Artifact wsdlArtifact = artifactFactory.createArtifact(wsdlA.getGroupId(), wsdlA.getArtifactId(),
-                                                                   wsdlA.getVersion(),
-                                                                   Artifact.SCOPE_COMPILE, wsdlA.getType());
-            wsdlArtifact = resolveRemoteWsdlArtifact(wsdlArtifact);
-            if (wsdlArtifact != null) {
-                File supposedFile = wsdlArtifact.getFile();
-                if (!supposedFile.exists() || !supposedFile.isFile()) {
-                    getLog().info("Apparent Maven bug: wsdl artifact 'resolved' to " 
-                        + supposedFile.getAbsolutePath() + " for " + wsdlArtifact.toString());
-                    continue;
-                }
-                String path = supposedFile.getAbsolutePath();
-                getLog().info("Resolved WSDL artifact to file " + path);
-                wsdlOption.setWsdl(path);
-            }
-        }
-    }
-
-    private void configureProxyServerSettings() throws MojoExecutionException {
-
-        Proxy proxy = mavenSession.getSettings().getActiveProxy();
-
-        if (proxy != null) {
-
-            getLog().info("Using proxy server configured in maven.");
-
-            if (proxy.getHost() == null) {
-                throw new MojoExecutionException("Proxy in settings.xml has no host");
-            } else {
-                System.setProperty("proxySet", "true");
-                System.setProperty("proxyHost", proxy.getHost());
-                System.setProperty("proxyPort", String.valueOf(proxy.getPort()));
-            }
-
-
-        }
+    public AbstractCodegenMoho() {
+        super();
     }
 
     public void execute() throws MojoExecutionException {
+        File classesDir = new File(classesDirectory);
+        /*
+         * This shouldn't be needed, but it's harmless.
+         */
+        classesDir.mkdirs();
+
         if (includes == null) {
             includes = new String[] {
                 "*.wsdl"
             };
         }
         defaultOptions.addDefaultBindingFileIfExists(project.getBasedir());
-        File classesDir = new File(classesDirectory);
-        classesDir.mkdirs();
         markerDirectory.mkdirs();
 
         configureProxyServerSettings();
@@ -393,7 +220,7 @@ public class WSDL2JavaMojo extends Abstr
                 forkOnce(cp, effectiveWsdlOptions);
             } else {
                 for (WsdlOption o : effectiveWsdlOptions) {
-                    bus = callWsdl2Java(o, bus, cp);
+                    bus = generate(o, bus, cp);
     
                     File dirs[] = o.getDeleteDirs();
                     if (dirs != null) {
@@ -410,20 +237,17 @@ public class WSDL2JavaMojo extends Abstr
             }
             classLoaderSwitcher.restoreClassLoader();
         }
-        if (project != null && sourceRoot != null && sourceRoot.exists()) {
-            project.addCompileSourceRoot(sourceRoot.getAbsolutePath());
-        }
-        if (project != null && testSourceRoot != null && testSourceRoot.exists()) {
-            project.addTestCompileSourceRoot(testSourceRoot.getAbsolutePath());
-        }
+
 
         System.gc();
     }
-    
-    private void addPluginArtifact(Set<URI> artifactsPath) {
-        //for Maven 2.x, the actual artifact isn't in the list....  need to try and find it
+
+    protected abstract Bus generate(WsdlOption o, Bus bus, Set<URI> cp) throws MojoExecutionException;
+
+    protected void addPluginArtifact(Set<URI> artifactsPath) {
+        // for Maven 2.x, the actual artifact isn't in the list.... need to try and find it
         URL url = getClass().getResource(getClass().getSimpleName() + ".class");
-        
+
         try {
             if ("jar".equals(url.getProtocol())) {
                 String s = url.getPath();
@@ -443,12 +267,87 @@ public class WSDL2JavaMojo extends Abstr
                 artifactsPath.add(file.toURI());
             }
         } catch (Exception ex) {
-            //ex.printStackTrace();
+            // ex.printStackTrace();
         }
 
     }
 
-    private void forkOnce(Set<URI> classPath, List<WsdlOption> effectiveWsdlOptions) 
+    protected void configureProxyServerSettings() throws MojoExecutionException {
+
+        Proxy proxy = mavenSession.getSettings().getActiveProxy();
+
+        if (proxy != null) {
+
+            getLog().info("Using proxy server configured in maven.");
+
+            if (proxy.getHost() == null) {
+                throw new MojoExecutionException("Proxy in settings.xml has no host");
+            } else {
+                System.setProperty("proxySet", "true");
+                System.setProperty("proxyHost", proxy.getHost());
+                System.setProperty("proxyPort", String.valueOf(proxy.getPort()));
+            }
+
+        }
+    }
+    
+    /**
+     * @return effective WsdlOptions
+     * @throws MojoExecutionException
+     */
+    protected List<WsdlOption> createWsdlOptionsFromScansAndExplicitWsdlOptions()
+        throws MojoExecutionException {
+        List<WsdlOption> effectiveWsdlOptions = new ArrayList<WsdlOption>();
+        List<WsdlOption> temp;
+        if (wsdlRoot != null && wsdlRoot.exists() && !disableDirectoryScan) {
+            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(wsdlRoot, includes, excludes, defaultOptions,
+                                                             getGeneratedSourceRoot());
+            effectiveWsdlOptions.addAll(temp);
+        }
+        if (testWsdlRoot != null && testWsdlRoot.exists() && !disableDirectoryScan) {
+            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(testWsdlRoot, includes, excludes,
+                                                             defaultOptions, getGeneratedTestRoot());
+            effectiveWsdlOptions.addAll(temp);
+        }
+        if (!disableDependencyScan) {
+            temp = WsdlOptionLoader.loadWsdlOptionsFromDependencies(project, 
+                                                                    defaultOptions, 
+                                                                    getGeneratedSourceRoot());
+            effectiveWsdlOptions.addAll(temp);
+        }
+        mergeOptions(effectiveWsdlOptions);
+        downloadRemoteWsdls(effectiveWsdlOptions);
+        // String buildDir = project.getBuild().getDirectory();
+        // File tempBindingDir = new File(buildDir, TEMPBINDINGS_DIR);
+        // for (WsdlOption o : effectiveWsdlOptions) {
+        // BindingFileHelper.setWsdlLocationInBindingsIfNotSet(project.getBasedir(), tempBindingDir, o,
+        // getLog());
+        // }
+        return effectiveWsdlOptions;
+    }
+
+    /**
+     * Recursively delete the given directory
+     * 
+     * @param f
+     * @return
+     */
+    protected boolean deleteDir(File f) {
+        if (f.isDirectory()) {
+            File files[] = f.listFiles();
+            for (int idx = 0; idx < files.length; ++idx) {
+                deleteDir(files[idx]);
+            }
+        }
+
+        if (f.exists()) {
+            return f.delete();
+        }
+
+        return true;
+    }
+    
+    protected void forkOnce(Set<URI> classPath, List<WsdlOption> effectiveWsdlOptions)
         throws MojoExecutionException {
         List<WsdlOption> toDo = new LinkedList<WsdlOption>();
         List<List<String>> wargs = new LinkedList<List<String>>();
@@ -463,32 +362,31 @@ public class WSDL2JavaMojo extends Abstr
                 continue;
             }
             doneFile.delete();
-            
+
             toDo.add(wsdlOption);
-            
+
             wargs.add(wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog()
-                                                               .isDebugEnabled()));
+                .isDebugEnabled()));
         }
         if (wargs.isEmpty()) {
             return;
         }
-        
+
         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());
+                throw new MojoExecutionException("Unable to find " + file + " for artifact " + a.getGroupId()
+                                                 + ":" + a.getArtifactId() + ":" + a.getVersion());
             }
             artifactsPath.add(file.toURI());
         }
         addPluginArtifact(artifactsPath);
         artifactsPath.addAll(classPath);
-        
+
         String args[] = createForkOnceArgs(wargs);
         runForked(artifactsPath, ForkOnceWSDL2Java.class, args);
-        
+
         for (WsdlOption wsdlOption : toDo) {
             File dirs[] = wsdlOption.getDeleteDirs();
             if (dirs != null) {
@@ -507,95 +405,28 @@ public class WSDL2JavaMojo extends Abstr
             }
         }
     }
+    
+    protected File getDoneFile(URI basedir, URI wsdlURI) {
+        String doneFileName = wsdlURI.toString();
 
-    private String[] createForkOnceArgs(List<List<String>> wargs) throws MojoExecutionException {
-        try {
-            File f = FileUtils.createTempFile("cxf-w2j", "args");
-            PrintWriter fw = new PrintWriter(new FileWriter(f));
-            for (List<String> args : wargs) {
-                fw.println(Integer.toString(args.size()));
-                for (String s : args) {
-                    fw.println(s);
-                }
-            }
-            fw.println("-1");
-            fw.close();
-            return new String[] {f.getAbsolutePath()};
-        } catch (IOException ex) {
-            throw new MojoExecutionException("Could not create argument file", ex);
+        // Strip the basedir from the doneFileName
+        if (doneFileName.startsWith(basedir.toString())) {
+            doneFileName = doneFileName.substring(basedir.toString().length());
         }
-    }
-
-    private Bus callWsdl2Java(WsdlOption wsdlOption, 
-                              Bus bus,
-                              Set<URI> classPath) throws MojoExecutionException {
-        File outputDirFile = wsdlOption.getOutputDir();
-        outputDirFile.mkdirs();
-        URI basedir = project.getBasedir().toURI();
-        URI wsdlURI = wsdlOption.getWsdlURI(basedir);
-        File doneFile = getDoneFile(basedir, wsdlURI);
-
-        if (!shouldRun(wsdlOption, doneFile, wsdlURI)) {
-            return bus;
-        }
-        doneFile.delete();
-
-        List<String> list = wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog()
-                                                           .isDebugEnabled());
-        String[] args = (String[])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 " + file + " for artifact "
-                                                     + a.getGroupId() + ":" + a.getArtifactId()
-                                                     + ":" + a.getVersion());
-                }
-                artifactsPath.add(file.toURI());
-            }
-            addPluginArtifact(artifactsPath);
-            artifactsPath.addAll(classPath);
-            
-            runForked(artifactsPath, WSDLToJava.class, args);
 
-        } else {
-            if (bus == null) {
-                bus = BusFactory.newInstance().createBus();
-                BusFactory.setThreadDefaultBus(bus);
-            }
-            try {
-                new WSDLToJava(args).run(new ToolContext());
-            } catch (Throwable e) {
-                getLog().debug(e);
-                throw new MojoExecutionException(e.getMessage(), e);
-            }  
-        }
-        
+        // If URL to WSDL, replace ? and & since they're invalid chars for file names
+        // Not to mention slashes.
+        doneFileName = doneFileName.replace('?', '_').replace('&', '_').replace('/', '_').replace('\\', '_')
+            .replace(':', '_');
 
-        try {
-            doneFile.createNewFile();
-        } catch (Throwable e) {
-            getLog().warn("Could not create marker file " + doneFile.getAbsolutePath());
-            getLog().debug(e);
-        }
-        return bus;
+        return new File(markerDirectory, "." + doneFileName + ".DONE");
     }
-    private File getJavaExecutable() throws IOException {
-        String exe = (SystemUtils.IS_OS_WINDOWS && !javaExecutable.endsWith(".exe")) ? ".exe" : "";
-        File javaExe = new File(javaExecutable + exe);
 
-        if (!javaExe.isFile()) {
-            throw new IOException("The java executable '" + javaExe
-                + "' doesn't exist or is not a file. Verify the <javaExecutable/> parameter.");
-        }
+    protected abstract File getGeneratedSourceRoot();
 
-        return javaExe;
-    }
+    protected abstract File getGeneratedTestRoot();
 
-    private void runForked(Set<URI> classPath, Class cls, String[] args) throws MojoExecutionException {
+    protected void runForked(Set<URI> classPath, Class cls, String[] args) throws MojoExecutionException {
         getLog().info("Running wsdl2java in fork mode...");
         getLog().debug("Running wsdl2java in fork mode with args " + Arrays.asList(args));
 
@@ -610,15 +441,15 @@ public class WSDL2JavaMojo extends Abstr
         }
 
         cmd.createArg().setLine(additionalJvmArgs);
-        
+
         File file = null;
         try {
-            //file = new File("/tmp/test.jar"); 
+            // file = new File("/tmp/test.jar");
             file = FileUtils.createTempFile("cxf-codegen", ".jar");
 
             JarArchiver jar = new JarArchiver();
             jar.setDestFile(file.getAbsoluteFile());
-            
+
             Manifest manifest = new Manifest();
             Attribute attr = new Attribute();
             attr.setName("Class-Path");
@@ -628,7 +459,7 @@ public class WSDL2JavaMojo extends Abstr
             }
             attr.setValue(b.toString());
             manifest.getMainSection().addConfiguredAttribute(attr);
-            
+
             attr = new Attribute();
             attr.setName("Main-Class");
             attr.setValue(cls.getName());
@@ -636,16 +467,14 @@ public class WSDL2JavaMojo extends Abstr
 
             jar.addConfiguredManifest(manifest);
             jar.createArchive();
-            
+
             cmd.createArg().setValue("-jar");
             cmd.createArg().setValue(file.getAbsolutePath());
 
-            
         } catch (Exception e1) {
             throw new MojoExecutionException("Could not create runtime jar", e1);
         }
         cmd.addArguments(args);
-        
 
         CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
         CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
@@ -691,22 +520,6 @@ public class WSDL2JavaMojo extends Abstr
 
     }
 
-    private File getDoneFile(URI basedir, URI wsdlURI) {
-        String doneFileName = wsdlURI.toString();
-        
-        // Strip the basedir from the doneFileName
-        if (doneFileName.startsWith(basedir.toString())) {
-            doneFileName = doneFileName.substring(basedir.toString().length());
-        }
-
-        // If URL to WSDL, replace ? and & since they're invalid chars for file names
-        // Not to mention slashes.
-        doneFileName = doneFileName.replace('?', '_').replace('&', '_').replace('/', '_').replace('\\', '_')
-            .replace(':', '_');
-
-        return new File(markerDirectory, "." + doneFileName + ".DONE");
-    }
-
     /**
      * Determine if code should be generated from the given wsdl
      * 
@@ -715,7 +528,7 @@ public class WSDL2JavaMojo extends Abstr
      * @param wsdlURI
      * @return
      */
-    private boolean shouldRun(WsdlOption wsdlOption, File doneFile, URI wsdlURI) {
+    protected boolean shouldRun(WsdlOption wsdlOption, File doneFile, URI wsdlURI) {
         long timestamp = 0;
         if ("file".equals(wsdlURI.getScheme())) {
             timestamp = new File(wsdlURI).lastModified();
@@ -746,25 +559,126 @@ public class WSDL2JavaMojo extends Abstr
         return doWork;
     }
 
+    private String[] createForkOnceArgs(List<List<String>> wargs) throws MojoExecutionException {
+        try {
+            File f = FileUtils.createTempFile("cxf-w2j", "args");
+            PrintWriter fw = new PrintWriter(new FileWriter(f));
+            for (List<String> args : wargs) {
+                fw.println(Integer.toString(args.size()));
+                for (String s : args) {
+                    fw.println(s);
+                }
+            }
+            fw.println("-1");
+            fw.close();
+            return new String[] {
+                f.getAbsolutePath()
+            };
+        } catch (IOException ex) {
+            throw new MojoExecutionException("Could not create argument file", ex);
+        }
+    }
+
+    private void downloadRemoteWsdls(List<WsdlOption> effectiveWsdlOptions) throws MojoExecutionException {
+
+        for (WsdlOption wsdlOption : effectiveWsdlOptions) {
+            WsdlArtifact wsdlA = wsdlOption.getWsdlArtifact();
+            if (wsdlA == null) {
+                return;
+            }
+            Artifact wsdlArtifact = artifactFactory.createArtifact(wsdlA.getGroupId(), wsdlA.getArtifactId(),
+                                                                   wsdlA.getVersion(),
+                                                                   Artifact.SCOPE_COMPILE, wsdlA.getType());
+            wsdlArtifact = resolveRemoteWsdlArtifact(wsdlArtifact);
+            if (wsdlArtifact != null) {
+                File supposedFile = wsdlArtifact.getFile();
+                if (!supposedFile.exists() || !supposedFile.isFile()) {
+                    getLog().info("Apparent Maven bug: wsdl artifact 'resolved' to "
+                                      + supposedFile.getAbsolutePath() + " for " + wsdlArtifact.toString());
+                    continue;
+                }
+                String path = supposedFile.getAbsolutePath();
+                getLog().info("Resolved WSDL artifact to file " + path);
+                wsdlOption.setWsdl(path);
+            }
+        }
+    }
+
+    private File getJavaExecutable() throws IOException {
+        String exe = SystemUtils.IS_OS_WINDOWS && !javaExecutable.endsWith(".exe") ? ".exe" : "";
+        File javaExe = new File(javaExecutable + exe);
+
+        if (!javaExe.isFile()) {
+            throw new IOException(
+                                  "The java executable '"
+                                      + javaExe
+                                      + "' doesn't exist or is not a file." 
+                                      + "Verify the <javaExecutable/> parameter.");
+        }
+
+        return javaExe;
+    }
+
     /**
-     * Recursively delete the given directory
+     * Merge WsdlOptions that point to the same file by adding the extraargs to the first option and deleting
+     * the second from the options list
      * 
-     * @param f
-     * @return
+     * @param options
      */
-    private boolean deleteDir(File f) {
-        if (f.isDirectory()) {
-            File files[] = f.listFiles();
-            for (int idx = 0; idx < files.length; ++idx) {
-                deleteDir(files[idx]);
+    private void mergeOptions(List<WsdlOption> effectiveWsdlOptions) {
+        if (wsdlOptions == null) {
+            return;
+        }
+        File outputDirFile = getGeneratedTestRoot() == null 
+                ? getGeneratedSourceRoot() : getGeneratedTestRoot();
+        for (WsdlOption o : wsdlOptions) {
+            if (defaultOptions != null) {
+                o.merge(defaultOptions);
+            }
+            if (o.getOutputDir() == null) {
+                o.setOutputDir(outputDirFile);
+            }
+
+            File file = o.getWsdlFile(project.getBasedir());
+            if (file != null && file.exists()) {
+                file = file.getAbsoluteFile();
+                for (WsdlOption o2 : effectiveWsdlOptions) {
+                    File file2 = o2.getWsdlFile(project.getBasedir());
+                    if (file2 != null && file2.exists() && file2.getAbsoluteFile().equals(file)) {
+                        o.getExtraargs().addAll(0, o2.getExtraargs());
+                        effectiveWsdlOptions.remove(o2);
+                        break;
+                    }
+                }
             }
+            effectiveWsdlOptions.add(o);
         }
+    }
 
-        if (f.exists()) {
-            return f.delete();
+    private Artifact resolveRemoteWsdlArtifact(Artifact artifact) throws MojoExecutionException {
+
+        Collection<String> scopes = new ArrayList<String>();
+        scopes.add(artifact.getScope());
+        Set<Artifact> resolvedArtifacts = null;
+        try {
+            resolvedArtifacts = projectDependencyResolver.resolve(project, scopes, mavenSession);
+        } catch (ArtifactResolutionException e) {
+            throw new MojoExecutionException("Error downloading wsdl artifact.", e);
+        } catch (ArtifactNotFoundException e) {
+            throw new MojoExecutionException("Resource can not be found.", e);
         }
 
-        return true;
+        if (resolvedArtifacts != null && resolvedArtifacts.size() != 0) {
+            for (Artifact pArtifact : resolvedArtifacts) {
+                if ("wsdl".equals(pArtifact.getType())) {
+                    getLog().info(String.format("%s resolved to %s", pArtifact.toString(), pArtifact
+                                      .getFile().getAbsolutePath()));
+                    return pArtifact;
+                }
+            }
+        }
+        throw new MojoExecutionException(String.format("Failed to resolve WSDL artifact %s",
+                                                       artifact.toString()));
     }
 
 }

Modified: cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java
URL: http://svn.apache.org/viewvc/cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java?rev=1213072&r1=1213071&r2=1213072&view=diff
==============================================================================
--- cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java (original)
+++ cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/WSDL2JavaMojo.java Sun Dec 11 21:11:20 2011
@@ -20,42 +20,18 @@
 package org.apache.cxf.maven_plugin;
 
 import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.net.URI;
-import java.net.URL;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.LinkedHashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.commons.lang.SystemUtils;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
-import org.apache.cxf.helpers.FileUtils;
 import org.apache.cxf.tools.common.ToolContext;
 import org.apache.cxf.tools.wsdlto.WSDLToJava;
-import org.apache.maven.ProjectDependenciesResolver;
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
-import org.apache.maven.artifact.resolver.ArtifactResolutionException;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.settings.Proxy;
-import org.codehaus.plexus.archiver.jar.JarArchiver;
-import org.codehaus.plexus.archiver.jar.Manifest;
-import org.codehaus.plexus.archiver.jar.Manifest.Attribute;
-import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.cli.CommandLineException;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
-import org.codehaus.plexus.util.cli.Commandline;
 
 /**
  * @goal wsdl2java
@@ -64,7 +40,7 @@ import org.codehaus.plexus.util.cli.Comm
  * @requiresDependencyResolution test
  * @threadSafe
  */
-public class WSDL2JavaMojo extends AbstractMojo {
+public class WSDL2JavaMojo extends AbstractCodegenMoho {
 
     /**
      * @parameter expression="${cxf.testSourceRoot}"
@@ -80,453 +56,8 @@ public class WSDL2JavaMojo extends Abstr
      */
     File sourceRoot;
 
-    /**
-     * @parameter expression="${project.build.outputDirectory}"
-     * @required
-     */
-    String classesDirectory;
-
-    /**
-     * @parameter expression="${project}"
-     * @required
-     */
-    MavenProject project;
-
-    /**
-     * Default options to be used when a wsdl has not had it's options explicitly specified.
-     * 
-     * @parameter
-     */
-    Option defaultOptions = new Option();
-
-    /**
-     * @parameter
-     */
-    WsdlOption wsdlOptions[];
-
-    /**
-     * @parameter expression="${cxf.wsdlRoot}" default-value="${basedir}/src/main/resources/wsdl"
-     */
-    File wsdlRoot;
-
-    /**
-     * @parameter expression="${cxf.testWsdlRoot}" default-value="${basedir}/src/test/resources/wsdl"
-     */
-    File testWsdlRoot;
-
-    /**
-     * Directory in which the "DONE" markers are saved that
-     * 
-     * @parameter expression="${cxf.markerDirectory}"
-     *            default-value="${project.build.directory}/cxf-codegen-plugin-markers"
-     */
-    File markerDirectory;
-
-    /**
-     * Use the compile classpath rather than the test classpath for execution useful if the test dependencies
-     * clash with those of wsdl2java
-     * 
-     * @parameter expression="${cxf.useCompileClasspath}" default-value="false"
-     */
-    boolean useCompileClasspath;
-    
-    
-    /**
-     * Disables the scanning of the wsdlRoot/testWsdlRoot directories configured above.
-     * By default, we scan for *.wsdl (see include/exclude params as well) in the wsdlRoot
-     * directories and run wsdl2java on all the wsdl's we find.    This disables that scan
-     * and requires an explicit wsdlOption to be set for each wsdl that needs to be processed.
-     * @parameter expression="${cxf.disableDirectoryScan}" default-value="false"
-     */
-    boolean disableDirectoryScan;
-
-    /**
-     * By default all maven dependencies of type "wsdl" are added to the effective wsdlOptions. Setting this
-     * parameter to true disables this functionality
-     * 
-     * @parameter expression="${cxf.disableDependencyScan}" default-value="false"
-     */
-    boolean disableDependencyScan;
-
-    /**
-     * A list of wsdl files to include. Can contain ant-style wildcards and double wildcards. Defaults to
-     * *.wsdl
-     * 
-     * @parameter
-     */
-    String includes[];
-
-    /**
-     * A list of wsdl files to exclude. Can contain ant-style wildcards and double wildcards.
-     * 
-     * @parameter
-     */
-    String excludes[];
-
-    /**
-     * Artifact factory, needed to create artifacts.
-     * 
-     * @component
-     * @readonly
-     * @required
-     */
-    private ArtifactFactory artifactFactory;
-
-    /**
-     * The Maven session.
-     * 
-     * @parameter expression="${session}"
-     * @readonly
-     * @required
-     */
-    private MavenSession mavenSession;
-    
-    /**
-     * @component
-     * @readonly
-     * @required
-     */
-    private ProjectDependenciesResolver projectDependencyResolver;
-
-    /**
-     * The plugin dependencies, needed for the fork mode.
-     *
-     * @parameter expression="${plugin.artifacts}"
-     * @required
-     * @readonly
-     */
-    private List<Artifact> pluginArtifacts;
-    /**
-     * Allows running the JavaToWs in a separate process.
-     * Valid values are "false", "always", and "once"
-     * The value of "true" is equal to "once"
-     *
-     * @parameter default-value="false"
-     * @since 2.4
-     */
-    private String fork;
-
-    /**
-     * Sets the Java executable to use when fork parameter is <code>true</code>.
-     *
-     * @parameter default-value="${java.home}/bin/java"
-     * @since 2.4
-     */
-    private String javaExecutable;
-
-    /**
-     * Sets the JVM arguments (i.e. <code>-Xms128m -Xmx128m</code>) if fork is set to <code>true</code>.
-     *
-     * @parameter
-     * @since 2.4
-     */
-    private String additionalJvmArgs;
-    
-    /**
-     * Merge WsdlOptions that point to the same file by adding the extraargs to the first option and deleting
-     * the second from the options list
-     * 
-     * @param options
-     */
-    private void mergeOptions(List<WsdlOption> effectiveWsdlOptions) {
-        if (wsdlOptions == null) {
-            return;
-        }
-        File outputDirFile = testSourceRoot == null ? sourceRoot : testSourceRoot;
-        for (WsdlOption o : wsdlOptions) {
-            if (defaultOptions != null) {
-                o.merge(defaultOptions);
-            }
-            if (o.getOutputDir() == null) {
-                o.setOutputDir(outputDirFile);
-            }
-
-            File file = o.getWsdlFile(project.getBasedir());
-            if (file != null && file.exists()) {
-                file = file.getAbsoluteFile();
-                for (WsdlOption o2 : effectiveWsdlOptions) {
-                    File file2 = o2.getWsdlFile(project.getBasedir());
-                    if (file2 != null && file2.exists() && file2.getAbsoluteFile().equals(file)) {
-                        o.getExtraargs().addAll(0, o2.getExtraargs());
-                        effectiveWsdlOptions.remove(o2);
-                        break;
-                    }
-                }
-            }
-            effectiveWsdlOptions.add(o);
-        }
-    }
-
-    /**
-     * @return effective WsdlOptions
-     * @throws MojoExecutionException
-     */
-    private List<WsdlOption> createWsdlOptionsFromScansAndExplicitWsdlOptions() 
-        throws MojoExecutionException {
-        List<WsdlOption> effectiveWsdlOptions = new ArrayList<WsdlOption>();
-        List<WsdlOption> temp;
-        if (wsdlRoot != null && wsdlRoot.exists() && !disableDirectoryScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(wsdlRoot, includes, excludes, defaultOptions,
-                                                             sourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        if (testWsdlRoot != null && testWsdlRoot.exists() && !disableDirectoryScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromFiles(testWsdlRoot, includes, excludes,
-                                                             defaultOptions, testSourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        if (!disableDependencyScan) {
-            temp = WsdlOptionLoader.loadWsdlOptionsFromDependencies(project, defaultOptions, sourceRoot);
-            effectiveWsdlOptions.addAll(temp);
-        }
-        mergeOptions(effectiveWsdlOptions);
-        downloadRemoteWsdls(effectiveWsdlOptions);
-//        String buildDir = project.getBuild().getDirectory();
-//        File tempBindingDir = new File(buildDir, TEMPBINDINGS_DIR);
-//        for (WsdlOption o : effectiveWsdlOptions) {
-//            BindingFileHelper.setWsdlLocationInBindingsIfNotSet(project.getBasedir(), tempBindingDir, o,
-//                                                                getLog());
-//        }
-        return effectiveWsdlOptions;
-    }
-    
-    private Artifact resolveRemoteWsdlArtifact(Artifact artifact)
-        throws MojoExecutionException {
-        
-        Collection<String> scopes = new ArrayList<String>();
-        scopes.add(artifact.getScope());
-        Set<Artifact> resolvedArtifacts = null;
-        try {
-            resolvedArtifacts = projectDependencyResolver.resolve(project, scopes, mavenSession);
-        } catch (ArtifactResolutionException e) {
-            throw new MojoExecutionException("Error downloading wsdl artifact.", e);
-        } catch (ArtifactNotFoundException e) {
-            throw new MojoExecutionException("Resource can not be found.", e);
-        } 
-        
-        if (resolvedArtifacts != null && resolvedArtifacts.size() != 0) {
-            for (Artifact pArtifact : resolvedArtifacts) {
-                if ("wsdl".equals(pArtifact.getType())) {
-                    getLog().info(String.format("%s resolved to %s", 
-                                                pArtifact.toString(), pArtifact.getFile().getAbsolutePath()));
-                    return pArtifact;
-                }
-            }
-        }
-        throw new MojoExecutionException(String.format("Failed to resolve WSDL artifact %s", 
-                                                       artifact.toString()));
-    }
-
-    private void downloadRemoteWsdls(List<WsdlOption> effectiveWsdlOptions) throws MojoExecutionException {
-        
-        for (WsdlOption wsdlOption : effectiveWsdlOptions) {
-            WsdlArtifact wsdlA = wsdlOption.getWsdlArtifact();
-            if (wsdlA == null) {
-                return;
-            }
-            Artifact wsdlArtifact = artifactFactory.createArtifact(wsdlA.getGroupId(), wsdlA.getArtifactId(),
-                                                                   wsdlA.getVersion(),
-                                                                   Artifact.SCOPE_COMPILE, wsdlA.getType());
-            wsdlArtifact = resolveRemoteWsdlArtifact(wsdlArtifact);
-            if (wsdlArtifact != null) {
-                File supposedFile = wsdlArtifact.getFile();
-                if (!supposedFile.exists() || !supposedFile.isFile()) {
-                    getLog().info("Apparent Maven bug: wsdl artifact 'resolved' to " 
-                        + supposedFile.getAbsolutePath() + " for " + wsdlArtifact.toString());
-                    continue;
-                }
-                String path = supposedFile.getAbsolutePath();
-                getLog().info("Resolved WSDL artifact to file " + path);
-                wsdlOption.setWsdl(path);
-            }
-        }
-    }
-
-    private void configureProxyServerSettings() throws MojoExecutionException {
-
-        Proxy proxy = mavenSession.getSettings().getActiveProxy();
-
-        if (proxy != null) {
-
-            getLog().info("Using proxy server configured in maven.");
-
-            if (proxy.getHost() == null) {
-                throw new MojoExecutionException("Proxy in settings.xml has no host");
-            } else {
-                System.setProperty("proxySet", "true");
-                System.setProperty("proxyHost", proxy.getHost());
-                System.setProperty("proxyPort", String.valueOf(proxy.getPort()));
-            }
-
-
-        }
-    }
-
-    public void execute() throws MojoExecutionException {
-        if (includes == null) {
-            includes = new String[] {
-                "*.wsdl"
-            };
-        }
-        defaultOptions.addDefaultBindingFileIfExists(project.getBasedir());
-        File classesDir = new File(classesDirectory);
-        classesDir.mkdirs();
-        markerDirectory.mkdirs();
-
-        configureProxyServerSettings();
-
-        List<WsdlOption> effectiveWsdlOptions = createWsdlOptionsFromScansAndExplicitWsdlOptions();
-
-        if (effectiveWsdlOptions.size() == 0) {
-            getLog().info("Nothing to generate");
-            return;
-        }
-
-        ClassLoaderSwitcher classLoaderSwitcher = new ClassLoaderSwitcher(getLog());
-        boolean result = true;
-
-        Bus bus = null;
-        try {
-            Set<URI> cp = classLoaderSwitcher.switchClassLoader(project, useCompileClasspath, classesDir);
-
-            if ("once".equals(fork) || "true".equals(fork)) {
-                forkOnce(cp, effectiveWsdlOptions);
-            } else {
-                for (WsdlOption o : effectiveWsdlOptions) {
-                    bus = callWsdl2Java(o, bus, cp);
-    
-                    File dirs[] = o.getDeleteDirs();
-                    if (dirs != null) {
-                        for (int idx = 0; idx < dirs.length; ++idx) {
-                            result = result && deleteDir(dirs[idx]);
-                        }
-                    }
-                }
-            }
-        } finally {
-            // cleanup as much as we can.
-            if (bus != null) {
-                bus.shutdown(true);
-            }
-            classLoaderSwitcher.restoreClassLoader();
-        }
-        if (project != null && sourceRoot != null && sourceRoot.exists()) {
-            project.addCompileSourceRoot(sourceRoot.getAbsolutePath());
-        }
-        if (project != null && testSourceRoot != null && testSourceRoot.exists()) {
-            project.addTestCompileSourceRoot(testSourceRoot.getAbsolutePath());
-        }
-
-        System.gc();
-    }
-    
-    private void addPluginArtifact(Set<URI> artifactsPath) {
-        //for Maven 2.x, the actual artifact isn't in the list....  need to try and find it
-        URL url = getClass().getResource(getClass().getSimpleName() + ".class");
-        
-        try {
-            if ("jar".equals(url.getProtocol())) {
-                String s = url.getPath();
-                if (s.contains("!")) {
-                    s = s.substring(0, s.indexOf('!'));
-                    url = new URL(s);
-                }
-            }
-            URI uri = new URI(url.getProtocol(), null, url.getPath(), null, null);
-            if (uri.getSchemeSpecificPart().endsWith(".class")) {
-                String s = uri.toString();
-                s = s.substring(0, s.length() - 6 - getClass().getName().length());
-                uri = new URI(s);
-            }
-            File file = new File(uri);
-            if (file.exists()) {
-                artifactsPath.add(file.toURI());
-            }
-        } catch (Exception ex) {
-            //ex.printStackTrace();
-        }
-
-    }
-
-    private void forkOnce(Set<URI> classPath, List<WsdlOption> effectiveWsdlOptions) 
-        throws MojoExecutionException {
-        List<WsdlOption> toDo = new LinkedList<WsdlOption>();
-        List<List<String>> wargs = new LinkedList<List<String>>();
-        for (WsdlOption wsdlOption : effectiveWsdlOptions) {
-            File outputDirFile = wsdlOption.getOutputDir();
-            outputDirFile.mkdirs();
-            URI basedir = project.getBasedir().toURI();
-            URI wsdlURI = wsdlOption.getWsdlURI(basedir);
-            File doneFile = getDoneFile(basedir, wsdlURI);
-
-            if (!shouldRun(wsdlOption, doneFile, wsdlURI)) {
-                continue;
-            }
-            doneFile.delete();
-            
-            toDo.add(wsdlOption);
-            
-            wargs.add(wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog()
-                                                               .isDebugEnabled()));
-        }
-        if (wargs.isEmpty()) {
-            return;
-        }
-        
-        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);
-        
-        String args[] = createForkOnceArgs(wargs);
-        runForked(artifactsPath, ForkOnceWSDL2Java.class, args);
-        
-        for (WsdlOption wsdlOption : toDo) {
-            File dirs[] = wsdlOption.getDeleteDirs();
-            if (dirs != null) {
-                for (int idx = 0; idx < dirs.length; ++idx) {
-                    deleteDir(dirs[idx]);
-                }
-            }
-            URI basedir = project.getBasedir().toURI();
-            URI wsdlURI = wsdlOption.getWsdlURI(basedir);
-            File doneFile = getDoneFile(basedir, wsdlURI);
-            try {
-                doneFile.createNewFile();
-            } catch (Throwable e) {
-                getLog().warn("Could not create marker file " + doneFile.getAbsolutePath());
-                getLog().debug(e);
-            }
-        }
-    }
-
-    private String[] createForkOnceArgs(List<List<String>> wargs) throws MojoExecutionException {
-        try {
-            File f = FileUtils.createTempFile("cxf-w2j", "args");
-            PrintWriter fw = new PrintWriter(new FileWriter(f));
-            for (List<String> args : wargs) {
-                fw.println(Integer.toString(args.size()));
-                for (String s : args) {
-                    fw.println(s);
-                }
-            }
-            fw.println("-1");
-            fw.close();
-            return new String[] {f.getAbsolutePath()};
-        } catch (IOException ex) {
-            throw new MojoExecutionException("Could not create argument file", ex);
-        }
-    }
-
-    private Bus callWsdl2Java(WsdlOption wsdlOption, 
+    @Override
+    protected Bus generate(WsdlOption wsdlOption, 
                               Bus bus,
                               Set<URI> classPath) throws MojoExecutionException {
         File outputDirFile = wsdlOption.getOutputDir();
@@ -580,191 +111,25 @@ public class WSDL2JavaMojo extends Abstr
         } 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());
         }
-        return bus;
-    }
-    private File getJavaExecutable() throws IOException {
-        String exe = (SystemUtils.IS_OS_WINDOWS && !javaExecutable.endsWith(".exe")) ? ".exe" : "";
-        File javaExe = new File(javaExecutable + exe);
-
-        if (!javaExe.isFile()) {
-            throw new IOException("The java executable '" + javaExe
-                + "' doesn't exist or is not a file. Verify the <javaExecutable/> parameter.");
-        }
-
-        return javaExe;
-    }
-
-    private void runForked(Set<URI> classPath, Class cls, String[] args) throws MojoExecutionException {
-        getLog().info("Running wsdl2java in fork mode...");
-        getLog().debug("Running wsdl2java in fork mode with args " + Arrays.asList(args));
-
-        Commandline cmd = new Commandline();
-        cmd.getShell().setQuotedArgumentsEnabled(false); // for JVM args
-        cmd.setWorkingDirectory(project.getBuild().getDirectory());
-        try {
-            cmd.setExecutable(getJavaExecutable().getAbsolutePath());
-        } catch (IOException e) {
-            getLog().debug(e);
-            throw new MojoExecutionException(e.getMessage(), e);
-        }
-
-        cmd.createArg().setLine(additionalJvmArgs);
-        
-        File file = null;
-        try {
-            //file = new File("/tmp/test.jar"); 
-            file = FileUtils.createTempFile("cxf-codegen", ".jar");
-
-            JarArchiver jar = new JarArchiver();
-            jar.setDestFile(file.getAbsoluteFile());
-            
-            Manifest manifest = new Manifest();
-            Attribute attr = new Attribute();
-            attr.setName("Class-Path");
-            StringBuilder b = new StringBuilder(8000);
-            for (URI cp : classPath) {
-                b.append(cp.toURL().toExternalForm()).append(' ');
-            }
-            attr.setValue(b.toString());
-            manifest.getMainSection().addConfiguredAttribute(attr);
-            
-            attr = new Attribute();
-            attr.setName("Main-Class");
-            attr.setValue(cls.getName());
-            manifest.getMainSection().addConfiguredAttribute(attr);
-
-            jar.addConfiguredManifest(manifest);
-            jar.createArchive();
-            
-            cmd.createArg().setValue("-jar");
-            cmd.createArg().setValue(file.getAbsolutePath());
-
-            
-        } catch (Exception e1) {
-            throw new MojoExecutionException("Could not create runtime jar", e1);
-        }
-        cmd.addArguments(args);
-        
-
-        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);
-            }
-
-            StringBuffer msg = new StringBuffer("\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 (file != null) {
-            file.delete();
+        if (project != null && getGeneratedSourceRoot() != null && getGeneratedSourceRoot().exists()) {
+            project.addCompileSourceRoot(getGeneratedSourceRoot().getAbsolutePath());
         }
-        if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("WSDL2Java Error")) {
-            StringBuffer msg = new StringBuffer();
-            msg.append(err.getOutput());
-            msg.append('\n');
-            msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
-            throw new MojoExecutionException(msg.toString());
+        if (project != null && getGeneratedTestRoot() != null && getGeneratedTestRoot().exists()) {
+            project.addTestCompileSourceRoot(getGeneratedTestRoot().getAbsolutePath());
         }
-
-    }
-
-    private File getDoneFile(URI basedir, URI wsdlURI) {
-        String doneFileName = wsdlURI.toString();
-        
-        // Strip the basedir from the doneFileName
-        if (doneFileName.startsWith(basedir.toString())) {
-            doneFileName = doneFileName.substring(basedir.toString().length());
-        }
-
-        // If URL to WSDL, replace ? and & since they're invalid chars for file names
-        // Not to mention slashes.
-        doneFileName = doneFileName.replace('?', '_').replace('&', '_').replace('/', '_').replace('\\', '_')
-            .replace(':', '_');
-
-        return new File(markerDirectory, "." + doneFileName + ".DONE");
+        return bus;
     }
 
-    /**
-     * Determine if code should be generated from the given wsdl
-     * 
-     * @param wsdlOption
-     * @param doneFile
-     * @param wsdlURI
-     * @return
-     */
-    private boolean shouldRun(WsdlOption wsdlOption, File doneFile, URI wsdlURI) {
-        long timestamp = 0;
-        if ("file".equals(wsdlURI.getScheme())) {
-            timestamp = new File(wsdlURI).lastModified();
-        } else {
-            try {
-                timestamp = wsdlURI.toURL().openConnection().getDate();
-            } catch (Exception e) {
-                // ignore
-            }
-        }
-        boolean doWork = false;
-        if (!doneFile.exists()) {
-            doWork = true;
-        } else if (timestamp > doneFile.lastModified()) {
-            doWork = true;
-        } else if (wsdlOption.isDefServiceName()) {
-            doWork = true;
-        } else {
-            File files[] = wsdlOption.getDependencies();
-            if (files != null) {
-                for (int z = 0; z < files.length; ++z) {
-                    if (files[z].lastModified() > doneFile.lastModified()) {
-                        doWork = true;
-                    }
-                }
-            }
-        }
-        return doWork;
+    @Override
+    protected File getGeneratedSourceRoot() {
+        return sourceRoot;
     }
 
-    /**
-     * Recursively delete the given directory
-     * 
-     * @param f
-     * @return
-     */
-    private boolean deleteDir(File f) {
-        if (f.isDirectory()) {
-            File files[] = f.listFiles();
-            for (int idx = 0; idx < files.length; ++idx) {
-                deleteDir(files[idx]);
-            }
-        }
-
-        if (f.exists()) {
-            return f.delete();
-        }
-
-        return true;
+    @Override
+    protected File getGeneratedTestRoot() {
+        return testSourceRoot;
     }
 
 }



Mime
View raw message