diff --git a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Constants.java b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Constants.java index 4e2ee5bd87..9a77e2449f 100644 --- a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Constants.java +++ b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Constants.java @@ -26,12 +26,6 @@ public enum Status { START_PENDING, RUNNING, STOP_PENDING, STOPPED; } - public static final String ENV_SYM_HOME = "SYM_HOME"; - - public static final String SYSPROP_TMPDIR = "java.io.tmpdir"; - - public static final String JAR_NAME = "symmetric-wrapper.jar"; - public static final int RC_BAD_USAGE = 1; public static final int RC_INVALID_ARGUMENT = 2; public static final int RC_MISSING_CONFIG_FILE = 3; @@ -53,7 +47,6 @@ public enum Status { public static final int RC_NATIVE_ERROR = 19; public static final int RC_MISSING_LIB_FOLDER = 20; public static final int RC_MISSING_SERVER_PROPERTIES = 21; - public static final int RC_MISSING_SYM_HOME = 22; public static final int RC_FAIL_CHECK_STATUS = 23; public static final int RC_ALREADY_RUNNING = 24; diff --git a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/UnixService.java b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/UnixService.java index 37d51962bb..b18ed6f22a 100644 --- a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/UnixService.java +++ b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/UnixService.java @@ -147,19 +147,12 @@ protected boolean isPidRunning(int pid) { if (procFile.canRead()) { try { List args = readProcFile(procFile); - String appName = config.getApplicationParameters().get(0); - boolean isJava = false; - boolean isMe = false; - for (String arg : args) { if (arg.contains(config.getJavaCommand())) { - isJava = true; - } - if (arg.contains(appName) || arg.contains(Constants.JAR_NAME)) { - isMe = true; + return true; } } - return isJava && isMe; + return false; } catch (IOException e) { } } diff --git a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Wrapper.java b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Wrapper.java index 80d2a43ab9..a2bd2e41ad 100644 --- a/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Wrapper.java +++ b/symmetric-wrapper/src/main/java/org/jumpmind/symmetric/wrapper/Wrapper.java @@ -21,30 +21,113 @@ package org.jumpmind.symmetric.wrapper; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; public class Wrapper { public static void main(String[] args) throws Exception { - - String dir = System.getenv(Constants.ENV_SYM_HOME); - - if (dir == null || dir.equals("")) { - // Backwards compatible with 3.6 by allowing config file argument to determine home - if (args.length > 1) { - int index = args[1].lastIndexOf(File.separator); - if (index == -1) { - dir = ".."; - } else { - dir = args[1].substring(0, index + 1) + ".."; - } + + if (args.length < 1) { + printUsage(); + System.exit(Constants.RC_BAD_USAGE); + } + + String appDir = null; + String configFile = null; + String jarFile = Wrapper.class.getProtectionDomain().getCodeSource().getLocation().getFile(); + + if (args.length > 1) { + configFile = args[1]; + appDir = getParentDir(configFile); + } 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("Missing " + Constants.ENV_SYM_HOME + " environment variable."); - System.exit(Constants.RC_MISSING_SYM_HOME); + 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); } - String configFile = dir + File.separator + "conf" + File.separator + "sym_service.conf"; - String jarFile = dir + File.separator + "lib" + File.separator + Constants.JAR_NAME; - - WrapperHelper.run(args, dir, configFile, jarFile); } + + 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: "); + 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"); + } + }