001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.remoting.support;
018
019import org.aopalliance.intercept.MethodInvocation;
020
021/**
022 * Abstract base class for remote service accessors that are based
023 * on serialization of {@link RemoteInvocation} objects.
024 *
025 * Provides a "remoteInvocationFactory" property, with a
026 * {@link DefaultRemoteInvocationFactory} as default strategy.
027 *
028 * @author Juergen Hoeller
029 * @since 1.1
030 * @see #setRemoteInvocationFactory
031 * @see RemoteInvocation
032 * @see RemoteInvocationFactory
033 * @see DefaultRemoteInvocationFactory
034 */
035public abstract class RemoteInvocationBasedAccessor extends UrlBasedRemoteAccessor {
036
037        private RemoteInvocationFactory remoteInvocationFactory = new DefaultRemoteInvocationFactory();
038
039
040        /**
041         * Set the RemoteInvocationFactory to use for this accessor.
042         * Default is a {@link DefaultRemoteInvocationFactory}.
043         * <p>A custom invocation factory can add further context information
044         * to the invocation, for example user credentials.
045         */
046        public void setRemoteInvocationFactory(RemoteInvocationFactory remoteInvocationFactory) {
047                this.remoteInvocationFactory =
048                                (remoteInvocationFactory != null ? remoteInvocationFactory : new DefaultRemoteInvocationFactory());
049        }
050
051        /**
052         * Return the RemoteInvocationFactory used by this accessor.
053         */
054        public RemoteInvocationFactory getRemoteInvocationFactory() {
055                return this.remoteInvocationFactory;
056        }
057
058        /**
059         * Create a new RemoteInvocation object for the given AOP method invocation.
060         * <p>The default implementation delegates to the configured
061         * {@link #setRemoteInvocationFactory RemoteInvocationFactory}.
062         * This can be overridden in subclasses in order to provide custom RemoteInvocation
063         * subclasses, containing additional invocation parameters (e.g. user credentials).
064         * <p>Note that it is preferable to build a custom RemoteInvocationFactory
065         * as a reusable strategy, instead of overriding this method.
066         * @param methodInvocation the current AOP method invocation
067         * @return the RemoteInvocation object
068         * @see RemoteInvocationFactory#createRemoteInvocation
069         */
070        protected RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
071                return getRemoteInvocationFactory().createRemoteInvocation(methodInvocation);
072        }
073
074        /**
075         * Recreate the invocation result contained in the given RemoteInvocationResult object.
076         * <p>The default implementation calls the default {@code recreate()} method.
077         * This can be overridden in subclass to provide custom recreation, potentially
078         * processing the returned result object.
079         * @param result the RemoteInvocationResult to recreate
080         * @return a return value if the invocation result is a successful return
081         * @throws Throwable if the invocation result is an exception
082         * @see RemoteInvocationResult#recreate()
083         */
084        protected Object recreateRemoteInvocationResult(RemoteInvocationResult result) throws Throwable {
085                return result.recreate();
086        }
087
088}