KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > server > HessianServlet


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.server;
50
51 import com.caucho.hessian.io.AbstractHessianOutput;
52 import com.caucho.hessian.io.Hessian2Input;
53 import com.caucho.hessian.io.Hessian2Output;
54 import com.caucho.hessian.io.HessianOutput;
55 import com.caucho.hessian.io.SerializerFactory;
56 import com.caucho.services.server.GenericService;
57 import com.caucho.services.server.Service;
58 import com.caucho.services.server.ServiceContext;
59
60 import javax.servlet.GenericServlet JavaDoc;
61 import javax.servlet.Servlet JavaDoc;
62 import javax.servlet.ServletConfig JavaDoc;
63 import javax.servlet.ServletException JavaDoc;
64 import javax.servlet.ServletRequest JavaDoc;
65 import javax.servlet.ServletResponse JavaDoc;
66 import javax.servlet.http.HttpServletRequest JavaDoc;
67 import javax.servlet.http.HttpServletResponse JavaDoc;
68 import java.io.IOException JavaDoc;
69 import java.io.InputStream JavaDoc;
70 import java.io.OutputStream JavaDoc;
71 import java.io.PrintWriter JavaDoc;
72
73 /**
74  * Servlet for serving Hessian services.
75  */

76 public class HessianServlet extends GenericServlet JavaDoc {
77   private Class JavaDoc _homeAPI;
78   private Object JavaDoc _homeImpl;
79   
80   private Class JavaDoc _objectAPI;
81   private Object JavaDoc _objectImpl;
82   
83   private HessianSkeleton _homeSkeleton;
84   private HessianSkeleton _objectSkeleton;
85
86   private SerializerFactory _serializerFactory;
87
88   public String JavaDoc getServletInfo()
89   {
90     return "Hessian Servlet";
91   }
92
93   /**
94    * Sets the home api.
95    */

96   public void setHomeAPI(Class JavaDoc api)
97   {
98     _homeAPI = api;
99   }
100
101   /**
102    * Sets the home implementation
103    */

104   public void setHome(Object JavaDoc home)
105   {
106     _homeImpl = home;
107   }
108
109   /**
110    * Sets the object api.
111    */

112   public void setObjectAPI(Class JavaDoc api)
113   {
114     _objectAPI = api;
115   }
116
117   /**
118    * Sets the object implementation
119    */

120   public void setObject(Object JavaDoc object)
121   {
122     _objectImpl = object;
123   }
124
125   /**
126    * Sets the service class.
127    */

128   public void setService(Object JavaDoc service)
129   {
130     setHome(service);
131   }
132
133   /**
134    * Sets the api-class.
135    */

136   public void setAPIClass(Class JavaDoc api)
137   {
138     setHomeAPI(api);
139   }
140
141   /**
142    * Gets the api-class.
143    */

144   public Class JavaDoc getAPIClass()
145   {
146     return _homeAPI;
147   }
148
149   /**
150    * Sets the serializer factory.
151    */

152   public void setSerializerFactory(SerializerFactory factory)
153   {
154     _serializerFactory = factory;
155   }
156
157   /**
158    * Gets the serializer factory.
159    */

160   public SerializerFactory getSerializerFactory()
161   {
162     if (_serializerFactory == null)
163       _serializerFactory = new SerializerFactory();
164
165     return _serializerFactory;
166   }
167
168   /**
169    * Sets the serializer send collection java type.
170    */

171   public void setSendCollectionType(boolean sendType)
172   {
173     getSerializerFactory().setSendCollectionType(sendType);
174   }
175
176   /**
177    * Initialize the service, including the service object.
178    */

179   public void init(ServletConfig JavaDoc config)
180     throws ServletException JavaDoc
181   {
182     super.init(config);
183     
184     try {
185       if (_homeImpl != null) {
186       }
187       else if (getInitParameter("home-class") != null) {
188     String JavaDoc className = getInitParameter("home-class");
189     
190     Class JavaDoc homeClass = loadClass(className);
191
192     _homeImpl = homeClass.newInstance();
193
194     init(_homeImpl);
195       }
196       else if (getInitParameter("service-class") != null) {
197     String JavaDoc className = getInitParameter("service-class");
198     
199     Class JavaDoc homeClass = loadClass(className);
200
201     _homeImpl = homeClass.newInstance();
202     
203     init(_homeImpl);
204       }
205       else {
206     if (getClass().equals(HessianServlet.class))
207       throw new ServletException JavaDoc("server must extend HessianServlet");
208
209     _homeImpl = this;
210       }
211
212       if (_homeAPI != null) {
213       }
214       else if (getInitParameter("home-api") != null) {
215     String JavaDoc className = getInitParameter("home-api");
216     
217     _homeAPI = loadClass(className);
218       }
219       else if (getInitParameter("api-class") != null) {
220     String JavaDoc className = getInitParameter("api-class");
221
222     _homeAPI = loadClass(className);
223       }
224       else if (_homeImpl != null) {
225     _homeAPI = findRemoteAPI(_homeImpl.getClass());
226
227     if (_homeAPI == null)
228       _homeAPI = _homeImpl.getClass();
229       }
230       
231       if (_objectImpl != null) {
232       }
233       else if (getInitParameter("object-class") != null) {
234     String JavaDoc className = getInitParameter("object-class");
235     
236     Class JavaDoc objectClass = loadClass(className);
237
238     _objectImpl = objectClass.newInstance();
239
240     init(_objectImpl);
241       }
242
243       if (_objectAPI != null) {
244       }
245       else if (getInitParameter("object-api") != null) {
246     String JavaDoc className = getInitParameter("object-api");
247     
248     _objectAPI = loadClass(className);
249       }
250       else if (_objectImpl != null)
251     _objectAPI = _objectImpl.getClass();
252
253       _homeSkeleton = new HessianSkeleton(_homeImpl, _homeAPI);
254       if (_objectAPI != null)
255     _homeSkeleton.setObjectClass(_objectAPI);
256
257       if (_objectImpl != null) {
258     _objectSkeleton = new HessianSkeleton(_objectImpl, _objectAPI);
259     _objectSkeleton.setHomeClass(_homeAPI);
260       }
261       else
262     _objectSkeleton = _homeSkeleton;
263     } catch (ServletException JavaDoc e) {
264       throw e;
265     } catch (Exception JavaDoc e) {
266       throw new ServletException JavaDoc(e);
267     }
268   }
269
270   private Class JavaDoc findRemoteAPI(Class JavaDoc implClass)
271   {
272     if (implClass == null || implClass.equals(GenericService.class))
273       return null;
274     
275     Class JavaDoc []interfaces = implClass.getInterfaces();
276
277     if (interfaces.length == 1)
278       return interfaces[0];
279
280     return findRemoteAPI(implClass.getSuperclass());
281   }
282
283   private Class JavaDoc loadClass(String JavaDoc className)
284     throws ClassNotFoundException JavaDoc
285   {
286     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
287
288     if (loader != null)
289       return Class.forName(className, false, loader);
290     else
291       return Class.forName(className);
292   }
293
294   private void init(Object JavaDoc service)
295     throws ServletException JavaDoc
296   {
297     if (service instanceof Service)
298       ((Service) service).init(getServletConfig());
299     else if (service instanceof Servlet JavaDoc)
300       ((Servlet JavaDoc) service).init(getServletConfig());
301   }
302   
303   /**
304    * Execute a request. The path-info of the request selects the bean.
305    * Once the bean's selected, it will be applied.
306    */

307   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
308     throws IOException JavaDoc, ServletException JavaDoc
309   {
310     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
311     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
312
313     if (! req.getMethod().equals("POST")) {
314       res.setStatus(500, "Hessian Requires POST");
315       PrintWriter JavaDoc out = res.getWriter();
316
317       res.setContentType("text/html");
318       out.println("<h1>Hessian Requires POST</h1>");
319       
320       return;
321     }
322
323     String JavaDoc serviceId = req.getPathInfo();
324     String JavaDoc objectId = req.getParameter("id");
325     if (objectId == null)
326       objectId = req.getParameter("ejbid");
327
328     ServiceContext.begin(req, serviceId, objectId);
329
330     try {
331       InputStream JavaDoc is = request.getInputStream();
332       OutputStream JavaDoc os = response.getOutputStream();
333
334       Hessian2Input in = new Hessian2Input(is);
335       AbstractHessianOutput out;
336
337       SerializerFactory serializerFactory = getSerializerFactory();
338       
339       in.setSerializerFactory(serializerFactory);
340
341       int code = in.read();
342
343       if (code != 'c') {
344     // XXX: deflate
345
throw new IOException JavaDoc("expected 'c' in hessian input at " + code);
346       }
347
348       int major = in.read();
349       int minor = in.read();
350
351       if (major >= 2)
352     out = new Hessian2Output(os);
353       else
354     out = new HessianOutput(os);
355       
356       out.setSerializerFactory(serializerFactory);
357
358       if (objectId != null)
359     _objectSkeleton.invoke(in, out);
360       else
361     _homeSkeleton.invoke(in, out);
362
363       out.close();
364     } catch (RuntimeException JavaDoc e) {
365       throw e;
366     } catch (ServletException JavaDoc e) {
367       throw e;
368     } catch (Throwable JavaDoc e) {
369       throw new ServletException JavaDoc(e);
370     } finally {
371       ServiceContext.end();
372     }
373   }
374 }
375
Popular Tags