/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/daemon/DefaultDaemonGeneratorService.java

https://github.com/cdegroot/appassembler · Java · 229 lines · 130 code · 44 blank · 55 comment · 14 complexity · 93b1b09935757fe08729af220dc0b89c MD5 · raw file

  1. package org.codehaus.mojo.appassembler.daemon;
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2005-2007 The Codehaus.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is furnished to do
  12. * so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. import org.apache.maven.artifact.repository.ArtifactRepository;
  26. import org.apache.maven.project.MavenProject;
  27. import org.codehaus.mojo.appassembler.daemon.merge.DaemonMerger;
  28. import org.codehaus.mojo.appassembler.model.Daemon;
  29. import org.codehaus.mojo.appassembler.model.io.stax.AppassemblerModelStaxReader;
  30. import org.codehaus.plexus.logging.AbstractLogEnabled;
  31. import org.codehaus.plexus.util.IOUtil;
  32. import org.codehaus.plexus.util.StringUtils;
  33. import javax.xml.stream.XMLStreamException;
  34. import java.io.File;
  35. import java.io.FileReader;
  36. import java.io.IOException;
  37. import java.util.Map;
  38. /**
  39. * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
  40. * @version $Id: DefaultDaemonGeneratorService.java 6952 2008-05-12 07:26:23Z brett $
  41. * @plexus.component
  42. */
  43. public class DefaultDaemonGeneratorService
  44. extends AbstractLogEnabled
  45. implements DaemonGeneratorService
  46. {
  47. /**
  48. * @plexus.requirement role="org.codehaus.mojo.appassembler.daemon.DaemonGenerator"
  49. */
  50. private Map generators;
  51. /**
  52. * @plexus.requirement
  53. */
  54. private DaemonMerger daemonMerger;
  55. // -----------------------------------------------------------------------
  56. // DaemonGeneratorService Implementation
  57. // -----------------------------------------------------------------------
  58. public void generateDaemon( String platform, File stubDescriptor, File outputDirectory, MavenProject mavenProject,
  59. ArtifactRepository localRepository )
  60. throws DaemonGeneratorException
  61. {
  62. generateDaemon( platform, stubDescriptor, null, outputDirectory, mavenProject, localRepository );
  63. }
  64. public void generateDaemon( String platform, File stubDescriptor, Daemon stubDaemon, File outputDirectory,
  65. MavenProject mavenProject, ArtifactRepository localRepository )
  66. throws DaemonGeneratorException
  67. {
  68. DaemonGenerationRequest request = new DaemonGenerationRequest();
  69. request.setPlatform( platform );
  70. request.setStubDescriptor( stubDescriptor );
  71. request.setStubDaemon( stubDaemon );
  72. request.setOutputDirectory( outputDirectory );
  73. request.setMavenProject( mavenProject );
  74. request.setLocalRepository( localRepository );
  75. generateDaemon( request );
  76. }
  77. public void generateDaemon( DaemonGenerationRequest request )
  78. throws DaemonGeneratorException
  79. {
  80. String platform = request.getPlatform();
  81. if ( platform == null || StringUtils.isEmpty( platform ) )
  82. {
  83. throw new DaemonGeneratorException( "Missing required property in request: platform." );
  84. }
  85. // -----------------------------------------------------------------------
  86. // Get the generator
  87. // -----------------------------------------------------------------------
  88. DaemonGenerator generator = (DaemonGenerator) generators.get( platform );
  89. if ( generator == null )
  90. {
  91. throw new DaemonGeneratorException( "Could not find a generator for platform '" + platform + "'." );
  92. }
  93. // -----------------------------------------------------------------------
  94. // Load the model
  95. // -----------------------------------------------------------------------
  96. Daemon fileDaemon = null;
  97. File stubDescriptor = request.getStubDescriptor();
  98. if ( stubDescriptor != null )
  99. {
  100. getLogger().debug( "Loading daemon descriptor: " + stubDescriptor.getAbsolutePath() );
  101. fileDaemon = loadModel( stubDescriptor );
  102. }
  103. // -----------------------------------------------------------------------
  104. // Merge the given stub daemon
  105. // -----------------------------------------------------------------------
  106. Daemon mergedDaemon = mergeDaemons( request.getStubDaemon(), fileDaemon );
  107. // -----------------------------------------------------------------------
  108. //
  109. // -----------------------------------------------------------------------
  110. validateDaemon( mergedDaemon, stubDescriptor );
  111. // -----------------------------------------------------------------------
  112. // Generate!
  113. // -----------------------------------------------------------------------
  114. request.setDaemon( mergedDaemon );
  115. generator.generate( request );
  116. }
  117. public Daemon mergeDaemons( Daemon dominant, Daemon recessive )
  118. throws DaemonGeneratorException
  119. {
  120. return daemonMerger.mergeDaemons( dominant, recessive );
  121. }
  122. public Daemon loadModel( File stubDescriptor )
  123. throws DaemonGeneratorException
  124. {
  125. FileReader fileReader = null;
  126. try
  127. {
  128. fileReader = new FileReader( stubDescriptor );
  129. AppassemblerModelStaxReader reader = new AppassemblerModelStaxReader();
  130. Daemon stubDaemon = reader.read( fileReader );
  131. validateDaemon( stubDaemon, stubDescriptor );
  132. return stubDaemon;
  133. }
  134. catch ( IOException e )
  135. {
  136. throw new DaemonGeneratorException(
  137. "Error while loading daemon descriptor from '" + stubDescriptor.getAbsolutePath() + "'.", e );
  138. }
  139. catch ( XMLStreamException e )
  140. {
  141. throw new DaemonGeneratorException(
  142. "Error while loading daemon descriptor from '" + stubDescriptor.getAbsolutePath() + "'.", e );
  143. }
  144. finally
  145. {
  146. IOUtil.close( fileReader );
  147. }
  148. }
  149. public void validateDaemon( Daemon daemon, File descriptor )
  150. throws DaemonGeneratorException
  151. {
  152. if ( daemon == null )
  153. {
  154. throw new DaemonGeneratorException( "Illegal argument: daemon must be passed." );
  155. }
  156. String mainClass = daemon.getMainClass();
  157. String missingRequiredField;
  158. if ( descriptor != null )
  159. {
  160. missingRequiredField = "Missing required field from '" + descriptor.getAbsolutePath() + "': ";
  161. }
  162. else
  163. {
  164. missingRequiredField = "Missing required field in daemon descriptor: ";
  165. }
  166. // -----------------------------------------------------------------------
  167. //
  168. // -----------------------------------------------------------------------
  169. if ( StringUtils.isEmpty( mainClass ) )
  170. {
  171. throw new DaemonGeneratorException( missingRequiredField + "main class." );
  172. }
  173. if ( StringUtils.isEmpty( daemon.getId() ) )
  174. {
  175. String id = mainClass;
  176. int i = id.lastIndexOf( '.' );
  177. if ( i > 0 )
  178. {
  179. id = mainClass.substring( i + 1 );
  180. }
  181. id = StringUtils.addAndDeHump( id );
  182. daemon.setId( id );
  183. }
  184. }
  185. }