KickJava   Java API By Example, From Geeks To Geeks.

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


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.burlap.client.BurlapProxyFactory;
25 import com.caucho.burlap.client.BurlapRuntimeException;
26 import org.aopalliance.intercept.MethodInterceptor;
27 import org.aopalliance.intercept.MethodInvocation;
28
29 import org.springframework.remoting.RemoteAccessException;
30 import org.springframework.remoting.RemoteConnectFailureException;
31 import org.springframework.remoting.RemoteLookupFailureException;
32 import org.springframework.remoting.RemoteProxyFailureException;
33 import org.springframework.remoting.support.UrlBasedRemoteAccessor;
34 import org.springframework.util.Assert;
35
36 /**
37  * Interceptor for accessing a Burlap service.
38  * Supports authentication via username and password.
39  * The service URL must be an HTTP URL exposing a Burlap service.
40  *
41  * <p>Burlap is a slim, XML-based RPC protocol.
42  * For information on Burlap, see the
43  * <a HREF="http://www.caucho.com/burlap">Burlap website</a>
44  *
45  * <p>Note: There is no requirement for services accessed with this proxy factory
46  * to have been exported using Spring's {@link BurlapServiceExporter}, as there is
47  * no special handling involved. As a consequence, you can also access services that
48  * have been exported using Caucho's {@link com.caucho.burlap.server.BurlapServlet}.
49  *
50  * @author Juergen Hoeller
51  * @since 29.09.2003
52  * @see #setServiceInterface
53  * @see #setServiceUrl
54  * @see #setUsername
55  * @see #setPassword
56  * @see BurlapServiceExporter
57  * @see BurlapProxyFactoryBean
58  * @see com.caucho.burlap.client.BurlapProxyFactory
59  * @see com.caucho.burlap.server.BurlapServlet
60  */

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

74     public void setProxyFactory(BurlapProxyFactory proxyFactory) {
75         this.proxyFactory = (proxyFactory != null ? proxyFactory : new BurlapProxyFactory());
76     }
77
78     /**
79      * Set the username that this factory should use to access the remote service.
80      * Default is none.
81      * <p>The username will be sent by Burlap via HTTP Basic Authentication.
82      * @see com.caucho.burlap.client.BurlapProxyFactory#setUser
83      */

84     public void setUsername(String JavaDoc username) {
85         this.proxyFactory.setUser(username);
86     }
87
88     /**
89      * Set the password that this factory should use to access the remote service.
90      * Default is none.
91      * <p>The password will be sent by Burlap via HTTP Basic Authentication.
92      * @see com.caucho.burlap.client.BurlapProxyFactory#setPassword
93      */

94     public void setPassword(String JavaDoc password) {
95         this.proxyFactory.setPassword(password);
96     }
97
98     /**
99      * Set whether overloaded methods should be enabled for remote invocations.
100      * Default is "false".
101      * @see com.caucho.burlap.client.BurlapProxyFactory#setOverloadEnabled
102      */

103     public void setOverloadEnabled(boolean overloadEnabled) {
104         this.proxyFactory.setOverloadEnabled(overloadEnabled);
105     }
106
107
108     public void afterPropertiesSet() {
109         super.afterPropertiesSet();
110         prepare();
111     }
112
113     /**
114      * Initialize the Burlap proxy for this interceptor.
115      * @throws RemoteLookupFailureException if the service URL is invalid
116      */

117     public void prepare() throws RemoteLookupFailureException {
118         try {
119             this.burlapProxy = createBurlapProxy(this.proxyFactory);
120         }
121         catch (MalformedURLException JavaDoc ex) {
122             throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex);
123         }
124     }
125
126     /**
127      * Create the Burlap proxy that is wrapped by this interceptor.
128      * @param proxyFactory the proxy factory to use
129      * @return the Burlap proxy
130      * @throws MalformedURLException if thrown by the proxy factory
131      * @see com.caucho.burlap.client.BurlapProxyFactory#create
132      */

133     protected Object JavaDoc createBurlapProxy(BurlapProxyFactory proxyFactory) throws MalformedURLException JavaDoc {
134         Assert.notNull(getServiceInterface(), "Property 'serviceInterface' is required");
135         return proxyFactory.create(getServiceInterface(), getServiceUrl());
136     }
137
138
139     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
140         if (this.burlapProxy == null) {
141             throw new IllegalStateException JavaDoc("BurlapClientInterceptor is not properly initialized - " +
142                     "invoke 'prepare' before attempting any operations");
143         }
144
145         try {
146             return invocation.getMethod().invoke(this.burlapProxy, invocation.getArguments());
147         }
148         catch (InvocationTargetException JavaDoc ex) {
149             if (ex.getTargetException() instanceof BurlapRuntimeException) {
150                 BurlapRuntimeException bre = (BurlapRuntimeException) ex.getTargetException();
151                 Throwable JavaDoc rootCause = (bre.getRootCause() != null ? bre.getRootCause() : bre);
152                 throw convertBurlapAccessException(rootCause);
153             }
154             else if (ex.getTargetException() instanceof UndeclaredThrowableException JavaDoc) {
155                 UndeclaredThrowableException JavaDoc utex = (UndeclaredThrowableException JavaDoc) ex.getTargetException();
156                 throw convertBurlapAccessException(utex.getUndeclaredThrowable());
157             }
158             throw ex.getTargetException();
159         }
160         catch (Throwable JavaDoc ex) {
161             throw new RemoteProxyFailureException(
162                     "Failed to invoke Burlap proxy for remote service [" + getServiceUrl() + "]", ex);
163         }
164     }
165
166     /**
167      * Convert the given Burlap access exception to an appropriate
168      * Spring RemoteAccessException.
169      * @param ex the exception to convert
170      * @return the RemoteAccessException to throw
171      */

172     protected RemoteAccessException convertBurlapAccessException(Throwable JavaDoc ex) {
173         if (ex instanceof ConnectException JavaDoc) {
174             throw new RemoteConnectFailureException(
175                     "Cannot connect to Burlap remote service at [" + getServiceUrl() + "]", ex);
176         }
177         else {
178             throw new RemoteAccessException(
179                 "Cannot access Burlap remote service at [" + getServiceUrl() + "]", ex);
180         }
181     }
182
183 }
184
Popular Tags