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.daemontools;
25  
26  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
27  import org.codehaus.mojo.appassembler.daemon.DaemonGenerator;
28  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
29  import org.codehaus.mojo.appassembler.model.Daemon;
30  import org.codehaus.plexus.logging.AbstractLogEnabled;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.InterpolationFilterReader;
34  
35  import java.io.File;
36  import java.io.FileWriter;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.io.InputStreamReader;
40  import java.io.Reader;
41  import java.io.Writer;
42  import java.util.HashMap;
43  import java.util.Map;
44  
45  /**
46   * @author Andrew Williams
47   * @version $Id$
48   * @plexus.component role-hint="daemontools"
49   */
50  public class DaemonToolsDaemonGenerator
51          extends AbstractLogEnabled
52          implements DaemonGenerator
53  {
54      // -----------------------------------------------------------------------
55      // DaemonGenerator Implementation
56      // -----------------------------------------------------------------------
57  
58      /* (non-Javadoc)
59       * @see org.codehaus.mojo.appassembler.daemon.DaemonGenerator#generate(org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest)
60       */
61      public void generate ( DaemonGenerationRequest request )
62              throws DaemonGeneratorException
63      {
64          Daemon daemon = request.getDaemon ( );
65  
66          try
67          {
68              FileUtils.forceMkdir ( request.getOutputDirectory ( ) );
69          }
70          catch ( IOException e )
71          {
72              throw new DaemonGeneratorException ( "Error creating output directory: "
73                      + request.getOutputDirectory ( ), e );
74          }
75  
76          File envDir = new File ( request.getOutputDirectory ( ), "env" );
77          envDir.mkdir ( );
78  
79          copyEnvFile ( "JAVA_HOME", envDir );
80          copyEnvFile ( "USER", envDir );
81  
82          File logDir = new File ( request.getOutputDirectory ( ), "logs" );
83          logDir.mkdir ( );
84  
85          File serviceDir = new File ( request.getOutputDirectory ( ), "service" );
86          serviceDir.mkdir ( );
87  
88          // -----------------------------------------------------------------------
89          //
90          // -----------------------------------------------------------------------
91  
92          InputStream in = this.getClass ( ).getResourceAsStream ( "run.sh.template" );
93  
94          if ( in == null )
95          {
96              throw new DaemonGeneratorException ( "Could not load template." );
97          }
98  
99          InputStreamReader reader = new InputStreamReader ( in );
100 
101         Map context = new HashMap ( );
102         context.put ( "MAINCLASS", daemon.getMainClass ( ) );
103         context.put ( "NAME", daemon.getId ( ) );
104 
105         InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader ( reader, context,
106                 "@", "@" );
107 
108         File runFile = new File ( request.getOutputDirectory ( ), "run" );
109         FileWriter out = null;
110 
111         try
112         {
113             // -----------------------------------------------------------------------
114             // Write the file
115             // -----------------------------------------------------------------------
116 
117             out = new FileWriter ( runFile );
118 
119             IOUtil.copy ( interpolationFilterReader, out );
120         }
121         catch ( IOException e )
122         {
123             throw new DaemonGeneratorException ( "Error writing output file: " + runFile.getAbsolutePath ( ), e );
124         }
125         finally
126         {
127             IOUtil.close ( interpolationFilterReader );
128             IOUtil.close ( out );
129         }
130 
131     }
132 
133     private void copyEnvFile ( String envName, File envDir )
134             throws DaemonGeneratorException
135     {
136         Writer out = null;
137         Reader envReader = null;
138 
139         File envFile = new File ( envDir, envName );
140 
141         try
142         {
143             envReader = new InputStreamReader ( this.getClass ( ).getResourceAsStream ( "env/" + envName ) );
144 
145             // -----------------------------------------------------------------------
146             // Write the file
147             // -----------------------------------------------------------------------
148 
149             out = new FileWriter ( envFile );
150 
151             IOUtil.copy ( envReader, out );
152         }
153         catch ( IOException e )
154         {
155             throw new DaemonGeneratorException ( "Error writing environment file: " + envFile, e );
156         }
157         finally
158         {
159             IOUtil.close ( envReader );
160             IOUtil.close ( out );
161         }
162     }
163 }