Skip to content

Commit a91d7db

Browse files
committed
Refactor JMX remote RMI registry creation
1 parent 2b2ed21 commit a91d7db

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

java/org/apache/catalina/mbeans/JmxRemoteLifecycleListener.java

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
import java.net.ServerSocket;
2626
import java.net.Socket;
2727
import java.net.UnknownHostException;
28+
import java.rmi.AccessException;
2829
import java.rmi.AlreadyBoundException;
30+
import java.rmi.NotBoundException;
31+
import java.rmi.Remote;
2932
import java.rmi.RemoteException;
30-
import java.rmi.registry.LocateRegistry;
31-
import java.rmi.registry.Registry;
3233
import java.rmi.server.RMIClientSocketFactory;
3334
import java.rmi.server.RMIServerSocketFactory;
3435
import java.security.NoSuchAlgorithmException;
@@ -300,18 +301,6 @@ private JMXConnectorServer createServer(String serverName,
300301
RMIClientSocketFactory registryCsf, RMIServerSocketFactory registrySsf,
301302
RMIClientSocketFactory serverCsf, RMIServerSocketFactory serverSsf) {
302303

303-
// Create the RMI registry
304-
Registry registry;
305-
try {
306-
registry = LocateRegistry.createRegistry(
307-
theRmiRegistryPort, registryCsf, registrySsf);
308-
} catch (RemoteException e) {
309-
log.error(sm.getString(
310-
"jmxRemoteLifecycleListener.createRegistryFailed",
311-
serverName, Integer.toString(theRmiRegistryPort)), e);
312-
return null;
313-
}
314-
315304
if (bindAddress == null) {
316305
bindAddress = "localhost";
317306
}
@@ -332,11 +321,20 @@ private JMXConnectorServer createServer(String serverName,
332321
cs = new RMIConnectorServer(serviceUrl, theEnv, server,
333322
ManagementFactory.getPlatformMBeanServer());
334323
cs.start();
335-
registry.bind("jmxrmi", server.toStub());
324+
Remote jmxServer = server.toStub();
325+
// Create the RMI registry
326+
try {
327+
new JmxRegistry(theRmiRegistryPort, registryCsf, registrySsf, "jmxrmi", jmxServer);
328+
} catch (RemoteException e) {
329+
log.error(sm.getString(
330+
"jmxRemoteLifecycleListener.createRegistryFailed",
331+
serverName, Integer.toString(theRmiRegistryPort)), e);
332+
return null;
333+
}
336334
log.info(sm.getString("jmxRemoteLifecycleListener.start",
337335
Integer.toString(theRmiRegistryPort),
338336
Integer.toString(theRmiServerPort), serverName));
339-
} catch (IOException | AlreadyBoundException e) {
337+
} catch (IOException e) {
340338
log.error(sm.getString(
341339
"jmxRemoteLifecycleListener.createServerFailed",
342340
serverName), e);
@@ -492,4 +490,39 @@ public boolean equals(Object obj) {
492490
return true;
493491
}
494492
}
493+
494+
495+
private static class JmxRegistry extends sun.rmi.registry.RegistryImpl {
496+
private static final long serialVersionUID = -3772054804656428217L;
497+
private final String jmxName;
498+
private final Remote jmxServer;
499+
public JmxRegistry(int port, RMIClientSocketFactory csf,
500+
RMIServerSocketFactory ssf, String jmxName, Remote jmxServer) throws RemoteException {
501+
super(port, csf, ssf);
502+
this.jmxName = jmxName;
503+
this.jmxServer = jmxServer;
504+
}
505+
@Override
506+
public Remote lookup(String name)
507+
throws RemoteException, NotBoundException {
508+
return (jmxName.equals(name)) ? jmxServer : null;
509+
}
510+
@Override
511+
public void bind(String name, Remote obj)
512+
throws RemoteException, AlreadyBoundException, AccessException {
513+
}
514+
@Override
515+
public void unbind(String name)
516+
throws RemoteException, NotBoundException, AccessException {
517+
}
518+
@Override
519+
public void rebind(String name, Remote obj)
520+
throws RemoteException, AccessException {
521+
}
522+
@Override
523+
public String[] list() throws RemoteException {
524+
return new String[] { jmxName };
525+
}
526+
}
527+
495528
}

webapps/docs/changelog.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
the final release. Note that this preview API has deprecated for over a
8686
year and may be removed as soon as the next 8.5.x release. (markt)
8787
</fix>
88+
<fix>
89+
Refactor JMX remote RMI registry creation. (remm)
90+
</fix>
8891
</changelog>
8992
</subsection>
9093
<subsection name="Coyote">

0 commit comments

Comments
 (0)