1 16 17 package org.springframework.remoting.caucho; 18 19 import java.io.IOException ; 20 import java.lang.reflect.Constructor ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import com.caucho.hessian.io.SerializerFactory; 27 import com.caucho.hessian.server.HessianSkeleton; 28 29 import org.springframework.beans.factory.BeanInitializationException; 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.remoting.support.RemoteExporter; 32 import org.springframework.util.Assert; 33 import org.springframework.util.ClassUtils; 34 import org.springframework.web.HttpRequestHandler; 35 import org.springframework.web.HttpRequestMethodNotSupportedException; 36 import org.springframework.web.util.NestedServletException; 37 38 62 public class HessianServiceExporter extends RemoteExporter 63 implements HttpRequestHandler, InitializingBean { 64 65 private static final boolean hessian2Available = 66 ClassUtils.isPresent("com.caucho.hessian.io.Hessian2Input", HessianServiceExporter.class.getClassLoader()); 67 68 69 private SerializerFactory serializerFactory = new SerializerFactory(); 70 71 private HessianSkeletonInvoker skeletonInvoker; 72 73 74 80 public void setSerializerFactory(SerializerFactory serializerFactory) { 81 this.serializerFactory = (serializerFactory != null ? serializerFactory : new SerializerFactory()); 82 } 83 84 88 public void setSendCollectionType(boolean sendCollectionType) { 89 this.serializerFactory.setSendCollectionType(sendCollectionType); 90 } 91 92 93 public void afterPropertiesSet() { 94 prepare(); 95 } 96 97 100 public void prepare() { 101 HessianSkeleton skeleton = null; 102 103 try { 104 try { 105 Constructor ctor = HessianSkeleton.class.getConstructor(new Class [] {Object .class, Class .class}); 107 checkService(); 108 checkServiceInterface(); 109 skeleton = (HessianSkeleton) 110 ctor.newInstance(new Object [] {getProxyForService(), getServiceInterface()}); 111 } 112 catch (NoSuchMethodException ex) { 113 Constructor ctor = HessianSkeleton.class.getConstructor(new Class [] {Object .class}); 115 skeleton = (HessianSkeleton) ctor.newInstance(new Object [] {getProxyForService()}); 116 } 117 } 118 catch (Throwable ex) { 119 throw new BeanInitializationException("Hessian skeleton initialization failed", ex); 120 } 121 122 if (hessian2Available) { 123 this.skeletonInvoker = new Hessian2SkeletonInvoker(skeleton, this.serializerFactory); 125 } 126 else { 127 this.skeletonInvoker = new Hessian1SkeletonInvoker(skeleton, this.serializerFactory); 129 } 130 } 131 132 133 136 public void handleRequest(HttpServletRequest request, HttpServletResponse response) 137 throws ServletException , IOException { 138 139 Assert.notNull(this.skeletonInvoker, "HessianServiceExporter has not been initialized"); 140 141 if (!"POST".equals(request.getMethod())) { 142 throw new HttpRequestMethodNotSupportedException( 143 "POST", "HessianServiceExporter only supports POST requests"); 144 } 145 146 try { 147 this.skeletonInvoker.invoke(request.getInputStream(), response.getOutputStream()); 148 } 149 catch (Throwable ex) { 150 throw new NestedServletException("Hessian skeleton invocation failed", ex); 151 } 152 } 153 154 } 155 | Popular Tags |