1 19 20 package org.apache.cayenne.remote.hessian.service; 21 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletException ; 24 25 import org.apache.cayenne.remote.RemoteService; 26 27 51 public class HessianServlet extends com.caucho.hessian.server.HessianServlet { 52 53 static final String API_CLASS_PARAMETER = "api-class"; 55 static final String SERVICE_CLASS_PARAMETER = "service-class"; 56 57 60 public void init(ServletConfig config) throws ServletException { 61 62 Class apiClass = createAPIClass(config); 63 if (apiClass == null) { 64 throw new ServletException ("Can't configure service API class"); 65 } 66 67 setAPIClass(apiClass); 68 69 HessianService service = createService(config); 70 if (service == null) { 71 throw new ServletException ("Error configuring service "); 72 } 73 74 service.init(config); 75 setSerializerFactory(service.createSerializerFactory()); 76 setService(service); 77 78 super.init(config); 80 } 81 82 protected HessianService createService(ServletConfig config) throws ServletException { 83 84 String className = config.getInitParameter(SERVICE_CLASS_PARAMETER); 85 if (className == null) { 86 return new HessianService(); 87 } 88 89 try { 90 Class serviceClass = Class.forName(className, true, Thread 91 .currentThread() 92 .getContextClassLoader()); 93 94 if (!HessianService.class.isAssignableFrom(serviceClass)) { 95 throw new ServletException ( 96 "Service class must be a subclass of HessianService: " 97 + className); 98 } 99 100 return (HessianService) serviceClass.newInstance(); 101 } 102 catch (ServletException e) { 103 throw e; 104 } 105 catch (Exception e) { 106 throw new ServletException ( 107 "Error instantiating service class " + className, 108 e); 109 } 110 } 111 112 protected Class createAPIClass(ServletConfig config) throws ServletException { 113 String interfaceName = config.getInitParameter(API_CLASS_PARAMETER); 114 if (interfaceName == null) { 115 return RemoteService.class; 116 } 117 try { 118 Class serviceInterface = Class.forName(interfaceName, true, Thread 119 .currentThread() 120 .getContextClassLoader()); 121 122 if (!RemoteService.class.isAssignableFrom(serviceInterface)) { 123 throw new ServletException ( 124 "Service interface must be a subinterface of RemoteService: " 125 + interfaceName); 126 } 127 128 return serviceInterface; 129 } 130 catch (ServletException e) { 131 throw e; 132 } 133 catch (Exception e) { 134 throw new ServletException ("Error instantiating service interface " 135 + interfaceName, e); 136 } 137 } 138 } 139 | Popular Tags |