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.FileOutputStream;
21  import java.io.IOException;
22  import java.util.List;
23  
24  import com.google.common.collect.Lists;
25  import com.googlecode.t7mp.AbstractArtifact;
26  import com.googlecode.t7mp.SetupUtil;
27  import com.googlecode.t7mp.TomcatSetupException;
28  import com.googlecode.t7mp.configuration.ResolutionException;
29  import com.googlecode.t7mp.util.CommonsSetupUtil;
30  
31  public abstract class AbstractDeploymentStep implements Step {
32  
33      protected SetupUtil setupUtil = new CommonsSetupUtil();
34  
35      //    protected MyArtifactResolver myArtifactResolver;
36  
37      protected Context context;
38  
39      //    protected ArtifactResolver artifactResolver;
40  
41      @Override
42      public void execute(Context context) {
43          this.context = context;
44          //        this.artifactResolver = context.getArtifactResolver();
45          //        this.myArtifactResolver = new MyArtifactResolver(context.getMojo());
46  
47          List<AbstractArtifact> artifactList = getArtifactList();
48          artifactList = resolveArtifacts(artifactList);
49          deployArtifacts(artifactList);
50      }
51  
52      protected abstract List<AbstractArtifact> getArtifactList();
53  
54      protected String createTargetFileName(AbstractArtifact abstractArtifact) {
55          return abstractArtifact.getArtifactId() + "-" + abstractArtifact.getVersion() + "."
56                  + abstractArtifact.getType();
57      }
58  
59      protected void deployArtifacts(List<AbstractArtifact> artifactList) {
60          for (AbstractArtifact artifact : artifactList) {
61              try {
62                  String targetFileName = createTargetFileName(artifact);
63                  File sourceFile = artifact.getFile();
64                  File targetFile = new File(context.getConfiguration().getCatalinaBase(), "/lib/" + targetFileName);
65                  context.getLog().debug(
66                          "Copy artifact from " + sourceFile.getAbsolutePath() + " to " + targetFile.getAbsolutePath());
67                  this.setupUtil.copy(new FileInputStream(sourceFile), new FileOutputStream(targetFile));
68              } catch (IOException e) {
69                  throw new TomcatSetupException(e.getMessage(), e);
70              }
71          }
72      }
73  
74      protected List<AbstractArtifact> resolveArtifacts(List<? extends AbstractArtifact> artifacts) {
75          List<AbstractArtifact> resolvedArtifacts = Lists.newArrayList();
76          for (AbstractArtifact abstractArtifact : artifacts) {
77              context.getLog().debug("Resolve artifact for " + abstractArtifact.toString());
78              //            if (!abstractArtifact.getGroupId().equals("local")) {
79              File artifact;
80              try {
81                  //                artifact = myArtifactResolver.resolve(abstractArtifact.getGroupId(), abstractArtifact.getArtifactId(),
82                  //                        abstractArtifact.getVersion(), abstractArtifact.getClassifier(), abstractArtifact.getType(),
83                  //                        Artifact.SCOPE_COMPILE);
84  
85                  artifact = context.getArtifactResolver().resolveArtifact(abstractArtifact.getArtifactCoordinates());
86              } catch (ResolutionException e) {
87                  throw new TomcatSetupException(e.getMessage(), e);
88              }
89              abstractArtifact.setFile(artifact);
90              resolvedArtifacts.add(abstractArtifact);
91              //            } else {
92              //                Artifact artifact = new DefaultArtifact(abstractArtifact.getGroupId(),
93              //                        abstractArtifact.getArtifactId(),
94              //                        VersionRange.createFromVersion(abstractArtifact.getVersion()), Artifact.SCOPE_COMPILE,
95              //                        abstractArtifact.getType(), null, new DefaultArtifactHandler("jar"), false);
96              //                String resourceName = "/com/googlecode/t7mp/repo/" + abstractArtifact.getArtifactId() + "/"
97              //                        + abstractArtifact.getVersion() + "/" + abstractArtifact.getArtifactId() + "-"
98              //                        + abstractArtifact.getVersion() + ".jar";
99              //                BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream(resourceName));
100             //                File tempFile;
101             //                try {
102             //                    tempFile = File.createTempFile("local_Artifact", ".maven.tmp");
103             //                    tempFile.deleteOnExit();
104             //                    IOUtils.copy(bis, new FileOutputStream(tempFile));
105             //                    artifact.setFile(tempFile);
106             //                    abstractArtifact.setArtifact(artifact);
107             //                    resolvedArtifacts.add(abstractArtifact);
108             //                } catch (FileNotFoundException e) {
109             //                    throw new TomcatSetupException(e.getMessage(), e);
110             //                } catch (IOException e) {
111             //                    throw new TomcatSetupException(e.getMessage(), e);
112             //                }
113             //            }
114         }
115         return resolvedArtifacts;
116     }
117 
118 }