1 /* 2 * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jstatd; 27 28 import java.util.*; 29 import java.nio.*; 30 import java.io.*; 31 import java.net.*; 32 import java.rmi.*; 33 import java.rmi.server.*; 34 import sun.jvmstat.monitor.*; 35 import sun.jvmstat.monitor.event.*; 36 import sun.jvmstat.monitor.remote.*; 37 38 /** 39 * Concrete implementation of the RemoteHost interface for the HotSpot 40 * PerfData <em>rmi:</em> protocol. 41 * <p> 42 * This class provides remote access to the instrumentation exported 43 * by HotSpot Java Virtual Machines through the PerfData shared memory 44 * interface. 45 * 46 * @author Brian Doherty 47 * @since 1.5 48 */ 49 public class RemoteHostImpl implements RemoteHost, HostListener { 50 51 private MonitoredHost monitoredHost; 52 private Set<Integer> activeVms; 53 private static RemoteVm rvm; 54 RemoteHostImpl()55 public RemoteHostImpl() throws MonitorException { 56 try { 57 monitoredHost = MonitoredHost.getMonitoredHost("localhost"); 58 } catch (URISyntaxException e) { } 59 60 activeVms = monitoredHost.activeVms(); 61 monitoredHost.addHostListener(this); 62 } 63 attachVm(int lvmid, String mode)64 public RemoteVm attachVm(int lvmid, String mode) 65 throws RemoteException, MonitorException { 66 Integer v = lvmid; 67 RemoteVm stub = null; 68 StringBuilder sb = new StringBuilder(); 69 70 sb.append("local://").append(lvmid).append("@localhost"); 71 if (mode != null) { 72 sb.append("?mode=").append(mode); 73 } 74 75 String vmidStr = sb.toString(); 76 77 try { 78 VmIdentifier vmid = new VmIdentifier(vmidStr); 79 MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid); 80 rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm); 81 stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0); 82 } 83 catch (URISyntaxException e) { 84 throw new RuntimeException("Malformed VmIdentifier URI: " 85 + vmidStr, e); 86 } 87 return stub; 88 } 89 detachVm(RemoteVm rvm)90 public void detachVm(RemoteVm rvm) throws RemoteException { 91 rvm.detach(); 92 } 93 activeVms()94 public int[] activeVms() throws MonitorException { 95 Object[] vms = null; 96 int[] vmids = null; 97 98 vms = monitoredHost.activeVms().toArray(); 99 vmids = new int[vms.length]; 100 101 for (int i = 0; i < vmids.length; i++) { 102 vmids[i] = ((Integer)vms[i]).intValue(); 103 } 104 return vmids; 105 } 106 vmStatusChanged(VmStatusChangeEvent ev)107 public void vmStatusChanged(VmStatusChangeEvent ev) { 108 synchronized(this.activeVms) { 109 activeVms.retainAll(ev.getActive()); 110 } 111 } 112 disconnected(HostEvent ev)113 public void disconnected(HostEvent ev) { 114 // we only monitor the local host, so this event shouldn't occur. 115 } 116 } 117