1 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 ; 61 import javax.servlet.Servlet ; 62 import javax.servlet.ServletConfig ; 63 import javax.servlet.ServletException ; 64 import javax.servlet.ServletRequest ; 65 import javax.servlet.ServletResponse ; 66 import javax.servlet.http.HttpServletRequest ; 67 import javax.servlet.http.HttpServletResponse ; 68 import java.io.IOException ; 69 import java.io.InputStream ; 70 import java.io.OutputStream ; 71 import java.io.PrintWriter ; 72 73 76 public class HessianServlet extends GenericServlet { 77 private Class _homeAPI; 78 private Object _homeImpl; 79 80 private Class _objectAPI; 81 private Object _objectImpl; 82 83 private HessianSkeleton _homeSkeleton; 84 private HessianSkeleton _objectSkeleton; 85 86 private SerializerFactory _serializerFactory; 87 88 public String getServletInfo() 89 { 90 return "Hessian Servlet"; 91 } 92 93 96 public void setHomeAPI(Class api) 97 { 98 _homeAPI = api; 99 } 100 101 104 public void setHome(Object home) 105 { 106 _homeImpl = home; 107 } 108 109 112 public void setObjectAPI(Class api) 113 { 114 _objectAPI = api; 115 } 116 117 120 public void setObject(Object object) 121 { 122 _objectImpl = object; 123 } 124 125 128 public void setService(Object service) 129 { 130 setHome(service); 131 } 132 133 136 public void setAPIClass(Class api) 137 { 138 setHomeAPI(api); 139 } 140 141 144 public Class getAPIClass() 145 { 146 return _homeAPI; 147 } 148 149 152 public void setSerializerFactory(SerializerFactory factory) 153 { 154 _serializerFactory = factory; 155 } 156 157 160 public SerializerFactory getSerializerFactory() 161 { 162 if (_serializerFactory == null) 163 _serializerFactory = new SerializerFactory(); 164 165 return _serializerFactory; 166 } 167 168 171 public void setSendCollectionType(boolean sendType) 172 { 173 getSerializerFactory().setSendCollectionType(sendType); 174 } 175 176 179 public void init(ServletConfig config) 180 throws ServletException 181 { 182 super.init(config); 183 184 try { 185 if (_homeImpl != null) { 186 } 187 else if (getInitParameter("home-class") != null) { 188 String className = getInitParameter("home-class"); 189 190 Class homeClass = loadClass(className); 191 192 _homeImpl = homeClass.newInstance(); 193 194 init(_homeImpl); 195 } 196 else if (getInitParameter("service-class") != null) { 197 String className = getInitParameter("service-class"); 198 199 Class 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 ("server must extend HessianServlet"); 208 209 _homeImpl = this; 210 } 211 212 if (_homeAPI != null) { 213 } 214 else if (getInitParameter("home-api") != null) { 215 String className = getInitParameter("home-api"); 216 217 _homeAPI = loadClass(className); 218 } 219 else if (getInitParameter("api-class") != null) { 220 String 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 className = getInitParameter("object-class"); 235 236 Class 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 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 e) { 264 throw e; 265 } catch (Exception e) { 266 throw new ServletException (e); 267 } 268 } 269 270 private Class findRemoteAPI(Class implClass) 271 { 272 if (implClass == null || implClass.equals(GenericService.class)) 273 return null; 274 275 Class []interfaces = implClass.getInterfaces(); 276 277 if (interfaces.length == 1) 278 return interfaces[0]; 279 280 return findRemoteAPI(implClass.getSuperclass()); 281 } 282 283 private Class loadClass(String className) 284 throws ClassNotFoundException 285 { 286 ClassLoader 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 service) 295 throws ServletException 296 { 297 if (service instanceof Service) 298 ((Service) service).init(getServletConfig()); 299 else if (service instanceof Servlet ) 300 ((Servlet ) service).init(getServletConfig()); 301 } 302 303 307 public void service(ServletRequest request, ServletResponse response) 308 throws IOException , ServletException 309 { 310 HttpServletRequest req = (HttpServletRequest ) request; 311 HttpServletResponse res = (HttpServletResponse ) response; 312 313 if (! req.getMethod().equals("POST")) { 314 res.setStatus(500, "Hessian Requires POST"); 315 PrintWriter out = res.getWriter(); 316 317 res.setContentType("text/html"); 318 out.println("<h1>Hessian Requires POST</h1>"); 319 320 return; 321 } 322 323 String serviceId = req.getPathInfo(); 324 String 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 is = request.getInputStream(); 332 OutputStream 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 throw new IOException ("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 e) { 365 throw e; 366 } catch (ServletException e) { 367 throw e; 368 } catch (Throwable e) { 369 throw new ServletException (e); 370 } finally { 371 ServiceContext.end(); 372 } 373 } 374 } 375 | Popular Tags |