Refactor JMX remote RMI registry creation · apache/tomcat@1fc9f58 · GitHub
Skip to content

Commit

Permalink
Refactor JMX remote RMI registry creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmaucher committed Nov 14, 2019
1 parent d7f64c6 commit 1fc9f58
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
65 changes: 49 additions & 16 deletions java/org/apache/catalina/mbeans/JmxRemoteLifecycleListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.util.HashMap;
Expand Down Expand Up @@ -417,18 +418,6 @@ private JMXConnectorServer createServer(String serverName,
RMIClientSocketFactory registryCsf, RMIServerSocketFactory registrySsf,
RMIClientSocketFactory serverCsf, RMIServerSocketFactory serverSsf) {

// Create the RMI registry
Registry registry;
try {
registry = LocateRegistry.createRegistry(
theRmiRegistryPort, registryCsf, registrySsf);
} catch (RemoteException e) {
log.error(sm.getString(
"jmxRemoteLifecycleListener.createRegistryFailed",
serverName, Integer.toString(theRmiRegistryPort)), e);
return null;
}

if (bindAddress == null) {
bindAddress = "localhost";
}
Expand All @@ -449,11 +438,20 @@ private JMXConnectorServer createServer(String serverName,
cs = new RMIConnectorServer(serviceUrl, theEnv, server,
ManagementFactory.getPlatformMBeanServer());
cs.start();
registry.bind("jmxrmi", server.toStub());
Remote jmxServer = server.toStub();
// Create the RMI registry
try {
new JmxRegistry(theRmiRegistryPort, registryCsf, registrySsf, "jmxrmi", jmxServer);
} catch (RemoteException e) {
log.error(sm.getString(
"jmxRemoteLifecycleListener.createRegistryFailed",
serverName, Integer.toString(theRmiRegistryPort)), e);
return null;
}
log.info(sm.getString("jmxRemoteLifecycleListener.start",
Integer.toString(theRmiRegistryPort),
Integer.toString(theRmiServerPort), serverName));
} catch (IOException | AlreadyBoundException e) {
} catch (IOException e) {
log.error(sm.getString(
"jmxRemoteLifecycleListener.createServerFailed",
serverName), e);
Expand Down Expand Up @@ -589,4 +587,39 @@ public boolean equals(Object obj) {
return true;
}
}


private static class JmxRegistry extends sun.rmi.registry.RegistryImpl {
private static final long serialVersionUID = -3772054804656428217L;
private final String jmxName;
private final Remote jmxServer;
public JmxRegistry(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf, String jmxName, Remote jmxServer) throws RemoteException {
super(port, csf, ssf);
this.jmxName = jmxName;
this.jmxServer = jmxServer;
}
@Override
public Remote lookup(String name)
throws RemoteException, NotBoundException {
return (jmxName.equals(name)) ? jmxServer : null;
}
@Override
public void bind(String name, Remote obj)
throws RemoteException, AlreadyBoundException, AccessException {
}
@Override
public void unbind(String name)
throws RemoteException, NotBoundException, AccessException {
}
@Override
public void rebind(String name, Remote obj)
throws RemoteException, AccessException {
}
@Override
public String[] list() throws RemoteException {
return new String[] { jmxName };
}
}

}
7 changes: 7 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
issues do not "pop up" wrt. others).
-->
<section name="Tomcat 9.0.29 (markt)" rtext="in development">
<subsection name="Catalina">
<changelog>
<fix>
Refactor JMX remote RMI registry creation. (remm)
</fix>
</changelog>
</subsection>
</section>
<section name="Tomcat 9.0.28 (markt)" rtext="release in progress">
<subsection name="Catalina">
Expand Down

0 comments on commit 1fc9f58

Please sign in to comment.