You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2012/04/17 21:58:09 UTC
svn commit: r1327257 -
/ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java
Author: doogie
Date: Tue Apr 17 19:58:09 2012
New Revision: 1327257
URL: http://svn.apache.org/viewvc?rev=1327257&view=rev
Log:
FEATURE: Start supporting commons-daemon explicitly.
Modified:
ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java
Modified: ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java?rev=1327257&r1=1327256&r2=1327257&view=diff
==============================================================================
--- ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java (original)
+++ ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Start.java Tue Apr 17 19:58:09 2012
@@ -94,7 +94,7 @@ public class Start {
}
}
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws StartupException {
Command command = null;
List<String> loaderArgs = new ArrayList<String>(args.length);
for (String arg: args) {
@@ -129,13 +129,18 @@ public class Start {
}
Start start = new Start();
start.init(args, command == Command.COMMAND);
- if (command == Command.STATUS) {
- System.out.println("Current Status : " + start.status());
- } else if (command == Command.SHUTDOWN) {
- System.out.println("Shutting down server : " + start.shutdown());
- } else {
- // general start
- start.start();
+ try {
+ if (command == Command.STATUS) {
+ System.out.println("Current Status : " + start.status());
+ } else if (command == Command.SHUTDOWN) {
+ System.out.println("Shutting down server : " + start.shutdown());
+ } else {
+ // general start
+ start.start();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(99);
}
}
@@ -153,7 +158,7 @@ public class Start {
private AtomicReference<ServerState> serverState = new AtomicReference<ServerState>(ServerState.STARTING);
private Thread adminPortThread = null;
- private void createListenerThread() throws IOException {
+ private void createListenerThread() throws StartupException {
if (config.adminPort > 0) {
this.adminPortThread = new AdminPortThread();
this.adminPortThread.start();
@@ -171,11 +176,11 @@ public class Start {
}
}
- public void init(String[] args) throws IOException {
+ public void init(String[] args) throws StartupException {
init(args, true);
}
- public void init(String[] args, boolean fullInit) throws IOException {
+ public void init(String[] args, boolean fullInit) throws StartupException {
String globalSystemPropsFileName = System.getProperty("ofbiz.system.props");
if (globalSystemPropsFileName != null) {
FileInputStream stream = null;
@@ -183,18 +188,22 @@ public class Start {
stream = new FileInputStream(globalSystemPropsFileName);
System.getProperties().load(stream);
} catch (IOException e) {
- throw (IOException) new IOException("Couldn't load global system props").initCause(e);
+ throw (StartupException) new StartupException("Couldn't load global system props").initCause(e);
} finally {
if (stream != null){
try {
stream.close();
} catch (IOException e) {
- throw new IOException(e);
+ throw (StartupException) new StartupException("Couldn't close stream").initCause(e);
}
}
}
}
- this.config = Config.getInstance(args);
+ try {
+ this.config = Config.getInstance(args);
+ } catch (IOException e) {
+ throw (StartupException) new StartupException("Couldn't not fetch config instance").initCause(e);
+ }
// parse the startup arguments
if (args.length > 1) {
this.loaderArgs.addAll(Arrays.asList(args).subList(1, args.length));
@@ -220,14 +229,16 @@ public class Start {
}
// initialize the startup loaders
- if (!initStartLoaders()) {
- System.exit(99);
- }
+ initStartLoaders();
}
- private void initClasspath() throws IOException {
+ private void initClasspath() throws StartupException {
Classpath classPath = new Classpath(System.getProperty("java.class.path"));
- this.config.initClasspath(classPath);
+ try {
+ this.config.initClasspath(classPath);
+ } catch (IOException e) {
+ throw (StartupException) new StartupException("Couldn't initialized classpath").initCause(e);
+ }
// Set the classpath/classloader
System.setProperty("java.class.path", classPath.toString());
ClassLoader classloader = classPath.getClassLoader();
@@ -238,27 +249,30 @@ public class Start {
}
}
- private boolean initStartLoaders() {
+ private void initStartLoaders() throws StartupException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
synchronized (this.loaders) {
// initialize the loaders
for (String loaderClassName: config.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
- return false;
+ return;
}
try {
Class<?> loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config, loaderArgs.toArray(new String[loaderArgs.size()]));
loaders.add(loader);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
+ } catch (ClassNotFoundException e) {
+ throw (StartupException) new StartupException(e.getMessage()).initCause(e);
+ } catch (InstantiationException e) {
+ throw (StartupException) new StartupException(e.getMessage()).initCause(e);
+ } catch (IllegalAccessException e) {
+ throw (StartupException) new StartupException(e.getMessage()).initCause(e);
}
}
this.loaders.trimToSize();
}
- return true;
+ return;
}
private String sendSocketCommand(Control control) throws IOException, ConnectException {
@@ -318,9 +332,14 @@ public class Start {
}
}
- public void start() {
+ // org.apache.commons.daemon.Daemon.start()
+ public void start() throws Exception {
if (!startStartLoaders()) {
- System.exit(99);
+ if (this.serverState.get() == ServerState.STOPPING) {
+ return;
+ } else {
+ throw new Exception("Error during start.");
+ }
}
if (config.shutdownAfterLoad) {
stopServer();
@@ -365,12 +384,26 @@ public class Start {
System.exit(0);
}
+ // org.apache.commons.daemon.Daemon.destroy()
+ public void destroy() {
+ // FIXME: undo init() calls.
+ }
+
+ // org.apache.commons.daemon.Daemon.stop()
+ public void stop() {
+ shutdownServer();
+ }
+
private class AdminPortThread extends Thread {
private ServerSocket serverSocket = null;
- AdminPortThread() throws IOException {
+ AdminPortThread() throws StartupException {
super("AdminPortThread");
- this.serverSocket = new ServerSocket(config.adminPort, 1, config.adminAddress);
+ try {
+ this.serverSocket = new ServerSocket(config.adminPort, 1, config.adminAddress);
+ } catch (IOException e) {
+ throw (StartupException) new StartupException("Couldn't create server socket(" + config.adminAddress + ":" + config.adminPort + ")").initCause(e);
+ }
setDaemon(false);
}