1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37 protected Context context;
38
39
40
41 @Override
42 public void execute(Context context) {
43 this.context = context;
44
45
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
79 File artifact;
80 try {
81
82
83
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 }
115 return resolvedArtifacts;
116 }
117
118 }