KickJava   Java API By Example, From Geeks To Geeks.

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


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.burlap.io.BurlapInput;
27 import com.caucho.burlap.io.BurlapOutput;
28 import com.caucho.burlap.server.BurlapSkeleton;
29
30 import org.springframework.beans.factory.BeanInitializationException;
31 import org.springframework.beans.factory.InitializingBean;
32 import org.springframework.remoting.support.RemoteExporter;
33 import org.springframework.util.Assert;
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  * Burlap service endpoint, accessible via a Burlap proxy.
41  *
42  * <p>Burlap is a slim, XML-based RPC protocol.
43  * For information on Burlap, see the
44  * <a HREF="http://www.caucho.com/burlap">Burlap website</a>
45  *
46  * <p>This exporter will work with both Burlap 2.x and 3.x (respectively
47  * Resin 2.x and 3.x), autodetecting the corresponding skeleton class.
48  *
49  * <p>Note: Burlap services exported with this class can be accessed by
50  * any Burlap client, as there isn't any special handling involved.
51  *
52  * @author Juergen Hoeller
53  * @since 13.05.2003
54  * @see BurlapClientInterceptor
55  * @see BurlapProxyFactoryBean
56  * @see org.springframework.remoting.caucho.HessianServiceExporter
57  * @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
58  * @see org.springframework.remoting.rmi.RmiServiceExporter
59  */

60 public class BurlapServiceExporter extends RemoteExporter
61         implements HttpRequestHandler, InitializingBean {
62
63     private BurlapSkeleton skeleton;
64
65
66     public void afterPropertiesSet() {
67         prepare();
68     }
69
70     /**
71      * Initialize this service exporter.
72      */

73     public void prepare() {
74         try {
75             try {
76                 // Try Burlap 3.x (with service interface argument).
77
Constructor JavaDoc ctor = BurlapSkeleton.class.getConstructor(new Class JavaDoc[] {Object JavaDoc.class, Class JavaDoc.class});
78                 checkService();
79                 checkServiceInterface();
80                 this.skeleton = (BurlapSkeleton)
81                         ctor.newInstance(new Object JavaDoc[] {getProxyForService(), getServiceInterface()});
82             }
83             catch (NoSuchMethodException JavaDoc ex) {
84                 // Fall back to Burlap 2.x (without service interface argument).
85
Constructor JavaDoc ctor = BurlapSkeleton.class.getConstructor(new Class JavaDoc[] {Object JavaDoc.class});
86                 this.skeleton = (BurlapSkeleton) ctor.newInstance(new Object JavaDoc[] {getProxyForService()});
87             }
88         }
89         catch (Exception JavaDoc ex) {
90             throw new BeanInitializationException("Burlap skeleton initialization failed", ex);
91         }
92     }
93
94
95     /**
96      * Processes the incoming Burlap request and creates a Burlap response.
97      */

98     public void handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99             throws ServletException JavaDoc, IOException JavaDoc {
100
101         Assert.notNull(this.skeleton, "BurlapServiceExporter has not been initialized");
102
103         if (!"POST".equals(request.getMethod())) {
104             throw new HttpRequestMethodNotSupportedException("POST",
105                     "BurlapServiceExporter only supports POST requests");
106         }
107
108         BurlapInput in = new BurlapInput(request.getInputStream());
109         BurlapOutput out = new BurlapOutput(response.getOutputStream());
110         try {
111           this.skeleton.invoke(in, out);
112         }
113         catch (Throwable JavaDoc ex) {
114           throw new NestedServletException("Burlap skeleton invocation failed", ex);
115         }
116     }
117
118 }
119
Popular Tags