Ensure regular ObjectName is returned to remote callers

Next patch will introduce new ObjectName implementation that
will use less memory. This new object won't be serializable.
This means it won't be possible to transport it to a remote
caller. We want to keep this new object local to JMX server as well.

This patch makes sure that every ObjectName returned
from APIBeanServer is transformed into a regular ObjectName.

It also makes sure that every ObjectInstance returned from
APIBeanServer has its ObjectName swapped with a regular ObjectName.

Signed-off-by: Piotr Jastrzebski <piotr@scylladb.com>
This commit is contained in:
Piotr Jastrzebski 2018-05-12 18:54:38 +02:00
parent 2c48bab91a
commit 48408dc6a3

View File

@ -5,6 +5,7 @@ import java.net.UnknownHostException;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.AttributeList; import javax.management.AttributeList;
@ -47,37 +48,45 @@ public class APIMBeanServer implements MBeanServer {
this.server = server; this.server = server;
} }
private static ObjectInstance prepareForRemote(final ObjectInstance i) {
return new ObjectInstance(prepareForRemote(i.getObjectName()), i.getClassName());
}
private static ObjectName prepareForRemote(final ObjectName n) {
return ObjectName.getInstance(n);
}
@Override @Override
public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException, public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException,
InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException {
return server.createMBean(className, name); return prepareForRemote(server.createMBean(className, name));
} }
@Override @Override
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName)
throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
NotCompliantMBeanException, InstanceNotFoundException { NotCompliantMBeanException, InstanceNotFoundException {
return server.createMBean(className, name, loaderName); return prepareForRemote(server.createMBean(className, name, loaderName));
} }
@Override @Override
public ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature) public ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature)
throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
NotCompliantMBeanException { NotCompliantMBeanException {
return server.createMBean(className, name, params, signature); return prepareForRemote(server.createMBean(className, name, params, signature));
} }
@Override @Override
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params, public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params,
String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException,
MBeanException, NotCompliantMBeanException, InstanceNotFoundException { MBeanException, NotCompliantMBeanException, InstanceNotFoundException {
return server.createMBean(className, name, loaderName, params, signature); return prepareForRemote(server.createMBean(className, name, loaderName, params, signature));
} }
@Override @Override
public ObjectInstance registerMBean(Object object, ObjectName name) public ObjectInstance registerMBean(Object object, ObjectName name)
throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
return server.registerMBean(object, name); return prepareForRemote(server.registerMBean(object, name));
} }
@Override @Override
@ -88,19 +97,19 @@ public class APIMBeanServer implements MBeanServer {
@Override @Override
public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException { public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException {
checkRegistrations(name); checkRegistrations(name);
return server.getObjectInstance(name); return prepareForRemote(server.getObjectInstance(name));
} }
@Override @Override
public Set<ObjectName> queryNames(ObjectName name, QueryExp query) { public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
checkRegistrations(name); checkRegistrations(name);
return server.queryNames(name, query); return server.queryNames(name, query).stream().map(n -> prepareForRemote(n)).collect(Collectors.toSet());
} }
@Override @Override
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
checkRegistrations(name); checkRegistrations(name);
return server.queryMBeans(name, query); return server.queryMBeans(name, query).stream().map(i -> prepareForRemote(i)).collect(Collectors.toSet());
} }
@Override @Override