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.jaxrpc;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.rmi.Remote;
  20. import java.rmi.RemoteException;
  21. import java.util.Arrays;
  22. import java.util.Iterator;
  23. import java.util.Properties;
  24. import javax.xml.namespace.QName;
  25. import javax.xml.rpc.Service;
  26. import javax.xml.rpc.ServiceException;
  27. import javax.xml.rpc.Stub;
  28. import org.aopalliance.intercept.MethodInterceptor;
  29. import org.aopalliance.intercept.MethodInvocation;
  30. import org.springframework.beans.factory.InitializingBean;
  31. import org.springframework.remoting.RemoteAccessException;
  32. /**
  33. * Interceptor for accessing a specific port of a JAX-RPC service.
  34. * Uses either LocalJaxRpcServiceFactory's facilities underneath,
  35. * or takes an explicit reference to an existing JAX-RPC Service instance,
  36. * for example looked up via JndiObjectFactoryBean.
  37. *
  38. * <p>Allows to set JAX-RPC's standard stub properties directly, via the
  39. * "username", "password", "endpointAddress" and "maintainSession" properties.
  40. *
  41. * <p>This invoker is typically used with an RMI service interface. Alternatively,
  42. * this invoker can also proxy a JAX-RPC service with a matching non-RMI business
  43. * interface, i.e. an interface that mirrors the RMI service methods but does not
  44. * declare RemoteExceptions. In the latter case, RemoteExceptions thrown by the
  45. * JAX-RPC stub will automatically get converted to Spring's unchecked
  46. * RemoteAccessException.
  47. *
  48. * <p>If exposing the JAX-RPC port interface (i.e. an RMI interface) directly,
  49. * setting "serviceInterface" is sufficient. If exposing a non-RMI business
  50. * interface, the business interface needs to be set as "serviceInterface",
  51. * and the JAX-RPC port interface as "portInterface".
  52. *
  53. * @author Juergen Hoeller
  54. * @since 15.12.2003
  55. * @see javax.xml.rpc.Service#getPort
  56. * @see javax.xml.rpc.Stub
  57. * @see org.springframework.remoting.RemoteAccessException
  58. * @see org.springframework.jndi.JndiObjectFactoryBean
  59. */
  60. public class JaxRpcPortClientInterceptor extends LocalJaxRpcServiceFactory
  61. implements MethodInterceptor, InitializingBean {
  62. private Service jaxRpcService;
  63. private String portName;
  64. private String username;
  65. private String password;
  66. private String endpointAddress;
  67. private boolean maintainSession;
  68. private Properties customProperties;
  69. private Class serviceInterface;
  70. private Class portInterface;
  71. private QName portQName;
  72. private Remote portProxy;
  73. /**
  74. * Set a reference to an existing JAX-RPC Service instance,
  75. * for example looked up via JndiObjectFactoryBean.
  76. * If not set, LocalJaxRpcServiceFactory's properties have to be set.
  77. * @see org.springframework.jndi.JndiObjectFactoryBean
  78. */
  79. public void setJaxRpcService(Service jaxRpcService) {
  80. this.jaxRpcService = jaxRpcService;
  81. }
  82. /**
  83. * Return a reference to an existing JAX-RPC Service instance, if any.
  84. */
  85. public Service getJaxRpcService() {
  86. return jaxRpcService;
  87. }
  88. /**
  89. * Set the name of the port.
  90. * Corresponds to the "wsdl:port" name.
  91. */
  92. public void setPortName(String portName) {
  93. this.portName = portName;
  94. }
  95. /**
  96. * Return the name of the port.
  97. */
  98. public String getPortName() {
  99. return portName;
  100. }
  101. public void setUsername(String username) {
  102. this.username = username;
  103. }
  104. public String getUsername() {
  105. return username;
  106. }
  107. public void setPassword(String password) {
  108. this.password = password;
  109. }
  110. public String getPassword() {
  111. return password;
  112. }
  113. public void setEndpointAddress(String endpointAddress) {
  114. this.endpointAddress = endpointAddress;
  115. }
  116. public String getEndpointAddress() {
  117. return endpointAddress;
  118. }
  119. public void setMaintainSession(boolean maintainSession) {
  120. this.maintainSession = maintainSession;
  121. }
  122. public boolean isMaintainSession() {
  123. return maintainSession;
  124. }
  125. /**
  126. * Set custom properties to be set on the stub for the port.
  127. * @see javax.xml.rpc.Stub#_setProperty
  128. */
  129. public void setCustomProperties(Properties customProperties) {
  130. this.customProperties = customProperties;
  131. }
  132. /**
  133. * Return custom properties to be set on the stub for the port.
  134. */
  135. public Properties getCustomProperties() {
  136. return customProperties;
  137. }
  138. /**
  139. * Set the interface of the service that this factory should create a proxy for.
  140. * Can be different from the JAX-RPC port interface, if using a non-RMI business
  141. * interface for exposed proxies.
  142. * <p>The interface must be suitable for a JAX-RPC port, it "portInterface"
  143. * is not set. Else, it must match the methods in the port interface but can
  144. * be a non-RMI business interface.
  145. * @see #setPortInterface
  146. */
  147. public void setServiceInterface(Class serviceInterface) {
  148. if (serviceInterface != null && !serviceInterface.isInterface()) {
  149. throw new IllegalArgumentException("serviceInterface must be an interface");
  150. }
  151. this.serviceInterface = serviceInterface;
  152. }
  153. /**
  154. * Return the interface of the service that this factory should create a proxy for.
  155. */
  156. public Class getServiceInterface() {
  157. return serviceInterface;
  158. }
  159. /**
  160. * Set the JAX-RPC port interface to use. Only needs to be set if the exposed
  161. * service interface is different from the port interface, i.e. when using
  162. * a non-RMI business interface as service interface for exposed proxies.
  163. * <p>The interface must be suitable for a JAX-RPC port.
  164. * @see #setServiceInterface
  165. */
  166. public void setPortInterface(Class portInterface) {
  167. if (portInterface != null &&
  168. (!portInterface.isInterface() || !Remote.class.isAssignableFrom(portInterface))) {
  169. throw new IllegalArgumentException("portInterface must be an interface derived from java.rmi.Remote");
  170. }
  171. this.portInterface = portInterface;
  172. }
  173. /**
  174. * Return the JAX-RPC port interface to use.
  175. */
  176. public Class getPortInterface() {
  177. return portInterface;
  178. }
  179. /**
  180. * Create and initialize the JAX-RPC proxy for the specified port.
  181. */
  182. public void afterPropertiesSet() throws ServiceException {
  183. if (this.portName == null) {
  184. throw new IllegalArgumentException("portName is required");
  185. }
  186. if (this.jaxRpcService == null) {
  187. this.jaxRpcService = createJaxRpcService();
  188. postProcessJaxRpcService(this.jaxRpcService);
  189. }
  190. this.portQName = getQName(this.portName);
  191. Class actualInterface = (this.portInterface != null ? this.portInterface : getServiceInterface());
  192. Remote remoteObj = this.jaxRpcService.getPort(this.portQName, actualInterface);
  193. if (getServiceInterface() != null) {
  194. boolean isImpl = getServiceInterface().isInstance(remoteObj);
  195. logger.info("Using service interface [" + getServiceInterface().getName() + "] for JAX-RPC object [" +
  196. this.portQName + "] - " + (!isImpl ? "not" : "") + " directly implemented");
  197. }
  198. // apply properties to stub
  199. Stub stub = (Stub) remoteObj;
  200. if (this.username != null) {
  201. stub._setProperty(Stub.USERNAME_PROPERTY, this.username);
  202. }
  203. if (this.password != null) {
  204. stub._setProperty(Stub.PASSWORD_PROPERTY, this.password);
  205. }
  206. if (this.endpointAddress != null) {
  207. stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, this.endpointAddress);
  208. }
  209. if (this.maintainSession) {
  210. stub._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(this.maintainSession));
  211. }
  212. if (this.customProperties != null) {
  213. for (Iterator it = this.customProperties.keySet().iterator(); it.hasNext();) {
  214. String key = (String) it.next();
  215. stub._setProperty(key, this.customProperties.getProperty(key));
  216. }
  217. }
  218. this.portProxy = remoteObj;
  219. postProcessPortProxy(this.portProxy);
  220. }
  221. /**
  222. * Post-process the given JAX-RPC Service. Called by afterPropertiesSet.
  223. * Useful for example to register custom type mappings.
  224. * @param service the current JAX-RPC Service
  225. * @see javax.xml.rpc.Service#getTypeMappingRegistry
  226. */
  227. protected void postProcessJaxRpcService(Service service) {
  228. }
  229. /**
  230. * Post-process the given JAX-RPC port proxy. Called by afterPropertiesSet.
  231. * @param portProxy the current JAX-RPC port proxy
  232. */
  233. protected void postProcessPortProxy(Remote portProxy) {
  234. }
  235. /**
  236. * Return the underlying JAX-RPC port proxy that this interceptor delegates to.
  237. */
  238. public Remote getPortProxy() {
  239. return portProxy;
  240. }
  241. public Object invoke(MethodInvocation invocation) throws Throwable {
  242. try {
  243. Method method = invocation.getMethod();
  244. if (method.getDeclaringClass().isInstance(this.portProxy)) {
  245. // directly implemented
  246. return method.invoke(this.portProxy, invocation.getArguments());
  247. }
  248. else {
  249. // not directly implemented
  250. Method proxyMethod = this.portProxy.getClass().getMethod(method.getName(), method.getParameterTypes());
  251. return proxyMethod.invoke(this.portProxy, invocation.getArguments());
  252. }
  253. }
  254. catch (InvocationTargetException ex) {
  255. Throwable targetException = ex.getTargetException();
  256. logger.debug("JAX-RPC method of service [" + this.portQName + "] threw exception", targetException);
  257. if (targetException instanceof RemoteException &&
  258. !Arrays.asList(invocation.getMethod().getExceptionTypes()).contains(RemoteException.class)) {
  259. throw new RemoteAccessException("Cannot access JAX-RPC service [" + this.portQName + "]", targetException);
  260. }
  261. else {
  262. throw targetException;
  263. }
  264. }
  265. catch (RuntimeException ex) {
  266. throw new RemoteAccessException("Failed to invoke JAX-RPC service [" + this.portQName + "]", ex);
  267. }
  268. }
  269. }