3535 * Class for managing the LocalMonitoredVm instances on the local system.
3636 * <p>
3737 * 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
38+ * HotSpot Java Virtual Machines on the local host that can be accessed
39+ * by the current user. The ability to detect all possible HotSpot Java Virtual
4040 * Machines on the local host may be limited by the permissions of the
41- * principal running this JVM.
41+ * current user running this JVM.
4242 *
4343 * @author Brian Doherty
4444 * @since 1.5
4545 */
4646public class LocalVmManager {
47- private String userName ; // user name for monitored jvm
48- private Pattern userPattern ;
49- private Matcher userMatcher ;
50- private FilenameFilter userFilter ;
51- private Pattern filePattern ;
52- private Matcher fileMatcher ;
53- private FilenameFilter fileFilter ;
54- private Pattern tmpFilePattern ;
55- private Matcher tmpFileMatcher ;
56- private FilenameFilter tmpFileFilter ;
47+ private FilenameFilter userDirFilter ;
48+ private FilenameFilter userDirFileFilter ;
49+ private FilenameFilter oldtmpFileFilter ;
5750
5851 /**
5952 * Creates a LocalVmManager instance for the local system.
6053 * <p>
61- * Manages LocalMonitoredVm instances for which the principal
54+ * Manages LocalMonitoredVm instances for which the current user
6255 * has appropriate permissions.
6356 */
6457 public LocalVmManager () {
65- this (null );
66- }
67-
68- /**
69- * Creates a LocalVmManager instance for the given user.
70- * <p>
71- * Manages LocalMonitoredVm instances for all JVMs owned by the specified
72- * user.
73- *
74- * @param user the name of the user
75- */
76- public LocalVmManager (String user ) {
77- this .userName = user ;
78-
79- if (userName == null ) {
80- userPattern = Pattern .compile (PerfDataFile .userDirNamePattern );
81- userMatcher = userPattern .matcher ("" );
82-
83- userFilter = new FilenameFilter () {
84- public boolean accept (File dir , String name ) {
85- userMatcher .reset (name );
86- return userMatcher .lookingAt ();
87- }
88- };
89- }
90-
91- filePattern = Pattern .compile (PerfDataFile .fileNamePattern );
92- fileMatcher = filePattern .matcher ("" );
93-
94- fileFilter = new FilenameFilter () {
58+ // 1.4.2 and later: The files are in {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
59+ Pattern userDirPattern = Pattern .compile (PerfDataFile .userDirNamePattern );
60+ userDirFilter = new FilenameFilter () {
9561 public boolean accept (File dir , String name ) {
96- fileMatcher .reset (name );
97- return fileMatcher .matches ();
62+ return userDirPattern .matcher (name ).lookingAt ();
9863 }
9964 };
10065
101- tmpFilePattern = Pattern .compile (PerfDataFile .tmpFileNamePattern );
102- tmpFileMatcher = tmpFilePattern .matcher ("" );
66+ Pattern userDirFilePattern = Pattern .compile (PerfDataFile .fileNamePattern );
67+ userDirFileFilter = new FilenameFilter () {
68+ public boolean accept (File dir , String name ) {
69+ return userDirFilePattern .matcher (name ).matches ();
70+ }
71+ };
10372
104- tmpFileFilter = new FilenameFilter () {
73+ // 1.4.1 (or earlier?): the files are stored directly under {tmpdir}/ with
74+ // the following pattern.
75+ Pattern oldtmpFilePattern = Pattern .compile (PerfDataFile .tmpFileNamePattern );
76+ oldtmpFileFilter = new FilenameFilter () {
10577 public boolean accept (File dir , String name ) {
106- tmpFileMatcher .reset (name );
107- return tmpFileMatcher .matches ();
78+ return oldtmpFilePattern .matcher (name ).matches ();
10879 }
10980 };
11081 }
11182
11283 /**
113- * Return the current set of monitorable Java Virtual Machines.
114- * <p>
115- * The set returned by this method depends on the user name passed
116- * to the constructor. If no user name was specified, then this
117- * method will return all candidate JVMs on the system. Otherwise,
118- * only the JVMs for the given user will be returned. This assumes
119- * that principal associated with this JVM has the appropriate
120- * permissions to access the target set of JVMs.
84+ * Return the current set of monitorable Java Virtual Machines that
85+ * are accessible by the current user.
12186 *
12287 * @return Set - the Set of monitorable Java Virtual Machines
12388 */
12489 public synchronized Set <Integer > activeVms () {
12590 /*
126- * This method is synchronized because the Matcher object used by
127- * fileFilter is not safe for concurrent use, and this method is
128- * called by multiple threads. Before this method was synchronized,
129- * we'd see strange file names being matched by the matcher.
91+ * TODO: this method was synchronized due to its thread-unsafe use of the regexp
92+ * Matcher objects. That is not the case anymore, but I am too afraid to change
93+ * it now. Maybe fix this later in a separate RFE.
13094 */
13195 Set <Integer > jvmSet = new HashSet <Integer >();
132- List <String > tmpdirs = PerfDataFile .getTempDirectories (userName , 0 );
96+ List <String > tmpdirs = PerfDataFile .getTempDirectories (0 );
13397
13498 for (String dir : tmpdirs ) {
13599 File tmpdir = new File (dir );
136100 if (! tmpdir .isDirectory ()) {
137101 continue ;
138102 }
139103
140- if (userName == null ) {
141- /*
142- * get a list of all of the user temporary directories and
143- * iterate over the list to find any files within those directories.
144- */
145- File [] dirs = tmpdir .listFiles (userFilter );
146- for (int i = 0 ; i < dirs .length ; i ++) {
147- if (!dirs [i ].isDirectory ()) {
148- continue ;
149- }
150104
151- // get a list of files from the directory
152- File [] files = dirs [i ].listFiles (fileFilter );
153- if (files != null ) {
154- for (int j = 0 ; j < files .length ; j ++) {
155- if (files [j ].isFile () && files [j ].canRead ()) {
156- int vmid = PerfDataFile .getLocalVmId (files [j ]);
157- if (vmid != -1 ) {
158- jvmSet .add (vmid );
159- }
160- }
161- }
162- }
105+ // 1.4.2 and later: Look for the files {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
106+ // that are readable by the current user.
107+ File [] dirs = tmpdir .listFiles (userDirFilter );
108+ for (int i = 0 ; i < dirs .length ; i ++) {
109+ if (!dirs [i ].isDirectory ()) {
110+ continue ;
163111 }
164- } else {
165- /*
166- * Check if the user directory can be accessed. Any of these
167- * conditions may have asynchronously changed between subsequent
168- * calls to this method.
169- */
170-
171- // get the list of files from the specified user directory
172- File [] files = tmpdir .listFiles (fileFilter );
173112
113+ // get a list of files from the directory
114+ File [] files = dirs [i ].listFiles (userDirFileFilter );
174115 if (files != null ) {
175116 for (int j = 0 ; j < files .length ; j ++) {
176117 if (files [j ].isFile () && files [j ].canRead ()) {
@@ -183,8 +124,8 @@ public synchronized Set<Integer> activeVms() {
183124 }
184125 }
185126
186- // look for any 1.4.1 files
187- File [] files = tmpdir .listFiles (tmpFileFilter );
127+ // look for any 1.4.1 files that are readable by the current user.
128+ File [] files = tmpdir .listFiles (oldtmpFileFilter );
188129 if (files != null ) {
189130 for (int j = 0 ; j < files .length ; j ++) {
190131 if (files [j ].isFile () && files [j ].canRead ()) {
0 commit comments