plugin stopper

This commit is contained in:
zzz
2010-02-08 20:37:49 +00:00
parent b7a0aeea34
commit c0135b592d
3 changed files with 58 additions and 9 deletions

View File

@ -76,7 +76,7 @@ public class PluginStarter implements Runnable {
Properties props = new Properties(); Properties props = new Properties();
DataHelper.loadProps(props, clientConfig); DataHelper.loadProps(props, clientConfig);
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig); List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig);
runClientApps(ctx, pluginDir, clients); runClientApps(ctx, pluginDir, clients, true);
} }
// start console webapps in console/webapps // start console webapps in console/webapps
@ -142,6 +142,37 @@ public class PluginStarter implements Runnable {
return true; return true;
} }
/** @return true on success */
static boolean stopPlugin(RouterContext ctx, String appName) throws Exception {
Log log = ctx.logManager().getLog(PluginStarter.class);
File pluginDir = new File(ctx.getAppDir(), PluginUpdateHandler.PLUGIN_DIR + '/' + appName);
if ((!pluginDir.exists()) || (!pluginDir.isDirectory())) {
log.error("Cannot stop nonexistent plugin: " + appName);
return false;
}
// stop things in clients.config
File clientConfig = new File(pluginDir, "clients.config");
if (clientConfig.exists()) {
Properties props = new Properties();
DataHelper.loadProps(props, clientConfig);
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig);
runClientApps(ctx, pluginDir, clients, false);
}
// stop console webapps in console/webapps
// remove summary bar link
Properties props = pluginProperties(ctx, appName);
String name = props.getProperty("consoleLinkName_" + Messages.getLanguage(ctx));
if (name == null)
name = props.getProperty("consoleLinkName");
if (name != null && name.length() > 0)
NavHelper.unregisterApp(name);
return true;
}
/** plugin.config */ /** plugin.config */
public static Properties pluginProperties(I2PAppContext ctx, String appName) { public static Properties pluginProperties(I2PAppContext ctx, String appName) {
File cfgFile = new File(ctx.getAppDir(), PluginUpdateHandler.PLUGIN_DIR + '/' + appName + '/' + "plugin.config"); File cfgFile = new File(ctx.getAppDir(), PluginUpdateHandler.PLUGIN_DIR + '/' + appName + '/' + "plugin.config");
@ -202,12 +233,21 @@ public class PluginStarter implements Runnable {
return null; return null;
} }
private static void runClientApps(RouterContext ctx, File pluginDir, List<ClientAppConfig> apps) { /** @param start true=start, false=stop */
private static void runClientApps(RouterContext ctx, File pluginDir, List<ClientAppConfig> apps, boolean start) {
Log log = ctx.logManager().getLog(PluginStarter.class); Log log = ctx.logManager().getLog(PluginStarter.class);
for(ClientAppConfig app : apps) { for(ClientAppConfig app : apps) {
if (app.disabled) if (start && app.disabled)
continue; continue;
String argVal[] = LoadClientAppsJob.parseArgs(app.args); String argVal[];
if (start) {
argVal = LoadClientAppsJob.parseArgs(app.args);
} else {
// stopargs must be present
if (app.stopargs == null || app.stopargs.length() <= 0)
continue;
argVal = LoadClientAppsJob.parseArgs(app.stopargs);
}
// do this after parsing so we don't need to worry about quoting // do this after parsing so we don't need to worry about quoting
for (int i = 0; i < argVal.length; i++) { for (int i = 0; i < argVal.length; i++) {
if (argVal[i].indexOf("$") >= 0) { if (argVal[i].indexOf("$") >= 0) {
@ -225,7 +265,7 @@ public class PluginStarter implements Runnable {
} }
addToClasspath(cp, app.clientName, log); addToClasspath(cp, app.clientName, log);
} }
if (app.delay == 0) { if (app.delay == 0 || !start) {
// run this guy now // run this guy now
LoadClientAppsJob.runClient(app.className, app.clientName, argVal, log); LoadClientAppsJob.runClient(app.className, app.clientName, argVal, log);
} else { } else {

View File

@ -287,7 +287,12 @@ public class PluginUpdateHandler extends UpdateHandler {
return; return;
} }
// check if it is running now and stop it? // check if it is running first?
try {
if (!PluginStarter.stopPlugin(_context, appName)) {
// failed, ignore
}
} catch (Exception e) {} // ignore
} else { } else {
if (Boolean.valueOf(props.getProperty("update-only")).booleanValue()) { if (Boolean.valueOf(props.getProperty("update-only")).booleanValue()) {

View File

@ -35,6 +35,8 @@ public class ClientAppConfig {
public boolean disabled; public boolean disabled;
/** @since 0.7.12 */ /** @since 0.7.12 */
public String classpath; public String classpath;
/** @since 0.7.12 */
public String stopargs;
public ClientAppConfig(String cl, String client, String a, long d, boolean dis) { public ClientAppConfig(String cl, String client, String a, long d, boolean dis) {
className = cl; className = cl;
@ -45,9 +47,10 @@ public class ClientAppConfig {
} }
/** @since 0.7.12 */ /** @since 0.7.12 */
public ClientAppConfig(String cl, String client, String a, long d, boolean dis, String cp) { public ClientAppConfig(String cl, String client, String a, long d, boolean dis, String cp, String sa) {
this(cl, client, a, d, dis); this(cl, client, a, d, dis);
classpath = cp; classpath = cp;
stopargs = sa;
} }
public static File configFile(I2PAppContext ctx) { public static File configFile(I2PAppContext ctx) {
@ -114,6 +117,7 @@ public class ClientAppConfig {
String onBoot = clientApps.getProperty(PREFIX + i + ".onBoot"); String onBoot = clientApps.getProperty(PREFIX + i + ".onBoot");
String disabled = clientApps.getProperty(PREFIX + i + ".startOnLoad"); String disabled = clientApps.getProperty(PREFIX + i + ".startOnLoad");
String classpath = clientApps.getProperty(PREFIX + i + ".classpath"); String classpath = clientApps.getProperty(PREFIX + i + ".classpath");
String stopargs = clientApps.getProperty(PREFIX + i + ".stopargs");
i++; i++;
boolean dis = disabled != null && "false".equals(disabled); boolean dis = disabled != null && "false".equals(disabled);
@ -125,12 +129,12 @@ public class ClientAppConfig {
if (delayStr != null && !onStartup) if (delayStr != null && !onStartup)
try { delay = 1000*Integer.parseInt(delayStr); } catch (NumberFormatException nfe) {} try { delay = 1000*Integer.parseInt(delayStr); } catch (NumberFormatException nfe) {}
rv.add(new ClientAppConfig(className, clientName, args, delay, dis, classpath)); rv.add(new ClientAppConfig(className, clientName, args, delay, dis, classpath, stopargs));
} }
return rv; return rv;
} }
/** classpath not supported */ /** classpath and stopargs not supported */
public static void writeClientAppConfig(RouterContext ctx, List apps) { public static void writeClientAppConfig(RouterContext ctx, List apps) {
File cfgFile = configFile(ctx); File cfgFile = configFile(ctx);
FileOutputStream fos = null; FileOutputStream fos = null;