2004-05-02 05:02:10 +00:00
|
|
|
package net.i2p.router.startup;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2004-08-19 17:42:47 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.File;
|
2004-05-02 05:02:10 +00:00
|
|
|
import java.util.ArrayList;
|
2004-05-17 03:38:53 +00:00
|
|
|
import java.util.List;
|
2004-08-19 17:42:47 +00:00
|
|
|
import java.util.Properties;
|
2004-05-17 03:38:53 +00:00
|
|
|
|
2004-08-19 17:42:47 +00:00
|
|
|
import net.i2p.data.DataHelper;
|
2004-05-17 03:38:53 +00:00
|
|
|
import net.i2p.router.JobImpl;
|
|
|
|
import net.i2p.router.RouterContext;
|
|
|
|
import net.i2p.util.I2PThread;
|
|
|
|
import net.i2p.util.Log;
|
2004-05-02 05:02:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run any client applications specified in the router.config. If any clientApp
|
|
|
|
* contains the config property ".onBoot=true" it'll be launched immediately, otherwise
|
|
|
|
* it'll get queued up for starting 2 minutes later.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class LoadClientAppsJob extends JobImpl {
|
|
|
|
private Log _log;
|
|
|
|
/** wait 2 minutes before starting up client apps */
|
|
|
|
private final static long STARTUP_DELAY = 2*60*1000;
|
2004-08-19 17:42:47 +00:00
|
|
|
|
|
|
|
private static final String PROP_CLIENT_CONFIG_FILENAME = "router.clientConfigFile";
|
|
|
|
private static final String DEFAULT_CLIENT_CONFIG_FILENAME = "clients.config";
|
2004-09-06 05:20:40 +00:00
|
|
|
private static boolean _loaded = false;
|
2004-08-19 17:42:47 +00:00
|
|
|
|
2004-05-02 05:02:10 +00:00
|
|
|
public LoadClientAppsJob(RouterContext ctx) {
|
|
|
|
super(ctx);
|
|
|
|
_log = ctx.logManager().getLog(LoadClientAppsJob.class);
|
|
|
|
}
|
|
|
|
public void runJob() {
|
2004-09-06 05:20:40 +00:00
|
|
|
synchronized (LoadClientAppsJob.class) {
|
|
|
|
if (_loaded) return;
|
|
|
|
_loaded = true;
|
|
|
|
}
|
2004-08-19 17:42:47 +00:00
|
|
|
Properties clientApps = getClientApps();
|
2004-05-02 05:02:10 +00:00
|
|
|
int i = 0;
|
|
|
|
while (true) {
|
2004-08-19 17:42:47 +00:00
|
|
|
String className = clientApps.getProperty("clientApp."+i+".main");
|
|
|
|
String clientName = clientApps.getProperty("clientApp."+i+".name");
|
|
|
|
String args = clientApps.getProperty("clientApp."+i+".args");
|
2004-11-25 21:57:19 +00:00
|
|
|
String delayStr = clientApps.getProperty("clientApp." + i + ".delay");
|
2004-08-19 17:42:47 +00:00
|
|
|
String onBoot = clientApps.getProperty("clientApp." + i + ".onBoot");
|
2004-05-02 05:02:10 +00:00
|
|
|
boolean onStartup = false;
|
|
|
|
if (onBoot != null)
|
|
|
|
onStartup = "true".equals(onBoot) || "yes".equals(onBoot);
|
2004-11-25 21:57:19 +00:00
|
|
|
|
|
|
|
long delay = (onStartup ? 0 : STARTUP_DELAY);
|
|
|
|
if (delayStr != null)
|
|
|
|
try { delay = 1000*Integer.parseInt(delayStr); } catch (NumberFormatException nfe) {}
|
|
|
|
|
2004-05-02 05:02:10 +00:00
|
|
|
if (className == null)
|
|
|
|
break;
|
|
|
|
|
|
|
|
String argVal[] = parseArgs(args);
|
|
|
|
if (onStartup) {
|
|
|
|
// run this guy now
|
|
|
|
runClient(className, clientName, argVal);
|
|
|
|
} else {
|
2004-11-25 21:57:19 +00:00
|
|
|
// wait before firing it up
|
|
|
|
getContext().jobQueue().addJob(new DelayedRunClient(className, clientName, argVal, delay));
|
2004-05-02 05:02:10 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-19 17:42:47 +00:00
|
|
|
private Properties getClientApps() {
|
|
|
|
Properties rv = new Properties();
|
|
|
|
String clientConfigFile = getContext().getProperty(PROP_CLIENT_CONFIG_FILENAME, DEFAULT_CLIENT_CONFIG_FILENAME);
|
|
|
|
File cfgFile = new File(clientConfigFile);
|
|
|
|
|
|
|
|
// fall back to use router.config's clientApp.* lines
|
|
|
|
if (!cfgFile.exists())
|
|
|
|
return new Properties(getContext().router().getConfigMap());
|
|
|
|
|
|
|
|
try {
|
|
|
|
DataHelper.loadProps(rv, cfgFile);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.warn("Error loading the client app properties from " + cfgFile.getName(), ioe);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-05-02 05:02:10 +00:00
|
|
|
private class DelayedRunClient extends JobImpl {
|
|
|
|
private String _className;
|
|
|
|
private String _clientName;
|
|
|
|
private String _args[];
|
2004-11-25 21:57:19 +00:00
|
|
|
public DelayedRunClient(String className, String clientName, String args[], long delay) {
|
2004-07-15 05:12:37 +00:00
|
|
|
super(LoadClientAppsJob.this.getContext());
|
2004-05-02 05:02:10 +00:00
|
|
|
_className = className;
|
|
|
|
_clientName = clientName;
|
|
|
|
_args = args;
|
2004-11-25 21:57:19 +00:00
|
|
|
getTiming().setStartAfter(LoadClientAppsJob.this.getContext().clock().now() + delay);
|
2004-05-02 05:02:10 +00:00
|
|
|
}
|
|
|
|
public String getName() { return "Delayed client job"; }
|
|
|
|
public void runJob() {
|
|
|
|
runClient(_className, _clientName, _args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static String[] parseArgs(String args) {
|
|
|
|
List argList = new ArrayList(4);
|
|
|
|
if (args != null) {
|
|
|
|
char data[] = args.toCharArray();
|
|
|
|
StringBuffer buf = new StringBuffer(32);
|
|
|
|
boolean isQuoted = false;
|
|
|
|
for (int i = 0; i < data.length; i++) {
|
|
|
|
switch (data[i]) {
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
|
|
|
if (isQuoted) {
|
|
|
|
String str = buf.toString().trim();
|
|
|
|
if (str.length() > 0)
|
|
|
|
argList.add(str);
|
|
|
|
buf = new StringBuffer(32);
|
|
|
|
} else {
|
|
|
|
isQuoted = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
// whitespace - if we're in a quoted section, keep this as part of the quote,
|
|
|
|
// otherwise use it as a delim
|
|
|
|
if (isQuoted) {
|
|
|
|
buf.append(data[i]);
|
|
|
|
} else {
|
|
|
|
String str = buf.toString().trim();
|
|
|
|
if (str.length() > 0)
|
|
|
|
argList.add(str);
|
|
|
|
buf = new StringBuffer(32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf.append(data[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (buf.length() > 0) {
|
|
|
|
String str = buf.toString().trim();
|
|
|
|
if (str.length() > 0)
|
|
|
|
argList.add(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String rv[] = new String[argList.size()];
|
|
|
|
for (int i = 0; i < argList.size(); i++)
|
|
|
|
rv[i] = (String)argList.get(i);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runClient(String className, String clientName, String args[]) {
|
|
|
|
_log.info("Loading up the client application " + clientName + ": " + className + " " + args);
|
|
|
|
I2PThread t = new I2PThread(new RunApp(className, clientName, args));
|
2004-06-20 00:40:16 +00:00
|
|
|
if (clientName == null)
|
|
|
|
clientName = className + " client";
|
2004-05-02 05:02:10 +00:00
|
|
|
t.setName(clientName);
|
|
|
|
t.setDaemon(true);
|
|
|
|
t.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class RunApp implements Runnable {
|
|
|
|
private String _className;
|
|
|
|
private String _appName;
|
|
|
|
private String _args[];
|
|
|
|
public RunApp(String className, String appName, String args[]) {
|
|
|
|
_className = className;
|
|
|
|
_appName = appName;
|
|
|
|
if (args == null)
|
|
|
|
_args = new String[0];
|
|
|
|
else
|
|
|
|
_args = args;
|
|
|
|
}
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Class cls = Class.forName(_className);
|
|
|
|
Method method = cls.getMethod("main", new Class[] { String[].class });
|
|
|
|
method.invoke(cls, new Object[] { _args });
|
|
|
|
} catch (Throwable t) {
|
|
|
|
_log.log(Log.CRIT, "Error starting up the client class " + _className, t);
|
|
|
|
}
|
|
|
|
_log.info("Done running client application " + _appName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() { return "Load up any client applications"; }
|
|
|
|
|
|
|
|
public static void main(String args[]) {
|
|
|
|
test(null);
|
|
|
|
test("hi how are you?");
|
|
|
|
test("hi how are you? ");
|
|
|
|
test(" hi how are you? ");
|
|
|
|
test(" hi how are \"y\"ou? ");
|
|
|
|
test("-nogui -e \"config localhost 17654\" -e \"httpclient 4544\"");
|
|
|
|
test("-nogui -e 'config localhost 17654' -e 'httpclient 4544'");
|
|
|
|
}
|
|
|
|
private static void test(String args) {
|
|
|
|
String parsed[] = parseArgs(args);
|
|
|
|
System.out.print("Parsed [" + args + "] into " + parsed.length + " elements: ");
|
|
|
|
for (int i = 0; i < parsed.length; i++)
|
|
|
|
System.out.print("[" + parsed[i] + "] ");
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
}
|