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.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Properties;
28
29 import org.apache.commons.io.IOUtils;
30
31 import com.googlecode.t7mp.SetupUtil;
32 import com.googlecode.t7mp.PluginLog;
33 import com.googlecode.t7mp.TomcatSetupException;
34 import com.googlecode.t7mp.util.CommonsSetupUtil;
35 import com.googlecode.t7mp.util.FilesOnlyFileFilter;
36
37
38
39
40
41
42 public class CopyUserConfigStep implements Step {
43
44 private File catalinaBaseDir;
45 private File userConfigDir;
46 private PluginLog log;
47
48 private final SetupUtil setupUtil = new CommonsSetupUtil();
49
50 @Override
51 public void execute(Context context) {
52
53 this.catalinaBaseDir = context.getConfiguration().getCatalinaBase();
54 this.userConfigDir = context.getConfiguration().getTomcatConfigDirectory();
55 this.log = context.getLog();
56
57
58 if (userConfigDir == null) {
59 log.info("No directory for userConfigFiles configured.");
60 return;
61 }
62 if (!userConfigDir.exists() || !userConfigDir.isDirectory()) {
63 log.warn("The configured Directory for configuration files does not exist. "
64 + userConfigDir.getAbsolutePath());
65 log.warn("Ignoring user configuration.");
66 }
67 if (userConfigDir.exists() && userConfigDir.isDirectory()) {
68 File[] files = userConfigDir.listFiles(new FilesOnlyFileFilter());
69 for (File configFile : files) {
70 try {
71 if (configFile.getName().equals("catalina.properties")) {
72 mergeUserCatalinaPropertiesWithDefault(configFile);
73 continue;
74 }
75 log.debug("Copy provided config file '" + configFile.getName() + "' to "
76 + catalinaBaseDir.getAbsolutePath() + "/conf/" + configFile.getName());
77 this.setupUtil.copy(new FileInputStream(configFile), new FileOutputStream(new File(catalinaBaseDir,
78 "/conf/" + configFile.getName())));
79 } catch (FileNotFoundException e) {
80 throw new TomcatSetupException(e.getMessage(), e);
81 } catch (IOException e) {
82 throw new TomcatSetupException(e.getMessage(), e);
83 }
84 }
85 }
86 }
87
88 private void mergeUserCatalinaPropertiesWithDefault(File userCatlinaPropertiesFile) throws IOException {
89 File defaultConfigFile = new File(catalinaBaseDir, "/conf/catalina.properties");
90
91 Properties userCatalinaProperties = loadPropertiesFromFile(userCatlinaPropertiesFile);
92 Properties defaultCatalinProperties = loadPropertiesFromFile(defaultConfigFile);
93
94 mergeProperties(defaultCatalinProperties, userCatalinaProperties, getExcludes());
95
96 writePropertiesToFile(defaultCatalinProperties, defaultConfigFile);
97 }
98
99 private Properties loadPropertiesFromFile(File propertiesFile) throws IOException {
100 Properties properties = new Properties();
101 InputStream in = new FileInputStream(propertiesFile);
102 try {
103 properties.load(in);
104 } finally {
105 IOUtils.closeQuietly(in);
106 }
107
108 return properties;
109 }
110
111 private Collection<String> getExcludes() {
112 Collection<String> excludes = new ArrayList<String>();
113 excludes.add("http.port");
114 excludes.add("shutdown.port");
115 excludes.add("shutdown.command");
116 return excludes;
117 }
118
119 private void mergeProperties(Properties target, Properties source, Collection<String> excludes) {
120 for (Object key : source.keySet()) {
121 if (!excludes.contains(key)) {
122 target.setProperty((String) key, (String) source.get(key));
123 } else {
124 log.debug("Skip property " + key + " from user catalina.properties");
125 }
126 }
127 }
128
129 private void writePropertiesToFile(Properties properties, File target) throws IOException {
130 OutputStream defaultOut = null;
131 try {
132 defaultOut = new FileOutputStream(target);
133 properties.store(defaultOut, "");
134 } finally {
135 IOUtils.closeQuietly(defaultOut);
136 }
137 }
138
139 }