6844054: (bf) Eliminate dependency on javax.management.ObjectName
authoralanb
Sat, 27 Jun 2009 21:49:41 +0100
changeset 3066 cd3861104f4d
parent 3065 452aaa2899fc
child 3068 f024ac2a48d1
6844054: (bf) Eliminate dependency on javax.management.ObjectName Reviewed-by: mchung
jdk/src/share/classes/java/lang/management/PlatformComponent.java
jdk/src/share/classes/java/nio/Bits.java
jdk/src/share/classes/java/nio/Direct-X-Buffer.java
jdk/src/share/classes/sun/management/ManagementFactoryHelper.java
jdk/src/share/classes/sun/misc/JavaNioAccess.java
jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
--- a/jdk/src/share/classes/java/lang/management/PlatformComponent.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/java/lang/management/PlatformComponent.java	Sat Jun 27 21:49:41 2009 +0100
@@ -34,7 +34,6 @@
 import java.util.logging.LogManager;
 import java.nio.BufferPoolMXBean;
 import javax.management.MBeanServerConnection;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 
 import com.sun.management.HotSpotDiagnosticMXBean;
@@ -198,10 +197,7 @@
         "java.nio", "BufferPool", keyProperties("name"),
         new MXBeanFetcher<BufferPoolMXBean>() {
             public List<BufferPoolMXBean> getMXBeans() {
-                List<BufferPoolMXBean> pools = new ArrayList<BufferPoolMXBean>(2);
-                pools.add( sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPoolMXBean() );
-                pools.add( sun.nio.ch.FileChannelImpl.getMappedBufferPoolMXBean() );
-                return pools;
+                return ManagementFactoryHelper.getBufferPoolMXBeans();
             }
         }),
 
--- a/jdk/src/share/classes/java/nio/Bits.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/java/nio/Bits.java	Sat Jun 27 21:49:41 2009 +0100
@@ -26,11 +26,8 @@
 package java.nio;
 
 import java.security.AccessController;
-import java.security.PrivilegedAction;
 import sun.misc.Unsafe;
 import sun.misc.VM;
-import javax.management.ObjectName;
-import javax.management.MalformedObjectNameException;
 
 /**
  * Access to bits, native and otherwise.
@@ -676,55 +673,34 @@
         }
     }
 
-    // -- Management interface for monitoring of direct buffer usage --
+    // -- Monitoring of direct buffer usage --
 
     static {
         // setup access to this package in SharedSecrets
         sun.misc.SharedSecrets.setJavaNioAccess(
             new sun.misc.JavaNioAccess() {
                 @Override
-                public BufferPoolMXBean getDirectBufferPoolMXBean() {
-                    return LazyInitialization.directBufferPoolMXBean;
+                public sun.misc.JavaNioAccess.BufferPool getDirectBufferPool() {
+                    return new sun.misc.JavaNioAccess.BufferPool() {
+                        @Override
+                        public String getName() {
+                            return "direct";
+                        }
+                        @Override
+                        public long getCount() {
+                            return Bits.count;
+                        }
+                        @Override
+                        public long getTotalCapacity() {
+                            return Bits.usedMemory;
+                        }
+                        @Override
+                        public long getMemoryUsed() {
+                            return Bits.reservedMemory;
+                        }
+                    };
                 }
-            }
-        );
-    }
-
-    // Lazy initialization of management interface
-    private static class LazyInitialization {
-        static final BufferPoolMXBean directBufferPoolMXBean = directBufferPoolMXBean();
-
-        private static BufferPoolMXBean directBufferPoolMXBean() {
-            final String pool = "direct";
-            final ObjectName obj;
-            try {
-                obj = new ObjectName("java.nio:type=BufferPool,name=" + pool);
-            } catch (MalformedObjectNameException x) {
-                throw new AssertionError(x);
-            }
-            return new BufferPoolMXBean() {
-                @Override
-                public ObjectName getObjectName() {
-                    return obj;
-                }
-                @Override
-                public String getName() {
-                    return pool;
-                }
-                @Override
-                public long getCount() {
-                    return Bits.count;
-                }
-                @Override
-                public long getTotalCapacity() {
-                    return Bits.usedMemory;
-                }
-                @Override
-                public long getMemoryUsed() {
-                    return Bits.reservedMemory;
-                }
-            };
-        }
+        });
     }
 
     // -- Bulk get/put acceleration --
--- a/jdk/src/share/classes/java/nio/Direct-X-Buffer.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/java/nio/Direct-X-Buffer.java	Sat Jun 27 21:49:41 2009 +0100
@@ -30,7 +30,6 @@
 import sun.misc.Cleaner;
 import sun.misc.Unsafe;
 import sun.nio.ch.DirectBuffer;
-import sun.nio.ch.FileChannelImpl;
 
 
 class Direct$Type$Buffer$RW$$BO$
--- a/jdk/src/share/classes/sun/management/ManagementFactoryHelper.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/sun/management/ManagementFactoryHelper.java	Sat Jun 27 21:49:41 2009 +0100
@@ -26,22 +26,15 @@
 package sun.management;
 
 import java.lang.management.*;
-import java.util.logging.LogManager;
 
-import javax.management.DynamicMBean;
 import javax.management.MBeanServer;
-import javax.management.MBeanServerFactory;
-import javax.management.MBeanInfo;
-import javax.management.NotificationEmitter;
 import javax.management.ObjectName;
-import javax.management.ObjectInstance;
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanRegistrationException;
 import javax.management.NotCompliantMBeanException;
 import javax.management.RuntimeOperationsException;
-import javax.management.StandardEmitterMBean;
-import javax.management.StandardMBean;
+import java.nio.BufferPoolMXBean;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
@@ -49,11 +42,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.Iterator;
-import java.util.ListIterator;
 import com.sun.management.OSMBeanFactory;
 import com.sun.management.HotSpotDiagnosticMXBean;
 
@@ -68,7 +56,6 @@
 
     private static VMManagement jvm;
 
-    private static boolean mbeansCreated = false;
     private static ClassLoadingImpl    classMBean = null;
     private static MemoryImpl          memoryMBean = null;
     private static ThreadImpl          threadMBean = null;
@@ -148,6 +135,58 @@
         return result;
     }
 
+    public static List<BufferPoolMXBean> getBufferPoolMXBeans() {
+        List<BufferPoolMXBean> pools = new ArrayList<BufferPoolMXBean>(2);
+        pools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
+            .getDirectBufferPool()));
+        pools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
+            .getMappedBufferPool()));
+        return pools;
+    }
+
+    private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
+
+    /**
+     * Creates management interface for the given buffer pool.
+     */
+    private static BufferPoolMXBean
+        createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
+    {
+        return new BufferPoolMXBean() {
+            private volatile ObjectName objname;  // created lazily
+            @Override
+            public ObjectName getObjectName() {
+                ObjectName result = objname;
+                if (result == null) {
+                    synchronized (this) {
+                        if (objname == null) {
+                            result = ObjectName.valueOf(BUFFER_POOL_MXBEAN_NAME +
+                                ",name=" + pool.getName());
+                            objname = result;
+                        }
+                    }
+                }
+                return result;
+            }
+            @Override
+            public String getName() {
+                return pool.getName();
+            }
+            @Override
+            public long getCount() {
+                return pool.getCount();
+            }
+            @Override
+            public long getTotalCapacity() {
+                return pool.getTotalCapacity();
+            }
+            @Override
+            public long getMemoryUsed() {
+                return pool.getMemoryUsed();
+            }
+        };
+    }
+
     private static HotSpotDiagnostic hsDiagMBean = null;
     private static HotspotRuntime hsRuntimeMBean = null;
     private static HotspotClassLoading hsClassMBean = null;
