Merge pull request #723 from rmannibucau/fix/compilation-sun.rmi.registry.RegistryImpl

Ensure to not depend on sun.rmi.registry.RegistryImpl
This commit is contained in:
Jean-Baptiste Onofré 2021-11-06 07:05:58 +01:00 committed by GitHub
commit 0b8e9f9768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 35 deletions

View File

@ -18,12 +18,13 @@ package org.apache.activemq.broker.jmx;
import java.io.IOException; import java.io.IOException;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.rmi.AlreadyBoundException; import java.lang.reflect.Proxy;
import java.rmi.NoSuchObjectException; import java.rmi.NoSuchObjectException;
import java.rmi.NotBoundException;
import java.rmi.Remote; import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject; import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList; import java.util.LinkedList;
@ -55,6 +56,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.MDC; import org.slf4j.MDC;
import static java.lang.ClassLoader.getSystemClassLoader;
/** /**
* An abstraction over JMX MBean registration * An abstraction over JMX MBean registration
* *
@ -538,7 +541,7 @@ public class ManagementContext implements Service {
try { try {
if (registry == null) { if (registry == null) {
LOG.debug("Creating RMIRegistry on port {}", connectorPort); LOG.debug("Creating RMIRegistry on port {}", connectorPort);
registry = new JmxRegistry(connectorPort); registry = jmxRegistry(connectorPort);
} }
namingServiceObjectName = ObjectName.getInstance("naming:type=rmiregistry"); namingServiceObjectName = ObjectName.getInstance("naming:type=rmiregistry");
@ -667,37 +670,32 @@ public class ManagementContext implements Service {
return suppressMBean; return suppressMBean;
} }
/* // do not use sun.rmi.registry.RegistryImpl! it is not always easily available
* Better to use the internal API than re-invent the wheel. private Registry jmxRegistry(final int port) throws RemoteException {
*/ final var loader = Thread.currentThread().getContextClassLoader();
@SuppressWarnings("restriction") final var delegate = LocateRegistry.createRegistry(port);
private class JmxRegistry extends sun.rmi.registry.RegistryImpl { return Registry.class.cast(Proxy.newProxyInstance(
loader == null ? getSystemClassLoader() : loader,
new Class<?>[]{Registry.class}, (proxy, method, args) -> {
public JmxRegistry(int port) throws RemoteException { final var name = method.getName();
super(port); if ("lookup".equals(name) &&
method.getParameterCount() == 1 &&
method.getParameterTypes()[0] == String.class) {
return lookupName.equals(args[0]) ? serverStub : null;
} }
switch (name) {
@Override case "bind":
public Remote lookup(String s) throws RemoteException, NotBoundException { case "unbind":
return lookupName.equals(s) ? serverStub : null; case "rebind":
} return null;
case "list":
@Override
public void bind(String s, Remote remote) throws RemoteException, AlreadyBoundException {
}
@Override
public void unbind(String s) throws RemoteException {
}
@Override
public void rebind(String s, Remote remote) throws RemoteException {
}
@Override
public String[] list() throws RemoteException {
return new String[] {lookupName}; return new String[] {lookupName};
} }
try {
return method.invoke(delegate, args);
} catch (final InvocationTargetException ite) {
throw ite.getTargetException();
}
}));
} }
} }