001: package net.sf.crispy.impl;
002:
003: import java.util.Hashtable;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import net.sf.crispy.util.Util;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.mortbay.http.HttpContext;
012: import org.mortbay.http.HttpServer;
013: import org.mortbay.http.handler.ResourceHandler;
014: import org.mortbay.jetty.servlet.ServletHandler;
015: import org.mortbay.jetty.servlet.ServletHolder;
016: import org.mortbay.util.InetAddrPort;
017:
018: /**
019: * This server is to set a web context and add servlets with mapping path.
020: *
021: * @author Linke
022: *
023: */
024: public class MiniHttpServer implements MiniServer {
025:
026: protected static final Log log = LogFactory
027: .getLog(MiniHttpServer.class);
028:
029: public static final int DEFAULT_PORT = 8123;
030:
031: private HttpServer server = null;
032: private String context = "";
033: private Map servlets = new Hashtable();
034: private int id = 0;
035: private int port = DEFAULT_PORT;
036:
037: public MiniHttpServer() {
038: this (DEFAULT_PORT);
039: }
040:
041: public MiniHttpServer(int pvPort) {
042: try {
043: port = pvPort;
044: server = new HttpServer();
045: server.addListener(new InetAddrPort(pvPort));
046: } catch (Exception e) {
047: if (ServiceManager.DEBUG_MODE_ON) {
048: e.printStackTrace();
049: }
050: }
051: }
052:
053: public void addService(String pvServiceInterface,
054: String pvServiceObject) {
055: if (log.isDebugEnabled()) {
056: log
057: .debug("The method addService in the MiniHttpServer class do nothing!");
058: }
059: }
060:
061: public void setContext(String pvContext) {
062: context = pvContext;
063: }
064:
065: public String getContext() {
066: if (context == null) {
067: context = "/";
068: }
069: if (!context.startsWith("/")) {
070: context = "/" + context;
071: }
072: return context;
073: }
074:
075: public void addServlet(String pvPath, String pvServletClassName) {
076: servlets.put(pvPath, pvServletClassName);
077: }
078:
079: public void addServlet(String pvPath, String pvServletClassName,
080: String pvParam, String pvParamValue) {
081: String lvStrArray[] = new String[] { pvPath,
082: pvServletClassName, pvParam, pvParamValue };
083: servlets.put(Integer.toString(++id), lvStrArray);
084: }
085:
086: public void start() {
087: try {
088: if (log.isDebugEnabled()) {
089: log.debug("Context: " + getContext());
090: }
091: HttpContext context = server.getContext(getContext());
092: context.addHandler(new ResourceHandler());
093: ServletHandler handler = new ServletHandler();
094:
095: Iterator it = servlets.keySet().iterator();
096: while (it.hasNext()) {
097: String lvKey = (String) it.next();
098: Object lvValue = servlets.get(lvKey);
099: if (lvValue instanceof String) {
100: if (log.isDebugEnabled()) {
101: log.debug("Path: " + lvKey + " Servlet: "
102: + lvValue);
103: }
104: handler.addServlet(lvKey, (String) lvValue);
105: } else if (lvValue instanceof String[]) {
106: String lvStrArray[] = (String[]) lvValue;
107: ServletHolder lvHolder = handler.addServlet(
108: lvStrArray[0], lvStrArray[1]);
109: lvHolder.setInitParameter(lvStrArray[2],
110: lvStrArray[3]);
111: }
112: }
113:
114: context.addHandler(handler);
115: handler.setAutoInitializeServlets(true);
116: handler.initializeServlets();
117:
118: boolean isFree = Util.isPortFree(port);
119: if (isFree == true) {
120: server.start();
121: }
122:
123: } catch (Exception e) {
124: if (ServiceManager.DEBUG_MODE_ON) {
125: e.printStackTrace();
126: }
127: }
128: }
129:
130: public void stop() {
131: try {
132: server.stop();
133: } catch (Exception e) {
134: if (ServiceManager.DEBUG_MODE_ON) {
135: e.printStackTrace();
136: }
137: }
138: }
139:
140: }
|