001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.burlap.server;
050:
051: import com.caucho.burlap.io.BurlapInput;
052: import com.caucho.burlap.io.BurlapOutput;
053: import com.caucho.services.server.Service;
054: import com.caucho.services.server.ServiceContext;
055:
056: import javax.servlet.GenericServlet;
057: import javax.servlet.Servlet;
058: import javax.servlet.ServletConfig;
059: import javax.servlet.ServletException;
060: import javax.servlet.ServletRequest;
061: import javax.servlet.ServletResponse;
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpServletResponse;
064: import java.io.IOException;
065: import java.io.InputStream;
066: import java.io.OutputStream;
067: import java.io.PrintWriter;
068:
069: /**
070: * Servlet for serving Burlap services.
071: */
072: public class BurlapServlet extends GenericServlet {
073: private Class _apiClass;
074: private Object _service;
075:
076: private BurlapSkeleton _skeleton;
077:
078: public String getServletInfo() {
079: return "Burlap Servlet";
080: }
081:
082: /**
083: * Sets the service class.
084: */
085: public void setService(Object service) {
086: _service = service;
087: }
088:
089: /**
090: * Sets the api-class.
091: */
092: public void setAPIClass(Class apiClass) {
093: _apiClass = apiClass;
094: }
095:
096: /**
097: * Initialize the service, including the service object.
098: */
099: public void init(ServletConfig config) throws ServletException {
100: super .init(config);
101:
102: try {
103: if (_service == null) {
104: String className = getInitParameter("service-class");
105: Class serviceClass = null;
106:
107: if (className != null) {
108: ClassLoader loader = Thread.currentThread()
109: .getContextClassLoader();
110:
111: if (loader != null)
112: serviceClass = Class.forName(className, false,
113: loader);
114: else
115: serviceClass = Class.forName(className);
116: } else {
117: if (getClass().equals(BurlapServlet.class))
118: throw new ServletException(
119: "server must extend BurlapServlet");
120:
121: serviceClass = getClass();
122: }
123:
124: _service = serviceClass.newInstance();
125:
126: if (_service instanceof BurlapServlet)
127: ((BurlapServlet) _service).setService(this );
128: if (_service instanceof Service)
129: ((Service) _service).init(getServletConfig());
130: else if (_service instanceof Servlet)
131: ((Servlet) _service).init(getServletConfig());
132: }
133:
134: if (_apiClass == null) {
135: String className = getInitParameter("api-class");
136:
137: if (className != null) {
138: ClassLoader loader = Thread.currentThread()
139: .getContextClassLoader();
140:
141: if (loader != null)
142: _apiClass = Class.forName(className, false,
143: loader);
144: else
145: _apiClass = Class.forName(className);
146: } else
147: _apiClass = _service.getClass();
148: }
149:
150: _skeleton = new BurlapSkeleton(_service, _apiClass);
151: } catch (ServletException e) {
152: throw e;
153: } catch (Exception e) {
154: throw new ServletException(e);
155: }
156: }
157:
158: /**
159: * Execute a request. The path-info of the request selects the bean.
160: * Once the bean's selected, it will be applied.
161: */
162: public void service(ServletRequest request, ServletResponse response)
163: throws IOException, ServletException {
164: HttpServletRequest req = (HttpServletRequest) request;
165: HttpServletResponse res = (HttpServletResponse) response;
166:
167: if (!req.getMethod().equals("POST")) {
168: res.setStatus(500, "Burlap Requires POST");
169: PrintWriter out = res.getWriter();
170:
171: res.setContentType("text/html");
172: out.println("<h1>Burlap Requires POST</h1>");
173:
174: return;
175: }
176:
177: String serviceId = req.getPathInfo();
178: String objectId = req.getParameter("id");
179: if (objectId == null)
180: objectId = req.getParameter("ejbid");
181:
182: ServiceContext.begin(req, serviceId, objectId);
183:
184: try {
185: InputStream is = request.getInputStream();
186: OutputStream os = response.getOutputStream();
187:
188: BurlapInput in = new BurlapInput(is);
189: BurlapOutput out = new BurlapOutput(os);
190:
191: _skeleton.invoke(in, out);
192: } catch (RuntimeException e) {
193: throw e;
194: } catch (ServletException e) {
195: throw e;
196: } catch (Throwable e) {
197: throw new ServletException(e);
198: } finally {
199: ServiceContext.end();
200: }
201: }
202: }
|