You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2010/09/17 03:27:38 UTC

svn commit: r997960 - in /cxf/trunk: maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/

Author: dkulp
Date: Fri Sep 17 01:27:38 2010
New Revision: 997960

URL: http://svn.apache.org/viewvc?rev=997960&view=rev
Log:
[CXF-2988,CXF-2989] Fix issues with generating wrapper beans from
java2ws plugin.

Added:
    cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java   (with props)
Modified:
    cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/Java2WSMojo.java
    cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/RequestWrapper.java
    cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ResponseWrapper.java
    cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/JavaToWSTest.java

Added: cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java
URL: http://svn.apache.org/viewvc/cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java?rev=997960&view=auto
==============================================================================
--- cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java (added)
+++ cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java Fri Sep 17 01:27:38 2010
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.maven_plugin;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Manages switching to the classloader needed for creating the java sources and restoring the old classloader
+ * when finished
+ */
+public class ClassLoaderSwitcher {
+
+    private Log log;
+    private String origClassPath;
+    private Map<Object, Object> origProps;
+    private ClassLoader origContextClassloader;
+
+    public ClassLoaderSwitcher(Log log) {
+        this.log = log;
+    }
+
+    /**
+     * Create and set the classloader that is needed for creating the java sources from wsdl
+     * 
+     * @param project
+     * @param useCompileClasspath
+     * @param classesDir
+     */
+    public String switchClassLoader(MavenProject project,
+                                    boolean useCompileClasspath,
+                                    String classpath,
+                                    List classpathElements) {
+        List<URL> urlList = new ArrayList<URL>();
+        StringBuilder buf = new StringBuilder();
+
+        
+        try {
+            buf.append(classpath);
+            buf.append(File.pathSeparatorChar);
+            urlList.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
+        } catch (MalformedURLException e) {
+            // ignore
+        }
+        for (Object classpathElement : classpathElements) {
+            buf.append(classpathElement.toString());
+            buf.append(File.pathSeparatorChar);
+        }
+
+        buf.append(File.pathSeparatorChar);
+        List<?> artifacts = useCompileClasspath ? project.getCompileArtifacts() : project.getTestArtifacts();
+        for (Artifact a : CastUtils.cast(artifacts, Artifact.class)) {
+            try {
+                if (a.getFile() != null && a.getFile().exists()) {
+                    urlList.add(a.getFile().toURI().toURL());
+                    buf.append(a.getFile().getAbsolutePath());
+                    buf.append(File.pathSeparatorChar);
+                    // System.out.println("     " +
+                    // a.getFile().getAbsolutePath());
+                }
+            } catch (MalformedURLException e) {
+                // ignore
+            }
+        }
+
+        origContextClassloader = Thread.currentThread().getContextClassLoader();
+        URLClassLoader loader = new URLClassLoader(urlList.toArray(new URL[urlList.size()]),
+                                                   origContextClassloader);
+        String newCp = buf.toString();
+
+        log.debug("Classpath: " + urlList.toString());
+
+        origProps = new HashMap<Object, Object>(System.getProperties());
+
+        origClassPath = System.getProperty("java.class.path");
+
+        Thread.currentThread().setContextClassLoader(loader);
+        System.setProperty("java.class.path", newCp);
+        return newCp;
+    }
+
+    /**
+     * Restore the old classloader
+     */
+    public void restoreClassLoader() {
+        Thread.currentThread().setContextClassLoader(origContextClassloader);
+        System.setProperty("java.class.path", origClassPath);
+
+        Map<Object, Object> newProps = new HashMap<Object, Object>(System.getProperties());
+        for (Object o : newProps.keySet()) {
+            if (!origProps.containsKey(o)) {
+                System.clearProperty(o.toString());
+            }
+        }
+        System.getProperties().putAll(origProps);
+    }
+}

