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.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  
23  import com.googlecode.t7mp.SetupUtil;
24  import com.googlecode.t7mp.TomcatSetupException;
25  import com.googlecode.t7mp.util.CommonsSetupUtil;
26  
27  /**
28   * 
29   * @author Joerg Bellmann
30   *
31   */
32  public class CopyConfigResourceFromClasspath implements Step {
33  
34      private static final String MSG_PREFIX = "Could not copy classpathresource ";
35      private static final String MSG_SUFFIX = " to tomcat-conf directory";
36  
37      private static final String RESOURCEPATH = "/com/googlecode/t7mp/conf/";
38  
39      private SetupUtil setupUtil = new CommonsSetupUtil();
40  
41      private final String resource;
42  
43      public CopyConfigResourceFromClasspath(String resource) {
44          this.resource = resource;
45      }
46  
47      @Override
48      public void execute(Context context) {
49          final File tomcatConfDirectory = new File(context.getConfiguration().getCatalinaBase(), "/conf/");
50          try {
51              FileOutputStream out = new FileOutputStream(new File(tomcatConfDirectory, resource));
52              this.setupUtil.copy(getClass().getResourceAsStream(RESOURCEPATH + resource), out);
53              out.close();
54          } catch (FileNotFoundException e) {
55              throw new TomcatSetupException(MSG_PREFIX + resource + MSG_SUFFIX, e);
56          } catch (IOException e) {
57              throw new TomcatSetupException(MSG_PREFIX + resource + MSG_SUFFIX, e);
58          }
59      }
60  }