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.script;
25  
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.FileNotFoundException;
29  import java.io.FileReader;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.InputStreamReader;
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.StringTokenizer;
39  
40  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
41  import org.codehaus.mojo.appassembler.model.Daemon;
42  import org.codehaus.plexus.logging.AbstractLogEnabled;
43  import org.codehaus.plexus.util.FileUtils;
44  import org.codehaus.plexus.util.IOUtil;
45  import org.codehaus.plexus.util.InterpolationFilterReader;
46  
47  /**
48   * @author <a href="mailto:trygve.laugstol@objectware.no">Trygve Laugst&oslash;l</a>
49   * @version $Id: DefaultScriptGenerator.java 16448 2012-04-22 15:47:18Z khmarbaise $
50   * @plexus.component
51   */
52  public class DefaultScriptGenerator
53          extends AbstractLogEnabled
54          implements ScriptGenerator
55  {
56  
57      private static final String DEFAULT_LICENSE_HEADER = "default-license-header.txt";
58  
59      private boolean isDefaultLicenseHeaderRequested ( Daemon daemon )
60      {
61          if ( daemon.getLicenseHeaderFile ( ) == null )
62          {
63              return true;
64          }
65  
66          if ( daemon.getLicenseHeaderFile ( ).trim ( ).length ( ) > 0 )
67          {
68              return false;
69          }
70  
71          return false;
72      }
73  
74      private String getLicenseHeader ( Platform platform, Daemon daemon ) throws DaemonGeneratorException
75      {
76          List lines = null;
77          if ( isDefaultLicenseHeaderRequested ( daemon ) )
78          {
79              getLogger ( ).debug ( "Using default licence file (" + DEFAULT_LICENSE_HEADER + "." );
80              lines = readLicenseHeader ( );
81          }
82          else
83          {
84              getLogger ( ).debug ( "Using license file: " + daemon.getLicenseHeaderFile ( ) );
85              lines = readLicenseHeaderFromFile ( new File ( daemon.getLicenseHeaderFile ( ) ) );
86          }
87          StringBuffer resultLines = new StringBuffer ( );
88          for ( int i = 0; i < lines.size ( ); i++ )
89          {
90              String licenseLine = platform.getCommentPrefix ( ) + lines.get ( i );
91              resultLines.append ( licenseLine.trim ( ) + platform.getNewLine ( ) );
92          }
93          return resultLines.toString ( );
94      }
95  
96      private List readLicenseHeader () throws DaemonGeneratorException
97      {
98          ArrayList result = new ArrayList ( );
99  
100         InputStream in = getClass ( ).getResourceAsStream ( DEFAULT_LICENSE_HEADER );
101 
102         InputStreamReader inr = new InputStreamReader ( in );
103         try
104         {
105             BufferedReader bufRead = new BufferedReader ( inr );
106             String str;
107             while ( ( str = bufRead.readLine ( ) ) != null )
108             {
109                 result.add ( str );
110             }
111             bufRead.close ( );
112         }
113         catch ( IOException e )
114         {
115             throw new DaemonGeneratorException (
116                     "Internal error: could not read license header template file (license-header.txt)" );
117         }
118         return result;
119     }
120 
121     private List readLicenseHeaderFromFile ( File licenseHeader ) throws DaemonGeneratorException
122     {
123         ArrayList result = new ArrayList ( );
124         try
125         {
126             BufferedReader in = new BufferedReader ( new FileReader ( licenseHeader ) );
127             String str;
128             while ( ( str = in.readLine ( ) ) != null )
129             {
130                 result.add ( str );
131             }
132             in.close ( );
133         }
134         catch ( IOException e )
135         {
136             throw new DaemonGeneratorException (
137                     "Internal error: could not read license header template file " + licenseHeader.getName ( ) );
138         }
139         return result;
140     }
141 
142     // -----------------------------------------------------------------------
143     // ScriptGenerator Implementation
144     // -----------------------------------------------------------------------
145 
146     /* (non-Javadoc)
147      * @see org.codehaus.mojo.appassembler.daemon.script.ScriptGenerator#createBinScript(java.lang.String, org.codehaus.mojo.appassembler.model.Daemon, java.io.File, java.lang.String)
148      */
149     public void createBinScript ( String platformName, Daemon daemon, File outputDirectory, String binFolder )
150             throws DaemonGeneratorException
151     {
152         Platform platform = Platform.getInstance ( platformName );
153 
154         InputStream in = null;
155 
156         FileWriter out = null;
157 
158         try
159         {
160             in = getClass ( ).getResourceAsStream ( platformName + "BinTemplate" );
161 
162             if ( in == null )
163             {
164                 throw new DaemonGeneratorException (
165                         "Internal error: could not find template for platform '" + platformName + "'." );
166             }
167             InputStreamReader reader = new InputStreamReader ( in );
168 
169             Map context = new HashMap ( );
170             context.put ( "MAINCLASS", daemon.getMainClass ( ) );
171             context.put ( "CLASSPATH", platform.getClassPath ( daemon ) );
172             context.put ( "EXTRA_JVM_ARGUMENTS", platform.getExtraJvmArguments ( daemon.getJvmSettings ( ) ) );
173             context.put ( "APP_NAME", daemon.getId ( ) );
174             context.put ( "ENV_SETUP", platform.getEnvSetup ( daemon ) );
175             context.put ( "REPO", daemon.getRepositoryName ( ) );
176             context.put ( "LICENSE_HEADER", getLicenseHeader ( platform, daemon ) );
177             if ( platform.isShowConsoleWindow ( daemon ) )
178             {
179                 context.put ( "JAVA_BINARY", "java" );
180             }
181             else
182             {
183                 context.put ( "JAVA_BINARY", "start /min javaw" );
184             }
185 
186             String appArguments = platform.getAppArguments ( daemon );
187             if ( appArguments != null )
188             {
189                 context.put ( "APP_ARGUMENTS", appArguments + " " );
190             }
191             else
192             {
193                 context.put ( "APP_ARGUMENTS", "" );
194             }
195 
196             String interpolationToken = platform.getInterpolationToken ( );
197             InterpolationFilterReader interpolationFilterReader =
198                     new InterpolationFilterReader ( reader, context, interpolationToken, interpolationToken );
199 
200             // Set the name of the bin file
201             String programName = "";
202 
203             if ( daemon.getId ( ) == null || daemon.getId ( ).trim ( ).equals ( "" ) )
204             {
205                 // Get class name and use it as the filename
206                 StringTokenizer tokenizer = new StringTokenizer ( daemon.getMainClass ( ), "." );
207                 while ( tokenizer.hasMoreElements ( ) )
208                 {
209                     programName = tokenizer.nextToken ( );
210                 }
211 
212                 programName = programName.toLowerCase ( );
213             }
214             else
215             {
216                 programName = daemon.getId ( );
217             }
218 
219             File binDir = new File ( outputDirectory, binFolder );
220             FileUtils.forceMkdir ( binDir );
221             File binFile = new File ( binDir, programName + platform.getBinFileExtension ( ) );
222 
223             out = new FileWriter ( binFile );
224             getLogger ( ).debug ( "Writing shell file for platform '" + platform.getName ( ) + "' to '"
225                     + binFile.getAbsolutePath ( ) + "'." );
226 
227             IOUtil.copy ( interpolationFilterReader, out );
228         }
229         catch ( FileNotFoundException e )
230         {
231             throw new DaemonGeneratorException ( "Failed to get template for bin file.", e );
232         }
233         catch ( IOException e )
234         {
235             throw new DaemonGeneratorException ( "Failed to write bin file.", e );
236         }
237         finally
238         {
239             IOUtil.close ( out );
240             IOUtil.close ( in );
241         }
242     }
243 }