2004-08-23 07:33:14 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2012-05-20 18:12:41 +00:00
|
|
|
import java.io.File;
|
2004-08-31 21:25:23 +00:00
|
|
|
import java.io.IOException;
|
2009-01-02 20:09:20 +00:00
|
|
|
import java.util.List;
|
2004-08-31 21:25:23 +00:00
|
|
|
|
2004-11-25 21:57:19 +00:00
|
|
|
import net.i2p.apps.systray.UrlLauncher;
|
2008-07-16 13:42:54 +00:00
|
|
|
import net.i2p.router.Router;
|
2011-07-15 01:13:35 +00:00
|
|
|
import net.i2p.router.RouterContext;
|
2009-01-02 20:09:20 +00:00
|
|
|
import net.i2p.router.startup.ClientAppConfig;
|
2012-10-13 13:06:22 +00:00
|
|
|
import net.i2p.util.PortMapper;
|
2012-09-09 15:40:14 +00:00
|
|
|
import net.i2p.util.SystemVersion;
|
2012-01-09 23:59:58 +00:00
|
|
|
import net.i2p.util.VersionComparator;
|
2008-07-16 13:42:54 +00:00
|
|
|
|
2004-08-23 21:32:24 +00:00
|
|
|
import org.tanukisoftware.wrapper.WrapperManager;
|
2004-08-23 07:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to deal with form submissions from the service config form and act
|
|
|
|
* upon the values.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ConfigServiceHandler extends FormHandler {
|
|
|
|
|
2012-01-09 23:59:58 +00:00
|
|
|
private static WrapperListener _wrapperListener;
|
2012-01-08 13:15:47 +00:00
|
|
|
|
2012-01-09 23:59:58 +00:00
|
|
|
private static final String LISTENER_AVAILABLE = "3.2.0";
|
2012-01-08 13:15:47 +00:00
|
|
|
|
2011-07-15 01:13:35 +00:00
|
|
|
/**
|
|
|
|
* Register two shutdown hooks, one to rekey and/or tell the wrapper we are stopping,
|
|
|
|
* and a final one to tell the wrapper we are stopped.
|
|
|
|
*
|
|
|
|
* @since 0.8.8
|
|
|
|
*/
|
|
|
|
private void registerWrapperNotifier(int code, boolean rekey) {
|
|
|
|
registerWrapperNotifier(_context, code, rekey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register two shutdown hooks, one to rekey and/or tell the wrapper we are stopping,
|
|
|
|
* and a final one to tell the wrapper we are stopped.
|
|
|
|
*
|
|
|
|
* @since 0.8.8
|
|
|
|
*/
|
|
|
|
public static void registerWrapperNotifier(RouterContext ctx, int code, boolean rekey) {
|
|
|
|
Runnable task = new UpdateWrapperOrRekeyTask(rekey, ctx.hasWrapper());
|
|
|
|
ctx.addShutdownTask(task);
|
|
|
|
if (ctx.hasWrapper()) {
|
|
|
|
task = new FinalWrapperTask(code);
|
|
|
|
ctx.addFinalShutdownTask(task);
|
2004-09-07 07:17:02 +00:00
|
|
|
}
|
2011-07-15 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rekey and/or tell the wrapper we are stopping,
|
|
|
|
*/
|
|
|
|
private static class UpdateWrapperOrRekeyTask implements Runnable {
|
|
|
|
private final boolean _rekey;
|
|
|
|
private final boolean _tellWrapper;
|
|
|
|
private static final int HASHCODE = -123999871;
|
|
|
|
private static final int WAIT = 30*1000;
|
|
|
|
|
|
|
|
public UpdateWrapperOrRekeyTask(boolean rekey, boolean tellWrapper) {
|
|
|
|
_rekey = rekey;
|
|
|
|
_tellWrapper = tellWrapper;
|
|
|
|
}
|
|
|
|
|
2004-09-07 07:17:02 +00:00
|
|
|
public void run() {
|
|
|
|
try {
|
2011-07-15 01:13:35 +00:00
|
|
|
if (_rekey)
|
|
|
|
ContextHelper.getContext(null).router().killKeys();
|
|
|
|
if (_tellWrapper)
|
|
|
|
WrapperManager.signalStopping(WAIT);
|
2004-09-07 07:17:02 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2011-07-15 01:13:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make them all look the same since the hooks are stored in a set
|
|
|
|
* and we don't want dups
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return HASHCODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make them all look the same since the hooks are stored in a set
|
|
|
|
* and we don't want dups
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
return (o != null) && (o instanceof UpdateWrapperOrRekeyTask);
|
|
|
|
}
|
2004-09-07 07:17:02 +00:00
|
|
|
}
|
2005-11-26 09:16:11 +00:00
|
|
|
|
2011-07-15 01:13:35 +00:00
|
|
|
/**
|
|
|
|
* Tell the wrapper we are stopped.
|
|
|
|
*
|
|
|
|
* @since 0.8.8
|
|
|
|
*/
|
|
|
|
private static class FinalWrapperTask implements Runnable {
|
|
|
|
private final int _exitCode;
|
|
|
|
private static final int HASHCODE = 123999871;
|
|
|
|
|
|
|
|
public FinalWrapperTask(int exitCode) {
|
2005-11-26 09:16:11 +00:00
|
|
|
_exitCode = exitCode;
|
|
|
|
}
|
2011-07-15 01:13:35 +00:00
|
|
|
|
2005-11-26 09:16:11 +00:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
WrapperManager.signalStopped(_exitCode);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2011-07-15 01:13:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make them all look the same since the hooks are stored in a set
|
|
|
|
* and we don't want dups
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return HASHCODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make them all look the same since the hooks are stored in a set
|
|
|
|
* and we don't want dups
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
return (o != null) && (o instanceof FinalWrapperTask);
|
|
|
|
}
|
2005-11-26 09:16:11 +00:00
|
|
|
}
|
2011-07-15 01:13:35 +00:00
|
|
|
|
2012-01-08 13:15:47 +00:00
|
|
|
/**
|
|
|
|
* Register a handler for signals,
|
2012-01-09 23:59:58 +00:00
|
|
|
* so we can handle HUP from the wrapper (non-Windows only, wrapper 3.2.0 or higher)
|
2012-01-08 13:15:47 +00:00
|
|
|
*
|
|
|
|
* @since 0.8.13
|
|
|
|
*/
|
|
|
|
synchronized static void registerSignalHandler(RouterContext ctx) {
|
2012-01-09 23:59:58 +00:00
|
|
|
if (ctx.hasWrapper() && _wrapperListener == null &&
|
2012-09-09 15:40:14 +00:00
|
|
|
!SystemVersion.isWindows()) {
|
2012-01-09 23:59:58 +00:00
|
|
|
String wv = System.getProperty("wrapper.version");
|
2013-05-26 17:25:02 +00:00
|
|
|
if (wv != null && VersionComparator.comp(wv, LISTENER_AVAILABLE) >= 0) {
|
2012-01-09 23:59:58 +00:00
|
|
|
try {
|
|
|
|
_wrapperListener = new WrapperListener(ctx);
|
|
|
|
} catch (Throwable t) {}
|
|
|
|
}
|
2012-01-08 13:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister the handler for signals
|
|
|
|
*
|
|
|
|
* @since 0.8.13
|
|
|
|
*/
|
|
|
|
public synchronized static void unregisterSignalHandler() {
|
2012-01-09 23:59:58 +00:00
|
|
|
if (_wrapperListener != null) {
|
|
|
|
_wrapperListener.unregister();
|
|
|
|
_wrapperListener = null;
|
2012-01-08 13:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-15 16:08:33 +00:00
|
|
|
@Override
|
2004-08-23 07:33:14 +00:00
|
|
|
protected void processForm() {
|
|
|
|
if (_action == null) return;
|
2004-08-23 17:11:38 +00:00
|
|
|
|
2009-10-26 02:21:15 +00:00
|
|
|
if (_("Shutdown gracefully").equals(_action)) {
|
2011-07-03 13:46:29 +00:00
|
|
|
if (_context.hasWrapper())
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_GRACEFUL, false);
|
2004-08-23 07:33:14 +00:00
|
|
|
_context.router().shutdownGracefully();
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Graceful shutdown initiated"));
|
|
|
|
} else if (_("Shutdown immediately").equals(_action)) {
|
2011-07-03 13:46:29 +00:00
|
|
|
if (_context.hasWrapper())
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_HARD, false);
|
2004-08-23 21:32:24 +00:00
|
|
|
_context.router().shutdown(Router.EXIT_HARD);
|
2013-06-12 13:45:27 +00:00
|
|
|
addFormNotice(_("Shutdown immediately"));
|
2009-10-26 02:21:15 +00:00
|
|
|
} else if (_("Cancel graceful shutdown").equals(_action)) {
|
2004-08-23 07:33:14 +00:00
|
|
|
_context.router().cancelGracefulShutdown();
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Graceful shutdown cancelled"));
|
|
|
|
} else if (_("Graceful restart").equals(_action)) {
|
2011-07-03 13:46:29 +00:00
|
|
|
// should have wrapper if restart button is visible
|
|
|
|
if (_context.hasWrapper())
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_GRACEFUL_RESTART, false);
|
2004-09-06 05:20:40 +00:00
|
|
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Graceful restart requested"));
|
|
|
|
} else if (_("Hard restart").equals(_action)) {
|
2011-07-03 13:46:29 +00:00
|
|
|
// should have wrapper if restart button is visible
|
|
|
|
if (_context.hasWrapper())
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_HARD_RESTART, false);
|
2004-08-24 18:02:48 +00:00
|
|
|
_context.router().shutdown(Router.EXIT_HARD_RESTART);
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Hard restart requested"));
|
|
|
|
} else if (_("Rekey and Restart").equals(_action)) {
|
|
|
|
addFormNotice(_("Rekeying after graceful restart"));
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_GRACEFUL_RESTART, true);
|
2005-11-26 09:16:11 +00:00
|
|
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
|
2009-10-26 02:21:15 +00:00
|
|
|
} else if (_("Rekey and Shutdown").equals(_action)) {
|
|
|
|
addFormNotice(_("Rekeying after graceful shutdown"));
|
2011-07-15 01:13:35 +00:00
|
|
|
registerWrapperNotifier(Router.EXIT_GRACEFUL, true);
|
2005-11-26 09:16:11 +00:00
|
|
|
_context.router().shutdownGracefully(Router.EXIT_GRACEFUL);
|
2009-10-26 02:21:15 +00:00
|
|
|
} else if (_("Run I2P on startup").equals(_action)) {
|
2004-08-31 21:25:23 +00:00
|
|
|
installService();
|
2009-10-26 02:21:15 +00:00
|
|
|
} else if (_("Don't run I2P on startup").equals(_action)) {
|
2004-08-31 21:25:23 +00:00
|
|
|
uninstallService();
|
2009-10-26 21:48:46 +00:00
|
|
|
} else if (_("Dump threads").equals(_action)) {
|
2004-08-26 03:12:52 +00:00
|
|
|
try {
|
|
|
|
WrapperManager.requestThreadDump();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
addFormError("Warning: unable to contact the service manager - " + t.getMessage());
|
|
|
|
}
|
2012-05-20 18:12:41 +00:00
|
|
|
File wlog = LogsHelper.wrapperLogFile(_context);
|
|
|
|
addFormNotice(_("Threads dumped to {0}", wlog.getAbsolutePath()));
|
2009-10-26 02:21:15 +00:00
|
|
|
} else if (_("View console on startup").equals(_action)) {
|
2004-11-25 21:57:19 +00:00
|
|
|
browseOnStartup(true);
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Console is to be shown on startup"));
|
|
|
|
} else if (_("Do not view console on startup").equals(_action)) {
|
2004-11-25 21:57:19 +00:00
|
|
|
browseOnStartup(false);
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Console is not to be shown on startup"));
|
2012-10-05 13:09:34 +00:00
|
|
|
} else if (_("Force GC").equals(_action)) {
|
|
|
|
Runtime.getRuntime().gc();
|
|
|
|
addFormNotice(_("Full garbage collection requested"));
|
2004-08-23 07:33:14 +00:00
|
|
|
} else {
|
2006-02-15 05:33:17 +00:00
|
|
|
//addFormNotice("Blah blah blah. whatever. I'm not going to " + _action);
|
2004-08-23 07:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-31 21:25:23 +00:00
|
|
|
|
|
|
|
private void installService() {
|
|
|
|
try {
|
|
|
|
Runtime.getRuntime().exec("install_i2p_service_winnt.bat");
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Service installed"));
|
2004-08-31 21:25:23 +00:00
|
|
|
} catch (IOException ioe) {
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormError(_("Warning: unable to install the service") + " - " + ioe.getMessage());
|
2004-08-31 21:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 13:15:47 +00:00
|
|
|
|
2004-08-31 21:25:23 +00:00
|
|
|
private void uninstallService() {
|
|
|
|
try {
|
|
|
|
Runtime.getRuntime().exec("uninstall_i2p_service_winnt.bat");
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormNotice(_("Service removed"));
|
2004-08-31 21:25:23 +00:00
|
|
|
} catch (IOException ioe) {
|
2009-10-26 02:21:15 +00:00
|
|
|
addFormError(_("Warning: unable to remove the service") + " - " + ioe.getMessage());
|
2004-08-31 21:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-25 21:57:19 +00:00
|
|
|
|
|
|
|
private void browseOnStartup(boolean shouldLaunchBrowser) {
|
2013-11-21 11:31:50 +00:00
|
|
|
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
|
2009-01-02 20:09:20 +00:00
|
|
|
boolean found = false;
|
|
|
|
for (int cur = 0; cur < clients.size(); cur++) {
|
2013-11-21 11:31:50 +00:00
|
|
|
ClientAppConfig ca = clients.get(cur);
|
2009-01-02 20:09:20 +00:00
|
|
|
if (UrlLauncher.class.getName().equals(ca.className)) {
|
|
|
|
ca.disabled = !shouldLaunchBrowser;
|
|
|
|
found = true;
|
|
|
|
break;
|
2004-11-25 21:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-02 20:09:20 +00:00
|
|
|
// releases <= 0.6.5 deleted the entry completely
|
|
|
|
if (shouldLaunchBrowser && !found) {
|
2012-10-13 13:06:22 +00:00
|
|
|
int port = _context.portMapper().getPort(PortMapper.SVC_CONSOLE, RouterConsoleRunner.DEFAULT_LISTEN_PORT);
|
|
|
|
ClientAppConfig ca = new ClientAppConfig(UrlLauncher.class.getName(), "consoleBrowser",
|
|
|
|
"http://127.0.0.1:" + port + '/', 5, false);
|
2009-01-02 20:09:20 +00:00
|
|
|
clients.add(ca);
|
|
|
|
}
|
|
|
|
ClientAppConfig.writeClientAppConfig(_context, clients);
|
2004-11-25 21:57:19 +00:00
|
|
|
}
|
2005-11-26 09:16:11 +00:00
|
|
|
}
|