changeset 10204:50ac02f81c69

8048710: Use ServiceLoader for the jvmstat protocols Reviewed-by: alanb
author sla
date Tue, 01 Jul 2014 16:08:45 +0200
parents f5c0ab598b7c
children 5534edfe96ae
files src/share/classes/sun/jvmstat/monitor/BufferedMonitoredVm.java src/share/classes/sun/jvmstat/monitor/META-INF/services/sun.jvmstat.monitor.MonitoredHostService src/share/classes/sun/jvmstat/monitor/MonitoredHost.java src/share/classes/sun/jvmstat/monitor/MonitoredHostService.java src/share/classes/sun/jvmstat/monitor/remote/BufferedMonitoredVm.java src/share/classes/sun/jvmstat/perfdata/monitor/protocol/file/MonitoredHostFileService.java src/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostLocalService.java src/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/MonitoredHostRmiService.java test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
diffstat 9 files changed, 271 insertions(+), 139 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/monitor/BufferedMonitoredVm.java	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.jvmstat.monitor;
+
+import sun.jvmstat.monitor.*;
+
+/**
+ * Interface to support asynchronous polling of the exported
+ * instrumentation of a target Java Virtual Machine.
+ *
+ * @author Brian Doherty
+ * @since 1.5
+ */
+public interface BufferedMonitoredVm extends MonitoredVm {
+
+    /**
+     * Interface to get the bytes associated with the instrumentation
+     * for the target Java Virtual Machine.
+     *
+     * @return byte[] - a byte array containing the current bytes
+     *                  for the instrumentation exported by the
+     *                  target Java Virtual Machine.
+     */
+    byte[] getBytes();
+
+    /**
+     * Interface to get the the size of the instrumentation buffer
+     * for the target Java Virtual Machine.
+     *
+     * @return int - the size of the instrumentation buffer for the
+     *               target Java Virtual Machine.
+     */
+    int getCapacity();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/monitor/META-INF/services/sun.jvmstat.monitor.MonitoredHostService	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,3 @@
+sun.jvmstat.perfdata.monitor.protocol.file.MonitoredHostFileService
+sun.jvmstat.perfdata.monitor.protocol.local.MonitoredHostLocalService
+sun.jvmstat.perfdata.monitor.protocol.rmi.MonitoredHostRmiService
--- a/src/share/classes/sun/jvmstat/monitor/MonitoredHost.java	Tue Jul 01 15:00:33 2014 +0200
+++ b/src/share/classes/sun/jvmstat/monitor/MonitoredHost.java	Tue Jul 01 16:08:45 2014 +0200
@@ -25,9 +25,11 @@
 
 package sun.jvmstat.monitor;
 
-import java.util.*;
-import java.net.*;
-import java.lang.reflect.*;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.Set;
 
 import sun.jvmstat.monitor.event.HostListener;
 
@@ -52,31 +54,6 @@
                 new HashMap<HostIdentifier, MonitoredHost>();
 
     /*
-     * The monitoring implementation override mechanism. The value of
-     * this property is used as the class name for the concrete MonitoredHost
-     * subclass that implements the monitoring APIs.  Setting this property
-     * will cause the remaining override mechanisms to be ignored. When
-     * this mechanism is used, the HostIdentifier scheme name, which
-     * indicates the communications protocol, is not used to locate a
-     * the protocol specific package. However, the HostIdentifier is
-     * still passed to the corresponding single arg constructor.
-     * This property is not expected to be set in normal circumstances.
-     */
-    private static final String IMPL_OVERRIDE_PROP_NAME =
-            "sun.jvmstat.monitor.MonitoredHost";
-
-    /*
-     * The monitoring package name override mechanism. The value
-     * the this property is used as base package name for the
-     * monitoring implementation package. This property is not
-     * expected to be set under normal circumstances.
-     */
-    private static final String IMPL_PKG_PROP_NAME =
-            "sun.jvmstat.monitor.package";
-    private static final String IMPL_PACKAGE =
-            System.getProperty(IMPL_PKG_PROP_NAME, "sun.jvmstat.perfdata");
-
-    /*
      * The default optimized local protocol override mechanism. The value
      * of this property is used to construct the default package name
      * for the default optimized local protocol as follows:
@@ -100,15 +77,6 @@
     private static final String REMOTE_PROTOCOL =
             System.getProperty(REMOTE_PROTOCOL_PROP_NAME, "rmi");
 
-    /*
-     * The default class name of the MonitoredHost implementation subclass.
-     * There is no override mechanism for this variable, other than the
-     * IMPL_OVERRIDE_PROP_NAME override, which is larger in scope. A concrete
-     * instance of this class is expected to be found in:
-     *     <IMPL_PACKAGE>.monitor.protocol.<protocol>.<MONITORED_HOST_CLASS>
-     */
-    private static final String MONITORED_HOST_CLASS = "MonitoredHostProvider";
-
     /**
      * The HostIdentifier for this MonitoredHost instance.
      */
@@ -165,6 +133,13 @@
         return getMonitoredHost(hostId);
     }
 
