KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
21 import java.net.ConnectException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23
24 import com.caucho.hessian.client.HessianProxyFactory;
25 import com.caucho.hessian.client.HessianRuntimeException;
26 import com.caucho.hessian.io.SerializerFactory;
27 import org.aopalliance.intercept.MethodInterceptor;
28 import org.aopalliance.intercept.MethodInvocation;
29
30 import org.springframework.remoting.RemoteAccessException;
31 import org.springframework.remoting.RemoteConnectFailureException;
32 import org.springframework.remoting.RemoteLookupFailureException;
33 import org.springframework.remoting.RemoteProxyFailureException;
34 import org.springframework.remoting.support.UrlBasedRemoteAccessor;
35 import org.springframework.util.Assert;
36
37 /**
38  * Interceptor for accessing a Hessian service.
39  * Supports authentication via username and password.
40  * The service URL must be an HTTP URL exposing a Hessian service.
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>Note: There is no requirement for services accessed with this proxy factory
47  * to have been exported using Spring's {@link HessianServiceExporter}, as there is
48  * no special handling involved. As a consequence, you can also access services that
49  * have been exported using Caucho's {@link com.caucho.hessian.server.HessianServlet}.
50  *
51  * @author Juergen Hoeller
52  * @since 29.09.2003
53  * @see #setServiceInterface
54  * @see #setServiceUrl
55  * @see #setUsername
56  * @see #setPassword
57  * @see HessianServiceExporter
58  * @see HessianProxyFactoryBean
59  * @see com.caucho.hessian.client.HessianProxyFactory
60  * @see com.caucho.hessian.server.HessianServlet
61  */

62 public class HessianClientInterceptor extends UrlBasedRemoteAccessor implements MethodInterceptor {
63
64     private HessianProxyFactory proxyFactory = new HessianProxyFactory();
65
66     private Object JavaDoc hessianProxy;
67
68
69     /**
70      * Set the HessianProxyFactory instance to use.
71      * If not specified, a default HessianProxyFactory will be created.
72      * <p>Allows to use an externally configured factory instance,
73      * in particular a custom HessianProxyFactory subclass.
74      */

75     public void setProxyFactory(HessianProxyFactory proxyFactory) {
76         this.proxyFactory = (proxyFactory != null ? proxyFactory : new HessianProxyFactory());
77     }
78
79     /**
80      * Specify the Hessian SerializerFactory to use.
81      * <p>This will typically be passed in as an inner bean definition
82      * of type <code>com.caucho.hessian.io.SerializerFactory</code>,
83      * with custom bean property values applied.
84      */

85     public void setSerializerFactory(SerializerFactory serializerFactory) {
86         this.proxyFactory.setSerializerFactory(serializerFactory);
87     }
88
89     /**
90      * Set whether to send the Java collection type for each serialized
91      * collection. Default is "true".
92      */

93     public void setSendCollectionType(boolean sendCollectionType) {
94         this.proxyFactory.getSerializerFactory().setSendCollectionType(sendCollectionType);
95     }
96     /**
97      * Set the username that this factory should use to access the remote service.
98      * Default is none.
99      * <p>The username will be sent by Hessian via HTTP Basic Authentication.
100      * @see com.caucho.hessian.client.HessianProxyFactory#setUser
101      */

102     public void setUsername(String JavaDoc username) {
103         this.proxyFactory.setUser(username);
104     }
105
106     /**
107      * Set the password that this factory should use to access the remote service.
108      * Default is none.
109      * <p>The password will be sent by Hessian via HTTP Basic Authentication.
110      * @see com.caucho.hessian.client.HessianProxyFactory#setPassword
111      */

112     public void setPassword(String JavaDoc password) {
113         this.proxyFactory.setPassword(password);
114     }
115
116     /**
117      * Set whether overloaded methods should be enabled for remote invocations.
118      * Default is "false".
119      * @see com.caucho.hessian.client.HessianProxyFactory#setOverloadEnabled
120      */

121     public void setOverloadEnabled(boolean overloadEnabled) {
122         this.proxyFactory.setOverloadEnabled(overloadEnabled);
123     }
124
125
126     public void afterPropertiesSet() {
127         super.afterPropertiesSet();
128         prepare();
129     }
130
131     /**
132      * Initialize the Hessian proxy for this interceptor.
133      * @throws RemoteLookupFailureException if the service URL is invalid
134      */

135     public void prepare() throws RemoteLookupFailureException {
136         try {
137             this.hessianProxy = createHessianProxy(this.proxyFactory);
138         }
139         catch (MalformedURLException JavaDoc ex) {
140             throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex);
141         }
142     }
143
144     /**
145      * Create the Hessian proxy that is wrapped by this interceptor.
146      * @param proxyFactory the proxy factory to use
147      * @return the Hessian proxy
148      * @throws MalformedURLException if thrown by the proxy factory
149      * @see com.caucho.hessian.client.HessianProxyFactory#create
150      */

151     protected Object JavaDoc createHessianProxy(HessianProxyFactory proxyFactory) throws MalformedURLException JavaDoc {
152         Assert.notNull(getServiceInterface(), "'serviceInterface' is required");
153         return proxyFactory.create(getServiceInterface(), getServiceUrl());
154     }
155
156
157     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
158         if (this.hessianProxy == null) {
159             throw new IllegalStateException JavaDoc("HessianClientInterceptor is not properly initialized - " +
160                     "invoke 'prepare' before attempting any operations");
161         }
162
163         try {
164             return invocation.getMethod().invoke(this.hessianProxy, invocation.getArguments());
165         }
166         catch (InvocationTargetException JavaDoc ex) {
167             if (ex.getTargetException() instanceof HessianRuntimeException) {
168                 HessianRuntimeException hre = (HessianRuntimeException) ex.getTargetException();
169                 Throwable JavaDoc rootCause = (hre.getRootCause() != null ? hre.getRootCause() : hre);
170                 throw convertHessianAccessException(rootCause);
171             }
172             else if (ex.getTargetException() instanceof UndeclaredThrowableException JavaDoc) {
173                 UndeclaredThrowableException JavaDoc utex = (UndeclaredThrowableException JavaDoc) ex.getTargetException();
174                 throw convertHessianAccessException(utex.getUndeclaredThrowable());
175             }
176             throw ex.getTargetException();
177         }
178         catch (Throwable JavaDoc ex) {
179             throw new RemoteProxyFailureException(
180                     "Failed to invoke Hessian proxy for remote service [" + getServiceUrl() + "]", ex);
181         }
182     }
183
184     /**
185      * Convert the given Hessian access exception to an appropriate
186      * Spring RemoteAccessException.
187      * @param ex the exception to convert
188      * @return the RemoteAccessException to throw
189      */

190     protected RemoteAccessException convertHessianAccessException(Throwable JavaDoc ex) {
191         if (ex instanceof ConnectException JavaDoc) {
192             throw new RemoteConnectFailureException(
193                     "Cannot connect to Hessian remote service at [" + getServiceUrl() + "]", ex);
194         }
195         else {
196             throw new RemoteAccessException(
197                 "Cannot access Hessian remote service at [" + getServiceUrl() + "]", ex);
198         }
199     }
200
201 }
202
Popular Tags