KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > burlap > server > BurlapServlet


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 "Burlap", "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.burlap.server;
50
51 import com.caucho.burlap.io.BurlapInput;
52 import com.caucho.burlap.io.BurlapOutput;
53 import com.caucho.services.server.Service;
54 import com.caucho.services.server.ServiceContext;
55
56 import javax.servlet.GenericServlet JavaDoc;
57 import javax.servlet.Servlet JavaDoc;
58 import javax.servlet.ServletConfig JavaDoc;
59 import javax.servlet.ServletException JavaDoc;
60 import javax.servlet.ServletRequest JavaDoc;
61 import javax.servlet.ServletResponse JavaDoc;
62 import javax.servlet.http.HttpServletRequest JavaDoc;
63 import javax.servlet.http.HttpServletResponse JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.InputStream JavaDoc;
66 import java.io.OutputStream JavaDoc;
67 import java.io.PrintWriter JavaDoc;
68
69 /**
70  * Servlet for serving Burlap services.
71  */

72 public class BurlapServlet extends GenericServlet JavaDoc {
73   private Class JavaDoc _apiClass;
74   private Object JavaDoc _service;
75   
76   private BurlapSkeleton _skeleton;
77
78   public String JavaDoc getServletInfo()
79   {
80     return "Burlap Servlet";
81   }
82
83   /**
84    * Sets the service class.
85    */

86   public void setService(Object JavaDoc service)
87   {
88     _service = service;
89   }
90
91   /**
92    * Sets the api-class.
93    */

94   public void setAPIClass(Class JavaDoc apiClass)
95   {
96     _apiClass = apiClass;
97   }
98
99   /**
100    * Initialize the service, including the service object.
101    */

102   public void init(ServletConfig JavaDoc config)
103     throws ServletException JavaDoc
104   {
105     super.init(config);
106     
107     try {
108       if (_service == null) {
109     String JavaDoc className = getInitParameter("service-class");
110     Class JavaDoc serviceClass = null;
111
112     if (className != null) {
113       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
114
115       if (loader != null)
116         serviceClass = Class.forName(className, false, loader);
117       else
118         serviceClass = Class.forName(className);
119     }
120     else {
121       if (getClass().equals(BurlapServlet.class))
122         throw new ServletException JavaDoc("server must extend BurlapServlet");
123
124       serviceClass = getClass();
125     }
126
127     _service = serviceClass.newInstance();
128
129     if (_service instanceof BurlapServlet)
130       ((BurlapServlet) _service).setService(this);
131     if (_service instanceof Service)
132       ((Service) _service).init(getServletConfig());
133     else if (_service instanceof Servlet JavaDoc)
134       ((Servlet JavaDoc) _service).init(getServletConfig());
135       }
136       
137       if (_apiClass == null) {
138     String JavaDoc className = getInitParameter("api-class");
139
140     if (className != null) {
141       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
142
143       if (loader != null)
144         _apiClass = Class.forName(className, false, loader);
145       else
146         _apiClass = Class.forName(className);
147     }
148     else
149       _apiClass = _service.getClass();
150       }
151
152       _skeleton = new BurlapSkeleton(_service, _apiClass);
153     } catch (ServletException JavaDoc e) {
154       throw e;
155     } catch (Exception JavaDoc e) {
156       throw new ServletException JavaDoc(e);
157     }
158   }
159   
160   /**
161    * Execute a request. The path-info of the request selects the bean.
162    * Once the bean's selected, it will be applied.
163    */

164   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
165     throws IOException JavaDoc, ServletException JavaDoc
166   {
167     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
168     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
169
170     if (! req.getMethod().equals("POST")) {
171       res.setStatus(500, "Burlap Requires POST");
172       PrintWriter JavaDoc out = res.getWriter();
173
174       res.setContentType("text/html");
175       out.println("<h1>Burlap Requires POST</h1>");
176       
177       return;
178     }
179
180     String JavaDoc serviceId = req.getPathInfo();
181     String JavaDoc objectId = req.getParameter("id");
182     if (objectId == null)
183       objectId = req.getParameter("ejbid");
184
185     ServiceContext.begin(req, serviceId, objectId);
186
187     try {
188       InputStream JavaDoc is = request.getInputStream();
189       OutputStream JavaDoc os = response.getOutputStream();
190
191       BurlapInput in = new BurlapInput(is);
192       BurlapOutput out = new BurlapOutput(os);
193
194       _skeleton.invoke(in, out);
195     } catch (RuntimeException JavaDoc e) {
196       throw e;
197     } catch (ServletException JavaDoc e) {
198       throw e;
199     } catch (Throwable JavaDoc e) {
200       throw new ServletException JavaDoc(e);
201     } finally {
202       ServiceContext.end();
203     }
204   }
205 }
206
Popular Tags