KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > hessian > service > HessianServlet


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote.hessian.service;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24
25 import org.apache.cayenne.remote.RemoteService;
26
27 /**
28  * An extension of the <code>com.caucho.hessian.server.HessianServlet</code> that
29  * installs default Cayenne handlers, simplifying <code>web.xml</code> configuration.
30  * Here is a sample configuration:
31  *
32  * <pre>
33  * &lt;servlet&gt;
34  * &lt;servlet-name&gt;cayenne&lt;/servlet-name&gt;
35  * &lt;servlet-class&gt;org.apache.cayenne.remote.hessian.service.HessianServlet&lt;/servlet-class&gt;
36  * &lt;/servlet&gt;
37  *
38  * &lt;servlet-mapping&gt;
39  * &lt;servlet-name&gt;cayenne&lt;/servlet-name&gt;
40  * &lt;url-pattern&gt;/cayenne&lt;/url-pattern&gt;
41  * &lt;/servlet-mapping&gt;
42  * </pre>
43  *
44  * Custom service class and interface can be specified in a manner compatible with Hessian
45  * recommendations, namely via <em>service-class</em> and <em>api-class</em> servlet
46  * parameters.
47  *
48  * @since 1.2
49  * @author Andrus Adamchik
50  */

51 public class HessianServlet extends com.caucho.hessian.server.HessianServlet {
52
53     // config parameters compatible with Hessian parameter names
54
static final String JavaDoc API_CLASS_PARAMETER = "api-class";
55     static final String JavaDoc SERVICE_CLASS_PARAMETER = "service-class";
56
57     /**
58      * Installs {@link HessianService} to respond to {@link RemoteService} requests.
59      */

60     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
61
62         Class JavaDoc apiClass = createAPIClass(config);
63         if (apiClass == null) {
64             throw new ServletException JavaDoc("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 JavaDoc("Error configuring service ");
72         }
73
74         service.init(config);
75         setSerializerFactory(service.createSerializerFactory());
76         setService(service);
77
78         // proceed to super
79
super.init(config);
80     }
81
82     protected HessianService createService(ServletConfig JavaDoc config) throws ServletException JavaDoc {
83
84         String JavaDoc className = config.getInitParameter(SERVICE_CLASS_PARAMETER);
85         if (className == null) {
86             return new HessianService();
87         }
88
89         try {
90             Class JavaDoc serviceClass = Class.forName(className, true, Thread
91                     .currentThread()
92                     .getContextClassLoader());
93
94             if (!HessianService.class.isAssignableFrom(serviceClass)) {
95                 throw new ServletException JavaDoc(
96                         "Service class must be a subclass of HessianService: "
97                                 + className);
98             }
99
100             return (HessianService) serviceClass.newInstance();
101         }
102         catch (ServletException JavaDoc e) {
103             throw e;
104         }
105         catch (Exception JavaDoc e) {
106             throw new ServletException JavaDoc(
107                     "Error instantiating service class " + className,
108                     e);
109         }
110     }
111
112     protected Class JavaDoc createAPIClass(ServletConfig JavaDoc config) throws ServletException JavaDoc {
113         String JavaDoc interfaceName = config.getInitParameter(API_CLASS_PARAMETER);
114         if (interfaceName == null) {
115             return RemoteService.class;
116         }
117         try {
118             Class JavaDoc serviceInterface = Class.forName(interfaceName, true, Thread
119                     .currentThread()
120                     .getContextClassLoader());
121
122             if (!RemoteService.class.isAssignableFrom(serviceInterface)) {
123                 throw new ServletException JavaDoc(
124                         "Service interface must be a subinterface of RemoteService: "
125                                 + interfaceName);
126             }
127
128             return serviceInterface;
129         }
130         catch (ServletException JavaDoc e) {
131             throw e;
132         }
133         catch (Exception JavaDoc e) {
134             throw new ServletException JavaDoc("Error instantiating service interface "
135                     + interfaceName, e);
136         }
137     }
138 }
139
Popular Tags