public class

JndiRmiClientInterceptor

extends JndiObjectLocator
implements InitializingBean
java.lang.Object
   ↳ org.springframework.jndi.JndiAccessor
     ↳ org.springframework.jndi.JndiLocatorSupport
       ↳ org.springframework.jndi.JndiObjectLocator
         ↳ org.springframework.remoting.rmi.JndiRmiClientInterceptor
Known Direct Subclasses

Class Overview

org.aopalliance.intercept.MethodInterceptor for accessing RMI services from JNDI. 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 PortableRemoteObject.

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.

The JNDI environment can be specified as "jndiEnvironment" property, or be configured in a jndi.properties file or as system properties. For example:

<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>

Summary

[Expand]
Inherited Constants
From class org.springframework.jndi.JndiLocatorSupport
[Expand]
Inherited Fields
From class org.springframework.jndi.JndiAccessor
Public Constructors
JndiRmiClientInterceptor()
Public Methods
void afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
RemoteInvocationFactory getRemoteInvocationFactory()
Return the RemoteInvocationFactory used by this accessor.
Class getServiceInterface()
Return the interface of the service to access.
Object invoke(MethodInvocation invocation)
Fetches an RMI stub and delegates to doInvoke(MethodInvocation, Object).
void prepare()
Fetches the RMI stub on startup, if necessary.
void setCacheStub(boolean cacheStub)
Set whether to cache the RMI stub once it has been located.
void setLookupStubOnStartup(boolean lookupStubOnStartup)
Set whether to look up the RMI stub on startup.
void setRefreshStubOnConnectFailure(boolean refreshStubOnConnectFailure)
Set whether to refresh the RMI stub on connect failure.
void setRemoteInvocationFactory(RemoteInvocationFactory remoteInvocationFactory)
Set the RemoteInvocationFactory to use for this accessor.
void setServiceInterface(Class serviceInterface)
Set the interface of the service to access.
Protected Methods
RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation)
Create a new RemoteInvocation object for the given AOP method invocation.
Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler)
Apply the given AOP method invocation to the given RmiInvocationHandler.
Object doInvoke(MethodInvocation invocation, Object stub)
Perform the given invocation on the given RMI stub.
Object getStub()
Return the RMI stub to use.
boolean isConnectFailure(RemoteException ex)
Determine whether the given RMI exception indicates a connect failure.
boolean isConnectFailure(SystemException ex)
Determine whether the given CORBA exception indicates a connect failure.
Object lookupStub()
Create the RMI stub, typically by looking it up.
Object refreshAndRetry(MethodInvocation invocation)
Refresh the RMI stub and retry the given invocation.
[Expand]
Inherited Methods
From class org.springframework.jndi.JndiObjectLocator
From class org.springframework.jndi.JndiLocatorSupport
From class org.springframework.jndi.JndiAccessor
From class java.lang.Object
From interface org.springframework.beans.factory.InitializingBean

Public Constructors

public JndiRmiClientInterceptor ()

Public Methods

public void afterPropertiesSet ()

Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

public RemoteInvocationFactory getRemoteInvocationFactory ()

Return the RemoteInvocationFactory used by this accessor.

public Class getServiceInterface ()

Return the interface of the service to access.

public Object invoke (MethodInvocation invocation)

Fetches an RMI stub and delegates to doInvoke(MethodInvocation, Object). If configured to refresh on connect failure, it will call refreshAndRetry(MethodInvocation) on corresponding RMI exceptions.

Throws
Throwable
See Also

public void prepare ()

Fetches the RMI stub on startup, if necessary.

Throws
RemoteLookupFailureException if RMI stub creation failed

public void setCacheStub (boolean cacheStub)

Set whether to cache the RMI stub once it has been located. Default is "true".

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.

public void setLookupStubOnStartup (boolean lookupStubOnStartup)

Set whether to look up the RMI stub on startup. Default is "true".

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.

public void setRefreshStubOnConnectFailure (boolean refreshStubOnConnectFailure)

Set whether to refresh the RMI stub on connect failure. Default is "false".

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 Also
  • java.rmi.ConnectException
  • java.rmi.ConnectIOException
  • java.rmi.NoSuchObjectException

public void setRemoteInvocationFactory (RemoteInvocationFactory remoteInvocationFactory)

Set the RemoteInvocationFactory to use for this accessor. Default is a DefaultRemoteInvocationFactory.

A custom invocation factory can add further context information to the invocation, for example user credentials.

public void setServiceInterface (Class serviceInterface)

Set the interface of the service to access. The interface must be suitable for the particular service and remoting tool.

Typically required to be able to create a suitable service proxy, but can also be optional if the lookup returns a typed stub.

Protected Methods

protected RemoteInvocation createRemoteInvocation (MethodInvocation methodInvocation)

Create a new RemoteInvocation object for the given AOP method invocation.

The default implementation delegates to the configured RemoteInvocationFactory. This can be overridden in subclasses in order to provide custom RemoteInvocation subclasses, containing additional invocation parameters (e.g. user credentials).

Note that it is preferable to build a custom RemoteInvocationFactory as a reusable strategy, instead of overriding this method.

Parameters
methodInvocation the current AOP method invocation
Returns
  • the RemoteInvocation object

protected Object doInvoke (MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler)

Apply the given AOP method invocation to the given RmiInvocationHandler.

The default implementation delegates to createRemoteInvocation(MethodInvocation).

Parameters
methodInvocation the current AOP method invocation
invocationHandler the RmiInvocationHandler to apply the invocation to
Returns
  • the invocation result
Throws
RemoteException in case of communication errors
NoSuchMethodException if the method name could not be resolved
IllegalAccessException if the method could not be accessed
InvocationTargetException if the method invocation resulted in an exception
See Also

protected Object doInvoke (MethodInvocation invocation, Object stub)

Perform the given invocation on the given RMI stub.

Parameters
invocation the AOP method invocation
stub the RMI stub to invoke
Returns
  • the invocation result, if any
Throws
Throwable in case of invocation failure

protected Object getStub ()

Return the RMI stub to use. Called for each invocation.

The default implementation returns the stub created on initialization, if any. Else, it invokes 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.

Returns
  • the RMI stub to use for an invocation
Throws
NamingException if stub creation failed
RemoteLookupFailureException if RMI stub creation failed

protected boolean isConnectFailure (RemoteException ex)

Determine whether the given RMI exception indicates a connect failure.

The default implementation delegates to isConnectFailure(RemoteException).

Parameters
ex the RMI exception to check
Returns
  • whether the exception should be treated as connect failure

protected boolean isConnectFailure (SystemException ex)

Determine whether the given CORBA exception indicates a connect failure.

The default implementation checks for CORBA's org.omg.CORBA.OBJECT_NOT_EXIST exception.

Parameters
ex the RMI exception to check
Returns
  • whether the exception should be treated as connect failure

protected Object lookupStub ()

Create the RMI stub, typically by looking it up.

Called on interceptor initialization if "cacheStub" is "true"; else called for each invocation by getStub().

The default implementation retrieves the service from the JNDI environment. This can be overridden in subclasses.

Returns
  • the RMI stub to store in this interceptor
Throws
RemoteLookupFailureException if RMI stub creation failed

protected Object refreshAndRetry (MethodInvocation invocation)

Refresh the RMI stub and retry the given invocation. Called by invoke on connect failure.

Parameters
invocation the AOP method invocation
Returns
  • the invocation result, if any
Throws
Throwable in case of invocation failure