KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > caucho > HessianServiceExporter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.remoting.caucho;
18
19 import java.io.IOException JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
39  * HTTP request handler that exports the specified service bean as
40  * Hessian service endpoint, accessible via a Hessian proxy.
41  *
42  * <p>Hessian is a slim, binary RPC protocol.
43  * For information on Hessian, see the
44  * <a HREF="http://www.caucho.com/hessian">Hessian website</a>
45  *
46  * <p>This exporter will work with both Hessian 2.x and 3.x (respectively
47  * Resin 2.x and 3.x), autodetecting the corresponding skeleton class.
48  * As of Spring 2.0, it is also compatible with the new Hessian 2 protocol
49  * (a.k.a. Hessian 3.0.20+), while remaining compatible with older versions.
50  *
51  * <p>Note: Hessian services exported with this class can be accessed by
52  * any Hessian client, as there isn't any special handling involved.
53  *
54  * @author Juergen Hoeller
55  * @since 13.05.2003
56  * @see HessianClientInterceptor
57  * @see HessianProxyFactoryBean
58  * @see org.springframework.remoting.caucho.BurlapServiceExporter
59  * @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
60  * @see org.springframework.remoting.rmi.RmiServiceExporter
61  */

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     /**
75      * Specify the Hessian SerializerFactory to use.
76      * <p>This will typically be passed in as an inner bean definition
77      * of type <code>com.caucho.hessian.io.SerializerFactory</code>,
78      * with custom bean property values applied.
79      */

80     public void setSerializerFactory(SerializerFactory serializerFactory) {
81         this.serializerFactory = (serializerFactory != null ? serializerFactory : new SerializerFactory());
82     }
83
84     /**
85      * Set whether to send the Java collection type for each serialized
86      * collection. Default is "true".
87      */

88     public void setSendCollectionType(boolean sendCollectionType) {
89         this.serializerFactory.setSendCollectionType(sendCollectionType);
90     }
91
92
93     public void afterPropertiesSet() {
94         prepare();
95     }
96
97     /**
98      * Initialize this service exporter.
99      */

100     public void prepare() {
101         HessianSkeleton skeleton = null;
102
103         try {
104             try {
105                 // Try Hessian 3.x (with service interface argument).
106
Constructor JavaDoc ctor = HessianSkeleton.class.getConstructor(new Class JavaDoc[] {Object JavaDoc.class, Class JavaDoc.class});
107                 checkService();
108                 checkServiceInterface();
109                 skeleton = (HessianSkeleton)
110                         ctor.newInstance(new Object JavaDoc[] {getProxyForService(), getServiceInterface()});
111             }
112             catch (NoSuchMethodException JavaDoc ex) {
113                 // Fall back to Hessian 2.x (without service interface argument).
114
Constructor JavaDoc ctor = HessianSkeleton.class.getConstructor(new Class JavaDoc[] {Object JavaDoc.class});
115                 skeleton = (HessianSkeleton) ctor.newInstance(new Object JavaDoc[] {getProxyForService()});
116             }
117         }
118         catch (Throwable JavaDoc ex) {
119             throw new BeanInitializationException("Hessian skeleton initialization failed", ex);
120         }
121
122         if (hessian2Available) {
123             // Hessian 2 (version 3.0.20+).
124
this.skeletonInvoker = new Hessian2SkeletonInvoker(skeleton, this.serializerFactory);
125         }
126         else {
127             // Hessian 1 (version 3.0.19-).
128
this.skeletonInvoker = new Hessian1SkeletonInvoker(skeleton, this.serializerFactory);
129         }
130     }
131
132
133     /**
134      * Processes the incoming Hessian request and creates a Hessian response.
135      */

136     public void handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
137             throws ServletException JavaDoc, IOException JavaDoc {
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 JavaDoc ex) {
150           throw new NestedServletException("Hessian skeleton invocation failed", ex);
151         }
152     }
153
154 }
155
Popular Tags