1 16 17 package org.springframework.remoting.caucho; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.UndeclaredThrowableException ; 21 import java.net.ConnectException ; 22 import java.net.MalformedURLException ; 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 61 public class BurlapClientInterceptor extends UrlBasedRemoteAccessor implements MethodInterceptor { 62 63 private BurlapProxyFactory proxyFactory = new BurlapProxyFactory(); 64 65 private Object burlapProxy; 66 67 68 74 public void setProxyFactory(BurlapProxyFactory proxyFactory) { 75 this.proxyFactory = (proxyFactory != null ? proxyFactory : new BurlapProxyFactory()); 76 } 77 78 84 public void setUsername(String username) { 85 this.proxyFactory.setUser(username); 86 } 87 88 94 public void setPassword(String password) { 95 this.proxyFactory.setPassword(password); 96 } 97 98 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 117 public void prepare() throws RemoteLookupFailureException { 118 try { 119 this.burlapProxy = createBurlapProxy(this.proxyFactory); 120 } 121 catch (MalformedURLException ex) { 122 throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex); 123 } 124 } 125 126 133 protected Object createBurlapProxy(BurlapProxyFactory proxyFactory) throws MalformedURLException { 134 Assert.notNull(getServiceInterface(), "Property 'serviceInterface' is required"); 135 return proxyFactory.create(getServiceInterface(), getServiceUrl()); 136 } 137 138 139 public Object invoke(MethodInvocation invocation) throws Throwable { 140 if (this.burlapProxy == null) { 141 throw new IllegalStateException ("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 ex) { 149 if (ex.getTargetException() instanceof BurlapRuntimeException) { 150 BurlapRuntimeException bre = (BurlapRuntimeException) ex.getTargetException(); 151 Throwable rootCause = (bre.getRootCause() != null ? bre.getRootCause() : bre); 152 throw convertBurlapAccessException(rootCause); 153 } 154 else if (ex.getTargetException() instanceof UndeclaredThrowableException ) { 155 UndeclaredThrowableException utex = (UndeclaredThrowableException ) ex.getTargetException(); 156 throw convertBurlapAccessException(utex.getUndeclaredThrowable()); 157 } 158 throw ex.getTargetException(); 159 } 160 catch (Throwable ex) { 161 throw new RemoteProxyFailureException( 162 "Failed to invoke Burlap proxy for remote service [" + getServiceUrl() + "]", ex); 163 } 164 } 165 166 172 protected RemoteAccessException convertBurlapAccessException(Throwable ex) { 173 if (ex instanceof ConnectException ) { 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 |