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 
26 package sun.jvmstat.perfdata.monitor.protocol.rmi;
27 
28 import java.util.*;
29 import java.util.regex.*;
30 import java.io.*;
31 import java.rmi.RemoteException;
32 import sun.jvmstat.monitor.*;
33 import sun.jvmstat.monitor.event.*;
34 import sun.jvmstat.monitor.remote.*;
35 
36 /**
37  * Class for managing the RemoteMonitoredVm instances on a remote system.
38  * <p>
39  * This class is responsible for the mechanism that detects the active
40  * HotSpot Java Virtual Machines on the remote host and possibly for a
41  * specific user. The ability to detect all possible HotSpot Java Virtual
42  * Machines on the remote host may be limited by the permissions of the
43  * principal running the RMI server application on the remote host.
44  *
45  * @author Brian Doherty
46  * @since 1.5
47  */
48 public class RemoteVmManager {
49 
50     private RemoteHost remoteHost;
51     private String user;
52 
53     /**
54      * Creates a RemoteVmManager instance for the remote system.
55      * <p>
56      * Manages RemoteMonitordVm instances for which the principal
57      * running the remote server has appropriate permissions.
58      *
59      * @param remoteHost the remote proxy object to the RMI server on
60      *                   the remote system.
61      */
RemoteVmManager(RemoteHost remoteHost)62     public RemoteVmManager(RemoteHost remoteHost) {
63         this(remoteHost, null);
64     }
65 
66     /**
67      * Creates a RemoteVmManager instance for the given user.
68      * <p>
69      * Manages RemoteMonitoredVm instances for all remote Java Virtual
70      * machines owned by the specified user on the remote system. The
71      * RMI server on the remote system must have the appropriate permissions
72      * to access the named users Java Virtual Machines.
73      *
74      * @param remoteHost the remote proxy object to the RMI server on
75      *                   the remote system.
76      * @param user the name of the user
77      */
RemoteVmManager(RemoteHost remoteHost, String user)78     public RemoteVmManager(RemoteHost remoteHost, String user) {
79         this.user = user;
80         this.remoteHost = remoteHost;
81     }
82 
83     /**
84      * Return the current set of monitorable Java Virtual Machines.
85      * <p>
86      * The set returned by this method depends on the user name passed
87      * to the constructor. If no user name was specified, then this
88      * method will return all candidate JVMs on the system. Otherwise,
89      * only the JVMs for the given user will be returned. This assumes
90      * that the RMI server process has the appropriate permissions to
91      * access the target set of JVMs.
92      *
93      * @return Set - the Set of monitorable Java Virtual Machines
94      */
activeVms()95     public Set<Integer> activeVms() throws MonitorException {
96         int[] active = null;
97 
98         try {
99             active = remoteHost.activeVms();
100 
101         } catch (RemoteException e) {
102             throw new MonitorException("Error communicating with remote host: "
103                                        + e.getMessage(), e);
104         }
105 
106         Set<Integer> activeSet = new HashSet<Integer>(active.length);
107 
108         for (int i = 0; i < active.length; i++) {
109             activeSet.add(active[i]);
110         }
111 
112         return activeSet;
113     }
114 }
115