1 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 ; 57 import javax.servlet.Servlet ; 58 import javax.servlet.ServletConfig ; 59 import javax.servlet.ServletException ; 60 import javax.servlet.ServletRequest ; 61 import javax.servlet.ServletResponse ; 62 import javax.servlet.http.HttpServletRequest ; 63 import javax.servlet.http.HttpServletResponse ; 64 import java.io.IOException ; 65 import java.io.InputStream ; 66 import java.io.OutputStream ; 67 import java.io.PrintWriter ; 68 69 72 public class BurlapServlet extends GenericServlet { 73 private Class _apiClass; 74 private Object _service; 75 76 private BurlapSkeleton _skeleton; 77 78 public String getServletInfo() 79 { 80 return "Burlap Servlet"; 81 } 82 83 86 public void setService(Object service) 87 { 88 _service = service; 89 } 90 91 94 public void setAPIClass(Class apiClass) 95 { 96 _apiClass = apiClass; 97 } 98 99 102 public void init(ServletConfig config) 103 throws ServletException 104 { 105 super.init(config); 106 107 try { 108 if (_service == null) { 109 String className = getInitParameter("service-class"); 110 Class serviceClass = null; 111 112 if (className != null) { 113 ClassLoader 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 ("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 ) 134 ((Servlet ) _service).init(getServletConfig()); 135 } 136 137 if (_apiClass == null) { 138 String className = getInitParameter("api-class"); 139 140 if (className != null) { 141 ClassLoader 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 e) { 154 throw e; 155 } catch (Exception e) { 156 throw new ServletException (e); 157 } 158 } 159 160 164 public void service(ServletRequest request, ServletResponse response) 165 throws IOException , ServletException 166 { 167 HttpServletRequest req = (HttpServletRequest ) request; 168 HttpServletResponse res = (HttpServletResponse ) response; 169 170 if (! req.getMethod().equals("POST")) { 171 res.setStatus(500, "Burlap Requires POST"); 172 PrintWriter out = res.getWriter(); 173 174 res.setContentType("text/html"); 175 out.println("<h1>Burlap Requires POST</h1>"); 176 177 return; 178 } 179 180 String serviceId = req.getPathInfo(); 181 String 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 is = request.getInputStream(); 189 OutputStream 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 e) { 196 throw e; 197 } catch (ServletException e) { 198 throw e; 199 } catch (Throwable e) { 200 throw new ServletException (e); 201 } finally { 202 ServiceContext.end(); 203 } 204 } 205 } 206 | Popular Tags |