diff options
Diffstat (limited to 'libjava/classpath/gnu/javax/management/Server.java')
-rw-r--r-- | libjava/classpath/gnu/javax/management/Server.java | 123 |
1 files changed, 60 insertions, 63 deletions
diff --git a/libjava/classpath/gnu/javax/management/Server.java b/libjava/classpath/gnu/javax/management/Server.java index 8e8d826c00f..e35c3b11745 100644 --- a/libjava/classpath/gnu/javax/management/Server.java +++ b/libjava/classpath/gnu/javax/management/Server.java @@ -48,12 +48,13 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; + import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.ConcurrentHashMap; import javax.management.Attribute; import javax.management.AttributeList; @@ -113,19 +114,20 @@ public class Server /** * The registered beans, represented as a map of * {@link javax.management.ObjectName}s to - * {@link java.lang.Object}s. + * {@link gnu.javax.management.Server.ServerInfo}s. */ - private final Map beans = new HashMap(); + private final ConcurrentHashMap<ObjectName,ServerInfo> beans = + new ConcurrentHashMap<ObjectName,ServerInfo>(); /** * The default domain. */ - private String defaultDomain; + private final String defaultDomain; /** * The outer server. */ - private MBeanServer outer; + private final MBeanServer outer; /** * The class loader repository. @@ -134,9 +136,15 @@ public class Server /** * The map of listener delegates to the true - * listener. + * listener. We wrap this in an inner class + * to delay initialisation until a listener + * is actually added. */ - private Map listeners; + private static class LazyListenersHolder + { + private static final Map<NotificationListener,NotificationListener> listeners = + new ConcurrentHashMap<NotificationListener,NotificationListener>(); + } /** * An MBean that emits notifications when an MBean is registered and @@ -145,7 +153,10 @@ public class Server */ private final MBeanServerDelegate delegate; - static private final AtomicLong sequenceNumber = new AtomicLong(1); + /** + * Provides sequencing for notifications about registrations. + */ + private static final AtomicLong sequenceNumber = new AtomicLong(1); /** * Initialise the delegate name. @@ -274,7 +285,7 @@ public class Server private Object getBean(ObjectName name) throws InstanceNotFoundException { - ServerInfo bean = (ServerInfo) beans.get(name); + ServerInfo bean = beans.get(name); if (bean == null) throw new InstanceNotFoundException("The bean, " + name + ", was not found."); @@ -319,12 +330,10 @@ public class Server if (bean instanceof NotificationBroadcaster) { NotificationBroadcaster bbean = (NotificationBroadcaster) bean; - if (listeners == null) - listeners = new HashMap(); NotificationListener indirection = new ServerNotificationListener(bean, name, listener); bbean.addNotificationListener(indirection, filter, passback); - listeners.put(listener, indirection); + LazyListenersHolder.listeners.put(listener, indirection); } } @@ -671,7 +680,7 @@ public class Server { try { - Class c = getClassLoaderRepository().loadClass(name); + Class<?> c = getClassLoaderRepository().loadClass(name); return new ServerInputStream(new ByteArrayInputStream(data), c.getClassLoader()); } @@ -717,7 +726,7 @@ public class Server { try { - Class c = getClassLoader(loader).loadClass(name); + Class<?> c = getClassLoader(loader).loadClass(name); return new ServerInputStream(new ByteArrayInputStream(data), c.getClassLoader()); } @@ -951,7 +960,6 @@ public class Server return defaultDomain; } - /** * Returns an array containing all the domains used by beans registered * with this server. The ordering of the array is undefined. @@ -975,11 +983,11 @@ public class Server public String[] getDomains() { checkSecurity(null, null, "getDomains"); - Set domains = new HashSet(); - Iterator iterator = beans.keySet().iterator(); + Set<String> domains = new HashSet<String>(); + Iterator<ObjectName> iterator = beans.keySet().iterator(); while (iterator.hasNext()) { - String d = ((ObjectName) iterator.next()).getDomain(); + String d = iterator.next().getDomain(); try { checkSecurity(new ObjectName(d + ":x=x"), null, "getDomains"); @@ -990,7 +998,7 @@ public class Server /* Ignored */ } } - return (String[]) domains.toArray(new String[domains.size()]); + return domains.toArray(new String[domains.size()]); } /** @@ -1077,7 +1085,7 @@ public class Server public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException { - ServerInfo bean = (ServerInfo) beans.get(name); + ServerInfo bean = beans.get(name); if (bean == null) throw new InstanceNotFoundException("The bean, " + name + ", was not found."); @@ -1158,7 +1166,7 @@ public class Server new IllegalArgumentException("The name was null."); throw new RuntimeOperationsException(e); } - Class[] sigTypes = new Class[sig.length]; + Class<?>[] sigTypes = new Class[sig.length]; for (int a = 0; a < sigTypes.length; ++a) { try @@ -1174,7 +1182,7 @@ public class Server } try { - Constructor cons = + Constructor<?> cons = repository.loadClass(name).getConstructor(sigTypes); return cons.newInstance(params); } @@ -1288,7 +1296,7 @@ public class Server throw new RuntimeOperationsException(e); } ClassLoader loader = getClassLoader(loaderName); - Class[] sigTypes = new Class[sig.length]; + Class<?>[] sigTypes = new Class[sig.length]; for (int a = 0; a < sig.length; ++a) { try @@ -1304,7 +1312,7 @@ public class Server } try { - Constructor cons = + Constructor<?> cons = Class.forName(name, true, loader).getConstructor(sigTypes); return cons.newInstance(params); } @@ -1433,10 +1441,10 @@ public class Server } if (info.getClassName().equals(className)) return true; - Class bclass = bean.getClass(); + Class<?> bclass = bean.getClass(); try { - Class oclass = Class.forName(className); + Class<?> oclass = Class.forName(className); return (bclass.getClassLoader().equals(oclass.getClassLoader()) && oclass.isAssignableFrom(bclass)); } @@ -1502,21 +1510,19 @@ public class Server * arise from the execution of the query, in which * case that particular bean will again be excluded. */ - public Set queryMBeans(ObjectName name, QueryExp query) + public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { checkSecurity(name, null, "queryMBeans"); - Set results = new HashSet(); - Iterator iterator = beans.entrySet().iterator(); - while (iterator.hasNext()) + Set<ObjectInstance> results = new HashSet<ObjectInstance>(); + for (Map.Entry<ObjectName,ServerInfo> entry : beans.entrySet()) { - Map.Entry entry = (Map.Entry) iterator.next(); - ObjectName nextName = (ObjectName) entry.getKey(); + ObjectName nextName = entry.getKey(); checkSecurity(name, nextName.toString(), "queryMBeans"); try { if ((name == null || name.apply(nextName)) && (query == null || query.apply(nextName))) - results.add(((ServerInfo) entry.getValue()).getInstance()); + results.add(entry.getValue().getInstance()); } catch (BadStringOperationException e) { @@ -1575,15 +1581,12 @@ public class Server * Note that these permissions are implied if the * <code>queryMBeans</code> permissions are available. */ - public Set queryNames(ObjectName name, QueryExp query) + public Set<ObjectName> queryNames(ObjectName name, QueryExp query) { checkSecurity(name, null, "queryNames"); - Set results = new HashSet(); - Iterator iterator = beans.entrySet().iterator(); - while (iterator.hasNext()) + Set<ObjectName> results = new HashSet<ObjectName>(); + for (ObjectName nextName : beans.keySet()) { - Map.Entry entry = (Map.Entry) iterator.next(); - ObjectName nextName = (ObjectName) entry.getKey(); checkSecurity(name, nextName.toString(), "queryNames"); try { @@ -1656,7 +1659,7 @@ public class Server NotCompliantMBeanException { SecurityManager sm = System.getSecurityManager(); - Class cl = obj.getClass(); + Class<?> cl = obj.getClass(); String className = cl.getName(); if (sm != null) { @@ -1712,14 +1715,13 @@ public class Server throw new MBeanRegistrationException(e, "Pre-registration failed."); } } - if (beans.containsKey(name)) + ObjectInstance obji = new ObjectInstance(name, className); + if (beans.putIfAbsent(name, new ServerInfo(obji, obj)) != null) { if (register != null) register.postRegister(Boolean.FALSE); throw new InstanceAlreadyExistsException(name + "is already registered."); } - ObjectInstance obji = new ObjectInstance(name, className); - beans.put(name, new ServerInfo(obji, obj)); if (register != null) register.postRegister(Boolean.TRUE); notify(name, MBeanServerNotification.REGISTRATION_NOTIFICATION); @@ -1758,15 +1760,8 @@ public class Server if (bean instanceof NotificationBroadcaster) { NotificationBroadcaster bbean = (NotificationBroadcaster) bean; - NotificationListener indirection = (NotificationListener) - listeners.get(listener); - if (indirection == null) - bbean.removeNotificationListener(listener); - else - { - bbean.removeNotificationListener(indirection); - listeners.remove(listener); - } + bbean.removeNotificationListener(listener); + LazyListenersHolder.listeners.remove(listener); } } @@ -1809,15 +1804,8 @@ public class Server if (bean instanceof NotificationEmitter) { NotificationEmitter bbean = (NotificationEmitter) bean; - NotificationListener indirection = (NotificationListener) - listeners.get(listener); - if (indirection == null) - bbean.removeNotificationListener(listener, filter, passback); - else - { - bbean.removeNotificationListener(indirection, filter, passback); - listeners.remove(listener); - } + bbean.removeNotificationListener(listener, filter, passback); + LazyListenersHolder.listeners.remove(listener); } } @@ -2011,7 +1999,7 @@ public class Server Object abean = getBean(name); checkSecurity(name, null, "setAttribute"); AttributeList list = new AttributeList(attributes.size()); - Iterator it = attributes.iterator(); + Iterator<Object> it = attributes.iterator(); while (it.hasNext()) { try @@ -2113,6 +2101,15 @@ public class Server register.postDeregister(); } + /** + * Notifies the delegate of beans being registered + * and unregistered. + * + * @param name the bean being registered. + * @param type the type of notification; + * {@code REGISTRATION_NOTIFICATION} or + * {@code UNREGISTRATION_NOTIFICATION}. + */ private void notify(ObjectName name, String type) { delegate.sendNotification @@ -2136,7 +2133,7 @@ public class Server this.cl = cl; } - protected Class resolveClass(ObjectStreamClass osc) + protected Class<?> resolveClass(ObjectStreamClass osc) throws ClassNotFoundException, IOException { try |