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.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
36
37
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
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 }