jdk/src/jdk.jvmstat.rmi/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/MonitoredHostProvider.java
author jbachorik
Fri, 13 Nov 2015 14:44:05 +0100
changeset 34351 f30070a16a4b
parent 25859 jdk/src/jdk.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/MonitoredHostProvider.java@3317bb8137f4
permissions -rw-r--r--
8043138: Attach API should not require jvmstat rmi protocol Reviewed-by: alanb, mchung, erikj, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2014, 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.perfdata.monitor.protocol.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.jvmstat.monitor.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.jvmstat.perfdata.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Concrete implementation of the MonitoredHost interface for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <em>rmi</em> protocol of the HotSpot PerfData monitoring implementation.
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
public class MonitoredHostProvider extends MonitoredHost {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String serverName = "/JStatRemoteHost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int DEFAULT_POLLING_INTERVAL = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ArrayList<HostListener> listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private NotifierTask task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private HashSet<Integer> activeVms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private RemoteVmManager vmManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private RemoteHost remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Create a MonitoredHostProvider instance using the given HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param hostId the host identifier for this MonitoredHost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @throws MonitorException Thrown on any error encountered while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *                          communicating with the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public MonitoredHostProvider(HostIdentifier hostId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
           throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.hostId = hostId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.listeners = new ArrayList<HostListener>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.interval = DEFAULT_POLLING_INTERVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.activeVms = new HashSet<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String rmiName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String sn = serverName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String path = hostId.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if ((path != null) && (path.length() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sn = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (hostId.getPort() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            rmiName = "rmi://" + hostId.getHost() + ":" + hostId.getPort() + sn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            rmiName = "rmi://" + hostId.getHost() + sn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            remoteHost = (RemoteHost)Naming.lookup(rmiName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * rmi registry not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * Access control exceptions, where the rmi server refuses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             * connection based on policy file configuration, come through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             * here on the client side. Unfortunately, the RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             * doesn't contain enough information to determine the true cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             * of the exception. So, we have to output a rather generic message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            String message = "RMI Registry not available at "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                             + hostId.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (hostId.getPort() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                message = message + ":"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                          + java.rmi.registry.Registry.REGISTRY_PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                message = message + ":" + hostId.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw new MonitorException(message + "\n" + e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new MonitorException(message, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (NotBoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // no server with given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            String message = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (message == null) message = rmiName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new MonitorException("RMI Server " + message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                       + " not available", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // this is a programming problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException("Malformed URL: " + rmiName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.vmManager = new RemoteVmManager(remoteHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.timer = new Timer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public MonitoredVm getMonitoredVm(VmIdentifier vmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                       throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return getMonitoredVm(vmid, DEFAULT_POLLING_INTERVAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public MonitoredVm getMonitoredVm(VmIdentifier vmid, int interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                       throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        VmIdentifier nvmid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            nvmid = hostId.resolve(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            RemoteVm rvm = remoteHost.attachVm(vmid.getLocalVmId(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                               vmid.getMode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            RemoteMonitoredVm rmvm = new RemoteMonitoredVm(rvm, nvmid, timer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                           interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            rmvm.attach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return rmvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new MonitorException("Remote Exception attaching to "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                       + nvmid.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             * the VmIdentifier is expected to be a valid and should resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
             * easonably against the host identifier. A URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
             * here is most likely a programming error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new IllegalArgumentException("Malformed URI: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                               + vmid.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void detach(MonitoredVm vm) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        RemoteMonitoredVm rmvm = (RemoteMonitoredVm)vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        rmvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            remoteHost.detachVm(rmvm.getRemoteVm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new MonitorException("Remote Exception detaching from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                       + vm.getVmIdentifier().toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void addHostListener(HostListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            listeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (task == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                task = new NotifierTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                timer.schedule(task, 0, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void removeHostListener(HostListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * XXX: if a disconnect method is added, make sure it calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * this method to unregister this object from the watcher. otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * an unused MonitoredHostProvider instance may go uncollected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            listeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (listeners.isEmpty() && (task != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                task.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                task = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void setInterval(int newInterval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (newInterval == interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            int oldInterval = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            super.setInterval(newInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (task != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                task.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                NotifierTask oldTask = task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                task = new NotifierTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                CountedTimerTaskUtils.reschedule(timer, oldTask, task,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                 oldInterval, newInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public Set<Integer> activeVms() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return vmManager.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Fire VmStatusChangeEvent events to HostListener objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param active Set of Integer objects containing the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *               Vm Identifiers of the active JVMs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param started Set of Integer objects containing the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *                Vm Identifiers of new JVMs started since last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *                interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param terminated Set of Integer objects containing the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *                   Vm Identifiers of terminated JVMs since last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *                   interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   249
    @SuppressWarnings("unchecked") // Cast of result of clone
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   250
    private void fireVmStatusChangedEvents(Set<Integer> active, Set<Integer> started,
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   251
                                           Set<Integer> terminated) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   252
        ArrayList<HostListener> registered = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        VmStatusChangeEvent ev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            registered = (ArrayList)listeners.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   259
        for (Iterator<HostListener> i = registered.iterator(); i.hasNext(); /* empty */) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   260
            HostListener l = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (ev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ev = new VmStatusChangeEvent(this, active, started, terminated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            l.vmStatusChanged(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Fire hostDisconnectEvent events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   271
    @SuppressWarnings("unchecked") // Cast of result of clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    void fireDisconnectedEvents() {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   273
        ArrayList<HostListener> registered = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        HostEvent ev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            registered = (ArrayList)listeners.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   280
        for (Iterator<HostListener> i = registered.iterator(); i.hasNext(); /* empty */) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   281
            HostListener l = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (ev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                ev = new HostEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            l.disconnected(ev);
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
     * class to poll the remote machine and generate local event notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private class NotifierTask extends CountedTimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            super.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // save the last set of active JVMs
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   297
            Set<Integer> lastActiveVms = activeVms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                // get the current set of active JVMs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                activeVms = (HashSet<Integer>)vmManager.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // XXX: use logging api
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                System.err.println("MonitoredHostProvider: polling task "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                   + "caught MonitorException:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // mark the HostManager as errored and notify listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                setLastException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                fireDisconnectedEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (activeVms.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   318
            Set<Integer> startedVms = new HashSet<>();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   319
            Set<Integer> terminatedVms = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   321
            for (Iterator<Integer> i = activeVms.iterator(); i.hasNext(); /* empty */ ) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   322
                Integer vmid = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (!lastActiveVms.contains(vmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    // a new file has been detected, add to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    startedVms.add(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   329
            for (Iterator<Integer> i = lastActiveVms.iterator(); i.hasNext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    /* empty */ ) {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   331
                Integer o = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (!activeVms.contains(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    // JVM has terminated, remove it from the active list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    terminatedVms.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                fireVmStatusChangedEvents(activeVms, startedVms, terminatedVms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}