author | prr |
Tue, 15 Jul 2014 11:22:14 -0700 | |
changeset 25522 | 10d789df41bb |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.jvmstat.perfdata.monitor.protocol.local; |
|
27 |
||
28 |
import sun.jvmstat.monitor.*; |
|
29 |
import sun.jvmstat.monitor.event.*; |
|
30 |
import java.util.*; |
|
31 |
import java.util.regex.*; |
|
32 |
import java.io.*; |
|
33 |
||
34 |
/** |
|
35 |
* Class for managing the LocalMonitoredVm instances on the local system. |
|
36 |
* <p> |
|
37 |
* This class is responsible for the mechanism that detects the active |
|
38 |
* HotSpot Java Virtual Machines on the local host and possibly for a |
|
39 |
* specific user. The ability to detect all possible HotSpot Java Virtual |
|
40 |
* Machines on the local host may be limited by the permissions of the |
|
41 |
* principal running this JVM. |
|
42 |
* |
|
43 |
* @author Brian Doherty |
|
44 |
* @since 1.5 |
|
45 |
*/ |
|
46 |
public class LocalVmManager { |
|
47 |
private String userName; // user name for monitored jvm |
|
48 |
private File tmpdir; |
|
49 |
private Pattern userPattern; |
|
50 |
private Matcher userMatcher; |
|
51 |
private FilenameFilter userFilter; |
|
52 |
private Pattern filePattern; |
|
53 |
private Matcher fileMatcher; |
|
54 |
private FilenameFilter fileFilter; |
|
55 |
private Pattern tmpFilePattern; |
|
56 |
private Matcher tmpFileMatcher; |
|
57 |
private FilenameFilter tmpFileFilter; |
|
58 |
||
59 |
/** |
|
60 |
* Creates a LocalVmManager instance for the local system. |
|
61 |
* <p> |
|
62 |
* Manages LocalMonitoredVm instances for which the principal |
|
63 |
* has appropriate permissions. |
|
64 |
*/ |
|
65 |
public LocalVmManager() { |
|
66 |
this(null); |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Creates a LocalVmManager instance for the given user. |
|
71 |
* <p> |
|
72 |
* Manages LocalMonitoredVm instances for all JVMs owned by the specified |
|
73 |
* user. |
|
74 |
* |
|
75 |
* @param user the name of the user |
|
76 |
*/ |
|
77 |
public LocalVmManager(String user) { |
|
78 |
this.userName = user; |
|
79 |
||
80 |
if (userName == null) { |
|
81 |
tmpdir = new File(PerfDataFile.getTempDirectory()); |
|
82 |
userPattern = Pattern.compile(PerfDataFile.userDirNamePattern); |
|
83 |
userMatcher = userPattern.matcher(""); |
|
84 |
||
85 |
userFilter = new FilenameFilter() { |
|
86 |
public boolean accept(File dir, String name) { |
|
87 |
userMatcher.reset(name); |
|
88 |
return userMatcher.lookingAt(); |
|
89 |
} |
|
90 |
}; |
|
91 |
} else { |
|
92 |
tmpdir = new File(PerfDataFile.getTempDirectory(userName)); |
|
93 |
} |
|
94 |
||
95 |
filePattern = Pattern.compile(PerfDataFile.fileNamePattern); |
|
96 |
fileMatcher = filePattern.matcher(""); |
|
97 |
||
98 |
fileFilter = new FilenameFilter() { |
|
99 |
public boolean accept(File dir, String name) { |
|
100 |
fileMatcher.reset(name); |
|
101 |
return fileMatcher.matches(); |
|
102 |
} |
|
103 |
}; |
|
104 |
||
105 |
tmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern); |
|
106 |
tmpFileMatcher = tmpFilePattern.matcher(""); |
|
107 |
||
108 |
tmpFileFilter = new FilenameFilter() { |
|
109 |
public boolean accept(File dir, String name) { |
|
110 |
tmpFileMatcher.reset(name); |
|
111 |
return tmpFileMatcher.matches(); |
|
112 |
} |
|
113 |
}; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Return the current set of monitorable Java Virtual Machines. |
|
118 |
* <p> |
|
119 |
* The set returned by this method depends on the user name passed |
|
120 |
* to the constructor. If no user name was specified, then this |
|
121 |
* method will return all candidate JVMs on the system. Otherwise, |
|
122 |
* only the JVMs for the given user will be returned. This assumes |
|
123 |
* that principal associated with this JVM has the appropriate |
|
124 |
* permissions to access the target set of JVMs. |
|
125 |
* |
|
126 |
* @return Set - the Set of monitorable Java Virtual Machines |
|
127 |
*/ |
|
128 |
public synchronized Set<Integer> activeVms() { |
|
129 |
/* |
|
130 |
* This method is synchronized because the Matcher object used by |
|
131 |
* fileFilter is not safe for concurrent use, and this method is |
|
132 |
* called by multiple threads. Before this method was synchronized, |
|
133 |
* we'd see strange file names being matched by the matcher. |
|
134 |
*/ |
|
135 |
Set<Integer> jvmSet = new HashSet<Integer>(); |
|
136 |
||
137 |
if (! tmpdir.isDirectory()) { |
|
138 |
return jvmSet; |
|
139 |
} |
|
140 |
||
141 |
if (userName == null) { |
|
142 |
/* |
|
143 |
* get a list of all of the user temporary directories and |
|
144 |
* iterate over the list to find any files within those directories. |
|
145 |
*/ |
|
146 |
File[] dirs = tmpdir.listFiles(userFilter); |
|
147 |
||
148 |
for (int i = 0 ; i < dirs.length; i ++) { |
|
149 |
if (!dirs[i].isDirectory()) { |
|
150 |
continue; |
|
151 |
} |
|
152 |
||
153 |
// get a list of files from the directory |
|
154 |
File[] files = dirs[i].listFiles(fileFilter); |
|
155 |
||
156 |
if (files != null) { |
|
157 |
for (int j = 0; j < files.length; j++) { |
|
158 |
if (files[j].isFile() && files[j].canRead()) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
159 |
jvmSet.add( |
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
160 |
PerfDataFile.getLocalVmId(files[j])); |
2 | 161 |
} |
162 |
} |
|
163 |
} |
|
164 |
} |
|
165 |
} else { |
|
166 |
/* |
|
167 |
* Check if the user directory can be accessed. Any of these |
|
168 |
* conditions may have asynchronously changed between subsequent |
|
169 |
* calls to this method. |
|
170 |
*/ |
|
171 |
||
172 |
// get the list of files from the specified user directory |
|
173 |
File[] files = tmpdir.listFiles(fileFilter); |
|
174 |
||
175 |
if (files != null) { |
|
176 |
for (int j = 0; j < files.length; j++) { |
|
177 |
if (files[j].isFile() && files[j].canRead()) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
178 |
jvmSet.add( |
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
179 |
PerfDataFile.getLocalVmId(files[j])); |
2 | 180 |
} |
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
// look for any 1.4.1 files |
|
186 |
File[] files = tmpdir.listFiles(tmpFileFilter); |
|
187 |
if (files != null) { |
|
188 |
for (int j = 0; j < files.length; j++) { |
|
189 |
if (files[j].isFile() && files[j].canRead()) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
190 |
jvmSet.add( |
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
5506
diff
changeset
|
191 |
PerfDataFile.getLocalVmId(files[j])); |
2 | 192 |
} |
193 |
} |
|
194 |
} |
|
195 |
||
196 |
return jvmSet; |
|
197 |
} |
|
198 |
} |