2010-02-06 18:40:55 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2010-02-18 16:33:47 +00:00
|
|
|
import java.util.Collection;
|
2010-02-06 18:40:55 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
import net.i2p.I2PAppContext;
|
|
|
|
|
2010-02-08 22:19:59 +00:00
|
|
|
import org.mortbay.http.HttpContext;
|
2010-02-18 16:33:47 +00:00
|
|
|
import org.mortbay.http.HttpListener;
|
2010-02-06 18:40:55 +00:00
|
|
|
import org.mortbay.jetty.Server;
|
|
|
|
import org.mortbay.jetty.servlet.WebApplicationContext;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-15 14:34:25 +00:00
|
|
|
* Add, start or stop a webapp.
|
|
|
|
* Add to the webapp classpath if specified in webapps.config.
|
2010-02-06 18:40:55 +00:00
|
|
|
*
|
|
|
|
* Sadly, setting Class-Path in MANIFEST.MF doesn't work for jetty wars.
|
2010-03-15 14:34:25 +00:00
|
|
|
* See WebAppConfiguration for more information.
|
2010-02-06 18:40:55 +00:00
|
|
|
* but let's just do it in webapps.config.
|
|
|
|
*
|
2010-03-15 14:34:25 +00:00
|
|
|
* No, wac.addClassPath() does not work. For more info see:
|
2010-02-06 18:40:55 +00:00
|
|
|
*
|
|
|
|
* http://servlets.com/archive/servlet/ReadMsg?msgId=511113&listName=jetty-support
|
|
|
|
*
|
|
|
|
* @since 0.7.12
|
|
|
|
* @author zzz
|
|
|
|
*/
|
|
|
|
public class WebAppStarter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds and starts
|
2010-02-11 21:41:54 +00:00
|
|
|
* @throws just about anything, caller would be wise to catch Throwable
|
2010-02-06 18:40:55 +00:00
|
|
|
*/
|
|
|
|
static void startWebApp(I2PAppContext ctx, Server server, String appName, String warPath) throws Exception {
|
|
|
|
File tmpdir = new File(ctx.getTempDir(), "jetty-work-" + appName + ctx.random().nextInt());
|
|
|
|
WebApplicationContext wac = addWebApp(ctx, server, appName, warPath, tmpdir);
|
|
|
|
wac.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add but don't start
|
2010-03-29 21:20:48 +00:00
|
|
|
* This is used only by RouterConsoleRunner, which adds all the webapps first
|
|
|
|
* and then starts all at once.
|
2010-02-06 18:40:55 +00:00
|
|
|
*/
|
|
|
|
static WebApplicationContext addWebApp(I2PAppContext ctx, Server server, String appName, String warPath, File tmpdir) throws IOException {
|
|
|
|
|
2010-03-29 21:20:48 +00:00
|
|
|
// Jetty will happily load one context on top of another without stopping
|
|
|
|
// the first one, so we remove any previous one here
|
|
|
|
try {
|
|
|
|
stopWebApp(server, appName);
|
|
|
|
} catch (Throwable t) {}
|
|
|
|
|
2010-02-06 18:40:55 +00:00
|
|
|
WebApplicationContext wac = server.addWebApplication("/"+ appName, warPath);
|
|
|
|
tmpdir.mkdir();
|
|
|
|
wac.setTempDirectory(tmpdir);
|
|
|
|
|
|
|
|
// this does the passwords...
|
|
|
|
RouterConsoleRunner.initialize(wac);
|
|
|
|
|
|
|
|
// see WebAppConfiguration for info
|
|
|
|
String[] classNames = server.getWebApplicationConfigurationClassNames();
|
|
|
|
String[] newClassNames = new String[classNames.length + 1];
|
|
|
|
for (int j = 0; j < classNames.length; j++)
|
|
|
|
newClassNames[j] = classNames[j];
|
|
|
|
newClassNames[classNames.length] = WebAppConfiguration.class.getName();
|
|
|
|
wac.setConfigurationClassNames(newClassNames);
|
|
|
|
return wac;
|
|
|
|
}
|
2010-02-08 22:19:59 +00:00
|
|
|
|
|
|
|
/**
|
2010-03-29 21:20:48 +00:00
|
|
|
* stop it and remove the context
|
2010-02-11 21:41:54 +00:00
|
|
|
* @throws just about anything, caller would be wise to catch Throwable
|
2010-02-08 22:19:59 +00:00
|
|
|
*/
|
|
|
|
static void stopWebApp(Server server, String appName) {
|
|
|
|
// this will return a new context if one does not exist
|
|
|
|
HttpContext wac = server.getContext('/' + appName);
|
|
|
|
try {
|
|
|
|
// false -> not graceful
|
|
|
|
wac.stop(false);
|
|
|
|
} catch (InterruptedException ie) {}
|
2010-03-29 21:20:48 +00:00
|
|
|
try {
|
|
|
|
server.removeContext(wac);
|
|
|
|
} catch (IllegalStateException ise) {}
|
2010-02-08 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2010-04-16 03:58:48 +00:00
|
|
|
static boolean isWebAppRunning(String appName) {
|
|
|
|
Server server = WebAppStarter.getConsoleServer();
|
|
|
|
// this will return a new context if one does not exist
|
|
|
|
HttpContext wac = server.getContext('/' + appName);
|
|
|
|
return wac.isStarted();
|
|
|
|
}
|
|
|
|
|
2010-02-18 16:33:47 +00:00
|
|
|
/** see comments in ConfigClientsHandler */
|
|
|
|
static Server getConsoleServer() {
|
|
|
|
Collection c = Server.getHttpServers();
|
|
|
|
for (int i = 0; i < c.size(); i++) {
|
|
|
|
Server s = (Server) c.toArray()[i];
|
|
|
|
HttpListener[] hl = s.getListeners();
|
|
|
|
for (int j = 0; j < hl.length; j++) {
|
|
|
|
if (hl[j].getPort() == 7657)
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-02-06 18:40:55 +00:00
|
|
|
}
|