@@ -163,8 +202,6 @@
     }
 
     /**
-
-    /**
      * This method is for testing only.
      */
     public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
--- a/jdk/src/share/classes/sun/misc/JavaNioAccess.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/sun/misc/JavaNioAccess.java	Sat Jun 27 21:49:41 2009 +0100
@@ -25,8 +25,15 @@
 
 package sun.misc;
 
-import java.nio.BufferPoolMXBean;
-
 public interface JavaNioAccess {
-    BufferPoolMXBean getDirectBufferPoolMXBean();
+    /**
+     * Provides access to information on buffer usage.
+     */
+    interface BufferPool {
+        String getName();
+        long getCount();
+        long getTotalCapacity();
+        long getMemoryUsed();
+    }
+    BufferPool getDirectBufferPool();
 }
--- a/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java	Sat Jun 27 21:46:53 2009 +0100
+++ b/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java	Sat Jun 27 21:49:41 2009 +0100
@@ -29,13 +29,10 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
-import java.nio.BufferPoolMXBean;
 import java.nio.channels.*;
 import java.util.ArrayList;
 import java.util.List;
 import java.security.AccessController;
-import javax.management.ObjectName;
-import javax.management.MalformedObjectNameException;
 import sun.misc.Cleaner;
 import sun.security.action.GetPropertyAction;
 
@@ -805,47 +802,28 @@
     }
 
     /**
-     * Returns the management interface for mapped buffers
+     * Invoked by sun.management.ManagementFactoryHelper to create the management
+     * interface for mapped buffers.
      */
-    public static BufferPoolMXBean getMappedBufferPoolMXBean() {
-        return LazyInitialization.mappedBufferPoolMXBean;
-    }
-
-    // Lazy initialization of management interface
-    private static class LazyInitialization {
-        static final BufferPoolMXBean mappedBufferPoolMXBean = mappedBufferPoolMXBean();
-
-        private static BufferPoolMXBean mappedBufferPoolMXBean() {
-            final String pool = "mapped";
-            final ObjectName obj;
-            try {
-                obj = new ObjectName("java.nio:type=BufferPool,name=" + pool);
-            } catch (MalformedObjectNameException x) {
-                throw new AssertionError(x);
+    public static sun.misc.JavaNioAccess.BufferPool getMappedBufferPool() {
+        return new sun.misc.JavaNioAccess.BufferPool() {
+            @Override
+            public String getName() {
+                return "mapped";
             }
-            return new BufferPoolMXBean() {
-                @Override
-                public ObjectName getObjectName() {
-                    return obj;
-                }
-                @Override
-                public String getName() {
-                    return pool;
-                }
-                @Override
-                public long getCount() {
-                    return Unmapper.count;
-                }
-                @Override
-                public long getTotalCapacity() {
-                    return Unmapper.totalCapacity;
-                }
-                @Override
-                public long getMemoryUsed() {
-                    return Unmapper.totalSize;
-                }
-            };
-        }
+            @Override
+            public long getCount() {
+                return Unmapper.count;
+            }
+            @Override
+            public long getTotalCapacity() {
+                return Unmapper.totalCapacity;
+            }
+            @Override
+            public long getMemoryUsed() {
+                return Unmapper.totalSize;
+            }
+        };
     }
 
     // -- Locks --