* Console, TunnelControllerGroup: Don't register shutdown hook if ClientAppManager is present

* JettyStart: Fixes for use by plugins
 * RouterAppManager: Add shutdown hook
This commit is contained in:
zzz
2013-04-24 15:45:15 +00:00
parent 57fd46d3a1
commit 813a1981d9
6 changed files with 75 additions and 14 deletions

View File

@@ -125,7 +125,9 @@ public class TunnelControllerGroup implements ClientApp {
loadControllers(_configFile);
if (_mgr != null)
_mgr.register(this);
_context.addShutdownTask(new Shutdown());
// RouterAppManager registers its own shutdown hook
else
_context.addShutdownTask(new Shutdown());
}
/**
@@ -194,7 +196,9 @@ public class TunnelControllerGroup implements ClientApp {
*
* @since 0.8.8
*/
public void shutdown() {
public synchronized void shutdown() {
if (_state != STARTING && _state != RUNNING)
return;
changeState(STOPPING);
if (_mgr != null)
_mgr.unregister(this);

View File

@@ -41,7 +41,6 @@ import org.eclipse.jetty.xml.XmlConfiguration;
*/
public class JettyStart implements ClientApp {
private final I2PAppContext _context;
private final ClientAppManager _mgr;
private final String[] _args;
private final List<LifeCycle> _jettys;
@@ -50,10 +49,12 @@ public class JettyStart implements ClientApp {
/**
* All args must be XML file names.
* Does not support any of the other argument types from org.mortbay.start.Main.
*
* @param context unused, may be null
* @param mgr may be null e.g. for use in plugins
*/
public JettyStart(I2PAppContext context, ClientAppManager mgr, String[] args) throws Exception {
_state = UNINITIALIZED;
_context = context;
_mgr = mgr;
_args = args;
_jettys = new ArrayList(args.length);
@@ -100,13 +101,13 @@ public class JettyStart implements ClientApp {
private class Starter extends Thread {
public Starter() {
super("JettyStarter");
changeState(STARTING);
}
/**
* Modified from XmlConfiguration.main()
*/
public void run() {
changeState(STARTING);
for (LifeCycle lc : _jettys) {
if (!lc.isRunning()) {
try {
@@ -118,11 +119,12 @@ public class JettyStart implements ClientApp {
}
}
changeState(RUNNING);
_mgr.register(JettyStart.this);
if (_mgr != null)
_mgr.register(JettyStart.this);
}
}
public void shutdown(String[] args) {
public synchronized void shutdown(String[] args) {
if (_state != RUNNING)
return;
if (_jettys.isEmpty()) {
@@ -135,10 +137,10 @@ public class JettyStart implements ClientApp {
private class Stopper extends Thread {
public Stopper() {
super("JettyStopper");
changeState(STOPPING);
}
public void run() {
changeState(STOPPING);
for (LifeCycle lc : _jettys) {
if (lc.isRunning()) {
try {
@@ -170,6 +172,22 @@ public class JettyStart implements ClientApp {
private synchronized void changeState(ClientAppState state, Exception e) {
_state = state;
_mgr.notify(this, state, null, e);
if (_mgr != null)
_mgr.notify(this, state, null, e);
}
/**
* For use in a plugin clients.config
* @param args passed to constructor
* @since 0.9.6
*/
public static void main(String[] args) {
try {
new JettyStart(null, null, args);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -214,15 +214,19 @@ public class RouterConsoleRunner implements RouterApp {
/////// ClientApp methods
/** @since 0.9.4 */
public void startup() {
public synchronized void startup() {
changeState(STARTING);
startTrayApp(_context);
startConsole();
}
/** @since 0.9.4 */
public void shutdown(String[] args) {
public synchronized void shutdown(String[] args) {
if (_state == STOPPED)
return;
changeState(STOPPING);
if (PluginStarter.pluginsEnabled(_context))
(new I2PAppThread(new PluginStopper(_context), "PluginStopper")).start();
try {
_server.stop();
} catch (Exception ie) {}
@@ -653,10 +657,11 @@ public class RouterConsoleRunner implements RouterApp {
t = new I2PAppThread(new PluginStarter(_context), "PluginStarter", true);
t.setPriority(Thread.NORM_PRIORITY - 1);
t.start();
_context.addShutdownTask(new PluginStopper(_context));
}
// stat summarizer registers its own hook
_context.addShutdownTask(new ServerShutdown());
// RouterAppManager registers its own hook
if (_mgr == null)
_context.addShutdownTask(new ServerShutdown());
ConfigServiceHandler.registerSignalHandler(_context);
}