001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.adaptor.http;
009:
010: import java.net.InetAddress;
011: import java.net.UnknownHostException;
012: import java.net.ServerSocket;
013: import java.net.Socket;
014: import java.io.IOException;
015: import java.io.File;
016:
017: import javax.management.MBeanServer;
018:
019: /**
020: *
021: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
022: */
023:
024: public class HttpServer implements Runnable {
025: private volatile boolean running = false;
026: private int port = 8082;
027: private String documentRoot = new File("./template")
028: .getAbsolutePath(); // the path that the template file in
029:
030: private MBeanServer server = null;
031: private ServerSocket ssocket = null;
032:
033: public HttpServer(MBeanServer server) {
034: this .server = server;
035: }
036:
037: public MBeanServer getServer() {
038: return server;
039: }
040:
041: public void setServer(MBeanServer server) {
042: this .server = server;
043: }
044:
045: public String getHost() {
046: String host = "localhost";
047: try {
048: host = InetAddress.getLocalHost().getHostAddress();
049: } catch (UnknownHostException e) {
050: e.printStackTrace();
051: }
052: return host;
053: }
054:
055: public void setPort(int port) {
056: this .port = port;
057: }
058:
059: public int getPort() {
060: return port;
061: }
062:
063: /**
064: * doStart a new thread ,wait for http connection
065: */
066: public void start() {
067: if (running)
068: return;
069: new Thread(this ).start();
070: }
071:
072: public void stop() {
073: running = false;
074: }
075:
076: public boolean isRunning() {
077: return running;
078: }
079:
080: public String getDocumentRoot() {
081: return documentRoot;
082: }
083:
084: public void setDocumentRoot(String path) {
085: documentRoot = path;
086: }
087:
088: public void run() {
089: try {
090: ssocket = new ServerSocket(port);
091: running = true;
092: System.out.println("Http Server is running at port: "
093: + ssocket.getLocalPort());
094: while (isRunning()) {
095: Socket csocket = ssocket.accept();
096: // System.out.println("Connection: " + csocket);
097: new HttpThread(this , csocket).start();
098: Thread.sleep(10L);
099: }
100: } catch (IOException e) {
101: e.printStackTrace();
102: running = false;
103: } catch (InterruptedException e) {
104: e.printStackTrace();
105: }
106: }
107:
108: }
|