1 package org.codehaus.mojo.jboss;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.apache.velocity.app.VelocityEngine;
25 import org.apache.velocity.Template;
26 import org.apache.velocity.VelocityContext;
27 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
28 import org.codehaus.plexus.util.FileUtils;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.FileWriter;
33 import java.util.Properties;
34
35
36
37
38
39
40 public class ConfigureJBossMojo
41 extends AbstractJBossServerMojo
42 {
43 private static final String SERVER_DIR_NAME = "server";
44
45
46
47
48
49
50 private File confDir;
51
52
53
54
55
56
57 private File libDir;
58
59
60
61
62
63
64 private File deployDir;
65
66
67
68
69 protected File outputDirectory;
70
71
72
73
74 protected String javaOpts;
75
76
77
78
79
80
81 protected String options;
82
83
84
85
86
87
88
89 public void execute()
90 throws MojoExecutionException, MojoFailureException
91 {
92 checkConfig();
93 checkJBossHome();
94
95 File serverDir = new File( outputDirectory.getAbsolutePath() + File.separator + serverName );
96
97 checkOutputDirectory( serverDir );
98 copyBaseConfDir( serverDir );
99 copyBaseDeployDir( serverDir );
100 copyBaseLibDir( serverDir );
101 overlayConfDir( serverDir );
102 overlayDeployDir( serverDir );
103 overlayLibDir( serverDir );
104 buildBinDir( serverDir );
105 }
106
107
108
109
110
111
112 private void checkJBossHome()
113 throws MojoFailureException
114 {
115 if ( !jbossHome.exists() )
116 {
117 throw new MojoFailureException( "The jbossHome specifed does not exist." );
118 }
119
120 File serverParentDir = new File( jbossHome.getAbsolutePath() + File.separator + SERVER_DIR_NAME );
121
122 if ( !serverParentDir.exists() )
123 {
124 throw new MojoFailureException( jbossHome.getAbsolutePath() + " does not appear to be a valid jboss home" );
125 }
126
127 File serverDir = new File( serverParentDir.getAbsolutePath() + File.separator + serverName );
128
129 if ( !serverDir.exists() )
130 {
131 throw new MojoFailureException( serverName + " is not a valid server in "
132 + serverParentDir.getAbsolutePath() );
133 }
134 }
135
136
137
138
139
140
141
142 private void checkOutputDirectory( File serverDir )
143 throws MojoFailureException
144 {
145 if ( outputDirectory == null )
146 {
147 throw new MojoFailureException( "I don't know how you did it, but the outputDirectory is null" );
148 }
149
150 if ( !outputDirectory.exists() )
151 {
152 outputDirectory.mkdirs();
153 }
154
155 if ( !serverDir.exists() )
156 {
157 serverDir.mkdir();
158 }
159 }
160
161
162
163
164
165
166
167 private void copyBaseConfDir( File serverDir )
168 throws MojoExecutionException
169 {
170 copyBaseDir( serverDir, "conf" );
171 }
172
173
174
175
176
177
178
179 private void copyBaseLibDir( File serverDir )
180 throws MojoExecutionException
181 {
182 copyBaseDir( serverDir, "lib" );
183 }
184
185
186
187
188
189
190
191 private void copyBaseDeployDir( File serverDir )
192 throws MojoExecutionException
193 {
194 copyBaseDir( serverDir, "deploy" );
195 }
196
197
198
199
200
201
202
203
204 private void copyBaseDir( File serverDir, String dirName )
205 throws MojoExecutionException
206 {
207 File baseDir =
208 new File( jbossHome + File.separator + SERVER_DIR_NAME + File.separator + serverName + File.separator
209 + dirName );
210 File dir = new File( serverDir.getAbsolutePath() + File.separator + dirName );
211 try
212 {
213 FileUtils.copyDirectoryStructure( baseDir, dir );
214 }
215 catch ( IOException e )
216 {
217 throw new MojoExecutionException( "Could Not Copy " + dirName + " Dir", e );
218 }
219 }
220
221
222
223
224
225
226
227 private void overlayConfDir( File serverDir )
228 throws MojoExecutionException
229 {
230 overLayDir( serverDir, confDir, "conf" );
231 }
232
233
234
235
236
237
238
239 private void overlayDeployDir( File serverDir )
240 throws MojoExecutionException
241 {
242 overLayDir( serverDir, deployDir, "deploy" );
243 }
244
245
246
247
248
249
250
251 private void overlayLibDir( File serverDir )
252 throws MojoExecutionException
253 {
254 overLayDir( serverDir, libDir, "lib" );
255 }
256
257
258
259
260
261
262
263
264
265 private void overLayDir( File serverDir, File overLayDir, String dirName )
266 throws MojoExecutionException
267 {
268 File baseDir = new File( serverDir + File.separator + dirName );
269 if ( overLayDir.exists() )
270 {
271 try
272 {
273 FileUtils.copyDirectoryStructure( overLayDir, baseDir );
274 }
275 catch ( IOException e )
276 {
277 throw new MojoExecutionException( "Could not overlay " + dirName + "Dir", e );
278 }
279 }
280 }
281
282
283
284
285
286
287
288 private void buildBinDir( File serverDir )
289 throws MojoExecutionException
290 {
291 File binDir = new File( outputDirectory.getAbsolutePath() + File.separator + "bin" );
292
293 if ( !binDir.exists() )
294 {
295 binDir.mkdirs();
296 }
297
298 VelocityEngine engine = new VelocityEngine();
299 Properties p = new Properties();
300 p.setProperty( "resource.loader", "class" );
301 p.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
302 try
303 {
304 engine.init( p );
305 }
306 catch ( Exception e )
307 {
308 throw new MojoExecutionException( "Problem creating initting velcoity engine", e );
309 }
310
311 VelocityContext context = new VelocityContext();
312 context.put( "jbossServerHome", serverDir.getAbsolutePath() );
313 context.put( "jbossHome", jbossHome.getAbsolutePath() );
314 context.put( "serverName", serverName );
315 context.put( "options", options == null ? "" : options );
316 context.put( "javaOpts", javaOpts == null ? "" : javaOpts );
317
318 String osName = System.getProperty( "os.name" );
319
320 if ( osName.startsWith( "Windows" ) )
321 {
322 buildWindowsScripts( binDir, engine, context );
323 }
324 else
325 {
326 buildUnixScipts( binDir, engine, context );
327 }
328 }
329
330 private void buildUnixScipts( File binDir, VelocityEngine engine, VelocityContext context )
331 throws MojoExecutionException
332 {
333 File runScript = new File( binDir + File.separator + "run.sh" );
334 File shutdownScript = new File( binDir + File.separator + "shutdown.sh" );
335
336 try
337 {
338 Runtime runtime = Runtime.getRuntime();
339
340 Template runTemplate = engine.getTemplate( "run.sh.vm" );
341 FileWriter fileWriter = new FileWriter( runScript );
342 runTemplate.merge( context, fileWriter );
343 fileWriter.flush();
344
345 String command = "chmod 755 " + runScript.getAbsolutePath();
346 Process process = runtime.exec( command );
347
348 Template shutdownTemplate = engine.getTemplate( "shutdown.sh.vm" );
349 fileWriter = new FileWriter( shutdownScript );
350 shutdownTemplate.merge( context, fileWriter );
351 fileWriter.flush();
352
353 command = "chmod 755 " + shutdownScript.getAbsolutePath();
354 process = runtime.exec( command );
355 }
356 catch ( Exception e )
357 {
358 throw new MojoExecutionException( "Problem generating scripts", e );
359 }
360 }
361
362 private void buildWindowsScripts( File binDir, VelocityEngine engine, VelocityContext context )
363 throws MojoExecutionException
364 {
365 File runScript = new File( binDir + File.separator + "run.bat" );
366 File shutdownScript = new File( binDir + File.separator + "shutdown.bat" );
367
368 try
369 {
370 Template runTemplate = engine.getTemplate( "run.bat.vm" );
371 FileWriter fileWriter = new FileWriter( runScript );
372 runTemplate.merge( context, fileWriter );
373 fileWriter.flush();
374
375 Template shutdownTemplate = engine.getTemplate( "shutdown.bat.vm" );
376 fileWriter = new FileWriter( shutdownScript );
377 shutdownTemplate.merge( context, fileWriter );
378 fileWriter.flush();
379 }
380 catch ( Exception e )
381 {
382 throw new MojoExecutionException( "Problem generating scripts", e );
383 }
384 }
385 }