Skip to content

Commit 6aa4986

Browse files
jisedlacthurka
authored andcommitted
Make sure an external snapshot is opened just once for the same snapshot file.
1 parent bcfa654 commit 6aa4986

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

visualvm/application/src/com/sun/tools/visualvm/application/snapshot/ApplicationSnapshotProvider.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ public void run() {
191191

192192
File storageDirectory = persistent ? ApplicationSnapshotsSupport.getStorageDirectory() :
193193
Storage.getTemporaryStorageDirectory();
194-
File snapshotDirectory = Utils.extractArchive(archive, storageDirectory);
194+
195+
File snapshotDirectory = new File(storageDirectory, archive.getName());
196+
197+
// Only extract the archive if not already extracted (subsequent opening of the same snapshot)
198+
if (!snapshotDirectory.isDirectory() || !snapshotDirectory.canRead())
199+
snapshotDirectory = Utils.extractArchive(archive, storageDirectory);
200+
195201
if (snapshotDirectory != null) {
196202
Storage storage = new Storage(snapshotDirectory, PROPERTIES_FILENAME);
197203
ApplicationSnapshot snapshot = new ApplicationSnapshot(snapshotDirectory, storage);

visualvm/core/src/com/sun/tools/visualvm/core/snapshot/Snapshot.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.sun.tools.visualvm.core.datasupport.Utils;
3131
import com.sun.tools.visualvm.core.ui.DataSourceWindowManager;
3232
import java.io.File;
33+
import java.util.Objects;
3334

3435
/**
3536
* Abstract implementation of Snapshot.
@@ -46,6 +47,8 @@ public abstract class Snapshot extends DataSource {
4647
private File file;
4748
private final SnapshotCategory category;
4849

50+
private String snapshotID;
51+
4952

5053
/**
5154
* Creates new instance of AbstractSnapshot with the data stored in a file.
@@ -98,6 +101,7 @@ protected final void setFile(File newFile) {
98101
if (file == null && newFile == null) return;
99102
File oldFile = file;
100103
file = newFile;
104+
if (oldFile == null) snapshotID = null;
101105
getChangeSupport().firePropertyChange(PROPERTY_FILE, oldFile, newFile);
102106
}
103107

@@ -193,5 +197,34 @@ protected Storage createStorage() {
193197
protected final boolean isInSnapshot() {
194198
return getOwner() instanceof Snapshot;
195199
}
200+
201+
202+
/**
203+
* Returns ID of the Snapshot. The ID should be based on the snapshot file
204+
* if available and will only be computed for the first non-null file.
205+
*
206+
* @return ID of the Snapshot
207+
*
208+
* @since VisualVM 1.4
209+
*/
210+
protected String computeSnapshotID() {
211+
File f = getFile();
212+
return f == null ? super.hashCode() + "-no_file" : f.getPath(); // NOI18N
213+
}
214+
215+
private String getSnapshotID() {
216+
if (snapshotID == null) snapshotID = computeSnapshotID();
217+
return snapshotID;
218+
}
219+
220+
221+
public boolean equals(Object o) {
222+
if (!(o instanceof Snapshot)) return false;
223+
return Objects.equals(getSnapshotID(), ((Snapshot)o).getSnapshotID());
224+
}
225+
226+
public int hashCode() {
227+
return Objects.hashCode(getSnapshotID());
228+
}
196229

197230
}

0 commit comments

Comments
 (0)