KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > support > RemoteInvocationBasedExporter


1 /*
2  * Copyright 2002-2005 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 JavaDoc;
20
21 /**
22  * Abstract base class for remote service exporters that are based on
23  * deserialization of RemoteInvocation objects. Provides a "remoteInvocationExecutor"
24  * property, with a DefaultRemoteInvocationExecutor as default.
25  *
26  * @author Juergen Hoeller
27  * @since 1.1
28  * @see RemoteInvocationExecutor
29  * @see DefaultRemoteInvocationExecutor
30  */

31 public abstract class RemoteInvocationBasedExporter extends RemoteExporter {
32
33     private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor();
34
35
36     /**
37      * Set the RemoteInvocationExecutor to use for this exporter.
38      * Default is a DefaultRemoteInvocationExecutor.
39      * <p>A custom invocation executor can extract further context information
40      * from the invocation, for example user credentials.
41      */

42     public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) {
43         this.remoteInvocationExecutor = remoteInvocationExecutor;
44     }
45
46     /**
47      * Return the RemoteInvocationExecutor used by this exporter.
48      */

49     public RemoteInvocationExecutor getRemoteInvocationExecutor() {
50         return remoteInvocationExecutor;
51     }
52
53
54     /**
55      * Apply the given remote invocation to the given target object.
56      * The default implementation delegates to the RemoteInvocationExecutor.
57      * <p>Can be overridden in subclasses for custom invocation behavior,
58      * possibly for applying additional invocation parameters from a
59      * custom RemoteInvocation subclass. Note that it is preferable to use
60      * a custom RemoteInvocationExecutor which is a reusable strategy.
61      * @param invocation the remote invocation
62      * @param targetObject the target object to apply the invocation to
63      * @return the invocation result
64      * @throws NoSuchMethodException if the method name could not be resolved
65      * @throws IllegalAccessException if the method could not be accessed
66      * @throws InvocationTargetException if the method invocation resulted in an exception
67      * @see RemoteInvocationExecutor#invoke
68      */

69     protected Object JavaDoc invoke(RemoteInvocation invocation, Object JavaDoc targetObject)
70             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
71
72         if (logger.isDebugEnabled()) {
73             logger.debug("Applying " + invocation);
74         }
75         try {
76             return getRemoteInvocationExecutor().invoke(invocation, targetObject);
77         }
78         catch (NoSuchMethodException JavaDoc ex) {
79             if (logger.isDebugEnabled()) {
80                 logger.warn("Could not find target method for " + invocation, ex);
81             }
82             throw ex;
83         }
84         catch (IllegalAccessException JavaDoc ex) {
85             if (logger.isDebugEnabled()) {
86                 logger.warn("Could not access target method for " + invocation, ex);
87             }
88             throw ex;
89         }
90         catch (InvocationTargetException JavaDoc ex) {
91             if (logger.isDebugEnabled()) {
92                 logger.debug("Target method failed for " + invocation, ex.getTargetException());
93             }
94             throw ex;
95         }
96     }
97
98     /**
99      * Apply the given remote invocation to the given target object, wrapping
100      * the invocation result in a serializable RemoteInvocationResult object.
101      * The default implementation creates a plain RemoteInvocationResult.
102      * <p>Can be overridden in subclasses for custom invocation behavior,
103      * for example to return additional context information. Note that this
104      * is not covered by the RemoteInvocationExecutor strategy!
105      * @param invocation the remote invocation
106      * @param targetObject the target object to apply the invocation to
107      * @return the invocation result
108      * @see #invoke
109      */

110     protected RemoteInvocationResult invokeAndCreateResult(RemoteInvocation invocation, Object JavaDoc targetObject) {
111         try {
112             Object JavaDoc value = invoke(invocation, targetObject);
113             return new RemoteInvocationResult(value);
114         }
115         catch (Throwable JavaDoc ex) {
116             return new RemoteInvocationResult(ex);
117         }
118     }
119
120 }
121
Popular Tags