add http admin server

This commit is contained in:
温少
2012-05-10 23:25:20 +08:00
parent 4dc8a4336f
commit 90f434bca6
7 changed files with 9617 additions and 6 deletions
+13 -6
View File
@@ -179,6 +179,12 @@
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
@@ -192,12 +198,6 @@
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
@@ -331,5 +331,12 @@
<version>0.9.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,167 @@
package com.alibaba.druid.http;
import java.io.IOException;
import java.net.BindException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.thread.QueuedThreadPool;
import org.mortbay.util.MultiException;
import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
public class HttpServer {
private final static Log LOG = LogFactory.getLog(HttpServer.class);
protected final Server webServer;
protected final Connector listener;
protected final WebAppContext webAppContext;
protected final Map<Context, Boolean> defaultContexts = new HashMap<Context, Boolean>();
public HttpServer() throws IOException{
this("stat", "0.0.0.0", 19790);
}
public HttpServer(String name, String bindAddress, int port) throws IOException{
webServer = new Server();
listener = createBaseListener();
listener.setHost(bindAddress);
listener.setPort(port);
webServer.addConnector(listener);
webServer.setThreadPool(new QueuedThreadPool());
final String appDir = getWebAppsPath();
ContextHandlerCollection contexts = new ContextHandlerCollection();
webServer.setHandler(contexts);
webAppContext = new WebAppContext();
webAppContext.setDisplayName("WepAppsContext");
webAppContext.setContextPath("/");
webAppContext.setWar(appDir + "/" + name);
webServer.addHandler(webAppContext);
addDefaultApps(contexts, appDir);
addDefaultServlets();
}
/**
* Create a required listener for the Jetty instance listening on the port provided. This wrapper and all subclasses
* must create at least one listener.
*/
public Connector createBaseListener() throws IOException {
return HttpServer.createDefaultChannelConnector();
}
// LimitedPrivate for creating secure datanodes
public static Connector createDefaultChannelConnector() {
SelectChannelConnector ret = new SelectChannelConnector();
ret.setLowResourceMaxIdleTime(10000);
ret.setAcceptQueueSize(128);
ret.setResolveNames(false);
ret.setUseDirectBuffers(false);
return ret;
}
/**
* Get the pathname to the webapps files.
*
* @return the pathname as a URL
* @throws IOException if 'webapps' directory cannot be found on CLASSPATH.
*/
protected String getWebAppsPath() throws IOException {
URL url = getClass().getClassLoader().getResource("druid-webapps");
if (url == null) throw new IOException("webapps not found in CLASSPATH");
return url.toString();
}
/**
* stop the server
*/
public void stop() throws Exception {
listener.close();
webServer.stop();
}
public void join() throws InterruptedException {
webServer.join();
}
/**
* Start the server. Does not wait for the server to start.
*/
public void start() throws IOException {
try {
int port = 0;
int oriPort = listener.getPort(); // The original requested port
while (true) {
try {
port = webServer.getConnectors()[0].getLocalPort();
LOG.debug("Port returned by webServer.getConnectors()[0]." + "getLocalPort() before open() is "
+ port + ". Opening the listener on " + oriPort);
listener.open();
port = listener.getLocalPort();
LOG.debug("listener.getLocalPort() returned " + listener.getLocalPort()
+ " webServer.getConnectors()[0].getLocalPort() returned "
+ webServer.getConnectors()[0].getLocalPort());
LOG.debug("Jetty bound to port " + port);
webServer.start();
break;
} catch (IOException ex) {
// if this is a bind exception,
// then try the next port number.
if (ex instanceof BindException) {
throw (BindException) ex;
} else {
LOG.debug("HttpServer.start() threw a non Bind IOException");
throw ex;
}
} catch (MultiException ex) {
LOG.debug("HttpServer.start() threw a MultiException");
throw ex;
}
}
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
throw new IOException("Problem starting http server", ex);
}
}
/**
* Add default apps.
*
* @param appDir The application directory
* @throws IOException
*/
protected void addDefaultApps(ContextHandlerCollection parent, final String appDir) throws IOException {
// set up the context for "/static/*"
Context staticContext = new Context(parent, "/static");
staticContext.setResourceBase(appDir + "/static");
staticContext.addServlet(DefaultServlet.class, "/*");
staticContext.setDisplayName("static");
setContextAttributes(staticContext);
defaultContexts.put(staticContext, true);
}
private void setContextAttributes(Context context) {
}
protected void addDefaultServlets() {
}
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</web-app>
@@ -0,0 +1,19 @@
<html>
<head>
<title>Druid Administration</title>
</head>
<body>
<h1>Druid Administration</h1>
<ul>
<li><a href="dfshealth.jsp">DFS Health/Status</a></li>
</ul>
</body>
</html>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
package com.alibaba.druid.bvt.http;
import com.alibaba.druid.http.HttpServer;
import junit.framework.TestCase;
public class HttpServerTest extends TestCase {
public void test_httpServer() throws Exception {
HttpServer server = new HttpServer();
server.start();
server.stop();
}
}
@@ -0,0 +1,12 @@
package com.alibaba.druid.http;
import junit.framework.TestCase;
public class HttpServerTest2 extends TestCase {
public void test_httpServer() throws Exception {
HttpServer server = new HttpServer();
server.start();
server.join();
}
}