1. /*
  2. * Copyright 2002-2004 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. package org.springframework.remoting.caucho;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.UndeclaredThrowableException;
  19. import java.net.MalformedURLException;
  20. import com.caucho.burlap.client.BurlapProxyFactory;
  21. import com.caucho.burlap.client.BurlapRuntimeException;
  22. import org.aopalliance.intercept.MethodInterceptor;
  23. import org.aopalliance.intercept.MethodInvocation;
  24. import org.springframework.beans.factory.InitializingBean;
  25. import org.springframework.remoting.RemoteAccessException;
  26. import org.springframework.remoting.support.UrlBasedRemoteAccessor;
  27. /**
  28. * Interceptor for accessing a Burlap service.
  29. * Supports authentication via username and password.
  30. * The service URL must be an HTTP URL exposing a Burlap service.
  31. *
  32. * <p>Burlap is a slim, XML-based RPC protocol.
  33. * For information on Burlap, see the
  34. * <a href="http://www.caucho.com/burlap">Burlap website</a>
  35. *
  36. * <p>Note: Burlap services accessed with this proxy factory do not
  37. * have to be exported via BurlapServiceExporter, as there isn't
  38. * any special handling involved. Therefore, you can also access
  39. * services that are exported via Caucho's BurlapServlet.
  40. *
  41. * @author Juergen Hoeller
  42. * @since 29.09.2003
  43. * @see #setServiceInterface
  44. * @see #setServiceUrl
  45. * @see #setUsername
  46. * @see #setPassword
  47. * @see com.caucho.burlap.client.BurlapProxyFactory
  48. */
  49. public class BurlapClientInterceptor extends UrlBasedRemoteAccessor implements MethodInterceptor, InitializingBean {
  50. private final BurlapProxyFactory proxyFactory = new BurlapProxyFactory();
  51. private Object burlapProxy;
  52. /**
  53. * Set the username that this factory should use to access the remote service.
  54. * Default is none.
  55. * <p>The username will be sent by Burlap via HTTP Basic Authentication.
  56. * @see com.caucho.burlap.client.BurlapProxyFactory#setUser
  57. */
  58. public void setUsername(String username) {
  59. this.proxyFactory.setUser(username);
  60. }
  61. /**
  62. * Set the password that this factory should use to access the remote service.
  63. * Default is none.
  64. * <p>The password will be sent by Burlap via HTTP Basic Authentication.
  65. * @see com.caucho.burlap.client.BurlapProxyFactory#setPassword
  66. */
  67. public void setPassword(String password) {
  68. this.proxyFactory.setPassword(password);
  69. }
  70. /**
  71. * Set whether overloaded methods should be enabled for remote invocations.
  72. * Default is false.
  73. * @see com.caucho.burlap.client.BurlapProxyFactory#setOverloadEnabled
  74. */
  75. public void setOverloadEnabled(boolean overloadEnabled) {
  76. this.proxyFactory.setOverloadEnabled(overloadEnabled);
  77. }
  78. public void afterPropertiesSet() throws MalformedURLException {
  79. if (getServiceInterface() == null) {
  80. throw new IllegalArgumentException("serviceInterface is required");
  81. }
  82. if (getServiceUrl() == null) {
  83. throw new IllegalArgumentException("serviceUrl is required");
  84. }
  85. this.burlapProxy = createBurlapProxy(this.proxyFactory);
  86. }
  87. /**
  88. * Create the Burlap proxy that is wrapped by this interceptor.
  89. * @param proxyFactory the proxy factory to use
  90. * @return the Burlap proxy
  91. * @throws MalformedURLException if thrown by the proxy factory
  92. * @see com.caucho.burlap.client.BurlapProxyFactory#create
  93. */
  94. protected Object createBurlapProxy(BurlapProxyFactory proxyFactory) throws MalformedURLException {
  95. return proxyFactory.create(getServiceInterface(), getServiceUrl());
  96. }
  97. public Object invoke(MethodInvocation invocation) throws Throwable {
  98. try {
  99. return invocation.getMethod().invoke(this.burlapProxy, invocation.getArguments());
  100. }
  101. catch (InvocationTargetException ex) {
  102. logger.debug("Burlap service [" + getServiceUrl() + "] threw exception", ex.getTargetException());
  103. if (ex.getTargetException() instanceof BurlapRuntimeException) {
  104. BurlapRuntimeException bre = (BurlapRuntimeException) ex.getTargetException();
  105. Throwable rootCause = (bre.getRootCause() != null) ? bre.getRootCause() : bre;
  106. throw new RemoteAccessException("Cannot access Burlap service", rootCause);
  107. }
  108. else if (ex.getTargetException() instanceof UndeclaredThrowableException) {
  109. UndeclaredThrowableException utex = (UndeclaredThrowableException) ex.getTargetException();
  110. throw new RemoteAccessException("Cannot access Burlap service", utex.getUndeclaredThrowable());
  111. }
  112. throw ex.getTargetException();
  113. }
  114. }
  115. }