View Javadoc

1   /**
2    * The MIT License
3    *
4    * Copyright 2006-2012 The Codehaus.
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  package org.codehaus.mojo.appassembler.daemon;
25  
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.mojo.appassembler.daemon.merge.DaemonMerger;
29  import org.codehaus.mojo.appassembler.model.Daemon;
30  import org.codehaus.mojo.appassembler.model.io.stax.AppassemblerModelStaxReader;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import javax.xml.stream.XMLStreamException;
36  import java.io.File;
37  import java.io.FileReader;
38  import java.io.IOException;
39  import java.util.Map;
40  
41  /**
42   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
43   * @version $Id: DefaultDaemonGeneratorService.java 16448 2012-04-22 15:47:18Z khmarbaise $
44   * @plexus.component
45   */
46  public class DefaultDaemonGeneratorService
47          extends AbstractLogEnabled
48          implements DaemonGeneratorService
49  {
50      /**
51       * @plexus.requirement role="org.codehaus.mojo.appassembler.daemon.DaemonGenerator"
52       */
53      private Map generators;
54  
55      /**
56       * @plexus.requirement
57       */
58      private DaemonMerger daemonMerger;
59  
60      // -----------------------------------------------------------------------
61      // DaemonGeneratorService Implementation
62      // -----------------------------------------------------------------------
63  
64      public void generateDaemon ( String platform, File stubDescriptor, File outputDirectory, MavenProject mavenProject,
65              ArtifactRepository localRepository )
66              throws DaemonGeneratorException
67      {
68          generateDaemon ( platform, stubDescriptor, null, outputDirectory, mavenProject, localRepository );
69      }
70  
71      public void generateDaemon ( String platform, File stubDescriptor, Daemon stubDaemon, File outputDirectory,
72              MavenProject mavenProject, ArtifactRepository localRepository )
73              throws DaemonGeneratorException
74      {
75          DaemonGenerationRequest request = new DaemonGenerationRequest ( );
76  
77          request.setPlatform ( platform );
78          request.setStubDescriptor ( stubDescriptor );
79          request.setStubDaemon ( stubDaemon );
80          request.setOutputDirectory ( outputDirectory );
81          request.setMavenProject ( mavenProject );
82          request.setLocalRepository ( localRepository );
83  
84          generateDaemon ( request );
85      }
86  
87      public void generateDaemon ( DaemonGenerationRequest request )
88              throws DaemonGeneratorException
89      {
90          String platform = request.getPlatform ( );
91  
92          if ( platform == null || StringUtils.isEmpty ( platform ) )
93          {
94              throw new DaemonGeneratorException ( "Missing required property in request: platform." );
95          }
96  
97          // -----------------------------------------------------------------------
98          // Get the generator
99          // -----------------------------------------------------------------------
100 
101         DaemonGenerator generator = ( DaemonGenerator ) generators.get ( platform );
102 
103         if ( generator == null )
104         {
105             throw new DaemonGeneratorException ( "Could not find a generator for platform '" + platform + "'." );
106         }
107 
108         // -----------------------------------------------------------------------
109         // Load the model
110         // -----------------------------------------------------------------------
111 
112         Daemon fileDaemon = null;
113 
114         File stubDescriptor = request.getStubDescriptor ( );
115 
116         if ( stubDescriptor != null )
117         {
118             getLogger ( ).debug ( "Loading daemon descriptor: " + stubDescriptor.getAbsolutePath ( ) );
119 
120             fileDaemon = loadModel ( stubDescriptor );
121         }
122 
123         // -----------------------------------------------------------------------
124         // Merge the given stub daemon
125         // -----------------------------------------------------------------------
126 
127         Daemon mergedDaemon = mergeDaemons ( request.getStubDaemon ( ), fileDaemon );
128 
129         // -----------------------------------------------------------------------
130         //
131         // -----------------------------------------------------------------------
132 
133         validateDaemon ( mergedDaemon, stubDescriptor );
134 
135         // -----------------------------------------------------------------------
136         // Generate!
137         // -----------------------------------------------------------------------
138 
139         request.setDaemon ( mergedDaemon );
140 
141         generator.generate ( request );
142     }
143 
144     public Daemon mergeDaemons ( Daemon dominant, Daemon recessive )
145             throws DaemonGeneratorException
146     {
147         return daemonMerger.mergeDaemons ( dominant, recessive );
148     }
149 
150     public Daemon loadModel ( File stubDescriptor )
151             throws DaemonGeneratorException
152     {
153         FileReader fileReader = null;
154 
155         try
156         {
157             fileReader = new FileReader ( stubDescriptor );
158 
159             AppassemblerModelStaxReader reader = new AppassemblerModelStaxReader ( );
160             Daemon stubDaemon = reader.read ( fileReader );
161 
162             validateDaemon ( stubDaemon, stubDescriptor );
163 
164             return stubDaemon;
165         }
166         catch ( IOException e )
167         {
168             throw new DaemonGeneratorException (
169                     "Error while loading daemon descriptor from '" + stubDescriptor.getAbsolutePath ( ) + "'.", e );
170         }
171         catch ( XMLStreamException e )
172         {
173             throw new DaemonGeneratorException (
174                     "Error while loading daemon descriptor from '" + stubDescriptor.getAbsolutePath ( ) + "'.", e );
175         }
176         finally
177         {
178             IOUtil.close ( fileReader );
179         }
180     }
181 
182     public void validateDaemon ( Daemon daemon, File descriptor )
183             throws DaemonGeneratorException
184     {
185         if ( daemon == null )
186         {
187             throw new DaemonGeneratorException ( "Illegal argument: daemon must be passed." );
188         }
189 
190         String mainClass = daemon.getMainClass ( );
191 
192         String missingRequiredField;
193 
194         if ( descriptor != null )
195         {
196             missingRequiredField = "Missing required field from '" + descriptor.getAbsolutePath ( ) + "': ";
197         }
198         else
199         {
200             missingRequiredField = "Missing required field in daemon descriptor: ";
201         }
202 
203         // -----------------------------------------------------------------------
204         //
205         // -----------------------------------------------------------------------
206 
207         if ( StringUtils.isEmpty ( mainClass ) )
208         {
209             throw new DaemonGeneratorException ( missingRequiredField + "main class." );
210         }
211 
212         if ( StringUtils.isEmpty ( daemon.getId ( ) ) )
213         {
214             String id = mainClass;
215 
216             int i = id.lastIndexOf ( '.' );
217 
218             if ( i > 0 )
219             {
220                 id = mainClass.substring ( i + 1 );
221             }
222 
223             id = StringUtils.addAndDeHump ( id );
224 
225             daemon.setId ( id );
226         }
227     }
228 }