PageRenderTime 47ms CodeModel.GetById 33ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/rmic-maven-plugin-1.1/src/main/java/org/codehaus/mojo/rmic/AbstractRmiMojo.java

#
Java | 394 lines | 196 code | 50 blank | 148 comment | 18 complexity | 65ee027f211725e89a3d675b13d954b9 MD5 | raw file
  1package org.codehaus.mojo.rmic;
  2
  3/*
  4 * Copyright (c) 2004, Codehaus.org
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 * this software and associated documentation files (the "Software"), to deal in
  8 * the Software without restriction, including without limitation the rights to
  9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 10 * of the Software, and to permit persons to whom the Software is furnished to do
 11 * so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in all
 14 * copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22 * SOFTWARE.
 23 */
 24
 25import java.io.File;
 26import java.net.URI;
 27import java.net.URL;
 28import java.net.URLClassLoader;
 29import java.util.ArrayList;
 30import java.util.Collection;
 31import java.util.Collections;
 32import java.util.HashSet;
 33import java.util.Iterator;
 34import java.util.List;
 35import java.util.Set;
 36
 37import org.apache.maven.plugin.AbstractMojo;
 38import org.apache.maven.plugin.MojoExecutionException;
 39import org.apache.maven.project.MavenProject;
 40import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
 41import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
 42import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
 43import org.codehaus.plexus.util.StringUtils;
 44
 45/**
 46 * Generic super class of rmi compiler mojos.
 47 * 
 48 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 49 * @version $Id: AbstractRmiMojo.java 8127 2008-11-21 19:05:31Z pgier $
 50 */
 51public abstract class AbstractRmiMojo
 52    extends AbstractMojo
 53    implements RmicConfig
 54{
 55    // ----------------------------------------------------------------------
 56    // Configurable parameters
 57    // ----------------------------------------------------------------------
 58
 59    /**
 60     * A list of inclusions when searching for classes to compile.
 61     * 
 62     * @parameter
 63     */
 64    protected Set includes;
 65
 66    /**
 67     * A list of exclusions when searching for classes to compile.
 68     * 
 69     * @parameter
 70     */
 71    protected Set excludes;
 72
 73    /**
 74     * The id of the rmi compiler to use.
 75     * 
 76     * @parameter default-value="sun"
 77     */
 78    protected String compilerId;
 79
 80    /**
 81     * The version of the rmi protocol to which the stubs should be compiled. Valid values include 1.1, 1.2, compat. See
 82     * the rmic documentation for more information. If nothing is specified the underlying rmi compiler will
 83     * choose the default value.  For example, in sun jdk 1.5 the default is 1.2.
 84     * 
 85     * @parameter
 86     */
 87    private String version;
 88
 89    /**
 90     * Create stubs for IIOP.
 91     * 
 92     * @parameter default-value="false"
 93     */
 94    private boolean iiop;
 95
 96    /**
 97     * Do not create stubs optimized for same process.
 98     * 
 99     * @parameter
100     */
101    private boolean noLocalStubs;
102
103    /**
104     * Create IDL.
105     * 
106     * @parameter default-value="false"
107     */
108    private boolean idl;
109
110    /**
111     * Do not generate methods for valuetypes.
112     * 
113     * @parameter
114     */
115    private boolean noValueMethods;
116
117    /**
118     * Do not delete intermediate generated source files.
119     * 
120     * @parameter default-value="false"
121     */
122    private boolean keep;
123
124    /**
125     * Turn off rmic warnings.
126     * 
127     * @parameter
128     */
129    private boolean nowarn;
130
131    /**
132     * Enable verbose rmic output.
133     * 
134     * @parameter
135     */
136    private boolean verbose;
137
138    /**
139     * Time in milliseconds between automatic recompilations. A value of 0 means that up to date rmic output classes
140     * will not be recompiled until the source classes change.
141     * 
142     * @parameter default-value=0
143     */
144    private int staleMillis;
145
146    // ----------------------------------------------------------------------
147    // Constant parameters
148    // ----------------------------------------------------------------------
149
150    /**
151     * @component org.apache.maven.plugin.rmic.RmiCompilerManager
152     */
153    private RmiCompilerManager rmiCompilerManager;
154
155    /**
156     * The maven project.
157     * 
158     * @parameter expression="${project}"
159     * @readonly
160     */
161    protected MavenProject project;
162
163    /**
164     * Get the list of elements to add to the classpath of rmic
165     * 
166     * @return list of classpath elements
167     */
168    public abstract List getProjectClasspathElements();
169
170    /**
171     * Get the directory where rmic generated class files are written.
172     * 
173     * @return the directory
174     */
175    public abstract File getOutputDirectory();
176
177    /**
178     * Get the directory where Remote impl classes are located.
179     * 
180     * @return path to compiled classes
181     */
182    public abstract File getClassesDirectory();
183
184    /**
185     * Main mojo execution.
186     * 
187     * @throws MojoExecutionException if there is a problem executing the mojo.
188     */
189    public void execute()
190        throws MojoExecutionException
191    {
192        if ( this.includes == null )
193        {
194            this.includes = Collections.singleton( "**/*" );
195        }
196
197        if ( this.excludes == null )
198        {
199            this.excludes = new HashSet();
200        }
201        // Exclude _Stub files from being recompiled by rmic.
202        excludes.add( "**/*_Stub.class" );
203
204        RmiCompiler rmiCompiler;
205
206        try
207        {
208            rmiCompiler = rmiCompilerManager.getRmiCompiler( compilerId );
209        }
210        catch ( NoSuchRmiCompilerException e )
211        {
212            throw new MojoExecutionException( "No such RMI compiler installed '" + compilerId + "'." );
213        }
214
215        if ( !getOutputDirectory().isDirectory() )
216        {
217            if ( !getOutputDirectory().mkdirs() )
218            {
219                throw new MojoExecutionException( "Could not make output directory: " + "'" 
220                                                  + getOutputDirectory().getAbsolutePath() + "'." );
221            }
222        }
223
224        try
225        {
226            // Get the list of classes to compile
227            List remoteClassesToCompile = scanForRemoteClasses();
228
229            if ( remoteClassesToCompile.size() == 0 )
230            {
231                getLog().info( "No out of date rmi classes to process." );
232                return;
233            }
234
235            getLog().info( "Compiling " + remoteClassesToCompile.size() + " remote classes" );
236            rmiCompiler.execute( this, remoteClassesToCompile );
237        }
238        catch ( RmiCompilerException e )
239        {
240            throw new MojoExecutionException( "Error while executing the RMI compiler.", e );
241        }
242    }
243
244    /**
245     * Get the list of elements to add to the classpath of rmic
246     * 
247     * @return list of classpath elements
248     */
249    public List getRmicClasspathElements()
250    {
251        List classpathElements = getProjectClasspathElements();
252        
253        if ( !classpathElements.contains( getClassesDirectory().getAbsolutePath() ) )
254        {
255            classpathElements.add( getClassesDirectory().getAbsolutePath() );
256        }
257
258        return classpathElements;
259    }
260
261    /**
262     * Search the input directory for classes to compile.
263     * 
264     * @return a list of class names to rmic
265     */
266    public List scanForRemoteClasses()
267    {
268        List remoteClasses = new ArrayList();
269
270        try
271        {
272            // Set up the classloader
273            List classpathList = generateUrlCompileClasspath();
274            URL[] classpathUrls = new URL[classpathList.size()];
275            classpathUrls[0] = getClassesDirectory().toURL();
276            classpathUrls = (URL[]) classpathList.toArray( classpathUrls );
277            URLClassLoader loader = new URLClassLoader( classpathUrls );
278
279            // Scan for remote classes
280            SourceInclusionScanner scanner = new StaleSourceScanner( staleMillis, this.includes, this.excludes );
281            scanner.addSourceMapping( new SuffixMapping( ".class", "_Stub.class" ) );
282            Collection staleRemoteClasses = scanner.getIncludedSources( getClassesDirectory(), getOutputDirectory() );
283
284            for ( Iterator iter = staleRemoteClasses.iterator(); iter.hasNext(); )
285            {
286                // Get the classname and load the class
287                File remoteClassFile = (File) iter.next();
288                URI relativeURI = getClassesDirectory().toURI().relativize( remoteClassFile.toURI() );
289                String className =
290                    StringUtils.replace( StringUtils.replace( relativeURI.toString(), ".class", "" ), "/", "." );
291                Class remoteClass = loader.loadClass( className );
292
293                // Check that each class implements java.rmi.Remote, ignore interfaces unless in IIOP mode
294                if ( java.rmi.Remote.class.isAssignableFrom( remoteClass ) 
295                                && ( !remoteClass.isInterface() || isIiop() ) )
296                {
297                    remoteClasses.add( className );
298                }
299            }
300
301            // Check for classes in a classpath jar
302            for ( Iterator iter = includes.iterator(); iter.hasNext(); )
303            {
304                String include = (String) iter.next();
305                File includeFile = new File( getClassesDirectory(), include );
306                if ( ( include.indexOf( "*" ) != -1 ) || includeFile.exists() )
307                {
308                    continue;
309                }
310                // We have found a class that is not in the classes dir.
311                String fqClassName = StringUtils.replace( StringUtils.replace( include, ".class", "" ), "/", "." );
312                remoteClasses.add( fqClassName );
313            }
314        }
315        catch ( Exception e )
316        {
317            getLog().warn( "Problem while scanning for classes: " + e );
318        }
319        return remoteClasses;
320    }
321
322    /**
323     * Returns a list of URL objects that represent the classpath elements. This is useful for using a URLClassLoader
324     * 
325     * @return list of url classpath elements
326     */
327    protected List generateUrlCompileClasspath()
328        throws MojoExecutionException
329    {
330        List rmiCompileClasspath = new ArrayList();
331        try
332        {
333            rmiCompileClasspath.add( getClassesDirectory().toURL() );
334            Iterator iter = getRmicClasspathElements().iterator();
335            while ( iter.hasNext() )
336            {
337                URL pathUrl = new File( (String) iter.next() ).toURL();
338                rmiCompileClasspath.add( pathUrl );
339            }
340        }
341        catch ( Exception e )
342        {
343            e.printStackTrace();
344            throw new MojoExecutionException( "Problem while generating classpath: " + e.getMessage() );
345        }
346        return rmiCompileClasspath;
347    }
348
349    public String getCompilerId()
350    {
351        return compilerId;
352    }
353
354    public boolean isIiop()
355    {
356        return iiop;
357    }
358
359    public boolean isIdl()
360    {
361        return idl;
362    }
363
364    public boolean isKeep()
365    {
366        return keep;
367    }
368
369    public String getVersion()
370    {
371        return version;
372    }
373
374    public boolean isNowarn()
375    {
376        return nowarn;
377    }
378
379    public boolean isVerbose()
380    {
381        return verbose;
382    }
383
384    public boolean isNoLocalStubs()
385    {
386        return noLocalStubs;
387    }
388
389    public boolean isNoValueMethods()
390    {
391        return noValueMethods;
392    }
393
394}