View Javadoc

1   package org.codehaus.mojo.jboss;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file 
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, 
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
17   * KIND, either express or implied.  See the License for the 
18   * specific language governing permissions and limitations 
19   * under the License.
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   * Allows you to configure JBoss installation by overlaying a conf, lib and deploy directory.
37   * 
38   * @goal configure
39   */
40  public class ConfigureJBossMojo
41      extends AbstractJBossServerMojo
42  {
43      private static final String SERVER_DIR_NAME = "server";
44  
45      /**
46       * The directory for overrides to the conf directory.
47       * 
48       * @parameter default-value="${basedir}/jboss/conf"
49       */
50      private File confDir;
51  
52      /**
53       * The directory for overrides to the lib directory.
54       * 
55       * @parameter default-value="${basedir}/jboss/lib"
56       */
57      private File libDir;
58  
59      /**
60       * The directory for overrides to the deploy directory.
61       * 
62       * @parameter default-value="${basedir}/jboss/deploy"
63       */
64      private File deployDir;
65  
66      /**
67       * @parameter default-value="${project.build.directory}/jboss"
68       */
69      protected File outputDirectory;
70  
71      /**
72       * @parameter expression="${jboss.javaOpts}"
73       */
74      protected String javaOpts;
75  
76      /**
77       * The set of options to pass to the JBoss "run" command.
78       * 
79       * @parameter expression="${jboss.options}"
80       */
81      protected String options;
82  
83      /**
84       * Main plugin execution.
85       * 
86       * @throws MojoExecutionException
87       * @throws MojoFailureException
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      * Check that jboss home is configured correctly.
109      * 
110      * @throws MojoFailureException
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      * Check that the output directory is configured.
138      * 
139      * @param serverDir
140      * @throws MojoFailureException
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      * Copy conf.
163      * 
164      * @param serverDir
165      * @throws MojoExecutionException
166      */
167     private void copyBaseConfDir( File serverDir )
168         throws MojoExecutionException
169     {
170         copyBaseDir( serverDir, "conf" );
171     }
172 
173     /**
174      * Copy lib dir.
175      * 
176      * @param serverDir
177      * @throws MojoExecutionException
178      */
179     private void copyBaseLibDir( File serverDir )
180         throws MojoExecutionException
181     {
182         copyBaseDir( serverDir, "lib" );
183     }
184 
185     /**
186      * Copy deploy dir.
187      * 
188      * @param serverDir
189      * @throws MojoExecutionException
190      */
191     private void copyBaseDeployDir( File serverDir )
192         throws MojoExecutionException
193     {
194         copyBaseDir( serverDir, "deploy" );
195     }
196 
197     /**
198      * Copy base dir.
199      * 
200      * @param serverDir
201      * @param dirName
202      * @throws MojoExecutionException
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      * Overlay conf dir.
223      * 
224      * @param serverDir
225      * @throws MojoExecutionException
226      */
227     private void overlayConfDir( File serverDir )
228         throws MojoExecutionException
229     {
230         overLayDir( serverDir, confDir, "conf" );
231     }
232 
233     /**
234      * Overlay deploy dir.
235      * 
236      * @param serverDir
237      * @throws MojoExecutionException
238      */
239     private void overlayDeployDir( File serverDir )
240         throws MojoExecutionException
241     {
242         overLayDir( serverDir, deployDir, "deploy" );
243     }
244 
245     /**
246      * Overlay lib dir.
247      * 
248      * @param serverDir
249      * @throws MojoExecutionException
250      */
251     private void overlayLibDir( File serverDir )
252         throws MojoExecutionException
253     {
254         overLayDir( serverDir, libDir, "lib" );
255     }
256 
257     /**
258      * Overlay dir.
259      * 
260      * @param serverDir
261      * @param overLayDir
262      * @param dirName
263      * @throws MojoExecutionException
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      * Build the bin dir.
284      * 
285      * @param serverDir
286      * @throws MojoExecutionException
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 }