/
Wrapper.java
138 lines (126 loc) · 5.24 KB
/
Wrapper.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
/**
* 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.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLDecoder;
public class Wrapper {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
printUsage();
System.exit(Constants.RC_BAD_USAGE);
}
String appDir = null;
String configFile = null;
// Decode spaces and other special characters
String jarFile = Wrapper.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
if (args.length == 2) {
configFile = args[1];
appDir = getParentDir(configFile);
} else if (args.length == 3) {
configFile = args[1];
appDir = args[2];
} else {
appDir = getParentDir(jarFile);
configFile = findConfigFile(appDir + File.separator + "conf");
}
WrapperService service = WrapperService.getInstance();
try {
service.loadConfig(appDir, configFile, jarFile);
} catch (FileNotFoundException e) {
System.out.println("Missing config file " + configFile);
System.out.println(e.getMessage());
System.exit(Constants.RC_MISSING_CONFIG_FILE);
} catch (IOException e) {
System.out.println("Cannot read config file " + configFile);
System.out.println(e.getMessage());
System.exit(Constants.RC_FAIL_READ_CONFIG_FILE);
}
try {
if (args[0].equalsIgnoreCase("start")) {
service.start();
} else if (args[0].equalsIgnoreCase("exec")) {
service.execJava(false);
} else if (args[0].equalsIgnoreCase("init")) {
service.init();
} else if (args[0].equalsIgnoreCase("stop")) {
service.stop();
} else if (args[0].equalsIgnoreCase("restart")) {
service.restart();
} else if (args[0].equalsIgnoreCase("install")) {
service.install();
} else if (args[0].equalsIgnoreCase("uninstall")) {
service.uninstall();
} else if (args[0].equalsIgnoreCase("status")) {
service.status();
} else if (args[0].equalsIgnoreCase("console")) {
service.console();
} else {
System.out.println("ERROR: Invalid argument");
printUsage();
System.exit(Constants.RC_INVALID_ARGUMENT);
}
} catch (WrapperException e) {
System.out.println("Error " + e.getErrorCode() + ": " + e.getMessage());
if (e.getCause() != null) {
System.out.println("Exception " + e.getCause().getClass().getSimpleName() + ": "
+ e.getCause().getMessage());
}
if (e.getNativeErrorCode() > 0) {
System.out.println("Native error " + e.getErrorCode());
}
System.exit(e.getErrorCode());
} catch (Throwable ex) {
ex.printStackTrace();
System.exit(-1);
}
}
protected static String getParentDir(String filepath) {
int index = filepath.lastIndexOf(File.separator);
if (index == -1) {
return "..";
} else {
return filepath.substring(0, index + 1) + "..";
}
}
protected static String findConfigFile(String dirpath) {
File dir = new File(dirpath);
if (dir.exists() && dir.isDirectory()) {
for (String name : dir.list()) {
if (name.endsWith("_service.conf")) {
return dirpath + File.separator + name;
}
}
}
return dirpath + File.separator + "wrapper_service.conf";
}
protected static void printUsage() {
System.out.println("Usage: <start|stop|restart|install|remove|console>");
System.out.println(" start - Start service");
System.out.println(" stop - Stop service");
System.out.println(" restart - Restart service");
System.out.println(" install - Install service");
System.out.println(" uninstall - Uninstall service");
System.out.println(" status - Status of service");
System.out.println(" console - Run from console");
}
}