jdk/src/share/classes/sun/jvmstat/monitor/MonitoredHost.java
author michaelm
Thu, 24 Oct 2013 20:39:21 +0100
changeset 22339 e91bfaf4360d
parent 5506 202f599c92aa
child 24969 afa6934dd8e8
permissions -rw-r--r--
8011786: Better applet networking Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.jvmstat.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.jvmstat.monitor.event.HostListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * An abstraction for a host that contains instrumented Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Machines. The class provides abstract factory methods for creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * concrete instances of this class and factory methods for creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@link MonitoredVm} instances. Concrete implementations of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * provide methods for managing the communications protocols and provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for event notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see HostIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see MonitoredVm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see HostListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class MonitoredHost {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static Map<HostIdentifier, MonitoredHost> monitoredHosts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                new HashMap<HostIdentifier, MonitoredHost>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The monitoring implementation override mechanism. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * this property is used as the class name for the concrete MonitoredHost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * subclass that implements the monitoring APIs.  Setting this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * will cause the remaining override mechanisms to be ignored. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * this mechanism is used, the HostIdentifier scheme name, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * indicates the communications protocol, is not used to locate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * the protocol specific package. However, the HostIdentifier is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * still passed to the corresponding single arg constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * This property is not expected to be set in normal circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final String IMPL_OVERRIDE_PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            "sun.jvmstat.monitor.MonitoredHost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The monitoring package name override mechanism. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the this property is used as base package name for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * monitoring implementation package. This property is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * expected to be set under normal circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String IMPL_PKG_PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            "sun.jvmstat.monitor.package";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final String IMPL_PACKAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.getProperty(IMPL_PKG_PROP_NAME, "sun.jvmstat.perfdata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The default optimized local protocol override mechanism. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * of this property is used to construct the default package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * for the default optimized local protocol as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *        <IMPL_PACKAGE>.monitor.<LOCAL_PROTOCOL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This property is not expected to be set under normal circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final String LOCAL_PROTOCOL_PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            "sun.jvmstat.monitor.local";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final String LOCAL_PROTOCOL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.getProperty(LOCAL_PROTOCOL_PROP_NAME, "local");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The default remote protocol override mechanism. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * this property is used to construct the default package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * for the default remote protocol protocol as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *        <IMPL_PACKAGE>.monitor.protocol.<REMOTE_PROTOCOL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * This property is not expected to be set under normal circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final String REMOTE_PROTOCOL_PROP_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            "sun.jvmstat.monitor.remote";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final String REMOTE_PROTOCOL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.getProperty(REMOTE_PROTOCOL_PROP_NAME, "rmi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The default class name of the MonitoredHost implementation subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * There is no override mechanism for this variable, other than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * IMPL_OVERRIDE_PROP_NAME override, which is larger in scope. A concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * instance of this class is expected to be found in:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *     <IMPL_PACKAGE>.monitor.protocol.<protocol>.<MONITORED_HOST_CLASS>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final String MONITORED_HOST_CLASS = "MonitoredHostProvider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The HostIdentifier for this MonitoredHost instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected HostIdentifier hostId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The polling interval, in milliseconds, for this MonitoredHost instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected int interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The last Exception encountered while polling this MonitoredHost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected Exception lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Factory method to construct MonitoredHost instances to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * connections to the host indicated by <tt>hostIdString</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param hostIdString a String representation of a {@link HostIdentifier}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return MonitoredHost - the MonitoredHost instance for communicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *                         with the indicated host using the protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                         specified in hostIdString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws MonitorException  Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws URISyntaxException Thrown when the hostIdString is poorly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                            formed. This exception may get encapsulated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                            into MonitorException in a future revision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static MonitoredHost getMonitoredHost(String hostIdString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                  throws MonitorException, URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        HostIdentifier hostId = new HostIdentifier(hostIdString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return getMonitoredHost(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Factory method to construct a MonitoredHost instance to manage the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * connection to the Java Virtual Machine indicated by <tt>vmid</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * This method provide a convenient short cut for attaching to a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * instrumented Java Virtual Machine. The information in the VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * is used to construct a corresponding HostIdentifier, which in turn is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * used to create the MonitoredHost instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param vmid The identifier for the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return MonitoredHost - The MonitoredHost object needed to attach to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *                         the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static MonitoredHost getMonitoredHost(VmIdentifier vmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // use the VmIdentifier to construct the corresponding HostIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        HostIdentifier hostId = new HostIdentifier(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return getMonitoredHost(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Factory method to construct a MonitoredHost instance to manage the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * connection to the host indicated by <tt>hostId</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param hostId the identifier for the target host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return MonitoredHost - The MonitoredHost object needed to attach to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *                         the target host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static MonitoredHost getMonitoredHost(HostIdentifier hostId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                  throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * determine the class name to load. If the system property is set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * use the indicated class. otherwise, use the default class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        String classname = System.getProperty(IMPL_OVERRIDE_PROP_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        MonitoredHost mh = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        synchronized(monitoredHosts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            mh = monitoredHosts.get(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (mh != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (mh.isErrored()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    monitoredHosts.remove(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    return mh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        hostId = resolveHostId(hostId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (classname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // construct the class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            classname = IMPL_PACKAGE + ".monitor.protocol."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        + hostId.getScheme() + "." + MONITORED_HOST_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // run the constructor taking a single String parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            Class<?> c = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            Constructor cons = c.getConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                new Class[] { hostId.getClass() }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            mh = (MonitoredHost)cons.newInstance(new Object[] { hostId } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            synchronized(monitoredHosts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                monitoredHosts.put(mh.hostId, mh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return mh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // from Class.forName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new IllegalArgumentException("Could not find " + classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                               + ": " + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // from Class.getConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                "Expected constructor missing in " + classname + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // from Constructor.newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                "Unexpected constructor access in " + classname + ": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new IllegalArgumentException(classname + "is abstract: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                               + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (cause instanceof MonitorException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw (MonitorException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new RuntimeException("Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Method to resolve unspecified components of the given HostIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * by constructing a new HostIdentifier that replaces the unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * components with the default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param hostId the unresolved HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return HostIdentifier - a resolved HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    protected static HostIdentifier resolveHostId(HostIdentifier hostId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                     throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        String hostname = hostId.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        String scheme = hostId.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assert hostname != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (scheme == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (hostname.compareTo("localhost") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                scheme = LOCAL_PROTOCOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                scheme = REMOTE_PROTOCOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        sb.append(scheme).append(":").append(hostId.getSchemeSpecificPart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        String frag = hostId.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (frag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            sb.append("#").append(frag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return new HostIdentifier(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // programming error - HostIdentifier was valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new IllegalArgumentException("Malformed URI created: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                               + sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Return the resolved HostIdentifier for this MonitoredHost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return HostIdentifier - the resolved HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public HostIdentifier getHostIdentifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return hostId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /* ---- Methods to support polled MonitoredHost Implementations ----- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Set the polling interval for this MonitoredHost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param interval the polling interval, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public void setInterval(int interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this.interval = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Get the polling interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return int - the polling interval in milliseconds for this MonitoredHost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public int getInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Set the last exception encountered while polling this MonitoredHost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param lastException the last exception encountered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void setLastException(Exception lastException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        this.lastException = lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Get the last exception encountered while polling this MonitoredHost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return Exception - the last exception occurred while polling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *                     MonitoredHost, or <tt>null</tt> if no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                     has occurred or the exception has been cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public Exception getLastException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Clear the last exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void clearLastException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Test if this MonitoredHost is in the errored state. If this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * returns true, then the Exception returned by getLastException()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * indicates the Exception that caused the error condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return boolean - true if the MonitoredHost instance has experienced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *                   an error, or false if it hasn't or if any past
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *                   error has been cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean isErrored() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return lastException != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Get the MonitoredVm for the given Java Virtual Machine. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * sampling interval is used for the MonitoredVm instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param id the VmIdentifier specifying the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return MonitoredVm - the MonitoredVm instance for the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *                       Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public abstract MonitoredVm getMonitoredVm(VmIdentifier id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Get the MonitoredVm for the given Java Virtual Machine. The sampling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * interval is set to the given interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param id the VmIdentifier specifying the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param interval the sampling interval for the target Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return MonitoredVm - the MonitoredVm instance for the target Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *                       Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public abstract MonitoredVm getMonitoredVm(VmIdentifier id, int interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Detach from the indicated MonitoredVm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param vm the monitored Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public abstract void detach(MonitoredVm vm) throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Add a HostListener. The given listener is added to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * of HostListener objects to be notified of MonitoredHost related events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param listener the HostListener to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public abstract void addHostListener(HostListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                         throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Remove a HostListener. The given listener is removed from the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * of HostListener objects to be notified of MonitoredHost related events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param listener the HostListener to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public abstract void removeHostListener(HostListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                         throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Return the current set of active Java Virtual Machines for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * MonitoredHost. The returned Set contains {@link Integer} instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * holding the local virtual machine identifier, or <em>lvmid</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * for each instrumented Java Virtual Machine currently available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return Set - the current set of active Java Virtual Machines associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *               with this MonitoredHost, or the empty set of none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @throws MonitorException Thrown if monitoring errors occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public abstract Set<Integer> activeVms() throws MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}