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