1/*
2 * Copyright (c) 2004, 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
26package sun.tools.jstatd;
27
28import java.util.*;
29import java.nio.*;
30import java.io.*;
31import java.net.*;
32import java.rmi.*;
33import java.rmi.server.*;
34import sun.jvmstat.monitor.*;
35import sun.jvmstat.monitor.event.*;
36import 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 */
49public class RemoteHostImpl implements RemoteHost, HostListener {
50
51    private MonitoredHost monitoredHost;
52    private Set<Integer> activeVms;
53
54    public RemoteHostImpl() throws MonitorException {
55        try {
56            monitoredHost = MonitoredHost.getMonitoredHost("localhost");
57        } catch (URISyntaxException e) { }
58
59        activeVms = monitoredHost.activeVms();
60        monitoredHost.addHostListener(this);
61    }
62
63    public RemoteVm attachVm(int lvmid, String mode)
64                    throws RemoteException, MonitorException {
65        Integer v = lvmid;
66        RemoteVm stub = null;
67        StringBuilder sb = new StringBuilder();
68
69        sb.append("local://").append(lvmid).append("@localhost");
70        if (mode != null) {
71            sb.append("?mode=").append(mode);
72        }
73
74        String vmidStr = sb.toString();
75
76        try {
77            VmIdentifier vmid = new VmIdentifier(vmidStr);
78            MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid);
79            RemoteVmImpl rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm);
80            stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0);
81        }
82        catch (URISyntaxException e) {
83            throw new RuntimeException("Malformed VmIdentifier URI: "
84                                       + vmidStr, e);
85        }
86        return stub;
87    }
88
89    public void detachVm(RemoteVm rvm) throws RemoteException {
90        rvm.detach();
91    }
92
93    public int[] activeVms() throws MonitorException {
94        Object[] vms = null;
95        int[] vmids = null;
96
97        vms = monitoredHost.activeVms().toArray();
98        vmids = new int[vms.length];
99
100        for (int i = 0; i < vmids.length; i++) {
101            vmids[i] = ((Integer)vms[i]).intValue();
102        }
103        return vmids;
104    }
105
106    public void vmStatusChanged(VmStatusChangeEvent ev) {
107        synchronized(this.activeVms) {
108            activeVms.retainAll(ev.getActive());
109        }
110    }
111
112    public void disconnected(HostEvent ev) {
113        // we only monitor the local host, so this event shouldn't occur.
114    }
115}
116