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.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.commons.io.IOUtils;
26  
27  import com.google.common.collect.Lists;
28  import com.googlecode.t7mp.AbstractArtifact;
29  import com.googlecode.t7mp.TomcatSetupException;
30  import com.googlecode.t7mp.WebappArtifact;
31  import com.googlecode.t7mp.util.ZipUtil;
32  
33  public class WebappsDeploymentStep extends AbstractDeploymentStep {
34  
35      @Override
36      protected List<AbstractArtifact> getArtifactList() {
37          List<AbstractArtifact> artifactList = Lists.newArrayList();
38          artifactList.addAll(context.getConfiguration().getWebapps());
39          return artifactList;
40      }
41  
42      @Override
43      protected void deployArtifacts(List<AbstractArtifact> artifactList) {
44          for (AbstractArtifact artifact : artifactList) {
45              final WebappArtifact webappArtifact = (WebappArtifact) artifact;
46              if (webappArtifact.isUnpack() || webappArtifact.getTestContextFile() != null) {
47                  unzipWebappArtifact(webappArtifact);
48                  if (webappArtifact.getTestContextFile() != null) {
49                      File metaInfDirectory = new File(createTargetFileName(webappArtifact) + "/META-INF");
50                      metaInfDirectory.mkdirs();
51                      try {
52                          IOUtils.copy(new FileInputStream(webappArtifact.getTestContextFile()), new FileOutputStream(
53                                  new File(metaInfDirectory, "context.xml")));
54                      } catch (FileNotFoundException e) {
55                          throw new TomcatSetupException(e.getMessage(), e);
56                      } catch (IOException e) {
57                          throw new TomcatSetupException(e.getMessage(), e);
58                      }
59                  }
60              } else {
61                  try {
62                      String targetFileName = createTargetFileName(artifact);
63                      File sourceFile = artifact.getFile();
64                      File targetFile = new File(this.context.getConfiguration().getCatalinaBase(), "/webapps/" + targetFileName);
65                      this.context.getLog().debug(
66                              "Copy artifact from " + sourceFile.getAbsolutePath() + " to "
67                                      + targetFile.getAbsolutePath());
68                      this.setupUtil.copy(new FileInputStream(sourceFile), new FileOutputStream(targetFile));
69                  } catch (IOException e) {
70                      throw new TomcatSetupException(e.getMessage(), e);
71                  }
72              }
73          }
74      }
75  
76      @Override
77      protected String createTargetFileName(AbstractArtifact abstractArtifact) {
78          return ((WebappArtifact) abstractArtifact).getContextPath() + "." + abstractArtifact.getType();
79      }
80  
81      protected void unzipWebappArtifact(WebappArtifact webappArtifact) {
82          ZipUtil.unzip(webappArtifact.getFile(), new File(createTargetFileName(webappArtifact)));
83      }
84  }