View Javadoc
1   /*
2    * Copyright 2002-2007 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.support;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  /**
22   * Abstract base class for remote service exporters that are based
23   * on deserialization of {@link RemoteInvocation} objects.
24   *
25   * <p>Provides a "remoteInvocationExecutor" property, with a
26   * {@link DefaultRemoteInvocationExecutor} as default strategy.
27   *
28   * @author Juergen Hoeller
29   * @since 1.1
30   * @see RemoteInvocationExecutor
31   * @see DefaultRemoteInvocationExecutor
32   */
33  public abstract class RemoteInvocationBasedExporter extends RemoteExporter {
34  
35  	private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor();
36  
37  
38  	/**
39  	 * Set the RemoteInvocationExecutor to use for this exporter.
40  	 * Default is a DefaultRemoteInvocationExecutor.
41  	 * <p>A custom invocation executor can extract further context information
42  	 * from the invocation, for example user credentials.
43  	 */
44  	public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) {
45  		this.remoteInvocationExecutor = remoteInvocationExecutor;
46  	}
47  
48  	/**
49  	 * Return the RemoteInvocationExecutor used by this exporter.
50  	 */
51  	public RemoteInvocationExecutor getRemoteInvocationExecutor() {
52  		return this.remoteInvocationExecutor;
53  	}
54  
55  
56  	/**
57  	 * Apply the given remote invocation to the given target object.
58  	 * The default implementation delegates to the RemoteInvocationExecutor.
59  	 * <p>Can be overridden in subclasses for custom invocation behavior,
60  	 * possibly for applying additional invocation parameters from a
61  	 * custom RemoteInvocation subclass. Note that it is preferable to use
62  	 * a custom RemoteInvocationExecutor which is a reusable strategy.
63  	 * @param invocation the remote invocation
64  	 * @param targetObject the target object to apply the invocation to
65  	 * @return the invocation result
66  	 * @throws NoSuchMethodException if the method name could not be resolved
67  	 * @throws IllegalAccessException if the method could not be accessed
68  	 * @throws InvocationTargetException if the method invocation resulted in an exception
69  	 * @see RemoteInvocationExecutor#invoke
70  	 */
71  	protected Object invoke(RemoteInvocation invocation, Object targetObject)
72  			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
73  
74  		if (logger.isTraceEnabled()) {
75  			logger.trace("Executing " + invocation);
76  		}
77  		try {
78  			return getRemoteInvocationExecutor().invoke(invocation, targetObject);
79  		}
80  		catch (NoSuchMethodException ex) {
81  			if (logger.isDebugEnabled()) {
82  				logger.warn("Could not find target method for " + invocation, ex);
83  			}
84  			throw ex;
85  		}
86  		catch (IllegalAccessException ex) {
87  			if (logger.isDebugEnabled()) {
88  				logger.warn("Could not access target method for " + invocation, ex);
89  			}
90  			throw ex;
91  		}
92  		catch (InvocationTargetException ex) {
93  			if (logger.isDebugEnabled()) {
94  				logger.debug("Target method failed for " + invocation, ex.getTargetException());
95  			}
96  			throw ex;
97  		}
98  	}
99  
100 	/**
101 	 * Apply the given remote invocation to the given target object, wrapping
102 	 * the invocation result in a serializable RemoteInvocationResult object.
103 	 * The default implementation creates a plain RemoteInvocationResult.
104 	 * <p>Can be overridden in subclasses for custom invocation behavior,
105 	 * for example to return additional context information. Note that this
106 	 * is not covered by the RemoteInvocationExecutor strategy!
107 	 * @param invocation the remote invocation
108 	 * @param targetObject the target object to apply the invocation to
109 	 * @return the invocation result
110 	 * @see #invoke
111 	 */
112 	protected RemoteInvocationResult invokeAndCreateResult(RemoteInvocation invocation, Object targetObject) {
113 		try {
114 			Object value = invoke(invocation, targetObject);
115 			return new RemoteInvocationResult(value);
116 		}
117 		catch (Throwable ex) {
118 			return new RemoteInvocationResult(ex);
119 		}
120 	}
121 
122 }