Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor Wrapper so it can be used for other products.
  • Loading branch information
gwilmer committed Oct 14, 2015
1 parent de375ff commit 18b0a2b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 69 deletions.
@@ -0,0 +1,82 @@
package org.jumpmind.symmetric.wrapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public abstract class AbstractWrapper {


protected static void run(String[] args, String applHomeDir, String configFileName) {

checkArgs(args);
System.setProperty("java.io.tmpdir", applHomeDir + File.separator + "tmp");

WrapperService service = WrapperService.getInstance();
try {
service.loadConfig(configFileName);
} catch (FileNotFoundException e) {
System.out.println("Missing config file " + args[1]);
System.out.println(e.getMessage());
System.exit(Constants.RC_MISSING_CONFIG_FILE);
} catch (IOException e) {
System.out.println("Cannot read config file " + args[1]);
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());
}
}

private static void checkArgs(String[] args) {
if (args.length < 1) {
printUsage();
System.exit(Constants.RC_BAD_USAGE);
}
}

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");
}
}
Expand Up @@ -21,18 +21,13 @@
package org.jumpmind.symmetric.wrapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Wrapper {
public class Wrapper extends AbstractWrapper {

public static void main(String[] args) throws Exception {
if (args.length < 1) {
printUsage();
System.exit(Constants.RC_BAD_USAGE);
}


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) {
Expand All @@ -47,67 +42,8 @@ public static void main(String[] args) throws Exception {
System.exit(Constants.RC_MISSING_SYM_HOME);
}
}
System.setProperty("java.io.tmpdir", dir + File.separator + "tmp");
String configFile = dir + File.separator + "conf" + File.separator + "sym_service.conf";

WrapperService service = WrapperService.getInstance();
try {
service.loadConfig(configFile);
} catch (FileNotFoundException e) {
System.out.println("Missing config file " + args[1]);
System.out.println(e.getMessage());
System.exit(Constants.RC_MISSING_CONFIG_FILE);
} catch (IOException e) {
System.out.println("Cannot read config file " + args[1]);
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());
}
}

private 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");

run(args, dir, configFile);
}
}

0 comments on commit 18b0a2b

Please sign in to comment.