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.RemoteException;
21  
22  import org.springframework.remoting.support.RemoteInvocation;
23  import org.springframework.util.Assert;
24  
25  /**
26   * Server-side implementation of {@link RmiInvocationHandler}. An instance
27   * of this class exists for each remote object. Automatically created
28   * by {@link RmiServiceExporter} for non-RMI service implementations.
29   *
30   * <p>This is an SPI class, not to be used directly by applications.
31   *
32   * @author Juergen Hoeller
33   * @since 14.05.2003
34   * @see RmiServiceExporter
35   */
36  class RmiInvocationWrapper implements RmiInvocationHandler {
37  
38  	private final Object wrappedObject;
39  
40  	private final RmiBasedExporter rmiExporter;
41  
42  
43  	/**
44  	 * Create a new RmiInvocationWrapper for the given object
45  	 * @param wrappedObject the object to wrap with an RmiInvocationHandler
46  	 * @param rmiExporter the RMI exporter to handle the actual invocation
47  	 */
48  	public RmiInvocationWrapper(Object wrappedObject, RmiBasedExporter rmiExporter) {
49  		Assert.notNull(wrappedObject, "Object to wrap is required");
50  		Assert.notNull(rmiExporter, "RMI exporter is required");
51  		this.wrappedObject = wrappedObject;
52  		this.rmiExporter = rmiExporter;
53  	}
54  
55  
56  	/**
57  	 * Exposes the exporter's service interface, if any, as target interface.
58  	 * @see RmiBasedExporter#getServiceInterface()
59  	 */
60  	@Override
61  	public String getTargetInterfaceName() {
62  		Class<?> ifc = this.rmiExporter.getServiceInterface();
63  		return (ifc != null ? ifc.getName() : null);
64  	}
65  
66  	/**
67  	 * Delegates the actual invocation handling to the RMI exporter.
68  	 * @see RmiBasedExporter#invoke(org.springframework.remoting.support.RemoteInvocation, Object)
69  	 */
70  	@Override
71  	public Object invoke(RemoteInvocation invocation)
72  		throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
73  
74  		return this.rmiExporter.invoke(invocation, this.wrappedObject);
75  	}
76  
77  }