Skip to content

Commit 1fc9f58

Browse files
committed
Refactor JMX remote RMI registry creation
1 parent d7f64c6 commit 1fc9f58

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-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.util.HashMap;
@@ -417,18 +418,6 @@ private JMXConnectorServer createServer(String serverName,
417418
RMIClientSocketFactory registryCsf, RMIServerSocketFactory registrySsf,
418419
RMIClientSocketFactory serverCsf, RMIServerSocketFactory serverSsf) {
419420

420-
// Create the RMI registry
421-
Registry registry;
422-
try {
423-
registry = LocateRegistry.createRegistry(
424-
theRmiRegistryPort, registryCsf, registrySsf);
425-
} catch (RemoteException e) {
426-
log.error(sm.getString(
427-
"jmxRemoteLifecycleListener.createRegistryFailed",
428-
serverName, Integer.toString(theRmiRegistryPort)), e);
429-
return null;
430-
}
431-
432421
if (bindAddress == null) {
433422
bindAddress = "localhost";
434423
}
@@ -449,11 +438,20 @@ private JMXConnectorServer createServer(String serverName,
449438
cs = new RMIConnectorServer(serviceUrl, theEnv, server,
450439
ManagementFactory.getPlatformMBeanServer());
451440
cs.start();
452-
registry.bind("jmxrmi", server.toStub());
441+
Remote jmxServer = server.toStub();
442+
// Create the RMI registry
443+
try {
444+
new JmxRegistry(theRmiRegistryPort, registryCsf, registrySsf, "jmxrmi", jmxServer);
445+
} catch (RemoteException e) {
446+
log.error(sm.getString(
447+
"jmxRemoteLifecycleListener.createRegistryFailed",
448+
serverName, Integer.toString(theRmiRegistryPort)), e);
449+
return null;
450+
}
453451
log.info(sm.getString("jmxRemoteLifecycleListener.start",
454452
Integer.toString(theRmiRegistryPort),
455453
Integer.toString(theRmiServerPort), serverName));
456-
} catch (IOException | AlreadyBoundException e) {
454+
} catch (IOException e) {
457455
log.error(sm.getString(
458456
"jmxRemoteLifecycleListener.createServerFailed",
459457
serverName), e);
@@ -589,4 +587,39 @@ public boolean equals(Object obj) {
589587
return true;
590588
}
591589
}
590+
591+
592+
private static class JmxRegistry extends sun.rmi.registry.RegistryImpl {
593+
private static final long serialVersionUID = -3772054804656428217L;
594+
private final String jmxName;
595+
private final Remote jmxServer;
596+
public JmxRegistry(int port, RMIClientSocketFactory csf,
597+
RMIServerSocketFactory ssf, String jmxName, Remote jmxServer) throws RemoteException {
598+
super(port, csf, ssf);
599+
this.jmxName = jmxName;
600+
this.jmxServer = jmxServer;
601+
}
602+
@Override
603+
public Remote lookup(String name)
604+
throws RemoteException, NotBoundException {
605+
return (jmxName.equals(name)) ? jmxServer : null;
606+
}
607+
@Override
608+
public void bind(String name, Remote obj)
609+
throws RemoteException, AlreadyBoundException, AccessException {
610+
}
611+
@Override
612+
public void unbind(String name)
613+
throws RemoteException, NotBoundException, AccessException {
614+
}
615+
@Override
616+
public void rebind(String name, Remote obj)
617+
throws RemoteException, AccessException {
618+
}
619+
@Override
620+
public String[] list() throws RemoteException {
621+
return new String[] { jmxName };
622+
}
623+
}
624+
592625
}

webapps/docs/changelog.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
issues do not "pop up" wrt. others).
4646
-->
4747
<section name="Tomcat 9.0.29 (markt)" rtext="in development">
48+
<subsection name="Catalina">
49+
<changelog>
50+
<fix>
51+
Refactor JMX remote RMI registry creation. (remm)
52+
</fix>
53+
</changelog>
54+
</subsection>
4855
</section>
4956
<section name="Tomcat 9.0.28 (markt)" rtext="release in progress">
5057
<subsection name="Catalina">

0 commit comments

Comments
 (0)