/**
* Create a status server on the given port.
* The jsp scripts are taken from src/webapps/<name>.
* @param name The name of the server
* @param port The port to use on the server
* @param findPort whether the server should start at the given port and
* increment by 1 until it finds a free port.
* @param conf Configuration
*/
public HttpServer(String name, String bindAddress, int port,
boolean findPort, Configuration conf) throws IOException {
webServer = new org.mortbay.jetty.Server();
this.findPort = findPort;
listener = new SocketListener();
listener.setPort(port);
listener.setHost(bindAddress);
webServer.addListener(listener); final String appDir = getWebAppsPath();
webAppContext = webServer.addWebApplication("/", appDir + "/" + name);
addDefaultApps(appDir); final FilterInitializer[] initializers = getFilterInitializers(conf);
if (initializers != null) {
for(FilterInitializer c : initializers) {
c.initFilter(this);
}
}
addDefaultServlets();
} protected void addDefaultServlets() {
// set up default servlets
addServlet("stacks", "/stacks", StackServlet.class);
addServlet("logLevel", "/logLevel", LogLevel.Servlet.class);
}
/**
* Add default apps.
* @param appDir The application directory
* @throws IOException
*/
protected void addDefaultApps(final String appDir) throws IOException {
// set up the context for "/logs/" if "hadoop.log.dir" property is defined.
String logDir = System.getProperty("hadoop.log.dir");
if (logDir != null) {
addContext("/logs/*", logDir, true);
} // set up the context for "/static/*"
addContext("/static/*", appDir + "/static", true);/**
* Add a context
* @param pathSpec The path spec for the context
* @param dir The directory containing the context
* @param isFiltered if true, the servlet is added to the filter path mapping
* @throws IOException
*/
protected void addContext(String pathSpec, String dir, boolean isFiltered) throws IOException {
WebApplicationContext webAppCtx = webServer.addWebApplication(pathSpec, dir);
defaultContexts.put(webAppCtx, isFiltered);
} } /** {@inheritDoc} */
public void addFilter(String name, String classname,
Map<String, String> parameters) { final String[] USER_FACING_URLS = {"*.html", "*.jsp"};
defineFilter(webAppContext, name, classname, parameters, USER_FACING_URLS); final String[] ALL_URLS = { "/*" };
for (Map.Entry<WebApplicationContext, Boolean> e : defaultContexts
.entrySet()) {
if (e.getValue()) {
WebApplicationContext ctx = e.getKey();
defineFilter(ctx, name, classname, parameters, ALL_URLS);
LOG.info("Added filter " + name + " (class=" + classname
+ ") to context " + ctx.getName());
}
}
filterNames.add(name);
} /**
* Define a filter for a context and set up default url mappings.
*/
protected void defineFilter(WebApplicationContext ctx, String name,
String classname, Map<String, String> parameters, String[] urls) { WebApplicationHandler handler = ctx.getWebApplicationHandler();
FilterHolder holder = handler.defineFilter(name, classname);
if (parameters != null) {
for(Map.Entry<String, String> e : parameters.entrySet()) {
holder.setInitParameter(e.getKey(), e.getValue());
}
} for (String url : urls) {
handler.addFilterPathMapping(url, name, Dispatcher.__ALL);
}
} /**
* Add the path spec to the filter path mapping.
* @param pathSpec The path spec
*/
protected void addFilterPathMapping(String pathSpec) {
WebApplicationHandler handler = webAppContext.getWebApplicationHandler();
for(String name : filterNames) {
handler.addFilterPathMapping(pathSpec, name, Dispatcher.__ALL);
}
}/**
* Start the server. Does not wait for the server to start.
*/
public void start() throws IOException {
try {
while (true) {
try {
webServer.start();
break;
} catch (org.mortbay.util.MultiException ex) {
// if the multi exception contains ONLY a bind exception,
// then try the next port number.
boolean needNewPort = false;
if(ex.size() == 1) {
Exception sub = ex.getException(0);
if (sub instanceof java.net.BindException) {
if(!findPort)
throw sub; // java.net.BindException
needNewPort = true;
}
}
if (!needNewPort)
throw ex;
listener.setPort(listener.getPort() + 1);
}
}
} catch (IOException ie) {
throw ie;
} catch (Exception e) {
IOException ie = new IOException("Problem starting http server");
ie.initCause(e);
throw ie;
}
} /**
* stop the server
*/
public void stop() throws InterruptedException {
webServer.stop();
}