1
1
/*
2
- * Copyright 2002-2008 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .lang .reflect .InvocationTargetException ;
20
20
import java .lang .reflect .Method ;
21
21
import java .rmi .RemoteException ;
22
-
22
+ import javax . naming . Context ;
23
23
import javax .naming .NamingException ;
24
24
import javax .rmi .PortableRemoteObject ;
25
25
54
54
* Spring's unchecked RemoteAccessException.
55
55
*
56
56
* <p>The JNDI environment can be specified as "jndiEnvironment" property,
57
- * or be configured in a < code> jndi.properties</code> file or as system properties.
57
+ * or be configured in a {@ code jndi.properties} file or as system properties.
58
58
* For example:
59
59
*
60
60
* <pre class="code"><property name="jndiEnvironment">
@@ -88,6 +88,8 @@ public class JndiRmiClientInterceptor extends JndiObjectLocator implements Metho
88
88
89
89
private boolean refreshStubOnConnectFailure = false ;
90
90
91
+ private boolean exposeAccessContext = false ;
92
+
91
93
private Object cachedStub ;
92
94
93
95
private final Object stubMonitor = new Object ();
@@ -166,6 +168,18 @@ public void setRefreshStubOnConnectFailure(boolean refreshStubOnConnectFailure)
166
168
this .refreshStubOnConnectFailure = refreshStubOnConnectFailure ;
167
169
}
168
170
171
+ /**
172
+ * Set whether to expose the JNDI environment context for all access to the target
173
+ * RMI stub, i.e. for all method invocations on the exposed object reference.
174
+ * <p>Default is "false", i.e. to only expose the JNDI context for object lookup.
175
+ * Switch this flag to "true" in order to expose the JNDI environment (including
176
+ * the authorization context) for each RMI invocation, as needed by WebLogic
177
+ * for RMI stubs with authorization requirements.
178
+ */
179
+ public void setExposeAccessContext (boolean exposeAccessContext ) {
180
+ this .exposeAccessContext = exposeAccessContext ;
181
+ }
182
+
169
183
170
184
@ Override
171
185
public void afterPropertiesSet () throws NamingException {
@@ -190,8 +204,8 @@ public void prepare() throws RemoteLookupFailureException {
190
204
else if (getServiceInterface () != null ) {
191
205
boolean isImpl = getServiceInterface ().isInstance (remoteObj );
192
206
logger .debug ("Using service interface [" + getServiceInterface ().getName () +
193
- "] for JNDI RMI object [" + getJndiName () + "] - " +
194
- (!isImpl ? "not " : "" ) + "directly implemented" );
207
+ "] for JNDI RMI object [" + getJndiName () + "] - " +
208
+ (!isImpl ? "not " : "" ) + "directly implemented" );
195
209
}
196
210
}
197
211
if (this .cacheStub ) {
@@ -268,13 +282,15 @@ protected Object getStub() throws NamingException, RemoteLookupFailureException
268
282
* @see java.rmi.NoSuchObjectException
269
283
*/
270
284
public Object invoke (MethodInvocation invocation ) throws Throwable {
271
- Object stub = null ;
285
+ Object stub ;
272
286
try {
273
287
stub = getStub ();
274
288
}
275
289
catch (NamingException ex ) {
276
290
throw new RemoteLookupFailureException ("JNDI lookup for RMI service [" + getJndiName () + "] failed" , ex );
277
291
}
292
+
293
+ Context ctx = (this .exposeAccessContext ? getJndiTemplate ().getContext () : null );
278
294
try {
279
295
return doInvoke (invocation , stub );
280
296
}
@@ -297,6 +313,9 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
297
313
throw ex ;
298
314
}
299
315
}
316
+ finally {
317
+ getJndiTemplate ().releaseContext (ctx );
318
+ }
300
319
}
301
320
302
321
/**
@@ -354,7 +373,7 @@ else if (logger.isWarnEnabled()) {
354
373
* @see #invoke
355
374
*/
356
375
protected Object refreshAndRetry (MethodInvocation invocation ) throws Throwable {
357
- Object freshStub = null ;
376
+ Object freshStub ;
358
377
synchronized (this .stubMonitor ) {
359
378
this .cachedStub = null ;
360
379
freshStub = lookupStub ();
@@ -426,7 +445,7 @@ else if (targetEx instanceof SystemException) {
426
445
* @see org.springframework.remoting.support.RemoteInvocation
427
446
*/
428
447
protected Object doInvoke (MethodInvocation methodInvocation , RmiInvocationHandler invocationHandler )
429
- throws RemoteException , NoSuchMethodException , IllegalAccessException , InvocationTargetException {
448
+ throws RemoteException , NoSuchMethodException , IllegalAccessException , InvocationTargetException {
430
449
431
450
if (AopUtils .isToStringMethod (methodInvocation .getMethod ())) {
432
451
return "RMI invoker proxy for service URL [" + getJndiName () + "]" ;
0 commit comments