1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
49
50
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
144
145
146
147
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
201 String programName = "";
202
203 if ( daemon.getId ( ) == null || daemon.getId ( ).trim ( ).equals ( "" ) )
204 {
205
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 }