/
WrapperConfig.java
251 lines (211 loc) · 8.34 KB
/
WrapperConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.wrapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WrapperConfig {
protected String configFile;
protected Map<String, ArrayList<String>> prop;
protected File workingDirectory;
protected String jarFile;
public WrapperConfig(String applHomeDir, String configFile, String jarFile) throws IOException {
prop = getProperties(configFile);
this.configFile = new File(configFile).getAbsolutePath();
this.jarFile = new File(jarFile).getAbsolutePath();
workingDirectory = new File(applHomeDir);
}
public String getWrapperJarPath() {
return jarFile;
}
public String getConfigFile() {
return configFile;
}
public File getWorkingDirectory() {
return workingDirectory;
}
public String getLogFile() {
return getProperty(prop, "wrapper.logfile", "logs/wrapper.log");
}
public String getWrapperPidFile() {
return getProperty(prop, "wrapper.pidfile", "tmp/wrapper.pid");
}
public String getServerPidFile() {
return getProperty(prop, "wrapper.server.pidfile", "tmp/server.pid");
}
public long getLogFileMaxSize() {
String str = getProperty(prop, "wrapper.logfile.maxsize", "10M").toUpperCase();
int multiplier = 1;
if (str.indexOf("K") != -1) {
multiplier = 1024;
} else if (str.indexOf("M") != -1) {
multiplier = 1048576;
}
return Long.parseLong(str.replaceAll("[^0-9]", "")) * multiplier;
}
public int getLogFileMaxFiles() {
return Integer.parseInt(getProperty(prop, "wrapper.logfile.maxfiles", "3"));
}
public String getLogFileLogLevel() {
return getProperty(prop, "wrapper.logfile.loglevel", "INFO");
}
public String getName() {
return getProperty(prop, "wrapper.name", "symmetricds");
}
public String getDisplayName() {
return getProperty(prop, "wrapper.displayname", "SymmetricDS");
}
public String getDescription() {
return getProperty(prop, "wrapper.description", "SymmetricDS Database Synchronization");
}
public boolean isAutoStart() {
return getProperty(prop, "wrapper.ntservice.starttype", "auto").equalsIgnoreCase("auto");
}
public boolean isDelayStart() {
return getProperty(prop, "wrapper.ntservice.starttype", "auto").equalsIgnoreCase("delay");
}
public List<String> getDependencies() {
return prop.get("wrapper.ntservice.dependency");
}
public String getJavaCommand() {
return getProperty(prop, "wrapper.java.command", "java");
}
public List<String> getOptions() {
return prop.get("wrapper.java.additional");
}
public ArrayList<String> getCommand(boolean isConsole) {
ArrayList<String> cmdList = new ArrayList<String>();
cmdList.add(getJavaCommand());
String initMem = getProperty(prop, "wrapper.java.initmemory", "256");
if (! initMem.toUpperCase().endsWith("M")) {
initMem += "M";
}
cmdList.add("-Xms" + initMem);
String maxMem = getProperty(prop, "wrapper.java.maxmemory", "256");
if (! maxMem.toUpperCase().endsWith("M")) {
maxMem += "M";
}
cmdList.add("-Xmx" + maxMem);
String version = System.getProperty("java.version");
boolean expandWildcard = version != null && version.startsWith("1.5");
List<String> cp = getListProperty(prop, "wrapper.java.classpath");
StringBuilder sb = new StringBuilder(cp.size());
for (int i = 0; i < cp.size(); i++) {
if (i > 0) {
sb.append(File.pathSeparator);
}
String classPath = cp.get(i);
if (expandWildcard) {
classPath = expandWildcard(classPath);
} else {
classPath = classPath.replaceAll("\\*\\.jar", "*");
}
sb.append(classPath);
}
cmdList.add("-cp");
cmdList.add(sb.toString());
List<String> javaAdditional = getListProperty(prop, "wrapper.java.additional");
cmdList.addAll(javaAdditional);
List<String> appParams = getListProperty(prop, "wrapper.app.parameter");
appParams.remove("--no-log-console");
cmdList.addAll(appParams);
if (!isConsole) {
cmdList.add("--no-log-console");
}
return cmdList;
}
private String expandWildcard(String classPath) {
int index = classPath.indexOf("*");
if (index != -1) {
String dirName = classPath.substring(0, index);
File dir = new File(dirName);
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar") || name.endsWith(".JAR");
}
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < files.length; i++) {
if (i > 0) {
sb.append(File.pathSeparator);
}
sb.append(dirName).append(files[i].getName());
}
classPath = sb.toString();
}
return classPath;
}
/**
* Read wrapper properties from symmetric-server.properties file
*
* @param filename String containing name location of symmetric-server.properties file
* @return Map keyed by property name with value of an ArrayList containing all values
* @throws IOException
*/
private Map<String, ArrayList<String>> getProperties(String filename) throws IOException {
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.matches("^\\s*#.*") && !line.matches("\\s*")) {
int index = line.indexOf("=");
if (index != -1) {
String name = line.substring(0, index).trim();
String value = line.substring(index + 1).trim();
if (name.matches(".*\\d{1,2}")) {
name = name.substring(0, name.lastIndexOf("."));
}
ArrayList<String> values = map.get(name);
if (values == null) {
values = new ArrayList<String>();
map.put(name, values);
}
values.add(value);
}
}
}
reader.close();
return map;
}
private String getProperty(Map<String, ArrayList<String>> prop, String name, String defaultValue) {
ArrayList<String> values = prop.get(name);
String value = null;
if (values != null && values.size() > 0) {
value = values.get(0);
} else {
value = defaultValue;
}
return value;
}
private List<String> getListProperty(Map<String, ArrayList<String>> prop, String name) {
ArrayList<String> value = prop.get(name);
if (value == null) {
value = new ArrayList<String>(0);
}
return value;
}
}