Propchange: cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/ClassLoaderSwitcher.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/Java2WSMojo.java
URL: http://svn.apache.org/viewvc/cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/Java2WSMojo.java?rev=997960&r1=997959&r2=997960&view=diff
==============================================================================
--- cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/Java2WSMojo.java (original)
+++ cxf/trunk/maven-plugins/java2ws-plugin/src/main/java/org/apache/cxf/maven_plugin/Java2WSMojo.java Fri Sep 17 01:27:38 2010
@@ -35,7 +35,8 @@ import org.apache.maven.project.MavenPro
 /**
  * @goal java2ws
  * @description CXF Java To Webservice Tool
- */
+ * @requiresDependencyResolution test
+*/
 public class Java2WSMojo extends AbstractMojo {
     /**
      * @parameter 
@@ -142,24 +143,21 @@ public class Java2WSMojo extends Abstrac
     private Boolean attachWsdl;
     
     public void execute() throws MojoExecutionException {
-        StringBuilder buf = new StringBuilder();
-        for (Object classpathElement : classpathElements) {
-            buf.append(classpathElement.toString());
-            buf.append(File.pathSeparatorChar);
-        }
-        String newCp = buf.toString();
-        String cp = System.getProperty("java.class.path");
+        ClassLoaderSwitcher classLoaderSwitcher = new ClassLoaderSwitcher(getLog());
+        
+
         try {
-            System.setProperty("java.class.path", newCp);
-            processJavaClass();
+            String cp = classLoaderSwitcher.switchClassLoader(project, false, 
+                                                              classpath, classpathElements);
+            processJavaClass(cp);
         } finally {
-            System.setProperty("java.class.path", cp);
+            classLoaderSwitcher.restoreClassLoader();
         }
 
         System.gc();
     }
 
-    private void processJavaClass() throws MojoExecutionException {
+    private void processJavaClass(String cp) throws MojoExecutionException {
         List<String> args = new ArrayList<String>();
 
         // outputfile arg
@@ -220,7 +218,7 @@ public class Java2WSMojo extends Abstrac
         
         // classpath arg
         args.add("-cp");
-        args.add(classpath);
+        args.add(cp);
 
         // soap12 arg
         if (soap12 != null && soap12.booleanValue()) {

Modified: cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/RequestWrapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/RequestWrapper.java?rev=997960&r1=997959&r2=997960&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/RequestWrapper.java (original)
+++ cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/RequestWrapper.java Fri Sep 17 01:27:38 2010
@@ -96,6 +96,11 @@ public class RequestWrapper extends Wrap
     @Override
     public WrapperBeanClass getWrapperBeanClass(final Method method) {
         javax.xml.ws.RequestWrapper reqWrapper = method.getAnnotation(javax.xml.ws.RequestWrapper.class);
+        javax.jws.WebMethod webMethod = method.getAnnotation(javax.jws.WebMethod.class);
+        String methName = webMethod == null ? null : webMethod.operationName();
+        if (StringUtils.isEmpty(methName)) {
+            methName = method.getName();
+        }
         String reqClassName = getClassName();
         String reqNs = null;
 
@@ -104,7 +109,7 @@ public class RequestWrapper extends Wrap
             reqNs = reqWrapper.targetNamespace().length() > 0 ? reqWrapper.targetNamespace() : null;
         }
         if (reqClassName == null) {
-            reqClassName = getPackageName(method) + ".jaxws." + StringUtils.capitalize(method.getName());
+            reqClassName = getPackageName(method) + ".jaxws." + StringUtils.capitalize(methName);
         }
 
         WrapperBeanClass jClass = new WrapperBeanClass();

Modified: cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ResponseWrapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ResponseWrapper.java?rev=997960&r1=997959&r2=997960&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ResponseWrapper.java (original)
+++ cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/processor/internal/jaxws/ResponseWrapper.java Fri Sep 17 01:27:38 2010
@@ -129,6 +129,11 @@ public final class ResponseWrapper exten
     @Override
     public WrapperBeanClass getWrapperBeanClass(final Method method) {
         javax.xml.ws.ResponseWrapper resWrapper = method.getAnnotation(javax.xml.ws.ResponseWrapper.class);
+        javax.jws.WebMethod webMethod = method.getAnnotation(javax.jws.WebMethod.class);
+        String methName = webMethod == null ? null : webMethod.operationName();
+        if (StringUtils.isEmpty(methName)) {
+            methName = method.getName();
+        }
         String resClassName = getClassName();
         String resNs = null;
 
@@ -138,7 +143,7 @@ public final class ResponseWrapper exten
         }
         if (resClassName == null) {
             resClassName = getPackageName(method) + ".jaxws."
-                + StringUtils.capitalize(method.getName())
+                + StringUtils.capitalize(methName)
                 + "Response";
         }
 

Modified: cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/JavaToWSTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/JavaToWSTest.java?rev=997960&r1=997959&r2=997960&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/JavaToWSTest.java (original)
+++ cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2ws/JavaToWSTest.java Fri Sep 17 01:27:38 2010
@@ -86,10 +86,10 @@ public class JavaToWSTest extends ToolTe
             "-wsdl", "-wrapperbean", 
             "-s", output.getPath(), 
             "-o", output.getPath() + "/cxf2941.wsdl", 
-            "org.apache.cxf.tools.fortest.cxf2941.WebResultService"
+            org.apache.cxf.tools.fortest.cxf2941.WebResultService.class.getName()
         };
         JavaToWS.main(args);
-        File wrapper = outputFile("org/apache/cxf/tools/fortest/cxf2941/jaxws/HelloResponse.java");
+        File wrapper = outputFile("org/apache/cxf/tools/fortest/cxf2941/jaxws/HelloStringResponse.java");
         String str = FileUtils.getStringFromFile(wrapper);
         assertTrue("namespace value in annoataion @XmlElement is not correct"
                    , str.indexOf("hello/name") > -1);
@@ -107,7 +107,7 @@ public class JavaToWSTest extends ToolTe
             "org.apache.cxf.tools.fortest.cxf2934.WebParamService"
         };
         JavaToWS.main(args);
-        File wrapper = outputFile("org/apache/cxf/tools/fortest/cxf2934/jaxws/HelloResponse.java");
+        File wrapper = outputFile("org/apache/cxf/tools/fortest/cxf2934/jaxws/HelloStringResponse.java");
         String str = FileUtils.getStringFromFile(wrapper);
         assertTrue("namespace value in annoataion @XmlElement is not correct: " + str,
                    str.indexOf("helloString/Name") > -1);