Drop RPC-style remoting
Closes gh-27422
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* Generic remote access exception. A service proxy for any remoting
|
||||
* protocol should throw this exception or subclasses of it, in order
|
||||
* to transparently expose a plain Java business interface.
|
||||
*
|
||||
* <p>When using conforming proxies, switching the actual remoting protocol
|
||||
* e.g. from Hessian does not affect client code. Clients work with a plain
|
||||
* natural Java business interface that the service exposes. A client object
|
||||
* simply receives an implementation for the interface that it needs via a
|
||||
* bean reference, like it does for a local bean as well.
|
||||
*
|
||||
* <p>A client may catch RemoteAccessException if it wants to, but as
|
||||
* remote access errors are typically unrecoverable, it will probably let
|
||||
* such exceptions propagate to a higher level that handles them generically.
|
||||
* In this case, the client code doesn't show any signs of being involved in
|
||||
* remote access, as there aren't any remoting-specific dependencies.
|
||||
*
|
||||
* <p>Even when switching from a remote service proxy to a local implementation
|
||||
* of the same interface, this amounts to just a matter of configuration. Obviously,
|
||||
* the client code should be somewhat aware that it <i>might be working</i>
|
||||
* against a remote service, for example in terms of repeated method calls that
|
||||
* cause unnecessary roundtrips etc. However, it doesn't have to be aware whether
|
||||
* it is <i>actually working</i> against a remote service or a local implementation,
|
||||
* or with which remoting protocol it is working under the hood.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 14.05.2003
|
||||
*/
|
||||
public class RemoteAccessException extends NestedRuntimeException {
|
||||
|
||||
/** Use serialVersionUID from Spring 1.2 for interoperability. */
|
||||
private static final long serialVersionUID = -4906825139312227864L;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for RemoteAccessException.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public RemoteAccessException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for RemoteAccessException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause (usually from using an underlying
|
||||
* remoting API such as RMI)
|
||||
*/
|
||||
public RemoteAccessException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
/**
|
||||
* RemoteAccessException subclass to be thrown when no connection
|
||||
* could be established with a remote service.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class RemoteConnectFailureException extends RemoteAccessException {
|
||||
|
||||
/**
|
||||
* Constructor for RemoteConnectFailureException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause from the remoting API in use
|
||||
*/
|
||||
public RemoteConnectFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
/**
|
||||
* RemoteAccessException subclass to be thrown when the execution
|
||||
* of the target method failed on the server side, for example
|
||||
* when a method was not found on the target object.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
* @see RemoteProxyFailureException
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class RemoteInvocationFailureException extends RemoteAccessException {
|
||||
|
||||
/**
|
||||
* Constructor for RemoteInvocationFailureException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause from the remoting API in use
|
||||
*/
|
||||
public RemoteInvocationFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
/**
|
||||
* RemoteAccessException subclass to be thrown in case of a lookup failure,
|
||||
* typically if the lookup happens on demand for each method invocation.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class RemoteLookupFailureException extends RemoteAccessException {
|
||||
|
||||
/**
|
||||
* Constructor for RemoteLookupFailureException.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public RemoteLookupFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for RemoteLookupFailureException.
|
||||
* @param msg message
|
||||
* @param cause the root cause from the remoting API in use
|
||||
*/
|
||||
public RemoteLookupFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
/**
|
||||
* RemoteAccessException subclass to be thrown in case of a failure
|
||||
* within the client-side proxy for a remote service, for example
|
||||
* when a method was not found on the underlying RMI stub.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.8
|
||||
* @see RemoteInvocationFailureException
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class RemoteProxyFailureException extends RemoteAccessException {
|
||||
|
||||
/**
|
||||
* Constructor for RemoteProxyFailureException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause from the remoting API in use
|
||||
*/
|
||||
public RemoteProxyFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting;
|
||||
|
||||
/**
|
||||
* RemoteAccessException subclass to be thrown when the execution
|
||||
* of the target method did not complete before a configurable
|
||||
* timeout, for example when a reply message was not received.
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.2
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class RemoteTimeoutException extends RemoteAccessException {
|
||||
|
||||
/**
|
||||
* Constructor for RemoteTimeoutException.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public RemoteTimeoutException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for RemoteTimeoutException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause from the remoting API in use
|
||||
*/
|
||||
public RemoteTimeoutException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
/**
|
||||
* Exception hierarchy for Spring's remoting infrastructure,
|
||||
* independent of any specific remote method invocation system.
|
||||
*/
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.remoting;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.rmi.server.RMIClassLoader;
|
||||
|
||||
import org.springframework.core.ConfigurableObjectInputStream;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Special ObjectInputStream subclass that falls back to a specified codebase
|
||||
* to load classes from if not found locally. In contrast to standard RMI
|
||||
* conventions for dynamic class download, it is the client that determines
|
||||
* the codebase URL here, rather than the "java.rmi.server.codebase" system
|
||||
* property on the server.
|
||||
*
|
||||
* <p>Uses the JDK's RMIClassLoader to load classes from the specified codebase.
|
||||
* The codebase can consist of multiple URLs, separated by spaces.
|
||||
* Note that RMIClassLoader requires a SecurityManager to be set, like when
|
||||
* using dynamic class download with standard RMI! (See the RMI documentation
|
||||
* for details.)
|
||||
*
|
||||
* <p>Despite residing in the RMI package, this class is <i>not</i> used for
|
||||
* RmiClientInterceptor, which uses the standard RMI infrastructure instead
|
||||
* and thus is only able to rely on RMI's standard dynamic class download via
|
||||
* "java.rmi.server.codebase". CodebaseAwareObjectInputStream is used by
|
||||
* HttpInvokerClientInterceptor (see the "codebaseUrl" property there).
|
||||
*
|
||||
* <p>Thanks to Lionel Mestre for suggesting the option and providing
|
||||
* a prototype!
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1.3
|
||||
* @see java.rmi.server.RMIClassLoader
|
||||
* @see RemoteInvocationSerializingExporter#createObjectInputStream
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor#setCodebaseUrl
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class CodebaseAwareObjectInputStream extends ConfigurableObjectInputStream {
|
||||
|
||||
private final String codebaseUrl;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
|
||||
* @param in the InputStream to read from
|
||||
* @param codebaseUrl the codebase URL to load classes from if not found locally
|
||||
* (can consist of multiple URLs, separated by spaces)
|
||||
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
|
||||
*/
|
||||
public CodebaseAwareObjectInputStream(InputStream in, String codebaseUrl) throws IOException {
|
||||
this(in, null, codebaseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
|
||||
* @param in the InputStream to read from
|
||||
* @param classLoader the ClassLoader to use for loading local classes
|
||||
* (may be {@code null} to indicate RMI's default ClassLoader)
|
||||
* @param codebaseUrl the codebase URL to load classes from if not found locally
|
||||
* (can consist of multiple URLs, separated by spaces)
|
||||
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
|
||||
*/
|
||||
public CodebaseAwareObjectInputStream(
|
||||
InputStream in, @Nullable ClassLoader classLoader, String codebaseUrl) throws IOException {
|
||||
|
||||
super(in, classLoader);
|
||||
this.codebaseUrl = codebaseUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
|
||||
* @param in the InputStream to read from
|
||||
* @param classLoader the ClassLoader to use for loading local classes
|
||||
* (may be {@code null} to indicate RMI's default ClassLoader)
|
||||
* @param acceptProxyClasses whether to accept deserialization of proxy classes
|
||||
* (may be deactivated as a security measure)
|
||||
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
|
||||
*/
|
||||
public CodebaseAwareObjectInputStream(
|
||||
InputStream in, @Nullable ClassLoader classLoader, boolean acceptProxyClasses) throws IOException {
|
||||
|
||||
super(in, classLoader, acceptProxyClasses);
|
||||
this.codebaseUrl = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> resolveFallbackIfPossible(String className, ClassNotFoundException ex)
|
||||
throws IOException, ClassNotFoundException {
|
||||
|
||||
// If codebaseUrl is set, try to load the class with the RMIClassLoader.
|
||||
// Else, propagate the ClassNotFoundException.
|
||||
if (this.codebaseUrl == null) {
|
||||
throw ex;
|
||||
}
|
||||
return RMIClassLoader.loadClass(this.codebaseUrl, className);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClassLoader getFallbackClassLoader() throws IOException {
|
||||
return RMIClassLoader.getClassLoader(this.codebaseUrl);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,452 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jndi.JndiObjectLocator;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.remoting.RemoteConnectFailureException;
|
||||
import org.springframework.remoting.RemoteInvocationFailureException;
|
||||
import org.springframework.remoting.RemoteLookupFailureException;
|
||||
import org.springframework.remoting.support.DefaultRemoteInvocationFactory;
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
import org.springframework.remoting.support.RemoteInvocationFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing RMI services
|
||||
* from JNDI. Typically used for RMI-IIOP but can also be used for EJB home objects
|
||||
* (for example, a Stateful Session Bean home). In contrast to a plain JNDI lookup,
|
||||
* this accessor also performs narrowing through PortableRemoteObject.
|
||||
*
|
||||
* <p>With conventional RMI services, this invoker is typically used with the RMI
|
||||
* service interface. Alternatively, this invoker can also proxy a remote RMI service
|
||||
* with a matching non-RMI business interface, i.e. an interface that mirrors the RMI
|
||||
* service methods but does not declare RemoteExceptions. In the latter case,
|
||||
* RemoteExceptions thrown by the RMI stub will automatically get converted to
|
||||
* Spring's unchecked RemoteAccessException.
|
||||
*
|
||||
* <p>The JNDI environment can be specified as "jndiEnvironment" property,
|
||||
* or be configured in a {@code jndi.properties} file or as system properties.
|
||||
* For example:
|
||||
*
|
||||
* <pre class="code"><property name="jndiEnvironment">
|
||||
* <props>
|
||||
* <prop key="java.naming.factory.initial">com.sun.jndi.cosnaming.CNCtxFactory</prop>
|
||||
* <prop key="java.naming.provider.url">iiop://localhost:1050</prop>
|
||||
* </props>
|
||||
* </property></pre>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see #setJndiTemplate
|
||||
* @see #setJndiEnvironment
|
||||
* @see #setJndiName
|
||||
* @see JndiRmiServiceExporter
|
||||
* @see JndiRmiProxyFactoryBean
|
||||
* @see org.springframework.remoting.RemoteAccessException
|
||||
* @see java.rmi.RemoteException
|
||||
* @see java.rmi.Remote
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class JndiRmiClientInterceptor extends JndiObjectLocator implements MethodInterceptor, InitializingBean {
|
||||
|
||||
private Class<?> serviceInterface;
|
||||
|
||||
private RemoteInvocationFactory remoteInvocationFactory = new DefaultRemoteInvocationFactory();
|
||||
|
||||
private boolean lookupStubOnStartup = true;
|
||||
|
||||
private boolean cacheStub = true;
|
||||
|
||||
private boolean refreshStubOnConnectFailure = false;
|
||||
|
||||
private boolean exposeAccessContext = false;
|
||||
|
||||
private Object cachedStub;
|
||||
|
||||
private final Object stubMonitor = new Object();
|
||||
|
||||
|
||||
/**
|
||||
* Set the interface of the service to access.
|
||||
* The interface must be suitable for the particular service and remoting tool.
|
||||
* <p>Typically required to be able to create a suitable service proxy,
|
||||
* but can also be optional if the lookup returns a typed stub.
|
||||
*/
|
||||
public void setServiceInterface(Class<?> serviceInterface) {
|
||||
Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
|
||||
Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
|
||||
this.serviceInterface = serviceInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the interface of the service to access.
|
||||
*/
|
||||
public Class<?> getServiceInterface() {
|
||||
return this.serviceInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the RemoteInvocationFactory to use for this accessor.
|
||||
* Default is a {@link DefaultRemoteInvocationFactory}.
|
||||
* <p>A custom invocation factory can add further context information
|
||||
* to the invocation, for example user credentials.
|
||||
*/
|
||||
public void setRemoteInvocationFactory(RemoteInvocationFactory remoteInvocationFactory) {
|
||||
this.remoteInvocationFactory = remoteInvocationFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RemoteInvocationFactory used by this accessor.
|
||||
*/
|
||||
public RemoteInvocationFactory getRemoteInvocationFactory() {
|
||||
return this.remoteInvocationFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to look up the RMI stub on startup. Default is "true".
|
||||
* <p>Can be turned off to allow for late start of the RMI server.
|
||||
* In this case, the RMI stub will be fetched on first access.
|
||||
* @see #setCacheStub
|
||||
*/
|
||||
public void setLookupStubOnStartup(boolean lookupStubOnStartup) {
|
||||
this.lookupStubOnStartup = lookupStubOnStartup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to cache the RMI stub once it has been located.
|
||||
* Default is "true".
|
||||
* <p>Can be turned off to allow for hot restart of the RMI server.
|
||||
* In this case, the RMI stub will be fetched for each invocation.
|
||||
* @see #setLookupStubOnStartup
|
||||
*/
|
||||
public void setCacheStub(boolean cacheStub) {
|
||||
this.cacheStub = cacheStub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to refresh the RMI stub on connect failure.
|
||||
* Default is "false".
|
||||
* <p>Can be turned on to allow for hot restart of the RMI server.
|
||||
* If a cached RMI stub throws an RMI exception that indicates a
|
||||
* remote connect failure, a fresh proxy will be fetched and the
|
||||
* invocation will be retried.
|
||||
* @see java.rmi.ConnectException
|
||||
* @see java.rmi.ConnectIOException
|
||||
* @see java.rmi.NoSuchObjectException
|
||||
*/
|
||||
public void setRefreshStubOnConnectFailure(boolean refreshStubOnConnectFailure) {
|
||||
this.refreshStubOnConnectFailure = refreshStubOnConnectFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to expose the JNDI environment context for all access to the target
|
||||
* RMI stub, i.e. for all method invocations on the exposed object reference.
|
||||
* <p>Default is "false", i.e. to only expose the JNDI context for object lookup.
|
||||
* Switch this flag to "true" in order to expose the JNDI environment (including
|
||||
* the authorization context) for each RMI invocation, as needed by WebLogic
|
||||
* for RMI stubs with authorization requirements.
|
||||
*/
|
||||
public void setExposeAccessContext(boolean exposeAccessContext) {
|
||||
this.exposeAccessContext = exposeAccessContext;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws NamingException {
|
||||
super.afterPropertiesSet();
|
||||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the RMI stub on startup, if necessary.
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
* @see #setLookupStubOnStartup
|
||||
* @see #lookupStub
|
||||
*/
|
||||
public void prepare() throws RemoteLookupFailureException {
|
||||
// Cache RMI stub on initialization?
|
||||
if (this.lookupStubOnStartup) {
|
||||
Object remoteObj = lookupStub();
|
||||
if (logger.isDebugEnabled()) {
|
||||
if (remoteObj instanceof RmiInvocationHandler) {
|
||||
logger.debug("JNDI RMI object [" + getJndiName() + "] is an RMI invoker");
|
||||
}
|
||||
else if (getServiceInterface() != null) {
|
||||
boolean isImpl = getServiceInterface().isInstance(remoteObj);
|
||||
logger.debug("Using service interface [" + getServiceInterface().getName() +
|
||||
"] for JNDI RMI object [" + getJndiName() + "] - " +
|
||||
(!isImpl ? "not " : "") + "directly implemented");
|
||||
}
|
||||
}
|
||||
if (this.cacheStub) {
|
||||
this.cachedStub = remoteObj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the RMI stub, typically by looking it up.
|
||||
* <p>Called on interceptor initialization if "cacheStub" is "true";
|
||||
* else called for each invocation by {@link #getStub()}.
|
||||
* <p>The default implementation retrieves the service from the
|
||||
* JNDI environment. This can be overridden in subclasses.
|
||||
* @return the RMI stub to store in this interceptor
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
* @see #setCacheStub
|
||||
* @see #lookup
|
||||
*/
|
||||
protected Object lookupStub() throws RemoteLookupFailureException {
|
||||
try {
|
||||
return lookup();
|
||||
}
|
||||
catch (NamingException ex) {
|
||||
throw new RemoteLookupFailureException("JNDI lookup for RMI service [" + getJndiName() + "] failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RMI stub to use. Called for each invocation.
|
||||
* <p>The default implementation returns the stub created on initialization,
|
||||
* if any. Else, it invokes {@link #lookupStub} to get a new stub for
|
||||
* each invocation. This can be overridden in subclasses, for example in
|
||||
* order to cache a stub for a given amount of time before recreating it,
|
||||
* or to test the stub whether it is still alive.
|
||||
* @return the RMI stub to use for an invocation
|
||||
* @throws NamingException if stub creation failed
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
*/
|
||||
protected Object getStub() throws NamingException, RemoteLookupFailureException {
|
||||
if (!this.cacheStub || (this.lookupStubOnStartup && !this.refreshStubOnConnectFailure)) {
|
||||
return (this.cachedStub != null ? this.cachedStub : lookupStub());
|
||||
}
|
||||
else {
|
||||
synchronized (this.stubMonitor) {
|
||||
if (this.cachedStub == null) {
|
||||
this.cachedStub = lookupStub();
|
||||
}
|
||||
return this.cachedStub;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches an RMI stub and delegates to {@link #doInvoke}.
|
||||
* If configured to refresh on connect failure, it will call
|
||||
* {@link #refreshAndRetry} on corresponding RMI exceptions.
|
||||
* @see #getStub
|
||||
* @see #doInvoke
|
||||
* @see #refreshAndRetry
|
||||
* @see java.rmi.ConnectException
|
||||
* @see java.rmi.ConnectIOException
|
||||
* @see java.rmi.NoSuchObjectException
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Object stub;
|
||||
try {
|
||||
stub = getStub();
|
||||
}
|
||||
catch (NamingException ex) {
|
||||
throw new RemoteLookupFailureException("JNDI lookup for RMI service [" + getJndiName() + "] failed", ex);
|
||||
}
|
||||
|
||||
Context ctx = (this.exposeAccessContext ? getJndiTemplate().getContext() : null);
|
||||
try {
|
||||
return doInvoke(invocation, stub);
|
||||
}
|
||||
catch (RemoteConnectFailureException ex) {
|
||||
return handleRemoteConnectFailure(invocation, ex);
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
if (isConnectFailure(ex)) {
|
||||
return handleRemoteConnectFailure(invocation, ex);
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
getJndiTemplate().releaseContext(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given RMI exception indicates a connect failure.
|
||||
* <p>The default implementation delegates to
|
||||
* {@link RmiClientInterceptorUtils#isConnectFailure}.
|
||||
* @param ex the RMI exception to check
|
||||
* @return whether the exception should be treated as connect failure
|
||||
*/
|
||||
protected boolean isConnectFailure(RemoteException ex) {
|
||||
return RmiClientInterceptorUtils.isConnectFailure(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the stub and retry the remote invocation if necessary.
|
||||
* <p>If not configured to refresh on connect failure, this method
|
||||
* simply rethrows the original exception.
|
||||
* @param invocation the invocation that failed
|
||||
* @param ex the exception raised on remote invocation
|
||||
* @return the result value of the new invocation, if succeeded
|
||||
* @throws Throwable an exception raised by the new invocation, if failed too.
|
||||
*/
|
||||
private Object handleRemoteConnectFailure(MethodInvocation invocation, Exception ex) throws Throwable {
|
||||
if (this.refreshStubOnConnectFailure) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not connect to RMI service [" + getJndiName() + "] - retrying", ex);
|
||||
}
|
||||
else if (logger.isInfoEnabled()) {
|
||||
logger.info("Could not connect to RMI service [" + getJndiName() + "] - retrying");
|
||||
}
|
||||
return refreshAndRetry(invocation);
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the RMI stub and retry the given invocation.
|
||||
* Called by invoke on connect failure.
|
||||
* @param invocation the AOP method invocation
|
||||
* @return the invocation result, if any
|
||||
* @throws Throwable in case of invocation failure
|
||||
* @see #invoke
|
||||
*/
|
||||
@Nullable
|
||||
protected Object refreshAndRetry(MethodInvocation invocation) throws Throwable {
|
||||
Object freshStub;
|
||||
synchronized (this.stubMonitor) {
|
||||
this.cachedStub = null;
|
||||
freshStub = lookupStub();
|
||||
if (this.cacheStub) {
|
||||
this.cachedStub = freshStub;
|
||||
}
|
||||
}
|
||||
return doInvoke(invocation, freshStub);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform the given invocation on the given RMI stub.
|
||||
* @param invocation the AOP method invocation
|
||||
* @param stub the RMI stub to invoke
|
||||
* @return the invocation result, if any
|
||||
* @throws Throwable in case of invocation failure
|
||||
*/
|
||||
@Nullable
|
||||
protected Object doInvoke(MethodInvocation invocation, Object stub) throws Throwable {
|
||||
if (stub instanceof RmiInvocationHandler) {
|
||||
// RMI invoker
|
||||
try {
|
||||
return doInvoke(invocation, (RmiInvocationHandler) stub);
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
throw convertRmiAccessException(ex, invocation.getMethod());
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex.getTargetException();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new RemoteInvocationFailureException("Invocation of method [" + invocation.getMethod() +
|
||||
"] failed in RMI service [" + getJndiName() + "]", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// traditional RMI stub
|
||||
try {
|
||||
return RmiClientInterceptorUtils.invokeRemoteMethod(invocation, stub);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Throwable targetEx = ex.getTargetException();
|
||||
if (targetEx instanceof RemoteException) {
|
||||
throw convertRmiAccessException((RemoteException) targetEx, invocation.getMethod());
|
||||
}
|
||||
else {
|
||||
throw targetEx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given AOP method invocation to the given {@link RmiInvocationHandler}.
|
||||
* <p>The default implementation delegates to {@link #createRemoteInvocation}.
|
||||
* @param methodInvocation the current AOP method invocation
|
||||
* @param invocationHandler the RmiInvocationHandler to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @throws RemoteException in case of communication errors
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
* @see org.springframework.remoting.support.RemoteInvocation
|
||||
*/
|
||||
protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler)
|
||||
throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
|
||||
return "RMI invoker proxy for service URL [" + getJndiName() + "]";
|
||||
}
|
||||
|
||||
return invocationHandler.invoke(createRemoteInvocation(methodInvocation));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocation object for the given AOP method invocation.
|
||||
* <p>The default implementation delegates to the configured
|
||||
* {@link #setRemoteInvocationFactory RemoteInvocationFactory}.
|
||||
* This can be overridden in subclasses in order to provide custom RemoteInvocation
|
||||
* subclasses, containing additional invocation parameters (e.g. user credentials).
|
||||
* <p>Note that it is preferable to build a custom RemoteInvocationFactory
|
||||
* as a reusable strategy, instead of overriding this method.
|
||||
* @param methodInvocation the current AOP method invocation
|
||||
* @return the RemoteInvocation object
|
||||
* @see RemoteInvocationFactory#createRemoteInvocation
|
||||
*/
|
||||
protected RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
|
||||
return getRemoteInvocationFactory().createRemoteInvocation(methodInvocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given RMI RemoteException that happened during remote access
|
||||
* to Spring's RemoteAccessException if the method signature does not declare
|
||||
* RemoteException. Else, return the original RemoteException.
|
||||
* @param method the invoked method
|
||||
* @param ex the RemoteException that happened
|
||||
* @return the exception to be thrown to the caller
|
||||
*/
|
||||
private Exception convertRmiAccessException(RemoteException ex, Method method) {
|
||||
return RmiClientInterceptorUtils.convertRmiAccessException(method, ex, isConnectFailure(ex), getJndiName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} for RMI proxies from JNDI.
|
||||
*
|
||||
* <p>Typically used for RMI-IIOP (CORBA), but can also be used for EJB home objects
|
||||
* (for example, a Stateful Session Bean home). In contrast to a plain JNDI lookup,
|
||||
* this accessor also performs narrowing through {@link javax.rmi.PortableRemoteObject}.
|
||||
*
|
||||
* <p>With conventional RMI services, this invoker is typically used with the RMI
|
||||
* service interface. Alternatively, this invoker can also proxy a remote RMI service
|
||||
* with a matching non-RMI business interface, i.e. an interface that mirrors the RMI
|
||||
* service methods but does not declare RemoteExceptions. In the latter case,
|
||||
* RemoteExceptions thrown by the RMI stub will automatically get converted to
|
||||
* Spring's unchecked RemoteAccessException.
|
||||
*
|
||||
* <p>The JNDI environment can be specified as "jndiEnvironment" property,
|
||||
* or be configured in a {@code jndi.properties} file or as system properties.
|
||||
* For example:
|
||||
*
|
||||
* <pre class="code"><property name="jndiEnvironment">
|
||||
* <props>
|
||||
* <prop key="java.naming.factory.initial">com.sun.jndi.cosnaming.CNCtxFactory</prop>
|
||||
* <prop key="java.naming.provider.url">iiop://localhost:1050</prop>
|
||||
* </props>
|
||||
* </property></pre>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see #setServiceInterface
|
||||
* @see #setJndiName
|
||||
* @see #setJndiTemplate
|
||||
* @see #setJndiEnvironment
|
||||
* @see #setJndiName
|
||||
* @see JndiRmiServiceExporter
|
||||
* @see org.springframework.remoting.RemoteAccessException
|
||||
* @see java.rmi.RemoteException
|
||||
* @see java.rmi.Remote
|
||||
* @see javax.rmi.PortableRemoteObject#narrow
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class JndiRmiProxyFactoryBean extends JndiRmiClientInterceptor
|
||||
implements FactoryBean<Object>, BeanClassLoaderAware {
|
||||
|
||||
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
|
||||
|
||||
private Object serviceProxy;
|
||||
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws NamingException {
|
||||
super.afterPropertiesSet();
|
||||
Class<?> ifc = getServiceInterface();
|
||||
Assert.notNull(ifc, "Property 'serviceInterface' is required");
|
||||
this.serviceProxy = new ProxyFactory(ifc, this).getProxy(this.beanClassLoader);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getObject() {
|
||||
return this.serviceProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return getServiceInterface();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jndi.JndiTemplate;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Service exporter which binds RMI services to JNDI.
|
||||
* Typically used for RMI-IIOP (CORBA).
|
||||
*
|
||||
* <p>Exports services via the {@link javax.rmi.PortableRemoteObject} class.
|
||||
* You need to run "rmic" with the "-iiop" option to generate corresponding
|
||||
* stubs and skeletons for each exported service.
|
||||
*
|
||||
* <p>Also supports exposing any non-RMI service via RMI invokers, to be accessed
|
||||
* via {@link JndiRmiClientInterceptor} / {@link JndiRmiProxyFactoryBean}'s
|
||||
* automatic detection of such invokers.
|
||||
*
|
||||
* <p>With an RMI invoker, RMI communication works on the {@link RmiInvocationHandler}
|
||||
* level, needing only one stub for any service. Service interfaces do not have to
|
||||
* extend {@code java.rmi.Remote} or throw {@code java.rmi.RemoteException}
|
||||
* on all methods, but in and out parameters have to be serializable.
|
||||
*
|
||||
* <p>The JNDI environment can be specified as "jndiEnvironment" bean property,
|
||||
* or be configured in a {@code jndi.properties} file or as system properties.
|
||||
* For example:
|
||||
*
|
||||
* <pre class="code"><property name="jndiEnvironment">
|
||||
* <props>
|
||||
* <prop key="java.naming.factory.initial">com.sun.jndi.cosnaming.CNCtxFactory</prop>
|
||||
* <prop key="java.naming.provider.url">iiop://localhost:1050</prop>
|
||||
* </props>
|
||||
* </property></pre>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see #setService
|
||||
* @see #setJndiTemplate
|
||||
* @see #setJndiEnvironment
|
||||
* @see #setJndiName
|
||||
* @see JndiRmiClientInterceptor
|
||||
* @see JndiRmiProxyFactoryBean
|
||||
* @see javax.rmi.PortableRemoteObject#exportObject
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class JndiRmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean {
|
||||
|
||||
@Nullable
|
||||
private static Method exportObject;
|
||||
|
||||
@Nullable
|
||||
private static Method unexportObject;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class<?> portableRemoteObject =
|
||||
JndiRmiServiceExporter.class.getClassLoader().loadClass("javax.rmi.PortableRemoteObject");
|
||||
exportObject = portableRemoteObject.getMethod("exportObject", Remote.class);
|
||||
unexportObject = portableRemoteObject.getMethod("unexportObject", Remote.class);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
// java.corba module not available on JDK 9+
|
||||
exportObject = null;
|
||||
unexportObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private JndiTemplate jndiTemplate = new JndiTemplate();
|
||||
|
||||
private String jndiName;
|
||||
|
||||
private Remote exportedObject;
|
||||
|
||||
|
||||
/**
|
||||
* Set the JNDI template to use for JNDI lookups.
|
||||
* You can also specify JNDI environment settings via "jndiEnvironment".
|
||||
* @see #setJndiEnvironment
|
||||
*/
|
||||
public void setJndiTemplate(JndiTemplate jndiTemplate) {
|
||||
this.jndiTemplate = (jndiTemplate != null ? jndiTemplate : new JndiTemplate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the JNDI environment to use for JNDI lookups.
|
||||
* Creates a JndiTemplate with the given environment settings.
|
||||
* @see #setJndiTemplate
|
||||
*/
|
||||
public void setJndiEnvironment(Properties jndiEnvironment) {
|
||||
this.jndiTemplate = new JndiTemplate(jndiEnvironment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the JNDI name of the exported RMI service.
|
||||
*/
|
||||
public void setJndiName(String jndiName) {
|
||||
this.jndiName = jndiName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws NamingException, RemoteException {
|
||||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize this service exporter, binding the specified service to JNDI.
|
||||
* @throws NamingException if service binding failed
|
||||
* @throws RemoteException if service export failed
|
||||
*/
|
||||
public void prepare() throws NamingException, RemoteException {
|
||||
if (this.jndiName == null) {
|
||||
throw new IllegalArgumentException("Property 'jndiName' is required");
|
||||
}
|
||||
|
||||
// Initialize and cache exported object.
|
||||
this.exportedObject = getObjectToExport();
|
||||
invokePortableRemoteObject(exportObject);
|
||||
|
||||
rebind();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebind the specified service to JNDI, for recovering in case
|
||||
* of the target registry having been restarted.
|
||||
* @throws NamingException if service binding failed
|
||||
*/
|
||||
public void rebind() throws NamingException {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Binding RMI service to JNDI location [" + this.jndiName + "]");
|
||||
}
|
||||
this.jndiTemplate.rebind(this.jndiName, this.exportedObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unbind the RMI service from JNDI on bean factory shutdown.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() throws NamingException, RemoteException {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Unbinding RMI service from JNDI location [" + this.jndiName + "]");
|
||||
}
|
||||
this.jndiTemplate.unbind(this.jndiName);
|
||||
invokePortableRemoteObject(unexportObject);
|
||||
}
|
||||
|
||||
|
||||
private void invokePortableRemoteObject(@Nullable Method method) throws RemoteException {
|
||||
if (method != null) {
|
||||
try {
|
||||
method.invoke(null, this.exportedObject);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Throwable targetEx = ex.getTargetException();
|
||||
if (targetEx instanceof RemoteException) {
|
||||
throw (RemoteException) targetEx;
|
||||
}
|
||||
ReflectionUtils.rethrowRuntimeException(targetEx);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("PortableRemoteObject invocation failed", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
import org.springframework.remoting.support.RemoteInvocationBasedExporter;
|
||||
import org.springframework.remoting.support.RemoteInvocationResult;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Abstract base class for remote service exporters that explicitly deserialize
|
||||
* {@link org.springframework.remoting.support.RemoteInvocation} objects and serialize
|
||||
* {@link org.springframework.remoting.support.RemoteInvocationResult} objects,
|
||||
* for example Spring's HTTP invoker.
|
||||
*
|
||||
* <p>Provides template methods for {@code ObjectInputStream} and
|
||||
* {@code ObjectOutputStream} handling.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5.1
|
||||
* @see java.io.ObjectInputStream
|
||||
* @see java.io.ObjectOutputStream
|
||||
* @see #doReadRemoteInvocation
|
||||
* @see #doWriteRemoteInvocationResult
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class RemoteInvocationSerializingExporter extends RemoteInvocationBasedExporter
|
||||
implements InitializingBean {
|
||||
|
||||
/**
|
||||
* Default content type: "application/x-java-serialized-object".
|
||||
*/
|
||||
public static final String CONTENT_TYPE_SERIALIZED_OBJECT = "application/x-java-serialized-object";
|
||||
|
||||
|
||||
private String contentType = CONTENT_TYPE_SERIALIZED_OBJECT;
|
||||
|
||||
private boolean acceptProxyClasses = true;
|
||||
|
||||
private Object proxy;
|
||||
|
||||
|
||||
/**
|
||||
* Specify the content type to use for sending remote invocation responses.
|
||||
* <p>Default is "application/x-java-serialized-object".
|
||||
*/
|
||||
public void setContentType(String contentType) {
|
||||
Assert.notNull(contentType, "'contentType' must not be null");
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the content type to use for sending remote invocation responses.
|
||||
*/
|
||||
public String getContentType() {
|
||||
return this.contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to accept deserialization of proxy classes.
|
||||
* <p>Default is "true". May be deactivated as a security measure.
|
||||
*/
|
||||
public void setAcceptProxyClasses(boolean acceptProxyClasses) {
|
||||
this.acceptProxyClasses = acceptProxyClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether to accept deserialization of proxy classes.
|
||||
*/
|
||||
public boolean isAcceptProxyClasses() {
|
||||
return this.acceptProxyClasses;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize this service exporter.
|
||||
*/
|
||||
public void prepare() {
|
||||
this.proxy = getProxyForService();
|
||||
}
|
||||
|
||||
protected final Object getProxy() {
|
||||
if (this.proxy == null) {
|
||||
throw new IllegalStateException(ClassUtils.getShortName(getClass()) + " has not been initialized");
|
||||
}
|
||||
return this.proxy;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create an ObjectInputStream for the given InputStream.
|
||||
* <p>The default implementation creates a Spring {@link CodebaseAwareObjectInputStream}.
|
||||
* @param is the InputStream to read from
|
||||
* @return the new ObjectInputStream instance to use
|
||||
* @throws java.io.IOException if creation of the ObjectInputStream failed
|
||||
*/
|
||||
protected ObjectInputStream createObjectInputStream(InputStream is) throws IOException {
|
||||
return new CodebaseAwareObjectInputStream(is, getBeanClassLoader(), isAcceptProxyClasses());
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the actual reading of an invocation result object from the
|
||||
* given ObjectInputStream.
|
||||
* <p>The default implementation simply calls
|
||||
* {@link java.io.ObjectInputStream#readObject()}.
|
||||
* Can be overridden for deserialization of a custom wrapper object rather
|
||||
* than the plain invocation, for example an encryption-aware holder.
|
||||
* @param ois the ObjectInputStream to read from
|
||||
* @return the RemoteInvocationResult object
|
||||
* @throws java.io.IOException in case of I/O failure
|
||||
* @throws ClassNotFoundException if case of a transferred class not
|
||||
* being found in the local ClassLoader
|
||||
*/
|
||||
protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois)
|
||||
throws IOException, ClassNotFoundException {
|
||||
|
||||
Object obj = ois.readObject();
|
||||
if (!(obj instanceof RemoteInvocation)) {
|
||||
throw new RemoteException("Deserialized object needs to be assignable to type [" +
|
||||
RemoteInvocation.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
|
||||
}
|
||||
return (RemoteInvocation) obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ObjectOutputStream for the given OutputStream.
|
||||
* <p>The default implementation creates a plain
|
||||
* {@link java.io.ObjectOutputStream}.
|
||||
* @param os the OutputStream to write to
|
||||
* @return the new ObjectOutputStream instance to use
|
||||
* @throws java.io.IOException if creation of the ObjectOutputStream failed
|
||||
*/
|
||||
protected ObjectOutputStream createObjectOutputStream(OutputStream os) throws IOException {
|
||||
return new ObjectOutputStream(os);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the actual writing of the given invocation result object
|
||||
* to the given ObjectOutputStream.
|
||||
* <p>The default implementation simply calls
|
||||
* {@link java.io.ObjectOutputStream#writeObject}.
|
||||
* Can be overridden for serialization of a custom wrapper object rather
|
||||
* than the plain invocation, for example an encryption-aware holder.
|
||||
* @param result the RemoteInvocationResult object
|
||||
* @param oos the ObjectOutputStream to write to
|
||||
* @throws java.io.IOException if thrown by I/O methods
|
||||
*/
|
||||
protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream oos)
|
||||
throws IOException {
|
||||
|
||||
oos.writeObject(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.rmi.Remote;
|
||||
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
import org.springframework.remoting.support.RemoteInvocationBasedExporter;
|
||||
|
||||
/**
|
||||
* Convenient superclass for RMI-based remote exporters. Provides a facility
|
||||
* to automatically wrap a given plain Java service object with an
|
||||
* RmiInvocationWrapper, exposing the {@link RmiInvocationHandler} remote interface.
|
||||
*
|
||||
* <p>Using the RMI invoker mechanism, RMI communication operates at the {@link RmiInvocationHandler}
|
||||
* level, sharing a common invoker stub for any number of services. Service interfaces are <i>not</i>
|
||||
* required to extend {@code java.rmi.Remote} or declare {@code java.rmi.RemoteException}
|
||||
* on all service methods. However, in and out parameters still have to be serializable.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.5
|
||||
* @see RmiServiceExporter
|
||||
* @see JndiRmiServiceExporter
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class RmiBasedExporter extends RemoteInvocationBasedExporter {
|
||||
|
||||
/**
|
||||
* Determine the object to export: either the service object itself
|
||||
* or a RmiInvocationWrapper in case of a non-RMI service object.
|
||||
* @return the RMI object to export
|
||||
* @see #setService
|
||||
* @see #setServiceInterface
|
||||
*/
|
||||
protected Remote getObjectToExport() {
|
||||
// determine remote object
|
||||
if (getService() instanceof Remote &&
|
||||
(getServiceInterface() == null || Remote.class.isAssignableFrom(getServiceInterface()))) {
|
||||
// conventional RMI service
|
||||
return (Remote) getService();
|
||||
}
|
||||
else {
|
||||
// RMI invoker
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("RMI service [" + getService() + "] is an RMI invoker");
|
||||
}
|
||||
return new RmiInvocationWrapper(getProxyForService(), this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redefined here to be visible to RmiInvocationWrapper.
|
||||
* Simply delegates to the corresponding superclass method.
|
||||
*/
|
||||
@Override
|
||||
protected Object invoke(RemoteInvocation invocation, Object targetObject)
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
return super.invoke(invocation, targetObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,424 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.RMIClientSocketFactory;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.remoting.RemoteConnectFailureException;
|
||||
import org.springframework.remoting.RemoteInvocationFailureException;
|
||||
import org.springframework.remoting.RemoteLookupFailureException;
|
||||
import org.springframework.remoting.support.RemoteInvocationBasedAccessor;
|
||||
import org.springframework.remoting.support.RemoteInvocationUtils;
|
||||
|
||||
/**
|
||||
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing conventional
|
||||
* RMI services or RMI invokers. The service URL must be a valid RMI URL
|
||||
* (e.g. "rmi://localhost:1099/myservice").
|
||||
*
|
||||
* <p>RMI invokers work at the RmiInvocationHandler level, needing only one stub for
|
||||
* any service. Service interfaces do not have to extend {@code java.rmi.Remote}
|
||||
* or throw {@code java.rmi.RemoteException}. Spring's unchecked
|
||||
* RemoteAccessException will be thrown on remote invocation failure.
|
||||
* Of course, in and out parameters have to be serializable.
|
||||
*
|
||||
* <p>With conventional RMI services, this invoker is typically used with the RMI
|
||||
* service interface. Alternatively, this invoker can also proxy a remote RMI service
|
||||
* with a matching non-RMI business interface, i.e. an interface that mirrors the RMI
|
||||
* service methods but does not declare RemoteExceptions. In the latter case,
|
||||
* RemoteExceptions thrown by the RMI stub will automatically get converted to
|
||||
* Spring's unchecked RemoteAccessException.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 29.09.2003
|
||||
* @see RmiServiceExporter
|
||||
* @see RmiProxyFactoryBean
|
||||
* @see RmiInvocationHandler
|
||||
* @see org.springframework.remoting.RemoteAccessException
|
||||
* @see java.rmi.RemoteException
|
||||
* @see java.rmi.Remote
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class RmiClientInterceptor extends RemoteInvocationBasedAccessor
|
||||
implements MethodInterceptor {
|
||||
|
||||
private boolean lookupStubOnStartup = true;
|
||||
|
||||
private boolean cacheStub = true;
|
||||
|
||||
private boolean refreshStubOnConnectFailure = false;
|
||||
|
||||
private RMIClientSocketFactory registryClientSocketFactory;
|
||||
|
||||
private Remote cachedStub;
|
||||
|
||||
private final Object stubMonitor = new Object();
|
||||
|
||||
|
||||
/**
|
||||
* Set whether to look up the RMI stub on startup. Default is "true".
|
||||
* <p>Can be turned off to allow for late start of the RMI server.
|
||||
* In this case, the RMI stub will be fetched on first access.
|
||||
* @see #setCacheStub
|
||||
*/
|
||||
public void setLookupStubOnStartup(boolean lookupStubOnStartup) {
|
||||
this.lookupStubOnStartup = lookupStubOnStartup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to cache the RMI stub once it has been located.
|
||||
* Default is "true".
|
||||
* <p>Can be turned off to allow for hot restart of the RMI server.
|
||||
* In this case, the RMI stub will be fetched for each invocation.
|
||||
* @see #setLookupStubOnStartup
|
||||
*/
|
||||
public void setCacheStub(boolean cacheStub) {
|
||||
this.cacheStub = cacheStub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to refresh the RMI stub on connect failure.
|
||||
* Default is "false".
|
||||
* <p>Can be turned on to allow for hot restart of the RMI server.
|
||||
* If a cached RMI stub throws an RMI exception that indicates a
|
||||
* remote connect failure, a fresh proxy will be fetched and the
|
||||
* invocation will be retried.
|
||||
* @see java.rmi.ConnectException
|
||||
* @see java.rmi.ConnectIOException
|
||||
* @see java.rmi.NoSuchObjectException
|
||||
*/
|
||||
public void setRefreshStubOnConnectFailure(boolean refreshStubOnConnectFailure) {
|
||||
this.refreshStubOnConnectFailure = refreshStubOnConnectFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI client socket factory to use for accessing the RMI registry.
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.registry.LocateRegistry#getRegistry(String, int, RMIClientSocketFactory)
|
||||
*/
|
||||
public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) {
|
||||
this.registryClientSocketFactory = registryClientSocketFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches RMI stub on startup, if necessary.
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
* @see #setLookupStubOnStartup
|
||||
* @see #lookupStub
|
||||
*/
|
||||
public void prepare() throws RemoteLookupFailureException {
|
||||
// Cache RMI stub on initialization?
|
||||
if (this.lookupStubOnStartup) {
|
||||
Remote remoteObj = lookupStub();
|
||||
if (logger.isDebugEnabled()) {
|
||||
if (remoteObj instanceof RmiInvocationHandler) {
|
||||
logger.debug("RMI stub [" + getServiceUrl() + "] is an RMI invoker");
|
||||
}
|
||||
else if (getServiceInterface() != null) {
|
||||
boolean isImpl = getServiceInterface().isInstance(remoteObj);
|
||||
logger.debug("Using service interface [" + getServiceInterface().getName() +
|
||||
"] for RMI stub [" + getServiceUrl() + "] - " +
|
||||
(!isImpl ? "not " : "") + "directly implemented");
|
||||
}
|
||||
}
|
||||
if (this.cacheStub) {
|
||||
this.cachedStub = remoteObj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the RMI stub, typically by looking it up.
|
||||
* <p>Called on interceptor initialization if "cacheStub" is "true";
|
||||
* else called for each invocation by {@link #getStub()}.
|
||||
* <p>The default implementation looks up the service URL via
|
||||
* {@code java.rmi.Naming}. This can be overridden in subclasses.
|
||||
* @return the RMI stub to store in this interceptor
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
* @see #setCacheStub
|
||||
* @see java.rmi.Naming#lookup
|
||||
*/
|
||||
protected Remote lookupStub() throws RemoteLookupFailureException {
|
||||
try {
|
||||
Remote stub = null;
|
||||
if (this.registryClientSocketFactory != null) {
|
||||
// RMIClientSocketFactory specified for registry access.
|
||||
// Unfortunately, due to RMI API limitations, this means
|
||||
// that we need to parse the RMI URL ourselves and perform
|
||||
// straight LocateRegistry.getRegistry/Registry.lookup calls.
|
||||
URL url = new URL(null, getServiceUrl(), new DummyURLStreamHandler());
|
||||
String protocol = url.getProtocol();
|
||||
if (protocol != null && !"rmi".equals(protocol)) {
|
||||
throw new MalformedURLException("Invalid URL scheme '" + protocol + "'");
|
||||
}
|
||||
String host = url.getHost();
|
||||
int port = url.getPort();
|
||||
String name = url.getPath();
|
||||
if (name != null && name.startsWith("/")) {
|
||||
name = name.substring(1);
|
||||
}
|
||||
Registry registry = LocateRegistry.getRegistry(host, port, this.registryClientSocketFactory);
|
||||
stub = registry.lookup(name);
|
||||
}
|
||||
else {
|
||||
// Can proceed with standard RMI lookup API...
|
||||
stub = Naming.lookup(getServiceUrl());
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Located RMI stub with URL [" + getServiceUrl() + "]");
|
||||
}
|
||||
return stub;
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex);
|
||||
}
|
||||
catch (NotBoundException ex) {
|
||||
throw new RemoteLookupFailureException(
|
||||
"Could not find RMI service [" + getServiceUrl() + "] in RMI registry", ex);
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
throw new RemoteLookupFailureException("Lookup of RMI stub failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RMI stub to use. Called for each invocation.
|
||||
* <p>The default implementation returns the stub created on initialization,
|
||||
* if any. Else, it invokes {@link #lookupStub} to get a new stub for
|
||||
* each invocation. This can be overridden in subclasses, for example in
|
||||
* order to cache a stub for a given amount of time before recreating it,
|
||||
* or to test the stub whether it is still alive.
|
||||
* @return the RMI stub to use for an invocation
|
||||
* @throws RemoteLookupFailureException if RMI stub creation failed
|
||||
* @see #lookupStub
|
||||
*/
|
||||
protected Remote getStub() throws RemoteLookupFailureException {
|
||||
if (!this.cacheStub || (this.lookupStubOnStartup && !this.refreshStubOnConnectFailure)) {
|
||||
return (this.cachedStub != null ? this.cachedStub : lookupStub());
|
||||
}
|
||||
else {
|
||||
synchronized (this.stubMonitor) {
|
||||
if (this.cachedStub == null) {
|
||||
this.cachedStub = lookupStub();
|
||||
}
|
||||
return this.cachedStub;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches an RMI stub and delegates to {@code doInvoke}.
|
||||
* If configured to refresh on connect failure, it will call
|
||||
* {@link #refreshAndRetry} on corresponding RMI exceptions.
|
||||
* @see #getStub
|
||||
* @see #doInvoke(MethodInvocation, Remote)
|
||||
* @see #refreshAndRetry
|
||||
* @see java.rmi.ConnectException
|
||||
* @see java.rmi.ConnectIOException
|
||||
* @see java.rmi.NoSuchObjectException
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Remote stub = getStub();
|
||||
try {
|
||||
return doInvoke(invocation, stub);
|
||||
}
|
||||
catch (RemoteConnectFailureException ex) {
|
||||
return handleRemoteConnectFailure(invocation, ex);
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
if (isConnectFailure(ex)) {
|
||||
return handleRemoteConnectFailure(invocation, ex);
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given RMI exception indicates a connect failure.
|
||||
* <p>The default implementation delegates to
|
||||
* {@link RmiClientInterceptorUtils#isConnectFailure}.
|
||||
* @param ex the RMI exception to check
|
||||
* @return whether the exception should be treated as connect failure
|
||||
*/
|
||||
protected boolean isConnectFailure(RemoteException ex) {
|
||||
return RmiClientInterceptorUtils.isConnectFailure(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the stub and retry the remote invocation if necessary.
|
||||
* <p>If not configured to refresh on connect failure, this method
|
||||
* simply rethrows the original exception.
|
||||
* @param invocation the invocation that failed
|
||||
* @param ex the exception raised on remote invocation
|
||||
* @return the result value of the new invocation, if succeeded
|
||||
* @throws Throwable an exception raised by the new invocation,
|
||||
* if it failed as well
|
||||
* @see #setRefreshStubOnConnectFailure
|
||||
* @see #doInvoke
|
||||
*/
|
||||
@Nullable
|
||||
private Object handleRemoteConnectFailure(MethodInvocation invocation, Exception ex) throws Throwable {
|
||||
if (this.refreshStubOnConnectFailure) {
|
||||
String msg = "Could not connect to RMI service [" + getServiceUrl() + "] - retrying";
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn(msg, ex);
|
||||
}
|
||||
else if (logger.isWarnEnabled()) {
|
||||
logger.warn(msg);
|
||||
}
|
||||
return refreshAndRetry(invocation);
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the RMI stub and retry the given invocation.
|
||||
* Called by invoke on connect failure.
|
||||
* @param invocation the AOP method invocation
|
||||
* @return the invocation result, if any
|
||||
* @throws Throwable in case of invocation failure
|
||||
* @see #invoke
|
||||
*/
|
||||
@Nullable
|
||||
protected Object refreshAndRetry(MethodInvocation invocation) throws Throwable {
|
||||
Remote freshStub = null;
|
||||
synchronized (this.stubMonitor) {
|
||||
this.cachedStub = null;
|
||||
freshStub = lookupStub();
|
||||
if (this.cacheStub) {
|
||||
this.cachedStub = freshStub;
|
||||
}
|
||||
}
|
||||
return doInvoke(invocation, freshStub);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the given invocation on the given RMI stub.
|
||||
* @param invocation the AOP method invocation
|
||||
* @param stub the RMI stub to invoke
|
||||
* @return the invocation result, if any
|
||||
* @throws Throwable in case of invocation failure
|
||||
*/
|
||||
@Nullable
|
||||
protected Object doInvoke(MethodInvocation invocation, Remote stub) throws Throwable {
|
||||
if (stub instanceof RmiInvocationHandler) {
|
||||
// RMI invoker
|
||||
try {
|
||||
return doInvoke(invocation, (RmiInvocationHandler) stub);
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
throw RmiClientInterceptorUtils.convertRmiAccessException(
|
||||
invocation.getMethod(), ex, isConnectFailure(ex), getServiceUrl());
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Throwable exToThrow = ex.getTargetException();
|
||||
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
|
||||
throw exToThrow;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new RemoteInvocationFailureException("Invocation of method [" + invocation.getMethod() +
|
||||
"] failed in RMI service [" + getServiceUrl() + "]", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// traditional RMI stub
|
||||
try {
|
||||
return RmiClientInterceptorUtils.invokeRemoteMethod(invocation, stub);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Throwable targetEx = ex.getTargetException();
|
||||
if (targetEx instanceof RemoteException) {
|
||||
RemoteException rex = (RemoteException) targetEx;
|
||||
throw RmiClientInterceptorUtils.convertRmiAccessException(
|
||||
invocation.getMethod(), rex, isConnectFailure(rex), getServiceUrl());
|
||||
}
|
||||
else {
|
||||
throw targetEx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given AOP method invocation to the given {@link RmiInvocationHandler}.
|
||||
* <p>The default implementation delegates to {@link #createRemoteInvocation}.
|
||||
* @param methodInvocation the current AOP method invocation
|
||||
* @param invocationHandler the RmiInvocationHandler to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @throws RemoteException in case of communication errors
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
* @see org.springframework.remoting.support.RemoteInvocation
|
||||
*/
|
||||
@Nullable
|
||||
protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler)
|
||||
throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
|
||||
return "RMI invoker proxy for service URL [" + getServiceUrl() + "]";
|
||||
}
|
||||
|
||||
return invocationHandler.invoke(createRemoteInvocation(methodInvocation));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Dummy URLStreamHandler that's just specified to suppress the standard
|
||||
* {@code java.net.URL} URLStreamHandler lookup, to be able to
|
||||
* use the standard URL class for parsing "rmi:..." URLs.
|
||||
*/
|
||||
private static class DummyURLStreamHandler extends URLStreamHandler {
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL url) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.SocketException;
|
||||
import java.rmi.ConnectException;
|
||||
import java.rmi.ConnectIOException;
|
||||
import java.rmi.NoSuchObjectException;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.StubNotFoundException;
|
||||
import java.rmi.UnknownHostException;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.remoting.RemoteAccessException;
|
||||
import org.springframework.remoting.RemoteConnectFailureException;
|
||||
import org.springframework.remoting.RemoteProxyFailureException;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Factored-out methods for performing invocations within an RMI client.
|
||||
* Can handle both RMI and non-RMI service interfaces working on an RMI stub.
|
||||
*
|
||||
* <p>Note: This is an SPI class, not intended to be used by applications.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class RmiClientInterceptorUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(RmiClientInterceptorUtils.class);
|
||||
|
||||
|
||||
/**
|
||||
* Perform a raw method invocation on the given RMI stub,
|
||||
* letting reflection exceptions through as-is.
|
||||
* @param invocation the AOP MethodInvocation
|
||||
* @param stub the RMI stub
|
||||
* @return the invocation result, if any
|
||||
* @throws InvocationTargetException if thrown by reflection
|
||||
*/
|
||||
@Nullable
|
||||
public static Object invokeRemoteMethod(MethodInvocation invocation, Object stub)
|
||||
throws InvocationTargetException {
|
||||
|
||||
Method method = invocation.getMethod();
|
||||
try {
|
||||
if (method.getDeclaringClass().isInstance(stub)) {
|
||||
// directly implemented
|
||||
return method.invoke(stub, invocation.getArguments());
|
||||
}
|
||||
else {
|
||||
// not directly implemented
|
||||
Method stubMethod = stub.getClass().getMethod(method.getName(), method.getParameterTypes());
|
||||
return stubMethod.invoke(stub, invocation.getArguments());
|
||||
}
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
throw new RemoteProxyFailureException("No matching RMI stub method found for: " + method, ex);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new RemoteProxyFailureException("Invocation of RMI stub method failed: " + method, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the given arbitrary exception that happened during remote access
|
||||
* in either a RemoteException or a Spring RemoteAccessException (if the
|
||||
* method signature does not support RemoteException).
|
||||
* <p>Only call this for remote access exceptions, not for exceptions
|
||||
* thrown by the target service itself!
|
||||
* @param method the invoked method
|
||||
* @param ex the exception that happened, to be used as cause for the
|
||||
* RemoteAccessException or RemoteException
|
||||
* @param message the message for the RemoteAccessException respectively
|
||||
* RemoteException
|
||||
* @return the exception to be thrown to the caller
|
||||
*/
|
||||
public static Exception convertRmiAccessException(Method method, Throwable ex, String message) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(message, ex);
|
||||
}
|
||||
if (ReflectionUtils.declaresException(method, RemoteException.class)) {
|
||||
return new RemoteException(message, ex);
|
||||
}
|
||||
else {
|
||||
return new RemoteAccessException(message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given RemoteException that happened during remote access
|
||||
* to Spring's RemoteAccessException if the method signature does not
|
||||
* support RemoteException. Else, return the original RemoteException.
|
||||
* @param method the invoked method
|
||||
* @param ex the RemoteException that happened
|
||||
* @param serviceName the name of the service (for debugging purposes)
|
||||
* @return the exception to be thrown to the caller
|
||||
*/
|
||||
public static Exception convertRmiAccessException(Method method, RemoteException ex, String serviceName) {
|
||||
return convertRmiAccessException(method, ex, isConnectFailure(ex), serviceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given RemoteException that happened during remote access
|
||||
* to Spring's RemoteAccessException if the method signature does not
|
||||
* support RemoteException. Else, return the original RemoteException.
|
||||
* @param method the invoked method
|
||||
* @param ex the RemoteException that happened
|
||||
* @param isConnectFailure whether the given exception should be considered
|
||||
* a connect failure
|
||||
* @param serviceName the name of the service (for debugging purposes)
|
||||
* @return the exception to be thrown to the caller
|
||||
*/
|
||||
public static Exception convertRmiAccessException(
|
||||
Method method, RemoteException ex, boolean isConnectFailure, String serviceName) {
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Remote service [" + serviceName + "] threw exception", ex);
|
||||
}
|
||||
if (ReflectionUtils.declaresException(method, ex.getClass())) {
|
||||
return ex;
|
||||
}
|
||||
else {
|
||||
if (isConnectFailure) {
|
||||
return new RemoteConnectFailureException("Could not connect to remote service [" + serviceName + "]", ex);
|
||||
}
|
||||
else {
|
||||
return new RemoteAccessException("Could not access remote service [" + serviceName + "]", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given RMI exception indicates a connect failure.
|
||||
* <p>Treats RMI's ConnectException, ConnectIOException, UnknownHostException,
|
||||
* NoSuchObjectException and StubNotFoundException as connect failure.
|
||||
* @param ex the RMI exception to check
|
||||
* @return whether the exception should be treated as connect failure
|
||||
* @see java.rmi.ConnectException
|
||||
* @see java.rmi.ConnectIOException
|
||||
* @see java.rmi.UnknownHostException
|
||||
* @see java.rmi.NoSuchObjectException
|
||||
* @see java.rmi.StubNotFoundException
|
||||
*/
|
||||
public static boolean isConnectFailure(RemoteException ex) {
|
||||
return (ex instanceof ConnectException || ex instanceof ConnectIOException ||
|
||||
ex instanceof UnknownHostException || ex instanceof NoSuchObjectException ||
|
||||
ex instanceof StubNotFoundException || ex.getCause() instanceof SocketException);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
|
||||
/**
|
||||
* Interface for RMI invocation handlers instances on the server,
|
||||
* wrapping exported services. A client uses a stub implementing
|
||||
* this interface to access such a service.
|
||||
*
|
||||
* <p>This is an SPI interface, not to be used directly by applications.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 14.05.2003
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public interface RmiInvocationHandler extends Remote {
|
||||
|
||||
/**
|
||||
* Return the name of the target interface that this invoker operates on.
|
||||
* @return the name of the target interface, or {@code null} if none
|
||||
* @throws RemoteException in case of communication errors
|
||||
* @see RmiServiceExporter#getServiceInterface()
|
||||
*/
|
||||
@Nullable
|
||||
public String getTargetInterfaceName() throws RemoteException;
|
||||
|
||||
/**
|
||||
* Apply the given invocation to the target object.
|
||||
* <p>Called by
|
||||
* {@link RmiClientInterceptor#doInvoke(org.aopalliance.intercept.MethodInvocation, RmiInvocationHandler)}.
|
||||
* @param invocation object that encapsulates invocation parameters
|
||||
* @return the object returned from the invoked method, if any
|
||||
* @throws RemoteException in case of communication errors
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
*/
|
||||
@Nullable
|
||||
public Object invoke(RemoteInvocation invocation)
|
||||
throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Server-side implementation of {@link RmiInvocationHandler}. An instance
|
||||
* of this class exists for each remote object. Automatically created
|
||||
* by {@link RmiServiceExporter} for non-RMI service implementations.
|
||||
*
|
||||
* <p>This is an SPI class, not to be used directly by applications.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 14.05.2003
|
||||
* @see RmiServiceExporter
|
||||
*/
|
||||
@Deprecated
|
||||
class RmiInvocationWrapper implements RmiInvocationHandler {
|
||||
|
||||
private final Object wrappedObject;
|
||||
|
||||
private final RmiBasedExporter rmiExporter;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new RmiInvocationWrapper for the given object.
|
||||
* @param wrappedObject the object to wrap with an RmiInvocationHandler
|
||||
* @param rmiExporter the RMI exporter to handle the actual invocation
|
||||
*/
|
||||
public RmiInvocationWrapper(Object wrappedObject, RmiBasedExporter rmiExporter) {
|
||||
Assert.notNull(wrappedObject, "Object to wrap is required");
|
||||
Assert.notNull(rmiExporter, "RMI exporter is required");
|
||||
this.wrappedObject = wrappedObject;
|
||||
this.rmiExporter = rmiExporter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exposes the exporter's service interface, if any, as target interface.
|
||||
* @see RmiBasedExporter#getServiceInterface()
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public String getTargetInterfaceName() {
|
||||
Class<?> ifc = this.rmiExporter.getServiceInterface();
|
||||
return (ifc != null ? ifc.getName() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates the actual invocation handling to the RMI exporter.
|
||||
* @see RmiBasedExporter#invoke(org.springframework.remoting.support.RemoteInvocation, Object)
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public Object invoke(RemoteInvocation invocation)
|
||||
throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
return this.rmiExporter.invoke(invocation, this.wrappedObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} for RMI proxies, supporting both conventional RMI services
|
||||
* and RMI invokers. Exposes the proxied service for use as a bean reference,
|
||||
* using the specified service interface. Proxies will throw Spring's unchecked
|
||||
* RemoteAccessException on remote invocation failure instead of RMI's RemoteException.
|
||||
*
|
||||
* <p>The service URL must be a valid RMI URL like "rmi://localhost:1099/myservice".
|
||||
* RMI invokers work at the RmiInvocationHandler level, using the same invoker stub
|
||||
* for any service. Service interfaces do not have to extend {@code java.rmi.Remote}
|
||||
* or throw {@code java.rmi.RemoteException}. Of course, in and out parameters
|
||||
* have to be serializable.
|
||||
*
|
||||
* <p>With conventional RMI services, this proxy factory is typically used with the
|
||||
* RMI service interface. Alternatively, this factory can also proxy a remote RMI
|
||||
* service with a matching non-RMI business interface, i.e. an interface that mirrors
|
||||
* the RMI service methods but does not declare RemoteExceptions. In the latter case,
|
||||
* RemoteExceptions thrown by the RMI stub will automatically get converted to
|
||||
* Spring's unchecked RemoteAccessException.
|
||||
*
|
||||
* <p>The major advantage of RMI, compared to Hessian, is serialization.
|
||||
* Effectively, any serializable Java object can be transported without hassle.
|
||||
* Hessian has its own (de-)serialization mechanisms, but is HTTP-based and thus
|
||||
* much easier to setup than RMI. Alternatively, consider Spring's HTTP invoker
|
||||
* to combine Java serialization with HTTP-based transport.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 13.05.2003
|
||||
* @see #setServiceInterface
|
||||
* @see #setServiceUrl
|
||||
* @see RmiClientInterceptor
|
||||
* @see RmiServiceExporter
|
||||
* @see java.rmi.Remote
|
||||
* @see java.rmi.RemoteException
|
||||
* @see org.springframework.remoting.RemoteAccessException
|
||||
* @see org.springframework.remoting.caucho.HessianProxyFactoryBean
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class RmiProxyFactoryBean extends RmiClientInterceptor implements FactoryBean<Object>, BeanClassLoaderAware {
|
||||
|
||||
private Object serviceProxy;
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Class<?> ifc = getServiceInterface();
|
||||
Assert.notNull(ifc, "Property 'serviceInterface' is required");
|
||||
this.serviceProxy = new ProxyFactory(ifc, this).getProxy(getBeanClassLoader());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getObject() {
|
||||
return this.serviceProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return getServiceInterface();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,317 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.RMIClientSocketFactory;
|
||||
import java.rmi.server.RMIServerSocketFactory;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} that locates a {@link java.rmi.registry.Registry} and
|
||||
* exposes it for bean references. Can also create a local RMI registry
|
||||
* on the fly if none exists already.
|
||||
*
|
||||
* <p>Can be used to set up and pass around the actual Registry object to
|
||||
* applications objects that need to work with RMI. One example for such an
|
||||
* object that needs to work with RMI is Spring's {@link RmiServiceExporter},
|
||||
* which either works with a passed-in Registry reference or falls back to
|
||||
* the registry as specified by its local properties and defaults.
|
||||
*
|
||||
* <p>Also useful to enforce creation of a local RMI registry at a given port,
|
||||
* for example for a JMX connector. If used in conjunction with
|
||||
* {@link org.springframework.jmx.support.ConnectorServerFactoryBean},
|
||||
* it is recommended to mark the connector definition (ConnectorServerFactoryBean)
|
||||
* as "depends-on" the registry definition (RmiRegistryFactoryBean),
|
||||
* to guarantee starting up the registry first.
|
||||
*
|
||||
* <p>Note: The implementation of this class mirrors the corresponding logic
|
||||
* in {@link RmiServiceExporter}, and also offers the same customization hooks.
|
||||
* RmiServiceExporter implements its own registry lookup as a convenience:
|
||||
* It is very common to simply rely on the registry defaults.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.3
|
||||
* @see RmiServiceExporter#setRegistry
|
||||
* @see org.springframework.jmx.support.ConnectorServerFactoryBean
|
||||
* @see java.rmi.registry.Registry
|
||||
* @see java.rmi.registry.LocateRegistry
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class RmiRegistryFactoryBean implements FactoryBean<Registry>, InitializingBean, DisposableBean {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private String host;
|
||||
|
||||
private int port = Registry.REGISTRY_PORT;
|
||||
|
||||
private RMIClientSocketFactory clientSocketFactory;
|
||||
|
||||
private RMIServerSocketFactory serverSocketFactory;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
private boolean alwaysCreate = false;
|
||||
|
||||
private boolean created = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set the host of the registry for the exported RMI service,
|
||||
* i.e. {@code rmi://HOST:port/name}
|
||||
* <p>Default is localhost.
|
||||
*/
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the host of the registry for the exported RMI service.
|
||||
*/
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the port of the registry for the exported RMI service,
|
||||
* i.e. {@code rmi://host:PORT/name}
|
||||
* <p>Default is {@code Registry.REGISTRY_PORT} (1099).
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the port of the registry for the exported RMI service.
|
||||
*/
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI client socket factory to use for the RMI registry.
|
||||
* <p>If the given object also implements {@code java.rmi.server.RMIServerSocketFactory},
|
||||
* it will automatically be registered as server socket factory too.
|
||||
* @see #setServerSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see java.rmi.registry.LocateRegistry#getRegistry(String, int, java.rmi.server.RMIClientSocketFactory)
|
||||
*/
|
||||
public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) {
|
||||
this.clientSocketFactory = clientSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI server socket factory to use for the RMI registry.
|
||||
* <p>Only needs to be specified when the client socket factory does not
|
||||
* implement {@code java.rmi.server.RMIServerSocketFactory} already.
|
||||
* @see #setClientSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see java.rmi.registry.LocateRegistry#createRegistry(int, RMIClientSocketFactory, java.rmi.server.RMIServerSocketFactory)
|
||||
*/
|
||||
public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) {
|
||||
this.serverSocketFactory = serverSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to always create the registry in-process,
|
||||
* not attempting to locate an existing registry at the specified port.
|
||||
* <p>Default is "false". Switch this flag to "true" in order to avoid
|
||||
* the overhead of locating an existing registry when you always
|
||||
* intend to create a new registry in any case.
|
||||
*/
|
||||
public void setAlwaysCreate(boolean alwaysCreate) {
|
||||
this.alwaysCreate = alwaysCreate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// Check socket factories for registry.
|
||||
if (this.clientSocketFactory instanceof RMIServerSocketFactory) {
|
||||
this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory;
|
||||
}
|
||||
if ((this.clientSocketFactory != null && this.serverSocketFactory == null) ||
|
||||
(this.clientSocketFactory == null && this.serverSocketFactory != null)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Both RMIClientSocketFactory and RMIServerSocketFactory or none required");
|
||||
}
|
||||
|
||||
// Fetch RMI registry to expose.
|
||||
this.registry = getRegistry(this.host, this.port, this.clientSocketFactory, this.serverSocketFactory);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry.
|
||||
* @param registryHost the registry host to use (if this is specified,
|
||||
* no implicit creation of a RMI registry will happen)
|
||||
* @param registryPort the registry port to use
|
||||
* @param clientSocketFactory the RMI client socket factory for the registry (if any)
|
||||
* @param serverSocketFactory the RMI server socket factory for the registry (if any)
|
||||
* @return the RMI registry
|
||||
* @throws java.rmi.RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(String registryHost, int registryPort,
|
||||
@Nullable RMIClientSocketFactory clientSocketFactory, @Nullable RMIServerSocketFactory serverSocketFactory)
|
||||
throws RemoteException {
|
||||
|
||||
if (registryHost != null) {
|
||||
// Host explicitly specified: only lookup possible.
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]");
|
||||
}
|
||||
Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
|
||||
else {
|
||||
return getRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry.
|
||||
* @param registryPort the registry port to use
|
||||
* @param clientSocketFactory the RMI client socket factory for the registry (if any)
|
||||
* @param serverSocketFactory the RMI server socket factory for the registry (if any)
|
||||
* @return the RMI registry
|
||||
* @throws RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(int registryPort,
|
||||
@Nullable RMIClientSocketFactory clientSocketFactory, @Nullable RMIServerSocketFactory serverSocketFactory)
|
||||
throws RemoteException {
|
||||
|
||||
if (clientSocketFactory != null) {
|
||||
if (this.alwaysCreate) {
|
||||
logger.debug("Creating new RMI registry");
|
||||
this.created = true;
|
||||
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "', using custom socket factory");
|
||||
}
|
||||
synchronized (LocateRegistry.class) {
|
||||
try {
|
||||
// Retrieve existing registry.
|
||||
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
logger.trace("RMI registry access threw exception", ex);
|
||||
logger.debug("Could not detect RMI registry - creating new one");
|
||||
// Assume no registry found -> create new one.
|
||||
this.created = true;
|
||||
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
return getRegistry(registryPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry.
|
||||
* @param registryPort the registry port to use
|
||||
* @return the RMI registry
|
||||
* @throws RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(int registryPort) throws RemoteException {
|
||||
if (this.alwaysCreate) {
|
||||
logger.debug("Creating new RMI registry");
|
||||
this.created = true;
|
||||
return LocateRegistry.createRegistry(registryPort);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "'");
|
||||
}
|
||||
synchronized (LocateRegistry.class) {
|
||||
try {
|
||||
// Retrieve existing registry.
|
||||
Registry reg = LocateRegistry.getRegistry(registryPort);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
logger.trace("RMI registry access threw exception", ex);
|
||||
logger.debug("Could not detect RMI registry - creating new one");
|
||||
// Assume no registry found -> create new one.
|
||||
this.created = true;
|
||||
return LocateRegistry.createRegistry(registryPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the given RMI registry, calling some operation on it to
|
||||
* check whether it is still active.
|
||||
* <p>Default implementation calls {@code Registry.list()}.
|
||||
* @param registry the RMI registry to test
|
||||
* @throws RemoteException if thrown by registry methods
|
||||
* @see java.rmi.registry.Registry#list()
|
||||
*/
|
||||
protected void testRegistry(Registry registry) throws RemoteException {
|
||||
registry.list();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Registry getObject() throws Exception {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Registry> getObjectType() {
|
||||
return (this.registry != null ? this.registry.getClass() : Registry.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unexport the RMI registry on bean factory shutdown,
|
||||
* provided that this bean actually created a registry.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() throws RemoteException {
|
||||
if (this.created) {
|
||||
logger.debug("Unexporting RMI registry");
|
||||
UnicastRemoteObject.unexportObject(this.registry, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,464 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.rmi;
|
||||
|
||||
import java.rmi.AlreadyBoundException;
|
||||
import java.rmi.NoSuchObjectException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.RMIClientSocketFactory;
|
||||
import java.rmi.server.RMIServerSocketFactory;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* RMI exporter that exposes the specified service as RMI object with the specified name.
|
||||
* Such services can be accessed via plain RMI or via {@link RmiProxyFactoryBean}.
|
||||
* Also supports exposing any non-RMI service via RMI invokers, to be accessed via
|
||||
* {@link RmiClientInterceptor} / {@link RmiProxyFactoryBean}'s automatic detection
|
||||
* of such invokers.
|
||||
*
|
||||
* <p>With an RMI invoker, RMI communication works on the {@link RmiInvocationHandler}
|
||||
* level, needing only one stub for any service. Service interfaces do not have to
|
||||
* extend {@code java.rmi.Remote} or throw {@code java.rmi.RemoteException}
|
||||
* on all methods, but in and out parameters have to be serializable.
|
||||
*
|
||||
* <p>The major advantage of RMI, compared to Hessian, is serialization.
|
||||
* Effectively, any serializable Java object can be transported without hassle.
|
||||
* Hessian has its own (de-)serialization mechanisms, but is HTTP-based and thus
|
||||
* much easier to setup than RMI. Alternatively, consider Spring's HTTP invoker
|
||||
* to combine Java serialization with HTTP-based transport.
|
||||
*
|
||||
* <p>Note: RMI makes a best-effort attempt to obtain the fully qualified host name.
|
||||
* If one cannot be determined, it will fall back and use the IP address. Depending
|
||||
* on your network configuration, in some cases it will resolve the IP to the loopback
|
||||
* address. To ensure that RMI will use the host name bound to the correct network
|
||||
* interface, you should pass the {@code java.rmi.server.hostname} property to the
|
||||
* JVM that will export the registry and/or the service using the "-D" JVM argument.
|
||||
* For example: {@code -Djava.rmi.server.hostname=myserver.com}
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 13.05.2003
|
||||
* @see RmiClientInterceptor
|
||||
* @see RmiProxyFactoryBean
|
||||
* @see java.rmi.Remote
|
||||
* @see java.rmi.RemoteException
|
||||
* @see org.springframework.remoting.caucho.HessianServiceExporter
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
|
||||
* @deprecated as of 5.3 (phasing out serialization-based remoting)
|
||||
*/
|
||||
@Deprecated
|
||||
public class RmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean {
|
||||
|
||||
private String serviceName;
|
||||
|
||||
private int servicePort = 0; // anonymous port
|
||||
|
||||
private RMIClientSocketFactory clientSocketFactory;
|
||||
|
||||
private RMIServerSocketFactory serverSocketFactory;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
private String registryHost;
|
||||
|
||||
private int registryPort = Registry.REGISTRY_PORT;
|
||||
|
||||
private RMIClientSocketFactory registryClientSocketFactory;
|
||||
|
||||
private RMIServerSocketFactory registryServerSocketFactory;
|
||||
|
||||
private boolean alwaysCreateRegistry = false;
|
||||
|
||||
private boolean replaceExistingBinding = true;
|
||||
|
||||
private Remote exportedObject;
|
||||
|
||||
private boolean createdRegistry = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set the name of the exported RMI service,
|
||||
* i.e. {@code rmi://host:port/NAME}
|
||||
*/
|
||||
public void setServiceName(String serviceName) {
|
||||
this.serviceName = serviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the port that the exported RMI service will use.
|
||||
* <p>Default is 0 (anonymous port).
|
||||
*/
|
||||
public void setServicePort(int servicePort) {
|
||||
this.servicePort = servicePort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI client socket factory to use for exporting the service.
|
||||
* <p>If the given object also implements {@code java.rmi.server.RMIServerSocketFactory},
|
||||
* it will automatically be registered as server socket factory too.
|
||||
* @see #setServerSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see UnicastRemoteObject#exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)
|
||||
*/
|
||||
public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) {
|
||||
this.clientSocketFactory = clientSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI server socket factory to use for exporting the service.
|
||||
* <p>Only needs to be specified when the client socket factory does not
|
||||
* implement {@code java.rmi.server.RMIServerSocketFactory} already.
|
||||
* @see #setClientSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see UnicastRemoteObject#exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)
|
||||
*/
|
||||
public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) {
|
||||
this.serverSocketFactory = serverSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the RMI registry to register the exported service with.
|
||||
* Typically used in combination with RmiRegistryFactoryBean.
|
||||
* <p>Alternatively, you can specify all registry properties locally.
|
||||
* This exporter will then try to locate the specified registry,
|
||||
* automatically creating a new local one if appropriate.
|
||||
* <p>Default is a local registry at the default port (1099),
|
||||
* created on the fly if necessary.
|
||||
* @see RmiRegistryFactoryBean
|
||||
* @see #setRegistryHost
|
||||
* @see #setRegistryPort
|
||||
* @see #setRegistryClientSocketFactory
|
||||
* @see #setRegistryServerSocketFactory
|
||||
*/
|
||||
public void setRegistry(Registry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the host of the registry for the exported RMI service,
|
||||
* i.e. {@code rmi://HOST:port/name}
|
||||
* <p>Default is localhost.
|
||||
*/
|
||||
public void setRegistryHost(String registryHost) {
|
||||
this.registryHost = registryHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the port of the registry for the exported RMI service,
|
||||
* i.e. {@code rmi://host:PORT/name}
|
||||
* <p>Default is {@code Registry.REGISTRY_PORT} (1099).
|
||||
* @see java.rmi.registry.Registry#REGISTRY_PORT
|
||||
*/
|
||||
public void setRegistryPort(int registryPort) {
|
||||
this.registryPort = registryPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI client socket factory to use for the RMI registry.
|
||||
* <p>If the given object also implements {@code java.rmi.server.RMIServerSocketFactory},
|
||||
* it will automatically be registered as server socket factory too.
|
||||
* @see #setRegistryServerSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see LocateRegistry#getRegistry(String, int, RMIClientSocketFactory)
|
||||
*/
|
||||
public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) {
|
||||
this.registryClientSocketFactory = registryClientSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom RMI server socket factory to use for the RMI registry.
|
||||
* <p>Only needs to be specified when the client socket factory does not
|
||||
* implement {@code java.rmi.server.RMIServerSocketFactory} already.
|
||||
* @see #setRegistryClientSocketFactory
|
||||
* @see java.rmi.server.RMIClientSocketFactory
|
||||
* @see java.rmi.server.RMIServerSocketFactory
|
||||
* @see LocateRegistry#createRegistry(int, RMIClientSocketFactory, RMIServerSocketFactory)
|
||||
*/
|
||||
public void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory) {
|
||||
this.registryServerSocketFactory = registryServerSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to always create the registry in-process,
|
||||
* not attempting to locate an existing registry at the specified port.
|
||||
* <p>Default is "false". Switch this flag to "true" in order to avoid
|
||||
* the overhead of locating an existing registry when you always
|
||||
* intend to create a new registry in any case.
|
||||
*/
|
||||
public void setAlwaysCreateRegistry(boolean alwaysCreateRegistry) {
|
||||
this.alwaysCreateRegistry = alwaysCreateRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to replace an existing binding in the RMI registry,
|
||||
* that is, whether to simply override an existing binding with the
|
||||
* specified service in case of a naming conflict in the registry.
|
||||
* <p>Default is "true", assuming that an existing binding for this
|
||||
* exporter's service name is an accidental leftover from a previous
|
||||
* execution. Switch this to "false" to make the exporter fail in such
|
||||
* a scenario, indicating that there was already an RMI object bound.
|
||||
*/
|
||||
public void setReplaceExistingBinding(boolean replaceExistingBinding) {
|
||||
this.replaceExistingBinding = replaceExistingBinding;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws RemoteException {
|
||||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize this service exporter, registering the service as RMI object.
|
||||
* <p>Creates an RMI registry on the specified port if none exists.
|
||||
* @throws RemoteException if service registration failed
|
||||
*/
|
||||
public void prepare() throws RemoteException {
|
||||
checkService();
|
||||
|
||||
if (this.serviceName == null) {
|
||||
throw new IllegalArgumentException("Property 'serviceName' is required");
|
||||
}
|
||||
|
||||
// Check socket factories for exported object.
|
||||
if (this.clientSocketFactory instanceof RMIServerSocketFactory) {
|
||||
this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory;
|
||||
}
|
||||
if ((this.clientSocketFactory != null && this.serverSocketFactory == null) ||
|
||||
(this.clientSocketFactory == null && this.serverSocketFactory != null)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Both RMIClientSocketFactory and RMIServerSocketFactory or none required");
|
||||
}
|
||||
|
||||
// Check socket factories for RMI registry.
|
||||
if (this.registryClientSocketFactory instanceof RMIServerSocketFactory) {
|
||||
this.registryServerSocketFactory = (RMIServerSocketFactory) this.registryClientSocketFactory;
|
||||
}
|
||||
if (this.registryClientSocketFactory == null && this.registryServerSocketFactory != null) {
|
||||
throw new IllegalArgumentException(
|
||||
"RMIServerSocketFactory without RMIClientSocketFactory for registry not supported");
|
||||
}
|
||||
|
||||
this.createdRegistry = false;
|
||||
|
||||
// Determine RMI registry to use.
|
||||
if (this.registry == null) {
|
||||
this.registry = getRegistry(this.registryHost, this.registryPort,
|
||||
this.registryClientSocketFactory, this.registryServerSocketFactory);
|
||||
this.createdRegistry = true;
|
||||
}
|
||||
|
||||
// Initialize and cache exported object.
|
||||
this.exportedObject = getObjectToExport();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Binding service '" + this.serviceName + "' to RMI registry: " + this.registry);
|
||||
}
|
||||
|
||||
// Export RMI object.
|
||||
if (this.clientSocketFactory != null) {
|
||||
UnicastRemoteObject.exportObject(
|
||||
this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory);
|
||||
}
|
||||
else {
|
||||
UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort);
|
||||
}
|
||||
|
||||
// Bind RMI object to registry.
|
||||
try {
|
||||
if (this.replaceExistingBinding) {
|
||||
this.registry.rebind(this.serviceName, this.exportedObject);
|
||||
}
|
||||
else {
|
||||
this.registry.bind(this.serviceName, this.exportedObject);
|
||||
}
|
||||
}
|
||||
catch (AlreadyBoundException ex) {
|
||||
// Already an RMI object bound for the specified service name...
|
||||
unexportObjectSilently();
|
||||
throw new IllegalStateException(
|
||||
"Already an RMI object bound for name '" + this.serviceName + "': " + ex.toString());
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
// Registry binding failed: let's unexport the RMI object as well.
|
||||
unexportObjectSilently();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry for this exporter.
|
||||
* @param registryHost the registry host to use (if this is specified,
|
||||
* no implicit creation of a RMI registry will happen)
|
||||
* @param registryPort the registry port to use
|
||||
* @param clientSocketFactory the RMI client socket factory for the registry (if any)
|
||||
* @param serverSocketFactory the RMI server socket factory for the registry (if any)
|
||||
* @return the RMI registry
|
||||
* @throws RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(String registryHost, int registryPort,
|
||||
@Nullable RMIClientSocketFactory clientSocketFactory, @Nullable RMIServerSocketFactory serverSocketFactory)
|
||||
throws RemoteException {
|
||||
|
||||
if (registryHost != null) {
|
||||
// Host explicitly specified: only lookup possible.
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]");
|
||||
}
|
||||
Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
|
||||
else {
|
||||
return getRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry for this exporter.
|
||||
* @param registryPort the registry port to use
|
||||
* @param clientSocketFactory the RMI client socket factory for the registry (if any)
|
||||
* @param serverSocketFactory the RMI server socket factory for the registry (if any)
|
||||
* @return the RMI registry
|
||||
* @throws RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(int registryPort,
|
||||
@Nullable RMIClientSocketFactory clientSocketFactory, @Nullable RMIServerSocketFactory serverSocketFactory)
|
||||
throws RemoteException {
|
||||
|
||||
if (clientSocketFactory != null) {
|
||||
if (this.alwaysCreateRegistry) {
|
||||
logger.debug("Creating new RMI registry");
|
||||
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "', using custom socket factory");
|
||||
}
|
||||
synchronized (LocateRegistry.class) {
|
||||
try {
|
||||
// Retrieve existing registry.
|
||||
Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
logger.trace("RMI registry access threw exception", ex);
|
||||
logger.debug("Could not detect RMI registry - creating new one");
|
||||
// Assume no registry found -> create new one.
|
||||
return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
return getRegistry(registryPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate or create the RMI registry for this exporter.
|
||||
* @param registryPort the registry port to use
|
||||
* @return the RMI registry
|
||||
* @throws RemoteException if the registry couldn't be located or created
|
||||
*/
|
||||
protected Registry getRegistry(int registryPort) throws RemoteException {
|
||||
if (this.alwaysCreateRegistry) {
|
||||
logger.debug("Creating new RMI registry");
|
||||
return LocateRegistry.createRegistry(registryPort);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for RMI registry at port '" + registryPort + "'");
|
||||
}
|
||||
synchronized (LocateRegistry.class) {
|
||||
try {
|
||||
// Retrieve existing registry.
|
||||
Registry reg = LocateRegistry.getRegistry(registryPort);
|
||||
testRegistry(reg);
|
||||
return reg;
|
||||
}
|
||||
catch (RemoteException ex) {
|
||||
logger.trace("RMI registry access threw exception", ex);
|
||||
logger.debug("Could not detect RMI registry - creating new one");
|
||||
// Assume no registry found -> create new one.
|
||||
return LocateRegistry.createRegistry(registryPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the given RMI registry, calling some operation on it to
|
||||
* check whether it is still active.
|
||||
* <p>Default implementation calls {@code Registry.list()}.
|
||||
* @param registry the RMI registry to test
|
||||
* @throws RemoteException if thrown by registry methods
|
||||
* @see java.rmi.registry.Registry#list()
|
||||
*/
|
||||
protected void testRegistry(Registry registry) throws RemoteException {
|
||||
registry.list();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unbind the RMI service from the registry on bean factory shutdown.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() throws RemoteException {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Unbinding RMI service '" + this.serviceName +
|
||||
"' from registry" + (this.createdRegistry ? (" at port '" + this.registryPort + "'") : ""));
|
||||
}
|
||||
try {
|
||||
this.registry.unbind(this.serviceName);
|
||||
}
|
||||
catch (NotBoundException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("RMI service '" + this.serviceName + "' is not bound to registry" +
|
||||
(this.createdRegistry ? (" at port '" + this.registryPort + "' anymore") : ""), ex);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
unexportObjectSilently();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unexport the registered RMI object, logging any exception that arises.
|
||||
*/
|
||||
private void unexportObjectSilently() {
|
||||
try {
|
||||
UnicastRemoteObject.unexportObject(this.exportedObject, true);
|
||||
}
|
||||
catch (NoSuchObjectException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("RMI object for service '" + this.serviceName + "' is not exported anymore", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Remoting classes for conventional RMI and transparent remoting via
|
||||
* RMI invokers. Provides a proxy factory for accessing RMI services,
|
||||
* and an exporter for making beans available to RMI clients.
|
||||
*/
|
||||
package org.springframework.remoting.rmi;
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.soap;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.springframework.remoting.RemoteInvocationFailureException;
|
||||
|
||||
/**
|
||||
* RemoteInvocationFailureException subclass that provides the details
|
||||
* of a SOAP fault.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
* @see javax.xml.rpc.soap.SOAPFaultException
|
||||
* @see javax.xml.ws.soap.SOAPFaultException
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class SoapFaultException extends RemoteInvocationFailureException {
|
||||
|
||||
/**
|
||||
* Constructor for SoapFaultException.
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause from the SOAP API in use
|
||||
*/
|
||||
protected SoapFaultException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the SOAP fault code.
|
||||
*/
|
||||
public abstract String getFaultCode();
|
||||
|
||||
/**
|
||||
* Return the SOAP fault code as a {@code QName} object.
|
||||
*/
|
||||
public abstract QName getFaultCodeAsQName();
|
||||
|
||||
/**
|
||||
* Return the descriptive SOAP fault string.
|
||||
*/
|
||||
public abstract String getFaultString();
|
||||
|
||||
/**
|
||||
* Return the actor that caused this fault.
|
||||
*/
|
||||
public abstract String getFaultActor();
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* SOAP-specific exceptions and support classes for Spring's remoting subsystem.
|
||||
*/
|
||||
package org.springframework.remoting.soap;
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link RemoteInvocationExecutor} interface.
|
||||
* Simply delegates to {@link RemoteInvocation}'s invoke method.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see RemoteInvocation#invoke
|
||||
*/
|
||||
public class DefaultRemoteInvocationExecutor implements RemoteInvocationExecutor {
|
||||
|
||||
@Override
|
||||
public Object invoke(RemoteInvocation invocation, Object targetObject)
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
|
||||
|
||||
Assert.notNull(invocation, "RemoteInvocation must not be null");
|
||||
Assert.notNull(targetObject, "Target object must not be null");
|
||||
return invocation.invoke(targetObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link RemoteInvocationFactory} interface.
|
||||
* Simply creates a new standard {@link RemoteInvocation} object.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
*/
|
||||
public class DefaultRemoteInvocationFactory implements RemoteInvocationFactory {
|
||||
|
||||
@Override
|
||||
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
|
||||
return new RemoteInvocation(methodInvocation);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract base class for classes that access a remote service.
|
||||
* Provides a "serviceInterface" bean property.
|
||||
*
|
||||
* <p>Note that the service interface being used will show some signs of
|
||||
* remotability, like the granularity of method calls that it offers.
|
||||
* Furthermore, it has to have serializable arguments etc.
|
||||
*
|
||||
* <p>Accessors are supposed to throw Spring's generic
|
||||
* {@link org.springframework.remoting.RemoteAccessException} in case
|
||||
* of remote invocation failure, provided that the service interface
|
||||
* does not declare {@code java.rmi.RemoteException}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 13.05.2003
|
||||
* @see org.springframework.remoting.RemoteAccessException
|
||||
* @see java.rmi.RemoteException
|
||||
*/
|
||||
public abstract class RemoteAccessor extends RemotingSupport {
|
||||
|
||||
private Class<?> serviceInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Set the interface of the service to access.
|
||||
* The interface must be suitable for the particular service and remoting strategy.
|
||||
* <p>Typically required to be able to create a suitable service proxy,
|
||||
* but can also be optional if the lookup returns a typed proxy.
|
||||
*/
|
||||
public void setServiceInterface(Class<?> serviceInterface) {
|
||||
Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
|
||||
Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
|
||||
this.serviceInterface = serviceInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the interface of the service to access.
|
||||
*/
|
||||
public Class<?> getServiceInterface() {
|
||||
return this.serviceInterface;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
|
||||
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Abstract base class for classes that export a remote service.
|
||||
* Provides "service" and "serviceInterface" bean properties.
|
||||
*
|
||||
* <p>Note that the service interface being used will show some signs of
|
||||
* remotability, like the granularity of method calls that it offers.
|
||||
* Furthermore, it has to have serializable arguments etc.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 26.12.2003
|
||||
*/
|
||||
public abstract class RemoteExporter extends RemotingSupport {
|
||||
|
||||
private Object service;
|
||||
|
||||
private Class<?> serviceInterface;
|
||||
|
||||
private Boolean registerTraceInterceptor;
|
||||
|
||||
private Object[] interceptors;
|
||||
|
||||
|
||||
/**
|
||||
* Set the service to export.
|
||||
* Typically populated via a bean reference.
|
||||
*/
|
||||
public void setService(Object service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the service to export.
|
||||
*/
|
||||
public Object getService() {
|
||||
return this.service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the interface of the service to export.
|
||||
* The interface must be suitable for the particular service and remoting strategy.
|
||||
*/
|
||||
public void setServiceInterface(Class<?> serviceInterface) {
|
||||
Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
|
||||
Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
|
||||
this.serviceInterface = serviceInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the interface of the service to export.
|
||||
*/
|
||||
public Class<?> getServiceInterface() {
|
||||
return this.serviceInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to register a RemoteInvocationTraceInterceptor for exported
|
||||
* services. Only applied when a subclass uses {@code getProxyForService}
|
||||
* for creating the proxy to expose.
|
||||
* <p>Default is "true". RemoteInvocationTraceInterceptor's most important value
|
||||
* is that it logs exception stacktraces on the server, before propagating an
|
||||
* exception to the client. Note that RemoteInvocationTraceInterceptor will <i>not</i>
|
||||
* be registered by default if the "interceptors" property has been specified.
|
||||
* @see #setInterceptors
|
||||
* @see #getProxyForService
|
||||
* @see RemoteInvocationTraceInterceptor
|
||||
*/
|
||||
public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) {
|
||||
this.registerTraceInterceptor = registerTraceInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set additional interceptors (or advisors) to be applied before the
|
||||
* remote endpoint, e.g. a PerformanceMonitorInterceptor.
|
||||
* <p>You may specify any AOP Alliance MethodInterceptors or other
|
||||
* Spring AOP Advices, as well as Spring AOP Advisors.
|
||||
* @see #getProxyForService
|
||||
* @see org.springframework.aop.interceptor.PerformanceMonitorInterceptor
|
||||
*/
|
||||
public void setInterceptors(Object[] interceptors) {
|
||||
this.interceptors = interceptors;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check whether the service reference has been set.
|
||||
* @see #setService
|
||||
*/
|
||||
protected void checkService() throws IllegalArgumentException {
|
||||
Assert.notNull(getService(), "Property 'service' is required");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a service reference has been set,
|
||||
* and whether it matches the specified service.
|
||||
* @see #setServiceInterface
|
||||
* @see #setService
|
||||
*/
|
||||
protected void checkServiceInterface() throws IllegalArgumentException {
|
||||
Class<?> serviceInterface = getServiceInterface();
|
||||
Assert.notNull(serviceInterface, "Property 'serviceInterface' is required");
|
||||
|
||||
Object service = getService();
|
||||
if (service instanceof String) {
|
||||
throw new IllegalArgumentException("Service [" + service + "] is a String " +
|
||||
"rather than an actual service reference: Have you accidentally specified " +
|
||||
"the service bean name as value instead of as reference?");
|
||||
}
|
||||
if (!serviceInterface.isInstance(service)) {
|
||||
throw new IllegalArgumentException("Service interface [" + serviceInterface.getName() +
|
||||
"] needs to be implemented by service [" + service + "] of class [" +
|
||||
service.getClass().getName() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a proxy for the given service object, implementing the specified
|
||||
* service interface.
|
||||
* <p>Used to export a proxy that does not expose any internals but just
|
||||
* a specific interface intended for remote access. Furthermore, a
|
||||
* {@link RemoteInvocationTraceInterceptor} will be registered (by default).
|
||||
* @return the proxy
|
||||
* @see #setServiceInterface
|
||||
* @see #setRegisterTraceInterceptor
|
||||
* @see RemoteInvocationTraceInterceptor
|
||||
*/
|
||||
protected Object getProxyForService() {
|
||||
checkService();
|
||||
checkServiceInterface();
|
||||
|
||||
ProxyFactory proxyFactory = new ProxyFactory();
|
||||
proxyFactory.addInterface(getServiceInterface());
|
||||
|
||||
if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) {
|
||||
proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
|
||||
}
|
||||
if (this.interceptors != null) {
|
||||
AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
|
||||
for (Object interceptor : this.interceptors) {
|
||||
proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor));
|
||||
}
|
||||
}
|
||||
|
||||
proxyFactory.setTarget(getService());
|
||||
proxyFactory.setOpaque(true);
|
||||
|
||||
return proxyFactory.getProxy(getBeanClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a short name for this exporter.
|
||||
* Used for tracing of remote invocations.
|
||||
* <p>Default is the unqualified class name (without package).
|
||||
* Can be overridden in subclasses.
|
||||
* @see #getProxyForService
|
||||
* @see RemoteInvocationTraceInterceptor
|
||||
* @see org.springframework.util.ClassUtils#getShortName
|
||||
*/
|
||||
protected String getExporterName() {
|
||||
return ClassUtils.getShortName(getClass());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Encapsulates a remote invocation, providing core method invocation properties
|
||||
* in a serializable fashion. Used for RMI and HTTP-based serialization invokers.
|
||||
*
|
||||
* <p>This is an SPI class, typically not used directly by applications.
|
||||
* Can be subclassed for additional invocation parameters.
|
||||
*
|
||||
* <p>Both {@link RemoteInvocation} and {@link RemoteInvocationResult} are designed
|
||||
* for use with standard Java serialization as well as JavaBean-style serialization.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 25.02.2004
|
||||
* @see RemoteInvocationResult
|
||||
* @see RemoteInvocationFactory
|
||||
* @see RemoteInvocationExecutor
|
||||
* @see org.springframework.remoting.rmi.RmiProxyFactoryBean
|
||||
* @see org.springframework.remoting.rmi.RmiServiceExporter
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
|
||||
*/
|
||||
public class RemoteInvocation implements Serializable {
|
||||
|
||||
/** use serialVersionUID from Spring 1.1 for interoperability. */
|
||||
private static final long serialVersionUID = 6876024250231820554L;
|
||||
|
||||
|
||||
private String methodName;
|
||||
|
||||
private Class<?>[] parameterTypes;
|
||||
|
||||
private Object[] arguments;
|
||||
|
||||
private Map<String, Serializable> attributes;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocation for the given AOP method invocation.
|
||||
* @param methodInvocation the AOP invocation to convert
|
||||
*/
|
||||
public RemoteInvocation(MethodInvocation methodInvocation) {
|
||||
this.methodName = methodInvocation.getMethod().getName();
|
||||
this.parameterTypes = methodInvocation.getMethod().getParameterTypes();
|
||||
this.arguments = methodInvocation.getArguments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocation for the given parameters.
|
||||
* @param methodName the name of the method to invoke
|
||||
* @param parameterTypes the parameter types of the method
|
||||
* @param arguments the arguments for the invocation
|
||||
*/
|
||||
public RemoteInvocation(String methodName, Class<?>[] parameterTypes, Object[] arguments) {
|
||||
this.methodName = methodName;
|
||||
this.parameterTypes = parameterTypes;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocation for JavaBean-style deserialization
|
||||
* (e.g. with Jackson).
|
||||
*/
|
||||
public RemoteInvocation() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the name of the target method.
|
||||
* <p>This setter is intended for JavaBean-style deserialization.
|
||||
*/
|
||||
public void setMethodName(String methodName) {
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the target method.
|
||||
*/
|
||||
public String getMethodName() {
|
||||
return this.methodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parameter types of the target method.
|
||||
* <p>This setter is intended for JavaBean-style deserialization.
|
||||
*/
|
||||
public void setParameterTypes(Class<?>[] parameterTypes) {
|
||||
this.parameterTypes = parameterTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parameter types of the target method.
|
||||
*/
|
||||
public Class<?>[] getParameterTypes() {
|
||||
return this.parameterTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the arguments for the target method call.
|
||||
* <p>This setter is intended for JavaBean-style deserialization.
|
||||
*/
|
||||
public void setArguments(Object[] arguments) {
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the arguments for the target method call.
|
||||
*/
|
||||
public Object[] getArguments() {
|
||||
return this.arguments;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add an additional invocation attribute. Useful to add additional
|
||||
* invocation context without having to subclass RemoteInvocation.
|
||||
* <p>Attribute keys have to be unique, and no overriding of existing
|
||||
* attributes is allowed.
|
||||
* <p>The implementation avoids to unnecessarily create the attributes
|
||||
* Map, to minimize serialization size.
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
* @throws IllegalStateException if the key is already bound
|
||||
*/
|
||||
public void addAttribute(String key, Serializable value) throws IllegalStateException {
|
||||
if (this.attributes == null) {
|
||||
this.attributes = new HashMap<>();
|
||||
}
|
||||
if (this.attributes.containsKey(key)) {
|
||||
throw new IllegalStateException("There is already an attribute with key '" + key + "' bound");
|
||||
}
|
||||
this.attributes.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the attribute for the given key, if any.
|
||||
* <p>The implementation avoids to unnecessarily create the attributes
|
||||
* Map, to minimize serialization size.
|
||||
* @param key the attribute key
|
||||
* @return the attribute value, or {@code null} if not defined
|
||||
*/
|
||||
@Nullable
|
||||
public Serializable getAttribute(String key) {
|
||||
if (this.attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return this.attributes.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the attributes Map. Only here for special purposes:
|
||||
* Preferably, use {@link #addAttribute} and {@link #getAttribute}.
|
||||
* @param attributes the attributes Map
|
||||
* @see #addAttribute
|
||||
* @see #getAttribute
|
||||
*/
|
||||
public void setAttributes(@Nullable Map<String, Serializable> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the attributes Map. Mainly here for debugging purposes:
|
||||
* Preferably, use {@link #addAttribute} and {@link #getAttribute}.
|
||||
* @return the attributes Map, or {@code null} if none created
|
||||
* @see #addAttribute
|
||||
* @see #getAttribute
|
||||
*/
|
||||
@Nullable
|
||||
public Map<String, Serializable> getAttributes() {
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform this invocation on the given target object.
|
||||
* Typically called when a RemoteInvocation is received on the server.
|
||||
* @param targetObject the target object to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
* @see java.lang.reflect.Method#invoke
|
||||
*/
|
||||
public Object invoke(Object targetObject)
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
Method method = targetObject.getClass().getMethod(this.methodName, this.parameterTypes);
|
||||
return method.invoke(targetObject, this.arguments);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RemoteInvocation: method name '" + this.methodName + "'; parameter types " +
|
||||
ClassUtils.classNamesToString(this.parameterTypes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Abstract base class for remote service accessors that are based
|
||||
* on serialization of {@link RemoteInvocation} objects.
|
||||
*
|
||||
* Provides a "remoteInvocationFactory" property, with a
|
||||
* {@link DefaultRemoteInvocationFactory} as default strategy.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see #setRemoteInvocationFactory
|
||||
* @see RemoteInvocation
|
||||
* @see RemoteInvocationFactory
|
||||
* @see DefaultRemoteInvocationFactory
|
||||
*/
|
||||
public abstract class RemoteInvocationBasedAccessor extends UrlBasedRemoteAccessor {
|
||||
|
||||
private RemoteInvocationFactory remoteInvocationFactory = new DefaultRemoteInvocationFactory();
|
||||
|
||||
|
||||
/**
|
||||
* Set the RemoteInvocationFactory to use for this accessor.
|
||||
* Default is a {@link DefaultRemoteInvocationFactory}.
|
||||
* <p>A custom invocation factory can add further context information
|
||||
* to the invocation, for example user credentials.
|
||||
*/
|
||||
public void setRemoteInvocationFactory(RemoteInvocationFactory remoteInvocationFactory) {
|
||||
this.remoteInvocationFactory =
|
||||
(remoteInvocationFactory != null ? remoteInvocationFactory : new DefaultRemoteInvocationFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RemoteInvocationFactory used by this accessor.
|
||||
*/
|
||||
public RemoteInvocationFactory getRemoteInvocationFactory() {
|
||||
return this.remoteInvocationFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocation object for the given AOP method invocation.
|
||||
* <p>The default implementation delegates to the configured
|
||||
* {@link #setRemoteInvocationFactory RemoteInvocationFactory}.
|
||||
* This can be overridden in subclasses in order to provide custom RemoteInvocation
|
||||
* subclasses, containing additional invocation parameters (e.g. user credentials).
|
||||
* <p>Note that it is preferable to build a custom RemoteInvocationFactory
|
||||
* as a reusable strategy, instead of overriding this method.
|
||||
* @param methodInvocation the current AOP method invocation
|
||||
* @return the RemoteInvocation object
|
||||
* @see RemoteInvocationFactory#createRemoteInvocation
|
||||
*/
|
||||
protected RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
|
||||
return getRemoteInvocationFactory().createRemoteInvocation(methodInvocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recreate the invocation result contained in the given RemoteInvocationResult object.
|
||||
* <p>The default implementation calls the default {@code recreate()} method.
|
||||
* This can be overridden in subclass to provide custom recreation, potentially
|
||||
* processing the returned result object.
|
||||
* @param result the RemoteInvocationResult to recreate
|
||||
* @return a return value if the invocation result is a successful return
|
||||
* @throws Throwable if the invocation result is an exception
|
||||
* @see RemoteInvocationResult#recreate()
|
||||
*/
|
||||
@Nullable
|
||||
protected Object recreateRemoteInvocationResult(RemoteInvocationResult result) throws Throwable {
|
||||
return result.recreate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Abstract base class for remote service exporters that are based
|
||||
* on deserialization of {@link RemoteInvocation} objects.
|
||||
*
|
||||
* <p>Provides a "remoteInvocationExecutor" property, with a
|
||||
* {@link DefaultRemoteInvocationExecutor} as default strategy.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see RemoteInvocationExecutor
|
||||
* @see DefaultRemoteInvocationExecutor
|
||||
*/
|
||||
public abstract class RemoteInvocationBasedExporter extends RemoteExporter {
|
||||
|
||||
private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor();
|
||||
|
||||
|
||||
/**
|
||||
* Set the RemoteInvocationExecutor to use for this exporter.
|
||||
* Default is a DefaultRemoteInvocationExecutor.
|
||||
* <p>A custom invocation executor can extract further context information
|
||||
* from the invocation, for example user credentials.
|
||||
*/
|
||||
public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) {
|
||||
this.remoteInvocationExecutor = remoteInvocationExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RemoteInvocationExecutor used by this exporter.
|
||||
*/
|
||||
public RemoteInvocationExecutor getRemoteInvocationExecutor() {
|
||||
return this.remoteInvocationExecutor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply the given remote invocation to the given target object.
|
||||
* The default implementation delegates to the RemoteInvocationExecutor.
|
||||
* <p>Can be overridden in subclasses for custom invocation behavior,
|
||||
* possibly for applying additional invocation parameters from a
|
||||
* custom RemoteInvocation subclass. Note that it is preferable to use
|
||||
* a custom RemoteInvocationExecutor which is a reusable strategy.
|
||||
* @param invocation the remote invocation
|
||||
* @param targetObject the target object to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
* @see RemoteInvocationExecutor#invoke
|
||||
*/
|
||||
protected Object invoke(RemoteInvocation invocation, Object targetObject)
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Executing " + invocation);
|
||||
}
|
||||
try {
|
||||
return getRemoteInvocationExecutor().invoke(invocation, targetObject);
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not find target method for " + invocation, ex);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not access target method for " + invocation, ex);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Target method failed for " + invocation, ex.getTargetException());
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given remote invocation to the given target object, wrapping
|
||||
* the invocation result in a serializable RemoteInvocationResult object.
|
||||
* The default implementation creates a plain RemoteInvocationResult.
|
||||
* <p>Can be overridden in subclasses for custom invocation behavior,
|
||||
* for example to return additional context information. Note that this
|
||||
* is not covered by the RemoteInvocationExecutor strategy!
|
||||
* @param invocation the remote invocation
|
||||
* @param targetObject the target object to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @see #invoke
|
||||
*/
|
||||
protected RemoteInvocationResult invokeAndCreateResult(RemoteInvocation invocation, Object targetObject) {
|
||||
try {
|
||||
Object value = invoke(invocation, targetObject);
|
||||
return new RemoteInvocationResult(value);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return new RemoteInvocationResult(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Strategy interface for executing a {@link RemoteInvocation} on a target object.
|
||||
*
|
||||
* <p>Used by {@link org.springframework.remoting.rmi.RmiServiceExporter} (for RMI invokers)
|
||||
* and by {@link org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see DefaultRemoteInvocationFactory
|
||||
* @see org.springframework.remoting.rmi.RmiServiceExporter#setRemoteInvocationExecutor
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter#setRemoteInvocationExecutor
|
||||
*/
|
||||
public interface RemoteInvocationExecutor {
|
||||
|
||||
/**
|
||||
* Perform this invocation on the given target object.
|
||||
* Typically called when a RemoteInvocation is received on the server.
|
||||
* @param invocation the RemoteInvocation
|
||||
* @param targetObject the target object to apply the invocation to
|
||||
* @return the invocation result
|
||||
* @throws NoSuchMethodException if the method name could not be resolved
|
||||
* @throws IllegalAccessException if the method could not be accessed
|
||||
* @throws InvocationTargetException if the method invocation resulted in an exception
|
||||
* @see java.lang.reflect.Method#invoke
|
||||
*/
|
||||
Object invoke(RemoteInvocation invocation, Object targetObject)
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException;
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
/**
|
||||
* Strategy interface for creating a {@link RemoteInvocation} from an AOP Alliance
|
||||
* {@link org.aopalliance.intercept.MethodInvocation}.
|
||||
*
|
||||
* <p>Used by {@link org.springframework.remoting.rmi.RmiClientInterceptor} (for RMI invokers)
|
||||
* and by {@link org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see DefaultRemoteInvocationFactory
|
||||
* @see org.springframework.remoting.rmi.RmiClientInterceptor#setRemoteInvocationFactory
|
||||
* @see org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor#setRemoteInvocationFactory
|
||||
*/
|
||||
public interface RemoteInvocationFactory {
|
||||
|
||||
/**
|
||||
* Create a serializable RemoteInvocation object from the given AOP
|
||||
* MethodInvocation.
|
||||
* <p>Can be implemented to add custom context information to the
|
||||
* remote invocation, for example user credentials.
|
||||
* @param methodInvocation the original AOP MethodInvocation object
|
||||
* @return the RemoteInvocation object
|
||||
*/
|
||||
RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation);
|
||||
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Encapsulates a remote invocation result, holding a result value or an exception.
|
||||
* Used for HTTP-based serialization invokers.
|
||||
*
|
||||
* <p>This is an SPI class, typically not used directly by applications.
|
||||
* Can be subclassed for additional invocation parameters.
|
||||
*
|
||||
* <p>Both {@link RemoteInvocation} and {@link RemoteInvocationResult} are designed
|
||||
* for use with standard Java serialization as well as JavaBean-style serialization.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.1
|
||||
* @see RemoteInvocation
|
||||
*/
|
||||
public class RemoteInvocationResult implements Serializable {
|
||||
|
||||
/** Use serialVersionUID from Spring 1.1 for interoperability. */
|
||||
private static final long serialVersionUID = 2138555143707773549L;
|
||||
|
||||
|
||||
@Nullable
|
||||
private Object value;
|
||||
|
||||
@Nullable
|
||||
private Throwable exception;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocationResult for the given result value.
|
||||
* @param value the result value returned by a successful invocation
|
||||
* of the target method
|
||||
*/
|
||||
public RemoteInvocationResult(@Nullable Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocationResult for the given exception.
|
||||
* @param exception the exception thrown by an unsuccessful invocation
|
||||
* of the target method
|
||||
*/
|
||||
public RemoteInvocationResult(@Nullable Throwable exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocationResult for JavaBean-style deserialization
|
||||
* (e.g. with Jackson).
|
||||
* @see #setValue
|
||||
* @see #setException
|
||||
*/
|
||||
public RemoteInvocationResult() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the result value returned by a successful invocation of the
|
||||
* target method, if any.
|
||||
* <p>This setter is intended for JavaBean-style deserialization.
|
||||
* Use {@link #RemoteInvocationResult(Object)} otherwise.
|
||||
* @see #RemoteInvocationResult()
|
||||
*/
|
||||
public void setValue(@Nullable Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the result value returned by a successful invocation
|
||||
* of the target method, if any.
|
||||
* @see #hasException
|
||||
*/
|
||||
@Nullable
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the exception thrown by an unsuccessful invocation of the
|
||||
* target method, if any.
|
||||
* <p>This setter is intended for JavaBean-style deserialization.
|
||||
* Use {@link #RemoteInvocationResult(Throwable)} otherwise.
|
||||
* @see #RemoteInvocationResult()
|
||||
*/
|
||||
public void setException(@Nullable Throwable exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the exception thrown by an unsuccessful invocation
|
||||
* of the target method, if any.
|
||||
* @see #hasException
|
||||
*/
|
||||
@Nullable
|
||||
public Throwable getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether this invocation result holds an exception.
|
||||
* If this returns {@code false}, the result value applies
|
||||
* (even if it is {@code null}).
|
||||
* @see #getValue
|
||||
* @see #getException
|
||||
*/
|
||||
public boolean hasException() {
|
||||
return (this.exception != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether this invocation result holds an InvocationTargetException,
|
||||
* thrown by an invocation of the target method itself.
|
||||
* @see #hasException()
|
||||
*/
|
||||
public boolean hasInvocationTargetException() {
|
||||
return (this.exception instanceof InvocationTargetException);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Recreate the invocation result, either returning the result value
|
||||
* in case of a successful invocation of the target method, or
|
||||
* rethrowing the exception thrown by the target method.
|
||||
* @return the result value, if any
|
||||
* @throws Throwable the exception, if any
|
||||
*/
|
||||
@Nullable
|
||||
public Object recreate() throws Throwable {
|
||||
if (this.exception != null) {
|
||||
Throwable exToThrow = this.exception;
|
||||
if (this.exception instanceof InvocationTargetException) {
|
||||
exToThrow = ((InvocationTargetException) this.exception).getTargetException();
|
||||
}
|
||||
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
|
||||
throw exToThrow;
|
||||
}
|
||||
else {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* AOP Alliance MethodInterceptor for tracing remote invocations.
|
||||
* Automatically applied by RemoteExporter and its subclasses.
|
||||
*
|
||||
* <p>Logs an incoming remote call as well as the finished processing of a remote call
|
||||
* at DEBUG level. If the processing of a remote call results in a checked exception,
|
||||
* the exception will get logged at INFO level; if it results in an unchecked
|
||||
* exception (or error), the exception will get logged at WARN level.
|
||||
*
|
||||
* <p>The logging of exceptions is particularly useful to save the stacktrace
|
||||
* information on the server-side rather than just propagating the exception
|
||||
* to the client (who might or might not log it properly).
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2
|
||||
* @see RemoteExporter#setRegisterTraceInterceptor
|
||||
* @see RemoteExporter#getProxyForService
|
||||
*/
|
||||
public class RemoteInvocationTraceInterceptor implements MethodInterceptor {
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(RemoteInvocationTraceInterceptor.class);
|
||||
|
||||
private final String exporterNameClause;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocationTraceInterceptor.
|
||||
*/
|
||||
public RemoteInvocationTraceInterceptor() {
|
||||
this.exporterNameClause = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RemoteInvocationTraceInterceptor.
|
||||
* @param exporterName the name of the remote exporter
|
||||
* (to be used as context information in log messages)
|
||||
*/
|
||||
public RemoteInvocationTraceInterceptor(String exporterName) {
|
||||
this.exporterNameClause = exporterName + " ";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Method method = invocation.getMethod();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Incoming " + this.exporterNameClause + "remote call: " +
|
||||
ClassUtils.getQualifiedMethodName(method));
|
||||
}
|
||||
try {
|
||||
Object retVal = invocation.proceed();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Finished processing of " + this.exporterNameClause + "remote call: " +
|
||||
ClassUtils.getQualifiedMethodName(method));
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (ex instanceof RuntimeException || ex instanceof Error) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Processing of " + this.exporterNameClause + "remote call resulted in fatal exception: " +
|
||||
ClassUtils.getQualifiedMethodName(method), ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Processing of " + this.exporterNameClause + "remote call resulted in exception: " +
|
||||
ClassUtils.getQualifiedMethodName(method), ex);
|
||||
}
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* General utilities for handling remote invocations.
|
||||
*
|
||||
* <p>Mainly intended for use within the remoting framework.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class RemoteInvocationUtils {
|
||||
|
||||
/**
|
||||
* Fill the current client-side stack trace into the given exception.
|
||||
* <p>The given exception is typically thrown on the server and serialized
|
||||
* as-is, with the client wanting it to contain the client-side portion
|
||||
* of the stack trace as well. What we can do here is to update the
|
||||
* {@code StackTraceElement} array with the current client-side stack
|
||||
* trace, provided that we run on JDK 1.4+.
|
||||
* @param ex the exception to update
|
||||
* @see Throwable#getStackTrace()
|
||||
* @see Throwable#setStackTrace(StackTraceElement[])
|
||||
*/
|
||||
public static void fillInClientStackTraceIfPossible(Throwable ex) {
|
||||
if (ex != null) {
|
||||
StackTraceElement[] clientStack = new Throwable().getStackTrace();
|
||||
Set<Throwable> visitedExceptions = new HashSet<>();
|
||||
Throwable exToUpdate = ex;
|
||||
while (exToUpdate != null && !visitedExceptions.contains(exToUpdate)) {
|
||||
StackTraceElement[] serverStack = exToUpdate.getStackTrace();
|
||||
StackTraceElement[] combinedStack = new StackTraceElement[serverStack.length + clientStack.length];
|
||||
System.arraycopy(serverStack, 0, combinedStack, 0, serverStack.length);
|
||||
System.arraycopy(clientStack, 0, combinedStack, serverStack.length, clientStack.length);
|
||||
exToUpdate.setStackTrace(combinedStack);
|
||||
visitedExceptions.add(exToUpdate);
|
||||
exToUpdate = exToUpdate.getCause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Generic support base class for remote accessor and exporters,
|
||||
* providing common bean ClassLoader handling.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public abstract class RemotingSupport implements BeanClassLoaderAware {
|
||||
|
||||
/** Logger available to subclasses. */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
|
||||
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ClassLoader that this accessor operates in,
|
||||
* to be used for deserializing and for generating proxies.
|
||||
*/
|
||||
protected ClassLoader getBeanClassLoader() {
|
||||
return this.beanClassLoader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Override the thread context ClassLoader with the environment's bean ClassLoader
|
||||
* if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
|
||||
* context ClassLoader already.
|
||||
* @return the original thread context ClassLoader, or {@code null} if not overridden
|
||||
*/
|
||||
@Nullable
|
||||
protected ClassLoader overrideThreadContextClassLoader() {
|
||||
return ClassUtils.overrideThreadContextClassLoader(getBeanClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the original thread context ClassLoader if necessary.
|
||||
* @param original the original thread context ClassLoader,
|
||||
* or {@code null} if not overridden (and hence nothing to reset)
|
||||
*/
|
||||
protected void resetThreadContextClassLoader(@Nullable ClassLoader original) {
|
||||
if (original != null) {
|
||||
Thread.currentThread().setContextClassLoader(original);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import com.sun.net.httpserver.Authenticator;
|
||||
import com.sun.net.httpserver.Filter;
|
||||
import com.sun.net.httpserver.HttpContext;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} that creates a simple
|
||||
* HTTP server, based on the HTTP server that is included in Sun's JRE 1.6.
|
||||
* Starts the HTTP server on initialization and stops it on destruction.
|
||||
* Exposes the resulting {@link com.sun.net.httpserver.HttpServer} object.
|
||||
*
|
||||
* <p>Allows for registering {@link com.sun.net.httpserver.HttpHandler HttpHandlers}
|
||||
* for specific {@link #setContexts context paths}. Alternatively,
|
||||
* register such context-specific handlers programmatically on the
|
||||
* {@link com.sun.net.httpserver.HttpServer} itself.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.5.1
|
||||
* @see #setPort
|
||||
* @see #setContexts
|
||||
* @deprecated as of Spring Framework 5.1, in favor of embedded Tomcat/Jetty/Undertow
|
||||
*/
|
||||
@Deprecated
|
||||
@org.springframework.lang.UsesSunHttpServer
|
||||
public class SimpleHttpServerFactoryBean implements FactoryBean<HttpServer>, InitializingBean, DisposableBean {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private int port = 8080;
|
||||
|
||||
private String hostname;
|
||||
|
||||
private int backlog = -1;
|
||||
|
||||
private int shutdownDelay = 0;
|
||||
|
||||
private Executor executor;
|
||||
|
||||
private Map<String, HttpHandler> contexts;
|
||||
|
||||
private List<Filter> filters;
|
||||
|
||||
private Authenticator authenticator;
|
||||
|
||||
private HttpServer server;
|
||||
|
||||
|
||||
/**
|
||||
* Specify the HTTP server's port. Default is 8080.
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the HTTP server's hostname to bind to. Default is localhost;
|
||||
* can be overridden with a specific network address to bind to.
|
||||
*/
|
||||
public void setHostname(String hostname) {
|
||||
this.hostname = hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the HTTP server's TCP backlog. Default is -1,
|
||||
* indicating the system's default value.
|
||||
*/
|
||||
public void setBacklog(int backlog) {
|
||||
this.backlog = backlog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the number of seconds to wait until HTTP exchanges have
|
||||
* completed when shutting down the HTTP server. Default is 0.
|
||||
*/
|
||||
public void setShutdownDelay(int shutdownDelay) {
|
||||
this.shutdownDelay = shutdownDelay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the JDK concurrent executor to use for dispatching incoming requests.
|
||||
* @see com.sun.net.httpserver.HttpServer#setExecutor
|
||||
*/
|
||||
public void setExecutor(Executor executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register {@link com.sun.net.httpserver.HttpHandler HttpHandlers}
|
||||
* for specific context paths.
|
||||
* @param contexts a Map with context paths as keys and HttpHandler
|
||||
* objects as values
|
||||
* @see org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter
|
||||
* @see org.springframework.remoting.caucho.SimpleHessianServiceExporter
|
||||
*/
|
||||
public void setContexts(Map<String, HttpHandler> contexts) {
|
||||
this.contexts = contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register common {@link com.sun.net.httpserver.Filter Filters} to be
|
||||
* applied to all locally registered {@link #setContexts contexts}.
|
||||
*/
|
||||
public void setFilters(List<Filter> filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a common {@link com.sun.net.httpserver.Authenticator} to be
|
||||
* applied to all locally registered {@link #setContexts contexts}.
|
||||
*/
|
||||
public void setAuthenticator(Authenticator authenticator) {
|
||||
this.authenticator = authenticator;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws IOException {
|
||||
InetSocketAddress address = (this.hostname != null ?
|
||||
new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
|
||||
this.server = HttpServer.create(address, this.backlog);
|
||||
if (this.executor != null) {
|
||||
this.server.setExecutor(this.executor);
|
||||
}
|
||||
if (this.contexts != null) {
|
||||
this.contexts.forEach((key, context) -> {
|
||||
HttpContext httpContext = this.server.createContext(key, context);
|
||||
if (this.filters != null) {
|
||||
httpContext.getFilters().addAll(this.filters);
|
||||
}
|
||||
if (this.authenticator != null) {
|
||||
httpContext.setAuthenticator(this.authenticator);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Starting HttpServer at address " + address);
|
||||
}
|
||||
this.server.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpServer getObject() {
|
||||
return this.server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HttpServer> getObjectType() {
|
||||
return (this.server != null ? this.server.getClass() : HttpServer.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
logger.info("Stopping HttpServer");
|
||||
this.server.stop(this.shutdownDelay);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.remoting.support;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* Abstract base class for classes that access remote services via URLs.
|
||||
* Provides a "serviceUrl" bean property, which is considered as required.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 15.12.2003
|
||||
*/
|
||||
public abstract class UrlBasedRemoteAccessor extends RemoteAccessor implements InitializingBean {
|
||||
|
||||
private String serviceUrl;
|
||||
|
||||
|
||||
/**
|
||||
* Set the URL of this remote accessor's target service.
|
||||
* The URL must be compatible with the rules of the particular remoting provider.
|
||||
*/
|
||||
public void setServiceUrl(String serviceUrl) {
|
||||
this.serviceUrl = serviceUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the URL of this remote accessor's target service.
|
||||
*/
|
||||
public String getServiceUrl() {
|
||||
return this.serviceUrl;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (getServiceUrl() == null) {
|
||||
throw new IllegalArgumentException("Property 'serviceUrl' is required");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Generic support classes for remoting implementations.
|
||||
* Provides abstract base classes for remote proxy factories.
|
||||
*/
|
||||
package org.springframework.remoting.support;
|
||||
Reference in New Issue
Block a user