RmiProxyFactoryBean.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  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.rmi;
  17. import org.springframework.aop.framework.ProxyFactory;
  18. import org.springframework.beans.factory.FactoryBean;
  19. /**
  20.  * Factory bean for RMI proxies, supporting both conventional RMI services and
  21.  * RMI invokers. Behaves like the proxied service when used as bean reference,
  22.  * exposing the specified service interface. Proxies will throw RemoteAccessException
  23.  * on remote invocation failure instead of RMI's RemoteException.
  24.  *
  25.  * <p>The service URL must be a valid RMI URL like "rmi://localhost:1099/myservice".
  26.  * RMI invokers work at the RmiInvocationHandler level, needing only one stub
  27.  * for any service. Service interfaces do not have to extend java.rmi.Remote or
  28.  * throw RemoteException. Of course, in and out parameters have to be serializable.
  29.  *
  30.  * <p>With conventional RMI services, this proxy factory is typically used with the
  31.  * RMI service interface. Alternatively, this factory can also proxy a remote RMI
  32.  * service with a matching non-RMI business interface, i.e. an interface that mirrors
  33.  * the RMI service methods but does not declare RemoteExceptions. In the latter case,
  34.  * RemoteExceptions thrown by the RMI stub will automatically get converted to
  35.  * Spring's unchecked RemoteAccessException.
  36.  *
  37.  * <p>The major advantage of RMI, compared to Hessian and Burlap, is serialization.
  38.  * Effectively, any serializable Java object can be transported without hassle.
  39.  * Hessian and Burlap have their own (de-)serialization mechanisms, but are
  40.  * HTTP-based and thus much easier to setup than RMI. Alternatively, use Spring's
  41.  * HTTP invoker to combine Java serialization with HTTP-based transport.
  42.  *
  43.  * @author Juergen Hoeller
  44.  * @since 13.05.2003
  45.  * @see #setServiceInterface
  46.  * @see #setServiceUrl
  47.  * @see RmiClientInterceptor
  48.  * @see RmiServiceExporter
  49.  * @see java.rmi.Remote
  50.  * @see java.rmi.RemoteException
  51.  * @see org.springframework.remoting.RemoteAccessException
  52.  * @see org.springframework.remoting.caucho.HessianProxyFactoryBean
  53.  * @see org.springframework.remoting.caucho.BurlapProxyFactoryBean
  54.  * @see org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean
  55.  */
  56. public class RmiProxyFactoryBean extends RmiClientInterceptor implements FactoryBean {
  57. private Object serviceProxy;
  58. public void afterPropertiesSet() throws Exception {
  59. super.afterPropertiesSet();
  60. if (getServiceInterface() == null) {
  61. throw new IllegalArgumentException("serviceInterface is required");
  62. }
  63. this.serviceProxy = ProxyFactory.getProxy(getServiceInterface(), this);
  64. }
  65. public Object getObject() {
  66. return this.serviceProxy;
  67. }
  68. public Class getObjectType() {
  69. return (this.serviceProxy != null) ? this.serviceProxy.getClass() : getServiceInterface();
  70. }
  71. public boolean isSingleton() {
  72. return true;
  73. }
  74. }