View Javadoc

1   package net.sf.crispy.impl.caucho;
2   
3   
4   import net.sf.crispy.impl.MiniHttpServer;
5   import net.sf.crispy.impl.MiniServer;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import test.crispy.example.service.Echo;
11  import test.crispy.example.service.EchoImpl;
12  
13  /**
14   * Mini web server implementation with two servlets (BurlapServlet and HessianServlet).
15   * 
16   * @author Linke
17   *
18   */
19  public class MiniCauchoServer implements MiniServer {
20  
21  	protected static final Log log = LogFactory.getLog (MiniServer.class);
22  	
23  	public static final int DEFAULT_PORT = 8091;
24  	
25  	public static final int SERVER_TYPE_BOTH = 0;
26  	public static final int SERVER_TYPE_BURLAP = 1;
27  	public static final int SERVER_TYPE_HESSIAN = 2;
28  	
29  	
30  	private MiniHttpServer server = null;
31  	private int serverType = SERVER_TYPE_BURLAP;
32  	
33  	public MiniCauchoServer() {
34  		this(SERVER_TYPE_BOTH, DEFAULT_PORT);
35  	}
36  	
37  	public MiniCauchoServer(int pvPort) {
38  		this(SERVER_TYPE_BOTH, pvPort);
39  	}
40  	
41  	public MiniCauchoServer(int pvServerType, int pvPort) {
42  		serverType = pvServerType;
43  		server = new MiniHttpServer(pvPort);
44  		server.setContext("/crispy");
45  	}
46  	
47  	public int getServerType() { return serverType;}
48  	
49  	public void addService (String pvServiceInterface, String pvServiceObject) {
50  		if (serverType == SERVER_TYPE_BURLAP) {
51  			server.addServlet("/burlap/*", BurlapServlet.class.getName(), pvServiceInterface, pvServiceObject);
52  		} else if (serverType == SERVER_TYPE_HESSIAN) {
53  			server.addServlet("/hessian/*", HessianServlet.class.getName(), pvServiceInterface, pvServiceObject);
54  		} else {
55  			server.addServlet("/burlap/*", BurlapServlet.class.getName(), pvServiceInterface, pvServiceObject);
56  			server.addServlet("/hessian/*", HessianServlet.class.getName(), pvServiceInterface, pvServiceObject);
57  		}
58  	}
59  	
60  	public void start() {
61  		try { 
62  			server.start();
63  		} catch (Exception e) {
64  			if (log.isWarnEnabled()) {
65  				log.warn("Error by MiniCauchoServer stop: " + e);
66  			}
67  		}
68  	}
69  
70  	public void stop() {
71  		try { 
72  			server.stop(); 
73  		} catch (Exception e) { 
74  			e.printStackTrace(); 
75  		}
76  	}
77  
78  	public static void main(String[] args) {
79  		MiniCauchoServer miniServer = new MiniCauchoServer();
80  		miniServer.addService(Echo.class.getName(), EchoImpl.class.getName());
81  		miniServer.start();
82  		System.out.println("Server is started ...");
83  	}
84  }