View Javadoc

1   /*
2    * Copyright 2006 Guillaume Nodet.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.mojo.jaxws;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  
22  import org.apache.maven.model.Resource;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  
26  import com.sun.tools.ws.wscompile.WsgenTool;
27  
28  /**
29   * <p>
30   * A Maven 2 plugin which reads a service endpoint implementation class 
31   *   and generates all of the portable artifacts for a JAX-WS web service.
32   * </p>
33   * 
34   * @goal wsgen
35   * @phase generate-sources
36   * @requiresDependencyResolution
37   * @description JAXWS 2.x Plugin.
38   * @author gnodet <gnodet@apache.org>
39   * @author dantran <dantran@apache.org>
40   * @version $Id: WsGenMojo.java 3169 2007-01-22 02:51:29Z dantran $
41   */
42  public class WsGenMojo
43      extends AbstractJaxwsMojo
44  {
45  
46      /**
47       * Specify that a WSDL file should be generated in ${resourceDestDir}
48       * 
49       * @parameter default-value="false"
50       */
51      private boolean genWsdl;
52  
53      
54      /**
55       * Directory containing the generated wsdl files.
56       * 
57       * @parameter default-value="${project.build.directory}/jaxws/wsgen/wsdl"
58       */
59      
60      private File resourceDestDir;
61  
62      /**
63       * service endpoint implementation class name.
64       * 
65       * @parameter 
66       * @required
67       */
68      private String sei;
69  
70      /**
71       * Used in conjunction with genWsdl to specify the protocol to use in the 
72       * wsdl:binding.  Value values are "soap1.1" or "Xsoap1.2", default is "soap1.1". 
73       * "Xsoap1.2" is not standard and can only be used in conjunction with the 
74       * -extensions option
75       * 
76       * @parameter 
77       */
78      private String protocol;
79  
80      
81      /**
82       * Specify where to place generated source files, keep is turned on with this option. 
83       * 
84       * @parameter 
85       */
86      private File sourceDestDir;
87      //default-value="${project.build.directory}/jaxws/java"
88      
89      public void execute()
90          throws MojoExecutionException, MojoFailureException
91      {
92          init();
93  
94          // Need to build a URLClassloader since Maven removed it form the chain
95          ClassLoader parent = this.getClass().getClassLoader();
96          String orginalSystemClasspath = this.initClassLoader( parent );
97  
98          try
99          {
100             ArrayList<String> args = getWsGenArgs();
101             args.add( this.sei );
102 
103             WsgenTool compTool = new WsgenTool( System.out );
104             if ( !compTool.run( args.toArray( new String[args.size()] ) ) )
105             {
106                 throw new MojoExecutionException( "Error executing: wsimport " + args );
107             }
108             
109         }
110         finally
111         {
112             // Set back the old classloader
113             Thread.currentThread().setContextClassLoader( parent );
114             System.setProperty( "java.class.path", orginalSystemClasspath );
115         }
116     }
117 
118     private void init()
119         throws MojoExecutionException, MojoFailureException
120     {
121         if ( !destDir.exists() )
122         {
123             destDir.mkdirs();
124         }
125     }
126 
127     /**
128      * Construct wsgen arguments
129      * @return a list of arguments
130      * @throws MojoExecutionException
131      */
132     private ArrayList<String> getWsGenArgs()
133         throws MojoExecutionException
134     {
135         ArrayList<String> args = new ArrayList<String>();
136 
137         if ( verbose )
138         {
139             args.add( "-verbose" );
140         }
141 
142         if ( keep )
143         {
144             args.add( "-keep" );
145         }
146 
147         if ( this.sourceDestDir != null )
148         {
149             args.add( "-s" );
150             args.add( this.sourceDestDir.getAbsolutePath() );
151             this.sourceDestDir.mkdirs();
152         }
153 
154         args.add( "-d" );
155         args.add( this.destDir.getAbsolutePath() );
156         this.destDir.mkdirs();
157 
158         if ( this.genWsdl )
159         {
160             if ( this.protocol != null )
161             {
162                 args.add( "-wsdl:" + this.protocol );
163             }
164             else
165             {
166                 args.add( "-wsdl" );
167             }
168 
169             args.add( "-r" );
170             args.add( this.resourceDestDir.getAbsolutePath() );
171             this.resourceDestDir.mkdirs();
172 
173         }
174 
175         args.add( sei );
176 
177         getLog().info( "jaxws:wsgen args: " + args );
178 
179         return args;
180     }
181 
182 }