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.IOException;
20  import java.util.Set;
21  import java.util.UUID;
22  
23  import org.apache.commons.io.FileUtils;
24  
25  import com.googlecode.t7mp.T7Configuration;
26  import com.googlecode.t7mp.PluginLog;
27  import com.googlecode.t7mp.TomcatSetupException;
28  import com.googlecode.t7mp.configuration.PluginArtifactResolver;
29  import com.googlecode.t7mp.configuration.ResolutionException;
30  import com.googlecode.t7mp.util.FileFilters;
31  import com.googlecode.t7mp.util.FileUtil;
32  import com.googlecode.t7mp.util.ZipUtil;
33  
34  /**
35   * Resolve the configurationArtifact and copy Files to conf-directory.
36   * 
37   * @author Joerg Bellmann
38   *
39   */
40  public class ResolveConfigurationArtifactStep implements Step {
41  
42      protected PluginLog logger;
43  
44      protected T7Configuration baseConfiguration;
45      protected PluginArtifactResolver artifactResolver;
46  
47      @Override
48      public void execute(Context context) {
49          this.artifactResolver = context.getArtifactResolver();
50          this.baseConfiguration = context.getConfiguration();
51          this.logger = context.getLog();
52          // skip this step if no configuationArtifact is found
53          if (baseConfiguration.getConfigArtifact() == null) {
54              this.logger.info("No configurationArtifact found, skip this step.");
55              return;
56          }
57          // lets do the hard work
58          this.logger.debug("resolve configurationArtifact ...");
59          File unpackDirectory = null;
60          try {
61              File resolvedArtifact = artifactResolver.resolveArtifact(this.baseConfiguration.getConfigArtifact()
62                      .getArtifactCoordinates());
63              unpackDirectory = getUnpackDirectory();
64              ZipUtil.unzip(resolvedArtifact, unpackDirectory);
65              this.logger.debug("unzipped to " + unpackDirectory.getAbsolutePath());
66              copyToTomcatConfDirectory(unpackDirectory);
67          } catch (ResolutionException e) {
68              this.logger.error(e.getMessage(), e);
69              throw new TomcatSetupException(e.getMessage(), e);
70          } catch (IOException e) {
71              this.logger.error(e.getMessage(), e);
72              throw new TomcatSetupException(e.getMessage(), e);
73          } finally {
74              if (unpackDirectory != null) {
75                  try {
76                      FileUtils.deleteDirectory(unpackDirectory);
77                  } catch (IOException e) {
78                      this.logger.error("Could not delete temp directory " + unpackDirectory.getAbsolutePath(), e);
79                  }
80              }
81          }
82      }
83  
84      private void copyToTomcatConfDirectory(File unpackDirectory) throws IOException {
85          this.logger.debug("copy conf-files ...");
86          File confDirectory = new File(this.baseConfiguration.getCatalinaBase(), "conf");
87          this.logger.debug("targetConfDirectory is " + confDirectory.getAbsolutePath());
88          Set<File> files = FileUtil.getAllFiles(unpackDirectory, FileFilters.forAll(), false);
89          for (File file : files) {
90              this.logger.debug("copy file " + file.getName() + ", path " + file.getAbsolutePath()
91                      + " to targetDirectory");
92              FileUtils.copyFileToDirectory(file, confDirectory);
93          }
94      }
95  
96      protected File getUnpackDirectory() {
97          File tempDir = new File(System.getProperty("java.io.tmpdir"));
98          File upackDirectory = new File(tempDir, UUID.randomUUID().toString());
99          upackDirectory.mkdirs();
100         return upackDirectory;
101     }
102 }