View Javadoc
1   package org.codehaus.mojo.appassembler.daemon.daemontools;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.io.FileWriter;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.io.Reader;
33  import java.io.Writer;
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
38  import org.codehaus.mojo.appassembler.daemon.DaemonGenerator;
39  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
40  import org.codehaus.mojo.appassembler.model.Daemon;
41  import org.codehaus.plexus.logging.AbstractLogEnabled;
42  import org.codehaus.plexus.util.FileUtils;
43  import org.codehaus.plexus.util.IOUtil;
44  import org.codehaus.plexus.util.InterpolationFilterReader;
45  
46  /**
47   * @author Andrew Williams
48   * @version $Id$
49   * @plexus.component role-hint="daemontools"
50   */
51  public class DaemonToolsDaemonGenerator
52      extends AbstractLogEnabled
53      implements DaemonGenerator
54  {
55      // -----------------------------------------------------------------------
56      // DaemonGenerator Implementation
57      // -----------------------------------------------------------------------
58  
59      /*
60       * (non-Javadoc)
61       * @see org.codehaus.mojo.appassembler.daemon.DaemonGenerator#generate(org.codehaus.mojo.appassembler.daemon.
62       * DaemonGenerationRequest)
63       */
64      public void generate( DaemonGenerationRequest request )
65          throws DaemonGeneratorException
66      {
67          Daemon daemon = request.getDaemon();
68  
69          try
70          {
71              FileUtils.forceMkdir( request.getOutputDirectory() );
72          }
73          catch ( IOException e )
74          {
75              throw new DaemonGeneratorException( "Error creating output directory: " + request.getOutputDirectory(), e );
76          }
77  
78          File envDir = new File( request.getOutputDirectory(), "env" );
79          envDir.mkdir();
80  
81          copyEnvFile( "JAVA_HOME", envDir );
82          copyEnvFile( "USER", envDir );
83  
84          File logDir = new File( request.getOutputDirectory(), "logs" );
85          logDir.mkdir();
86  
87          File serviceDir = new File( request.getOutputDirectory(), "service" );
88          serviceDir.mkdir();
89  
90          // -----------------------------------------------------------------------
91          //
92          // -----------------------------------------------------------------------
93  
94          InputStream in = this.getClass().getResourceAsStream( "run.sh.template" );
95  
96          if ( in == null )
97          {
98              throw new DaemonGeneratorException( "Could not load template." );
99          }
100 
101         InputStreamReader reader = new InputStreamReader( in );
102 
103         Map<Object, Object> context = new HashMap<Object, Object>();
104         context.put( "MAINCLASS", daemon.getMainClass() );
105         context.put( "NAME", daemon.getId() );
106 
107         InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( reader, context, "@", "@" );
108 
109         File runFile = new File( request.getOutputDirectory(), "run" );
110         FileWriter out = null;
111 
112         try
113         {
114             // -----------------------------------------------------------------------
115             // Write the file
116             // -----------------------------------------------------------------------
117 
118             out = new FileWriter( runFile );
119 
120             IOUtil.copy( interpolationFilterReader, out );
121         }
122         catch ( IOException e )
123         {
124             throw new DaemonGeneratorException( "Error writing output file: " + runFile.getAbsolutePath(), e );
125         }
126         finally
127         {
128             IOUtil.close( interpolationFilterReader );
129             IOUtil.close( out );
130         }
131 
132     }
133 
134     private void copyEnvFile( String envName, File envDir )
135         throws DaemonGeneratorException
136     {
137         Writer out = null;
138         Reader envReader = null;
139 
140         File envFile = new File( envDir, envName );
141 
142         try
143         {
144             envReader = new InputStreamReader( this.getClass().getResourceAsStream( "env/" + envName ) );
145 
146             // -----------------------------------------------------------------------
147             // Write the file
148             // -----------------------------------------------------------------------
149 
150             out = new FileWriter( envFile );
151 
152             IOUtil.copy( envReader, out );
153         }
154         catch ( IOException e )
155         {
156             throw new DaemonGeneratorException( "Error writing environment file: " + envFile, e );
157         }
158         finally
159         {
160             IOUtil.close( envReader );
161             IOUtil.close( out );
162         }
163     }
164 }