001    /*
002     * Licensed under the Apache License, Version 2.0 (the "License" );
003     * you may not use this file except in compliance with the License.
004     * You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     * Unless required by applicable law or agreed to in writing, software
009     * distributed under the License is distributed on an "AS IS" BASIS,
010     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     * See the License for the specific language governing permissions and
012     * limitations under the License.
013     */
014    package org.apache.servicemix.maven.plugin.xfire;
015    
016    import org.apache.maven.plugin.AbstractMojo;
017    import org.apache.maven.plugin.MojoExecutionException;
018    import org.apache.maven.project.MavenProject;
019    import org.apache.tools.ant.BuildException;
020    import org.apache.tools.ant.Project;
021    import org.apache.tools.ant.BuildListener;
022    import org.apache.tools.ant.BuildEvent;
023    
024    import org.codehaus.xfire.gen.WsGenTask;
025    
026    import java.io.*;
027    import java.util.Iterator;
028    import java.util.List;
029    
030    /**
031     * WsGen mojo.
032     * <p/>
033     * Implemented as a wrapper around the XFire WsGen Ant task.
034     *
035     * @author <a href="jerome@coffeebreaks.org">Jerome Lacoste</a>
036     * @version $Id$
037     * @goal wsgen
038     * @phase generate-sources
039     * @requiresProject
040     * @requiresDependencyResolution
041     */
042    public class WsgenMojo
043        extends AbstractMojo
044    {
045        /**
046         * Project.
047         *
048         * @parameter expression="${project}"
049         * @required
050         * @readonly
051         */
052        private MavenProject project;
053    
054        /**
055         * URLs
056         *
057         * @parameter
058         * @required
059         */
060        private List wsdls;
061    
062        /**
063         * @parameter expression="${package}" alias="package"
064         */
065        private String _package; // reserved keyword...
066    
067        /**
068         * @parameter expression="${profile}"
069         */
070        private String profile;
071    
072        /**
073         * @parameter expression="${binding}"
074         */
075        private String binding;
076    
077        /**
078         * Will be added to the compileSourceRoot
079         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/xfire/wsgen"
080         * @required
081         */
082        private File outputDirectory;
083    
084        private PrintStream systemErr;
085        private PrintStream systemOut;
086        private final PrintStream mySystemErr = new PrintStream(new MyErrorStream());
087        private final PrintStream mySystemOut = new PrintStream(new MyOutputStream());
088    
089        public void execute()
090            throws MojoExecutionException
091        {
092    
093            systemErr = System.err;
094            systemOut = System.out;
095            System.setErr(mySystemErr);
096            // System.setOut(mySystemOut); // causes java.lang.OutOfMemoryError: Java heap space  on my box
097    
098            try {
099                exec();
100            } finally {
101                System.setErr( systemErr );
102                // System.setOut( systemOut );
103            }
104        }
105    
106        class MyErrorStream extends OutputStream {
107            private StringBuffer buffer = new StringBuffer();
108    
109            public void write( final int b ) throws IOException {
110                final char c = (char) b;
111                // shouldn't we handle '\r' as well ??
112                if (c == '\n') {
113                    getLog().error( buffer );
114                    buffer = new StringBuffer();
115                } else {
116                    buffer.append( c );
117                }
118            }
119        }
120    
121        class MyOutputStream extends OutputStream {
122            private StringBuffer buffer = new StringBuffer();
123    
124            public void write( final int b ) throws IOException {
125                final char c = (char) b;
126                // shouldn't we handle '\r' as well ??
127                if (c == '\n') {
128                    getLog().info( buffer );
129                    buffer = new StringBuffer();
130                } else {
131                    buffer.append( c );
132                }
133            }
134        }
135    
136        private void exec() throws MojoExecutionException {
137    
138            if ( wsdls.size() == 0 ) {
139                return;
140            }
141    
142            if ( ! outputDirectory.exists() && ! outputDirectory.mkdirs() ) {
143               getLog().warn( "the output directory " + outputDirectory
144                       + " doesn't exist and couldn't be created. The goal with probably fail." );
145            }
146    
147            final Project antProject = new Project();
148    
149            antProject.addBuildListener(new DebugAntBuildListener());
150    
151            final WsGenTask task = new WsGenTask();
152    
153            task.setProject( antProject );
154    
155            if ( binding != null) {
156                task.setBinding( binding );
157            }
158    
159            if ( profile != null) {
160                task.setProfile( profile );
161            }
162    
163            if ( _package != null) {
164                task.setPackage( _package );
165            }
166    
167            task.setOutputDirectory( outputDirectory.getAbsolutePath() );
168    
169            for (Iterator iterator = wsdls.iterator(); iterator.hasNext();) {
170                String wsdlUrl = (String) iterator.next();
171    
172                if ( ! wsdlUrl.contains("://") ) {
173                    wsdlUrl = new File( wsdlUrl ).toURI().toString();
174                }
175    
176                task.setWsdl( wsdlUrl );
177    
178                getLog().info( "Executing XFire WsGen task with url: " + wsdlUrl );
179    
180                try
181                {
182                    task.execute();
183                }
184                catch ( BuildException e )
185                {
186                    throw new MojoExecutionException( "command execution failed", e );
187                }
188            }
189    
190            getLog().debug( "Adding outputDirectory to source root: " + outputDirectory );
191    
192            this.project.addCompileSourceRoot( outputDirectory.getAbsolutePath() );
193        }
194    
195        private class DebugAntBuildListener implements BuildListener {
196            public void buildStarted( final BuildEvent buildEvent ) {
197                getLog().debug(buildEvent.getMessage());
198            }
199    
200            public void buildFinished( final BuildEvent buildEvent ) {
201                getLog().debug(buildEvent.getMessage());
202            }
203    
204            public void targetStarted( final BuildEvent buildEvent ) {
205                getLog().debug(buildEvent.getMessage());
206            }
207    
208            public void targetFinished( final BuildEvent buildEvent ) {
209                getLog().debug(buildEvent.getMessage());
210            }
211    
212            public void taskStarted( final BuildEvent buildEvent ) {
213                getLog().debug(buildEvent.getMessage());
214            }
215    
216            public void taskFinished( final BuildEvent buildEvent ) {
217                getLog().debug(buildEvent.getMessage());
218            }
219    
220            public void messageLogged( final BuildEvent buildEvent ) {
221                getLog().debug(buildEvent.getMessage());
222            }
223        }
224    }