View Javadoc
1   /*
2    * Copyright 2002-2012 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.rmi;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.rmi.Remote;
21  
22  import org.springframework.remoting.support.RemoteInvocation;
23  import org.springframework.remoting.support.RemoteInvocationBasedExporter;
24  
25  /**
26   * Convenient superclass for RMI-based remote exporters. Provides a facility
27   * to automatically wrap a given plain Java service object with an
28   * RmiInvocationWrapper, exposing the {@link RmiInvocationHandler} remote interface.
29   *
30   * <p>Using the RMI invoker mechanism, RMI communication operates at the {@link RmiInvocationHandler}
31   * level, sharing a common invoker stub for any number of services. Service interfaces are <i>not</i>
32   * required to extend {@code java.rmi.Remote} or declare {@code java.rmi.RemoteException}
33   * on all service methods. However, in and out parameters still have to be serializable.
34   *
35   * @author Juergen Hoeller
36   * @since 1.2.5
37   * @see RmiServiceExporter
38   * @see JndiRmiServiceExporter
39   */
40  public abstract class RmiBasedExporter extends RemoteInvocationBasedExporter {
41  
42  	/**
43  	 * Determine the object to export: either the service object itself
44  	 * or a RmiInvocationWrapper in case of a non-RMI service object.
45  	 * @return the RMI object to export
46  	 * @see #setService
47  	 * @see #setServiceInterface
48  	 */
49  	protected Remote getObjectToExport() {
50  		// determine remote object
51  		if (getService() instanceof Remote &&
52  				(getServiceInterface() == null || Remote.class.isAssignableFrom(getServiceInterface()))) {
53  			// conventional RMI service
54  			return (Remote) getService();
55  		}
56  		else {
57  			// RMI invoker
58  			if (logger.isDebugEnabled()) {
59  				logger.debug("RMI service [" + getService() + "] is an RMI invoker");
60  			}
61  			return new RmiInvocationWrapper(getProxyForService(), this);
62  		}
63  	}
64  
65  	/**
66  	 * Redefined here to be visible to RmiInvocationWrapper.
67  	 * Simply delegates to the corresponding superclass method.
68  	 */
69  	@Override
70  	protected Object invoke(RemoteInvocation invocation, Object targetObject)
71  			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
72  
73  		return super.invoke(invocation, targetObject);
74  	}
75  
76  }