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.IOException;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.apache.commons.io.FileUtils;
24
25 import com.googlecode.t7mp.T7Configuration;
26 import com.googlecode.t7mp.PluginLog;
27 import com.googlecode.t7mp.TomcatSetupException;
28 import com.googlecode.t7mp.configuration.PluginArtifactResolver;
29 import com.googlecode.t7mp.configuration.ResolutionException;
30 import com.googlecode.t7mp.util.FileFilters;
31 import com.googlecode.t7mp.util.FileUtil;
32 import com.googlecode.t7mp.util.ZipUtil;
33
34
35
36
37
38
39
40 public class ResolveConfigurationArtifactStep implements Step {
41
42 protected PluginLog logger;
43
44 protected T7Configuration baseConfiguration;
45 protected PluginArtifactResolver artifactResolver;
46
47 @Override
48 public void execute(Context context) {
49 this.artifactResolver = context.getArtifactResolver();
50 this.baseConfiguration = context.getConfiguration();
51 this.logger = context.getLog();
52
53 if (baseConfiguration.getConfigArtifact() == null) {
54 this.logger.info("No configurationArtifact found, skip this step.");
55 return;
56 }
57
58 this.logger.debug("resolve configurationArtifact ...");
59 File unpackDirectory = null;
60 try {
61 File resolvedArtifact = artifactResolver.resolveArtifact(this.baseConfiguration.getConfigArtifact()
62 .getArtifactCoordinates());
63 unpackDirectory = getUnpackDirectory();
64 ZipUtil.unzip(resolvedArtifact, unpackDirectory);
65 this.logger.debug("unzipped to " + unpackDirectory.getAbsolutePath());
66 copyToTomcatConfDirectory(unpackDirectory);
67 } catch (ResolutionException e) {
68 this.logger.error(e.getMessage(), e);
69 throw new TomcatSetupException(e.getMessage(), e);
70 } catch (IOException e) {
71 this.logger.error(e.getMessage(), e);
72 throw new TomcatSetupException(e.getMessage(), e);
73 } finally {
74 if (unpackDirectory != null) {
75 try {
76 FileUtils.deleteDirectory(unpackDirectory);
77 } catch (IOException e) {
78 this.logger.error("Could not delete temp directory " + unpackDirectory.getAbsolutePath(), e);
79 }
80 }
81 }
82 }
83
84 private void copyToTomcatConfDirectory(File unpackDirectory) throws IOException {
85 this.logger.debug("copy conf-files ...");
86 File confDirectory = new File(this.baseConfiguration.getCatalinaBase(), "conf");
87 this.logger.debug("targetConfDirectory is " + confDirectory.getAbsolutePath());
88 Set<File> files = FileUtil.getAllFiles(unpackDirectory, FileFilters.forAll(), false);
89 for (File file : files) {
90 this.logger.debug("copy file " + file.getName() + ", path " + file.getAbsolutePath()
91 + " to targetDirectory");
92 FileUtils.copyFileToDirectory(file, confDirectory);
93 }
94 }
95
96 protected File getUnpackDirectory() {
97 File tempDir = new File(System.getProperty("java.io.tmpdir"));
98 File upackDirectory = new File(tempDir, UUID.randomUUID().toString());
99 upackDirectory.mkdirs();
100 return upackDirectory;
101 }
102 }