View Javadoc
1   package org.codehaus.mojo.appassembler.daemon.generic;
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.FileOutputStream;
29  import java.io.IOException;
30  import java.io.OutputStreamWriter;
31  import java.util.Iterator;
32  
33  import javax.xml.stream.XMLStreamException;
34  
35  import org.apache.maven.artifact.Artifact;
36  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
39  import org.codehaus.mojo.appassembler.daemon.DaemonGenerator;
40  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
41  import org.codehaus.mojo.appassembler.daemon.merge.DaemonMerger;
42  import org.codehaus.mojo.appassembler.model.Classpath;
43  import org.codehaus.mojo.appassembler.model.Daemon;
44  import org.codehaus.mojo.appassembler.model.Dependency;
45  import org.codehaus.mojo.appassembler.model.io.stax.AppassemblerModelStaxWriter;
46  import org.codehaus.mojo.appassembler.util.DependencyFactory;
47  import org.codehaus.plexus.logging.AbstractLogEnabled;
48  import org.codehaus.plexus.util.FileUtils;
49  import org.codehaus.plexus.util.IOUtil;
50  
51  /**
52   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
53   * @version $Id$
54   * @plexus.component role-hint="generic"
55   */
56  public class GenericDaemonGenerator
57      extends AbstractLogEnabled
58      implements DaemonGenerator
59  {
60      /**
61       * @plexus.requirement
62       */
63      private DaemonMerger daemonMerger;
64  
65      // -----------------------------------------------------------------------
66      // DaemonGenerator Implementation
67      // -----------------------------------------------------------------------
68  
69      /*
70       * (non-Javadoc)
71       * @see org.codehaus.mojo.appassembler.daemon.DaemonGenerator#generate(org.codehaus.mojo.appassembler.daemon.
72       * DaemonGenerationRequest)
73       */
74      public void generate( DaemonGenerationRequest request )
75          throws DaemonGeneratorException
76      {
77          // -----------------------------------------------------------------------
78          // Create the daemon from the Maven project
79          // -----------------------------------------------------------------------
80  
81          Daemon createdDaemon =
82              createDaemon( request.getMavenProject(), request.getRepositoryLayout(), request.getOutputFileNameMapping() );
83  
84          // -----------------------------------------------------------------------
85          // Merge the given stub daemon and the generated
86          // -----------------------------------------------------------------------
87  
88          Daemon mergedDaemon = daemonMerger.mergeDaemons( request.getDaemon(), createdDaemon );
89  
90          // -----------------------------------------------------------------------
91          // Write out the project
92          // -----------------------------------------------------------------------
93  
94          OutputStreamWriter writer = null;
95  
96          try
97          {
98  
99              FileUtils.forceMkdir( request.getOutputDirectory() );
100 
101             File outputFile = new File( request.getOutputDirectory(), mergedDaemon.getId() + ".xml" );
102 
103             FileOutputStream fos = new FileOutputStream( outputFile );
104 
105             writer = new OutputStreamWriter( fos, "UTF-8" );
106 
107             AppassemblerModelStaxWriter staxWriter = new AppassemblerModelStaxWriter();
108             staxWriter.write( writer, mergedDaemon );
109         }
110         catch ( IOException e )
111         {
112             throw new DaemonGeneratorException( "Error while writing output file: " + request.getOutputDirectory(), e );
113         }
114         catch ( XMLStreamException e )
115         {
116             throw new DaemonGeneratorException( "Error while writing output file: " + request.getOutputDirectory(), e );
117         }
118         finally
119         {
120             IOUtil.close( writer );
121         }
122     }
123 
124     // -----------------------------------------------------------------------
125     // Private
126     // -----------------------------------------------------------------------
127 
128     private Daemon createDaemon( MavenProject project, ArtifactRepositoryLayout layout, String outputFileNameMapping )
129     {
130         Daemon complete = new Daemon();
131 
132         complete.setClasspath( new Classpath() );
133 
134         // -----------------------------------------------------------------------
135         // Add the project itself as a dependency.
136         // -----------------------------------------------------------------------
137         complete.getClasspath().addDependency( DependencyFactory.create( project.getArtifact(), layout,
138                                                                          outputFileNameMapping ) );
139 
140         // -----------------------------------------------------------------------
141         // Add all the dependencies of the project.
142         // -----------------------------------------------------------------------
143         for ( Iterator it = project.getRuntimeArtifacts().iterator(); it.hasNext(); )
144         {
145             Artifact artifact = (Artifact) it.next();
146 
147             Dependency dependency = DependencyFactory.create( artifact, layout, outputFileNameMapping );
148 
149             complete.getClasspath().addDependency( dependency );
150         }
151 
152         return complete;
153     }
154 }