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.booter;
25  
26  import java.io.File;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
32  import org.codehaus.mojo.appassembler.daemon.DaemonGenerator;
33  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
34  import org.codehaus.mojo.appassembler.daemon.script.AbstactScriptDaemonGenerator;
35  import org.codehaus.mojo.appassembler.model.Classpath;
36  import org.codehaus.mojo.appassembler.model.Daemon;
37  import org.codehaus.mojo.appassembler.model.Dependency;
38  import org.codehaus.mojo.appassembler.model.Directory;
39  import org.codehaus.mojo.appassembler.model.JvmSettings;
40  
41  /**
42   * This contains all common code which is used in the {@link UnixBooterDaemonGenerator} and in the
43   * {@link WindowsBooterDaemonGenerator}.
44   * 
45   * @author <a href="mailto:trygve.laugstol@objectware.no">Trygve Laugst&oslash;l</a>
46   * @version $Id: AbstractBooterDaemonGenerator.java 16492 2012-04-29 18:26:21Z khmarbaise $
47   */
48  public abstract class AbstractBooterDaemonGenerator
49          extends AbstactScriptDaemonGenerator
50  {
51      /**
52       * @plexus.requirement role-hint="generic"
53       */
54      private DaemonGenerator genericDaemonGenerator;
55  
56      protected AbstractBooterDaemonGenerator ( String platformName )
57      {
58          super ( platformName );
59      }
60  
61      // -----------------------------------------------------------------------
62      // DaemonGenerator Implementation
63      // -----------------------------------------------------------------------
64  
65      public void generate ( DaemonGenerationRequest request )
66              throws DaemonGeneratorException
67      {
68          Daemon daemon = request.getDaemon ( );
69          JvmSettings jvmSettings = daemon.getJvmSettings ( );
70  
71          File outputDirectory = request.getOutputDirectory ( );
72  
73          // -----------------------------------------------------------------------
74          // Generate the generic XML file
75          // -----------------------------------------------------------------------
76  
77          request.setOutputDirectory ( new File ( outputDirectory, "etc" ) );
78  
79          // TODO: we're assuming state for things that don't really appear stateful
80          /*
81           * The JVM settings are written to the script, and do not need to go into
82           * the manifest.
83           */
84          daemon.setJvmSettings ( null );
85  
86          genericDaemonGenerator.generate ( request );
87  
88          // set back
89          daemon.setJvmSettings ( jvmSettings );
90  
91          // -----------------------------------------------------------------------
92          // Generate the shell script
93          // -----------------------------------------------------------------------
94  
95          Daemon booterDaemon = new Daemon ( );
96          booterDaemon.setId ( daemon.getId ( ) );
97          booterDaemon.setEnvironmentSetupFileName ( daemon.getEnvironmentSetupFileName ( ) );
98          booterDaemon.setModelEncoding ( daemon.getModelEncoding ( ) );
99          // TODO: replace with org.codehaus.mojo.appassembler.booter.AppassemblerBooter.class.getName() and test - trygve
100         booterDaemon.setMainClass ( "org.codehaus.mojo.appassembler.booter.AppassemblerBooter" );
101         booterDaemon.setShowConsoleWindow ( daemon.isShowConsoleWindow ( ) );
102 
103         booterDaemon.setJvmSettings ( jvmSettings );
104 
105         MavenProject project = request.getMavenProject ( );
106 
107         Classpath classpath = new Classpath ( );
108         booterDaemon.setClasspath ( classpath );
109         classpath.addDirectory ( createDirectory ( "etc" ) );
110         classpath.addDependency ( createDependency ( project, "org.codehaus.mojo.appassembler:appassembler-booter",
111                 request.getRepositoryLayout ( ) ) );
112 
113         // TODO: Transitively resolve the dependencies of the booter - for now we're just hardcoding them in
114         classpath.addDependency ( createDependency ( project, "org.codehaus.mojo.appassembler:appassembler-model",
115                 request.getRepositoryLayout ( ) ) );
116         classpath.addDependency ( createDependency ( project, "org.codehaus.plexus:plexus-utils",
117                 request.getRepositoryLayout ( ) ) );
118         classpath.addDependency ( createDependency ( project, "stax:stax-api",
119                 request.getRepositoryLayout ( ) ) );
120         classpath.addDependency ( createDependency ( project, "stax:stax",
121                 request.getRepositoryLayout ( ) ) );
122 
123         // FIXME: Check if this is correct new File("bin") ?
124         scriptGenerator.createBinScript ( getPlatformName ( ), booterDaemon, outputDirectory, "bin" );
125     }
126 
127     // -----------------------------------------------------------------------
128     // Private
129     // -----------------------------------------------------------------------
130 
131     private static Dependency createDependency ( MavenProject project, String id,
132             ArtifactRepositoryLayout artifactRepositoryLayout )
133             throws DaemonGeneratorException
134     {
135         Artifact artifact = ( Artifact ) project.getArtifactMap ( ).get ( id );
136 
137         if ( artifact == null )
138         {
139             throw new DaemonGeneratorException ( "The project has to have a dependency on '" + id + "'." );
140         }
141 
142         Dependency dependency = new Dependency ( );
143 
144         dependency.setRelativePath ( artifactRepositoryLayout.pathOf ( artifact ) );
145         return dependency;
146     }
147 
148     private static Directory createDirectory ( String relativePath )
149     {
150         Directory directory = new Directory ( );
151         directory.setRelativePath ( relativePath );
152         return directory;
153     }
154 }