1 package net.sf.crispy.impl;
2
3 import java.util.Hashtable;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import net.sf.crispy.util.Util;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.mortbay.http.HttpContext;
12 import org.mortbay.http.HttpServer;
13 import org.mortbay.http.handler.ResourceHandler;
14 import org.mortbay.jetty.servlet.ServletHandler;
15 import org.mortbay.jetty.servlet.ServletHolder;
16 import org.mortbay.util.InetAddrPort;
17
18 /**
19 * This server is to set a web context and add servlets with mapping path.
20 *
21 * @author Linke
22 *
23 */
24 public class MiniHttpServer implements MiniServer {
25
26 protected static final Log log = LogFactory.getLog (MiniHttpServer.class);
27
28 public static final int DEFAULT_PORT = 8123;
29
30 private HttpServer server = null;
31 private String context = "";
32 private Map servlets = new Hashtable();
33 private int id = 0;
34 private int port = DEFAULT_PORT;
35
36 public MiniHttpServer() {
37 this(DEFAULT_PORT);
38 }
39
40 public MiniHttpServer(int pvPort) {
41 try {
42 port = pvPort;
43 server = new HttpServer();
44 server.addListener(new InetAddrPort(pvPort));
45 } catch (Exception e) {
46 if (ServiceManager.DEBUG_MODE_ON) {
47 e.printStackTrace();
48 }
49 }
50 }
51
52 public void addService (String pvServiceInterface, String pvServiceObject) {
53 if (log.isDebugEnabled()) { log.debug("The method addService in the MiniHttpServer class do nothing!"); }
54 }
55
56 public void setContext(String pvContext) { context = pvContext; }
57 public String getContext() {
58 if (context == null) { context = "/"; }
59 if (!context.startsWith("/")) { context = "/" + context; }
60 return context;
61 }
62
63 public void addServlet(String pvPath, String pvServletClassName) {
64 servlets.put(pvPath, pvServletClassName);
65 }
66
67 public void addServlet(String pvPath, String pvServletClassName, String pvParam, String pvParamValue) {
68 String lvStrArray[] = new String[] { pvPath, pvServletClassName, pvParam, pvParamValue };
69 servlets.put(Integer.toString(++id), lvStrArray);
70 }
71
72
73 public void start() {
74 try {
75 if (log.isDebugEnabled()) { log.debug("Context: " + getContext()); }
76 HttpContext context = server.getContext(getContext());
77 context.addHandler(new ResourceHandler());
78 ServletHandler handler= new ServletHandler();
79
80 Iterator it = servlets.keySet().iterator();
81 while (it.hasNext()) {
82 String lvKey = (String) it.next();
83 Object lvValue = servlets.get(lvKey);
84 if (lvValue instanceof String) {
85 if (log.isDebugEnabled()) { log.debug("Path: " + lvKey + " Servlet: " + lvValue); }
86 handler.addServlet(lvKey, (String) lvValue);
87 }
88 else if (lvValue instanceof String[]) {
89 String lvStrArray[] = (String[]) lvValue;
90 ServletHolder lvHolder = handler.addServlet(lvStrArray[0], lvStrArray[1]);
91 lvHolder.setInitParameter(lvStrArray[2], lvStrArray[3]);
92 }
93 }
94
95 context.addHandler(handler);
96 handler.setAutoInitializeServlets(true);
97 handler.initializeServlets();
98
99 boolean isFree = Util.isPortFree(port);
100 if (isFree == true) {
101 server.start();
102 }
103
104 } catch (Exception e) {
105 if (ServiceManager.DEBUG_MODE_ON) {
106 e.printStackTrace();
107 }
108 }
109 }
110
111 public void stop() {
112 try {
113 server.stop();
114 } catch (Exception e) {
115 if (ServiceManager.DEBUG_MODE_ON) {
116 e.printStackTrace();
117 }
118 }
119 }
120
121
122 }