package net.i2p.router.web; import net.i2p.data.DataHelper; import net.i2p.router.Router; import net.i2p.router.RouterContext; import net.i2p.util.RandomSource; /** * simple helper to control restarts/shutdowns in the left hand nav * */ public class ConfigRestartBean { /** all these are tagged below so no need to _x them here. * order is: form value, form class, display text. */ private static final String[] SET1 = {"shutdownImmediate", "stop", "Shutdown immediately", "cancelShutdown", "cancel", "Cancel shutdown"}; private static final String[] SET2 = {"restartImmediate", "reload", "Restart immediately", "cancelShutdown", "cancel", "Cancel restart"}; private static final String[] SET3 = {"restart", "reload", "Restart", "shutdown", "stop", "Shutdown"}; private static final String[] SET4 = {"shutdown", "stop", "Shutdown"}; private static final String _systemNonce = Long.toString(RandomSource.getInstance().nextLong()); /** formerly System.getProperty("console.nonce") */ public static String getNonce() { return _systemNonce; } /** this also initiates the restart/shutdown based on action */ public static String renderStatus(String urlBase, String action, String nonce) { RouterContext ctx = ContextHelper.getContext(null); String systemNonce = getNonce(); if ( (nonce != null) && (systemNonce.equals(nonce)) && (action != null) ) { // Normal browsers send value, IE sends button label if ("shutdownImmediate".equals(action) || _("Shutdown immediately", ctx).equals(action)) { if (ctx.hasWrapper()) ConfigServiceHandler.registerWrapperNotifier(ctx, Router.EXIT_HARD, false); //ctx.router().shutdown(Router.EXIT_HARD); // never returns ctx.router().shutdownGracefully(Router.EXIT_HARD); // give the UI time to respond } else if ("cancelShutdown".equals(action) || _("Cancel shutdown", ctx).equals(action) || _("Cancel restart", ctx).equals(action)) { ctx.router().cancelGracefulShutdown(); } else if ("restartImmediate".equals(action) || _("Restart immediately", ctx).equals(action)) { if (ctx.hasWrapper()) ConfigServiceHandler.registerWrapperNotifier(ctx, Router.EXIT_HARD_RESTART, false); //ctx.router().shutdown(Router.EXIT_HARD_RESTART); // never returns ctx.router().shutdownGracefully(Router.EXIT_HARD_RESTART); // give the UI time to respond } else if ("restart".equals(action) || _("Restart", ctx).equals(action)) { if (ctx.hasWrapper()) ConfigServiceHandler.registerWrapperNotifier(ctx, Router.EXIT_GRACEFUL_RESTART, false); ctx.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART); } else if ("shutdown".equals(action) || _("Shutdown", ctx).equals(action)) { if (ctx.hasWrapper()) ConfigServiceHandler.registerWrapperNotifier(ctx, Router.EXIT_GRACEFUL, false); ctx.router().shutdownGracefully(); } } boolean shuttingDown = isShuttingDown(ctx); boolean restarting = isRestarting(ctx); long timeRemaining = ctx.router().getShutdownTimeRemaining(); StringBuilder buf = new StringBuilder(128); if ((shuttingDown || restarting) && timeRemaining <= 5*1000) { buf.append("