001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstatd;
027:
028: import java.util.*;
029: import java.nio.*;
030: import java.io.*;
031: import java.net.*;
032: import java.rmi.*;
033: import java.rmi.server.*;
034: import sun.jvmstat.monitor.*;
035: import sun.jvmstat.monitor.event.*;
036: import sun.jvmstat.monitor.remote.*;
037:
038: /**
039: * Concrete implementation of the RemoteHost interface for the HotSpot
040: * PerfData <em>rmi:</em> protocol.
041: * <p>
042: * This class provides remote access to the instrumentation exported
043: * by HotSpot Java Virtual Machines through the PerfData shared memory
044: * interface.
045: *
046: * @author Brian Doherty
047: * @version 1.9, 05/09/07
048: * @since 1.5
049: */
050: public class RemoteHostImpl implements RemoteHost, HostListener {
051:
052: private MonitoredHost monitoredHost;
053: private Set<Integer> activeVms;
054:
055: public RemoteHostImpl() throws MonitorException {
056: try {
057: monitoredHost = MonitoredHost.getMonitoredHost("localhost");
058: } catch (URISyntaxException e) {
059: }
060:
061: activeVms = monitoredHost.activeVms();
062: monitoredHost.addHostListener(this );
063: }
064:
065: public RemoteVm attachVm(int lvmid, String mode)
066: throws RemoteException, MonitorException {
067: Integer v = new Integer(lvmid);
068: RemoteVm stub = null;
069: StringBuffer sb = new StringBuffer();
070:
071: sb.append("local://").append(lvmid).append("@localhost");
072: if (mode != null) {
073: sb.append("?mode=" + mode);
074: }
075:
076: String vmidStr = sb.toString();
077:
078: try {
079: VmIdentifier vmid = new VmIdentifier(vmidStr);
080: MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid);
081: RemoteVmImpl rvm = new RemoteVmImpl(
082: (BufferedMonitoredVm) mvm);
083: stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0);
084: } catch (URISyntaxException e) {
085: throw new RuntimeException("Malformed VmIdentifier URI: "
086: + vmidStr, e);
087: }
088: return stub;
089: }
090:
091: public void detachVm(RemoteVm rvm) throws RemoteException {
092: rvm.detach();
093: }
094:
095: public int[] activeVms() throws MonitorException {
096: Object[] vms = null;
097: int[] vmids = null;
098:
099: vms = monitoredHost.activeVms().toArray();
100: vmids = new int[vms.length];
101:
102: for (int i = 0; i < vmids.length; i++) {
103: vmids[i] = ((Integer) vms[i]).intValue();
104: }
105: return vmids;
106: }
107:
108: public void vmStatusChanged(VmStatusChangeEvent ev) {
109: synchronized (this .activeVms) {
110: activeVms.retainAll(ev.getActive());
111: }
112: }
113:
114: public void disconnected(HostEvent ev) {
115: // we only monitor the local host, so this event shouldn't occur.
116: }
117: }
|