+
+    /*
+     * Load the MonitoredHostServices
+     */
+    private static ServiceLoader<MonitoredHostService> monitoredHostServiceLoader =
+        ServiceLoader.load(MonitoredHostService.class, MonitoredHostService.class.getClassLoader());
+
     /**
      * Factory method to construct a MonitoredHost instance to manage the
      * connection to the host indicated by <tt>hostId</tt>.
@@ -177,11 +152,6 @@
      */
     public static MonitoredHost getMonitoredHost(HostIdentifier hostId)
                   throws MonitorException {
-        /*
-         * determine the class name to load. If the system property is set,
-         * use the indicated class. otherwise, use the default class.
-         */
-        String classname = System.getProperty(IMPL_OVERRIDE_PROP_NAME);
         MonitoredHost mh = null;
 
         synchronized(monitoredHosts) {
@@ -197,50 +167,21 @@
 
         hostId = resolveHostId(hostId);
 
-        if (classname == null) {
-            // construct the class name
-            classname = IMPL_PACKAGE + ".monitor.protocol."
-                        + hostId.getScheme() + "." + MONITORED_HOST_CLASS;
+        for (MonitoredHostService mhs : monitoredHostServiceLoader) {
+            if (mhs.getScheme().equals(hostId.getScheme())) {
+                mh = mhs.getMonitoredHost(hostId);
+            }
         }
 
-        try {
-            // run the constructor taking a single String parameter.
-            Class<?> c = Class.forName(classname);
+        if (mh == null) {
+            throw new IllegalArgumentException("Could not find MonitoredHost for scheme: " + hostId.getScheme());
+        }
 
-            Constructor cons = c.getConstructor(
-                new Class[] { hostId.getClass() }
-            );
+        synchronized(monitoredHosts) {
+            monitoredHosts.put(mh.hostId, mh);
+        }
 
-            mh = (MonitoredHost)cons.newInstance(new Object[] { hostId } );
-
-            synchronized(monitoredHosts) {
-                monitoredHosts.put(mh.hostId, mh);
-            }
-            return mh;
-        } catch (ClassNotFoundException e) {
-            // from Class.forName();
-            throw new IllegalArgumentException("Could not find " + classname
-                                               + ": " + e.getMessage(), e);
-        } catch (NoSuchMethodException e) {
-            // from Class.getConstructor();
-            throw new IllegalArgumentException(
-                "Expected constructor missing in " + classname + ": "
-                + e.getMessage(), e);
-        } catch (IllegalAccessException e) {
-            // from Constructor.newInstance()
-            throw new IllegalArgumentException(
-                "Unexpected constructor access in " + classname + ": "
-                + e.getMessage(), e);
-        } catch (InstantiationException e) {
-            throw new IllegalArgumentException(classname + "is abstract: "
-                                               + e.getMessage(), e);
-        } catch (InvocationTargetException e) {
-            Throwable cause = e.getCause();
-            if (cause instanceof MonitorException) {
-                throw (MonitorException)cause;
-            }
-            throw new RuntimeException("Unexpected exception", e);
-        }
+        return mh;
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/monitor/MonitoredHostService.java	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.jvmstat.monitor;
+
+public interface MonitoredHostService {
+
+    /**
+     * Construct a MonitoredHost instance to manage the
+     * connection to the host indicated by <tt>hostId</tt>.
+     *
+     * @param hostId the identifier for the target host.
+     * @return MonitoredHost - The MonitoredHost object needed to attach to
+     *                         the target host.
+     *
+     * @throws MonitorException Thrown if monitoring errors occur.
+     */
+    public MonitoredHost getMonitoredHost(HostIdentifier hostId) throws MonitorException;
+
+    /**
+     * Get the scheme that this service supports.
+     *
+     * @return scheme name
+     */
+    public String getScheme();
+}
--- a/src/share/classes/sun/jvmstat/monitor/remote/BufferedMonitoredVm.java	Tue Jul 01 15:00:33 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package sun.jvmstat.monitor.remote;
-
-import sun.jvmstat.monitor.*;
-
-/**
- * Interface to support asynchronous polling of the exported
- * instrumentation of a target Java Virtual Machine.
- *
- * @author Brian Doherty
- * @since 1.5
- */
-public interface BufferedMonitoredVm extends MonitoredVm {
-
-    /**
-     * Interface to get the bytes associated with the instrumentation
-     * for the target Java Virtual Machine.
-     *
-     * @return byte[] - a byte array containing the current bytes
-     *                  for the instrumentation exported by the
-     *                  target Java Virtual Machine.
-     */
-    byte[] getBytes();
-
-    /**
-     * Interface to get the the size of the instrumentation buffer
-     * for the target Java Virtual Machine.
-     *
-     * @return int - the size of the instrumentation buffer for the
-     *               target Java Virtual Machine.
-     */
-    int getCapacity();
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/perfdata/monitor/protocol/file/MonitoredHostFileService.java	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.jvmstat.perfdata.monitor.protocol.file;
+
+import sun.jvmstat.monitor.HostIdentifier;
+import sun.jvmstat.monitor.MonitorException;
+import sun.jvmstat.monitor.MonitoredHost;
+import sun.jvmstat.monitor.MonitoredHostService;
+
+public final class MonitoredHostFileService implements MonitoredHostService {
+
+    @Override
+    public MonitoredHost getMonitoredHost(HostIdentifier hostId)
+            throws MonitorException {
+        return new MonitoredHostProvider(hostId);
+    }
+
+    @Override
+    public String getScheme() {
+        return "file";
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/MonitoredHostLocalService.java	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.jvmstat.perfdata.monitor.protocol.local;
+
+import sun.jvmstat.monitor.HostIdentifier;
+import sun.jvmstat.monitor.MonitorException;
+import sun.jvmstat.monitor.MonitoredHost;
+import sun.jvmstat.monitor.MonitoredHostService;
+
+public final class MonitoredHostLocalService implements MonitoredHostService {
+
+    @Override
+    public MonitoredHost getMonitoredHost(HostIdentifier hostId)
+            throws MonitorException {
+        return new MonitoredHostProvider(hostId);
+    }
+
+    @Override
+    public String getScheme() {
+        return "local";
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/MonitoredHostRmiService.java	Tue Jul 01 16:08:45 2014 +0200
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.jvmstat.perfdata.monitor.protocol.rmi;
+
+import sun.jvmstat.monitor.HostIdentifier;
+import sun.jvmstat.monitor.MonitorException;
+import sun.jvmstat.monitor.MonitoredHost;
+import sun.jvmstat.monitor.MonitoredHostService;
+
+public final class MonitoredHostRmiService implements MonitoredHostService {
+
+    @Override
+    public MonitoredHost getMonitoredHost(HostIdentifier hostId) throws MonitorException {
+        return new MonitoredHostProvider(hostId);
+    }
+
+    @Override
+    public String getScheme() {
+        return "rmi";
+    }
+
+}
--- a/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Tue Jul 01 15:00:33 2014 +0200
+++ b/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Tue Jul 01 16:08:45 2014 +0200
@@ -41,6 +41,8 @@
 import java.util.concurrent.TimeoutException;
 import java.util.function.Predicate;
 import java.util.function.Consumer;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
 
 import sun.management.VMManagement;
 
@@ -135,6 +137,7 @@
                                        long timeout,
                                        TimeUnit unit)
     throws IOException, InterruptedException, TimeoutException {
+        System.out.println("["+name+"]:" + processBuilder.command().stream().collect(Collectors.joining(" ")));
         Process p = processBuilder.start();
         StreamPumper stdout = new StreamPumper(p.getInputStream());
         StreamPumper stderr = new StreamPumper(p.getErrorStream());