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.FileFilter;
20  import java.io.IOException;
21  import java.util.UUID;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.lang.StringUtils;
25  
26  import com.googlecode.t7mp.T7Configuration;
27  import com.googlecode.t7mp.PluginLog;
28  import com.googlecode.t7mp.TomcatArtifact;
29  import com.googlecode.t7mp.TomcatSetupException;
30  import com.googlecode.t7mp.configuration.PluginArtifactResolver;
31  import com.googlecode.t7mp.configuration.ResolutionException;
32  import com.googlecode.t7mp.util.ZipUtil;
33  
34  /**
35   * TODO Comment.
36   * 
37   * @author Joerg Bellmann
38   *
39   */
40  public class ResolveTomcatStep implements Step {
41  
42      protected PluginLog logger;
43      protected T7Configuration configuration;
44      protected PluginArtifactResolver artifactResolver;
45  
46      @Override
47      public void execute(Context context) {
48          this.configuration = context.getConfiguration();
49          this.artifactResolver = context.getArtifactResolver();
50          this.logger = context.getLog();
51          if (StringUtils.isEmpty(configuration.getTomcatVersion())) {
52              throw new TomcatSetupException("Version should not be null or empty.");
53          }
54  
55          File unpackDirectory = null;
56          try {
57  
58              TomcatArtifact tomcatArtifact = configuration.getTomcatArtifact();
59              tomcatArtifact.setVersion(configuration.getTomcatVersion());
60              File resolvedArtifact = artifactResolver.resolveArtifact(tomcatArtifact.getArtifactCoordinates());
61              unpackDirectory = getUnpackDirectory();
62              ZipUtil.unzip(resolvedArtifact, unpackDirectory);
63              copyToTomcatDirectory(unpackDirectory);
64          } catch (ResolutionException e) {
65              logger.error(e.getMessage(), e);
66              throw new TomcatSetupException(e.getMessage(), e);
67          } catch (IOException e) {
68              logger.error(e.getMessage(), e);
69              throw new TomcatSetupException(e.getMessage(), e);
70          } finally {
71              if (unpackDirectory != null) {
72                  try {
73                      FileUtils.deleteDirectory(unpackDirectory);
74                  } catch (IOException e) {
75                      logger.error("Could not delete tomcat upack directory : " + unpackDirectory.getAbsolutePath());
76                      logger.error(e.getMessage(), e);
77                  }
78              }
79          }
80      }
81  
82      private void copyToTomcatDirectory(File unpackDirectory) throws IOException {
83          File[] files = unpackDirectory.listFiles(new FileFilter() {
84              @Override
85              public boolean accept(File file) {
86                  return file.isDirectory();
87              }
88          });
89          // should only be one
90          FileUtils.copyDirectory(files[0], this.configuration.getCatalinaBase());
91      }
92  
93      protected File getUnpackDirectory() {
94          File tempDir = new File(System.getProperty("java.io.tmpdir"));
95          File upackDirectory = new File(tempDir, UUID.randomUUID().toString());
96          upackDirectory.mkdirs();
97          return upackDirectory;
98      }
99  
100 }