Skip to content

Commit f5eb113

Browse files
committed
GH-89 Introducing %pid and %PID parameters for visualvm.display.name
1 parent 1ac5031 commit f5eb113

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

visualvm/application/nbproject/project.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<compile-dependency/>
1313
<run-dependency>
1414
<release-version>0</release-version>
15-
<specification-version>1.5</specification-version>
15+
<specification-version>1.7</specification-version>
1616
</run-dependency>
1717
</dependency>
1818
<dependency>

visualvm/application/src/org/graalvm/visualvm/application/ApplicationDescriptor.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
public class ApplicationDescriptor extends DataSourceDescriptor<Application> {
4444

4545
private static final String DISPLAY_NAME_PROPERTY = "-Dvisualvm.display.name="; // NOI18N
46-
46+
47+
private static final String pid_PARAM = "%pid"; // NOI18N
48+
private static final String PID_PARAM = "%PID"; // NOI18N
49+
50+
4751
private String name;
4852

4953
/**
@@ -88,7 +92,7 @@ public void propertyChange(PropertyChangeEvent evt) {
8892
} else {
8993
// Descriptor doesn't support renaming, set name for overriden getName()
9094
String oldName = name;
91-
name = createGenericName(application, type.getName());
95+
name = formatName(createGenericName(application, type.getName()));
9296
PropertyChangeSupport pcs = ApplicationDescriptor.this.getChangeSupport();
9397
pcs.firePropertyChange(PROPERTY_NAME, oldName, name);
9498
}
@@ -152,6 +156,21 @@ private static String resolveCustomName(Application application) {
152156
return null;
153157
}
154158

159+
protected String formatName(String namePattern) {
160+
if (namePattern == null) return null;
161+
162+
String formatted = namePattern;
163+
164+
Integer pid = namePattern.contains(pid_PARAM) || namePattern.contains(PID_PARAM) ? getDataSource().getPid() : null;
165+
if (pid != null) {
166+
boolean unknownPid = Application.UNKNOWN_PID == pid;
167+
formatted = formatted.replace(pid_PARAM, unknownPid ? "unknown" : pid.toString()); // NOI18N
168+
formatted = formatted.replace(PID_PARAM, unknownPid ? " (unknown pid) " : " (pid " + pid.toString() + ") ").trim(); // NOI18N
169+
}
170+
171+
return formatted;
172+
}
173+
155174
private static String createGenericName(Application application, String nameBase) {
156175
int pid = application.getPid();
157176
String id = Application.CURRENT_APPLICATION.getPid() == pid ||

visualvm/core/src/org/graalvm/visualvm/core/datasource/descriptor/DataSourceDescriptor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public DataSourceDescriptor(X ds, String n, String desc, Image ic, int pos, int
123123

124124
dataSource = ds;
125125
changeSupport = new PropertyChangeSupport(dataSource);
126-
name = n;
126+
name = formatName(n); // NOTE: called after dataSource is set, should work fine in subclasses with overriden formatName()
127127
description = desc;
128128
icon = ic;
129129
preferredPosition = pos;
@@ -158,9 +158,9 @@ public void setName(String newName) {
158158
if (!supportsRename()) throw new UnsupportedOperationException("Rename not supported for this descriptor"); // NOI18N
159159
if (newName == null) throw new IllegalArgumentException("Name cannot be null"); // NOI18N
160160
String oldName = name;
161-
name = newName;
161+
name = formatName(newName);
162162
getDataSource().getStorage().setCustomProperties(new String[] { PROPERTY_NAME }, new String[] { newName });
163-
getChangeSupport().firePropertyChange(PROPERTY_NAME, oldName, newName);
163+
getChangeSupport().firePropertyChange(PROPERTY_NAME, oldName, name);
164164
}
165165

166166
/**
@@ -172,6 +172,18 @@ public String getName() {
172172
return name;
173173
}
174174

175+
/**
176+
* Enables subclasses to process (format) the provided name of the DataSource.
177+
*
178+
* @param namePattern name of the DataSource to be processed (formatted)
179+
* @return processed (formatted) name of the DataSource.
180+
*
181+
* @since VisualVM 1.4.3
182+
*/
183+
protected String formatName(String namePattern) {
184+
return namePattern;
185+
}
186+
175187
/**
176188
* Returns description of the DataSource.
177189
*

0 commit comments

Comments
 (0)