View Javadoc

1   package net.sf.crispy.impl.caucho;
2   
3   import java.io.IOException;
4   import java.util.Enumeration;
5   
6   import javax.servlet.ServletConfig;
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import net.sf.crispy.InvocationException;
12  import net.sf.crispy.util.Util;
13  
14  import com.caucho.hessian.io.HessianInput;
15  import com.caucho.hessian.io.HessianOutput;
16  import com.caucho.hessian.server.HessianSkeleton;
17  
18  /**
19   * This class execute the services and answer to request from the hessian client.
20   * 
21   * @author Linke
22   *
23   */
24  public class HessianServlet extends CauchoServlet {
25  
26  	private static final long serialVersionUID = 471335728357L;
27  	
28  	public HessianServlet() {
29  //		super.addCache(Echo.class.getName(), new HessianSkeleton(new EchoImpl(), Echo.class));
30  //		super.addCache(Calculator.class.getName(), new HessianSkeleton(new CalculatorImpl(), Calculator.class));
31  	}
32  
33  	public void init(ServletConfig pvConfig) throws ServletException {
34  		super.init(pvConfig);
35  		Enumeration lvEnumeration = pvConfig.getInitParameterNames();
36  		while (lvEnumeration.hasMoreElements()) {
37  			String lvKey = (String) lvEnumeration.nextElement();
38  			String lvValue = getInitParameter(lvKey);
39  			try {
40  				Object lvServiceObject = Util.createObject(lvValue);
41  				Class clazz = null;
42  				try {
43  					clazz = Class.forName(lvKey);
44  				} catch (Exception e) {
45  					clazz = lvServiceObject.getClass();
46  				}
47  				addCache(lvKey, new HessianSkeleton(lvServiceObject, clazz));
48  			} catch (Exception e) {
49  				throw new ServletException("Exception in init-method: " + e, e);
50  			}
51  		}		
52  	}
53  
54  	
55  	protected void doPost(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
56  		String lvKey = getCacheKey(pvRequest);
57  		HessianSkeleton skeleton = (HessianSkeleton) getFromCache(getCacheKey(pvRequest));
58  		if (log.isDebugEnabled()) { log.debug("CachKey: " + lvKey + " / Skeleton: " + skeleton); }
59  		if (skeleton == null) { throw new InvocationException("For the key: " + lvKey + " can't find a register service in the HessianServlet!"); }
60  
61  		HessianInput in = new HessianInput(pvRequest.getInputStream());
62          HessianOutput out = new HessianOutput(pvResponse.getOutputStream());
63  
64          try { 
65          	skeleton.invoke(in, out); 
66          } catch (Throwable e) { 
67          	throw new ServletException(e); 
68          }
69  	}
70  
71  }