Skip to content

Commit 78681c6

Browse files
committed
JndiRmiServiceExporter still calls PortableRemoteObject when available
Issue: SPR-16670
1 parent 0bc01fc commit 78681c6

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

spring-context/src/main/java/org/springframework/remoting/rmi/JndiRmiServiceExporter.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.remoting.rmi;
1818

19-
import java.rmi.NoSuchObjectException;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
2021
import java.rmi.Remote;
2122
import java.rmi.RemoteException;
2223
import java.util.Properties;
@@ -25,6 +26,8 @@
2526
import org.springframework.beans.factory.DisposableBean;
2627
import org.springframework.beans.factory.InitializingBean;
2728
import org.springframework.jndi.JndiTemplate;
29+
import org.springframework.lang.Nullable;
30+
import org.springframework.util.ReflectionUtils;
2831

2932
/**
3033
* Service exporter which binds RMI services to JNDI.
@@ -66,6 +69,27 @@
6669
*/
6770
public class JndiRmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean {
6871

72+
@Nullable
73+
private static Method exportObject;
74+
75+
@Nullable
76+
private static Method unexportObject;
77+
78+
static {
79+
try {
80+
Class<?> portableRemoteObject =
81+
JndiRmiServiceExporter.class.getClassLoader().loadClass("javax.rmi.PortableRemoteObject");
82+
exportObject = portableRemoteObject.getMethod("exportObject", Remote.class);
83+
unexportObject = portableRemoteObject.getMethod("unexportObject", Remote.class);
84+
}
85+
catch (Throwable ex) {
86+
// java.corba module not available on JDK 9+
87+
exportObject = null;
88+
unexportObject = null;
89+
}
90+
}
91+
92+
6993
private JndiTemplate jndiTemplate = new JndiTemplate();
7094

7195
private String jndiName;
@@ -116,6 +140,7 @@ public void prepare() throws NamingException, RemoteException {
116140

117141
// Initialize and cache exported object.
118142
this.exportedObject = getObjectToExport();
143+
invokePortableRemoteObject(exportObject);
119144

120145
rebind();
121146
}
@@ -136,11 +161,31 @@ public void rebind() throws NamingException {
136161
* Unbind the RMI service from JNDI on bean factory shutdown.
137162
*/
138163
@Override
139-
public void destroy() throws NamingException, NoSuchObjectException {
164+
public void destroy() throws NamingException, RemoteException {
140165
if (logger.isInfoEnabled()) {
141166
logger.info("Unbinding RMI service from JNDI location [" + this.jndiName + "]");
142167
}
143168
this.jndiTemplate.unbind(this.jndiName);
169+
invokePortableRemoteObject(unexportObject);
170+
}
171+
172+
173+
private void invokePortableRemoteObject(@Nullable Method method) throws RemoteException {
174+
if (method != null) {
175+
try {
176+
method.invoke(null, this.exportedObject);
177+
}
178+
catch (InvocationTargetException ex) {
179+
Throwable targetEx = ex.getTargetException();
180+
if (targetEx instanceof RemoteException) {
181+
throw (RemoteException) targetEx;
182+
}
183+
ReflectionUtils.rethrowRuntimeException(targetEx);
184+
}
185+
catch (Throwable ex) {
186+
throw new IllegalStateException("PortableRemoteObject invocation failed", ex);
187+
}
188+
}
144189
}
145190

146191
}

0 commit comments

Comments
 (0)