Clover coverage report - CRISPY - 1.1.1
Coverage timestamp: Mi Nov 15 2006 13:09:46 CET
file stats: LOC: 122   Methods: 8
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MiniHttpServer.java 91,7% 100% 100% 98,1%
coverage coverage
 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  1 public MiniHttpServer() {
 37  1 this(DEFAULT_PORT);
 38    }
 39   
 40  17 public MiniHttpServer(int pvPort) {
 41  17 try {
 42  17 port = pvPort;
 43  17 server = new HttpServer();
 44  17 server.addListener(new InetAddrPort(pvPort));
 45    } catch (Exception e) {
 46    if (ServiceManager.DEBUG_MODE_ON) {
 47    e.printStackTrace();
 48    }
 49    }
 50    }
 51   
 52  2 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  18 public void setContext(String pvContext) { context = pvContext; }
 57  16 public String getContext() {
 58  1 if (context == null) { context = "/"; }
 59  2 if (!context.startsWith("/")) { context = "/" + context; }
 60  16 return context;
 61    }
 62   
 63  2 public void addServlet(String pvPath, String pvServletClassName) {
 64  2 servlets.put(pvPath, pvServletClassName);
 65    }
 66   
 67  27 public void addServlet(String pvPath, String pvServletClassName, String pvParam, String pvParamValue) {
 68  27 String lvStrArray[] = new String[] { pvPath, pvServletClassName, pvParam, pvParamValue };
 69  27 servlets.put(Integer.toString(++id), lvStrArray);
 70    }
 71   
 72   
 73  13 public void start() {
 74  13 try {
 75    if (log.isDebugEnabled()) { log.debug("Context: " + getContext()); }
 76  13 HttpContext context = server.getContext(getContext());
 77  13 context.addHandler(new ResourceHandler());
 78  13 ServletHandler handler= new ServletHandler();
 79   
 80  13 Iterator it = servlets.keySet().iterator();
 81  13 while (it.hasNext()) {
 82  27 String lvKey = (String) it.next();
 83  27 Object lvValue = servlets.get(lvKey);
 84  27 if (lvValue instanceof String) {
 85    if (log.isDebugEnabled()) { log.debug("Path: " + lvKey + " Servlet: " + lvValue); }
 86  3 handler.addServlet(lvKey, (String) lvValue);
 87    }
 88  24 else if (lvValue instanceof String[]) {
 89  24 String lvStrArray[] = (String[]) lvValue;
 90  24 ServletHolder lvHolder = handler.addServlet(lvStrArray[0], lvStrArray[1]);
 91  24 lvHolder.setInitParameter(lvStrArray[2], lvStrArray[3]);
 92    }
 93    }
 94   
 95  13 context.addHandler(handler);
 96  13 handler.setAutoInitializeServlets(true);
 97  13 handler.initializeServlets();
 98   
 99  13 boolean isFree = Util.isPortFree(port);
 100  13 if (isFree == true) {
 101  12 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    }