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