View Javadoc

1   /**
2    * Copyright (C) 2010-2012 Joerg Bellmann <joerg.bellmann@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.googlecode.t7mp.steps;
17  
18  import java.io.File;
19  import java.io.FileWriter;
20  import java.io.Writer;
21  
22  import org.apache.velocity.Template;
23  import org.apache.velocity.VelocityContext;
24  import org.apache.velocity.app.Velocity;
25  import org.apache.velocity.runtime.RuntimeServices;
26  import org.apache.velocity.runtime.log.LogChute;
27  import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
28  
29  import com.googlecode.t7mp.T7Configuration;
30  import com.googlecode.t7mp.TomcatSetupException;
31  
32  public class BuildCatalinaPropertiesFileStep implements Step {
33  
34      @Override
35      public void execute(Context context) {
36          final T7Configuration configuration = context.getConfiguration();
37          try {
38              Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new LogNothingLogChute());
39              Velocity.setProperty(Velocity.RESOURCE_LOADER, "class");
40              Velocity.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
41              Velocity.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
42              Velocity.init();
43              Template template = Velocity.getTemplate("com/googlecode/t7mp/conf/catalina.properties");
44              VelocityContext velocityContext = new VelocityContext();
45              velocityContext.put("tomcatHttpPort", configuration.getTomcatHttpPort() + "");
46              velocityContext.put("tomcatShutdownPort", configuration.getTomcatShutdownPort() + "");
47              velocityContext.put("tomcatShutdownCommand", configuration.getTomcatShutdownCommand());
48              velocityContext.put("tomcatHostName", configuration.getTomcatHostName());
49              Writer writer = new FileWriter(new File(configuration.getCatalinaBase(), "/conf/catalina.properties"));
50              template.merge(velocityContext, writer);
51              writer.flush();
52              writer.close();
53          } catch (Exception e) {
54              throw new TomcatSetupException(e.getMessage(), e);
55          }
56      }
57      
58      /**
59       * 
60       * @author Joerg Bellmann
61       *
62       */
63      static class LogNothingLogChute implements LogChute {
64  
65          @Override
66          public void init(RuntimeServices rs) throws Exception {
67  
68          }
69  
70          @Override
71          public void log(int level, String message) {
72  
73          }
74  
75          @Override
76          public void log(int level, String message, Throwable t) {
77  
78          }
79  
80          @Override
81          public boolean isLevelEnabled(int level) {
82              return false;
83          }
84  
85      }
86  
87  }