Skip to content

Commit 24c6618

Browse files
committed
Handle VisualVM closing, notify DataSource views when being closed
- rolling back the core.boot package
1 parent 61ae623 commit 24c6618

File tree

9 files changed

+120
-36
lines changed

9 files changed

+120
-36
lines changed

visualvm/core/manifest.mf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Manifest-Version: 1.0
22
AutoUpdate-Show-In-Client: false
33
AutoUpdate-Essential-Module: true
44
OpenIDE-Module: org.graalvm.visualvm.core/2
5-
OpenIDE-Module-Layer: org/graalvm/visualvm/core/boot/layer.xml
6-
OpenIDE-Module-Localizing-Bundle: org/graalvm/visualvm/core/boot/Bundle.properties
7-
OpenIDE-Module-Install: org/graalvm/visualvm/core/boot/Install.class
8-
OpenIDE-Module-Specification-Version: 2.1
5+
OpenIDE-Module-Layer: org/graalvm/visualvm/core/layer.xml
6+
OpenIDE-Module-Localizing-Bundle: org/graalvm/visualvm/core/Bundle.properties
7+
OpenIDE-Module-Install: org/graalvm/visualvm/core/Install$Impl.class
8+
OpenIDE-Module-Specification-Version: 2.2
99

visualvm/core/src/org/graalvm/visualvm/core/boot/Install.java renamed to visualvm/core/src/org/graalvm/visualvm/core/Install.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,9 +23,7 @@
2323
* questions.
2424
*/
2525

26-
package org.graalvm.visualvm.core.boot;
27-
28-
//import org.graalvm.visualvm.application.ApplicationsSupport;
26+
package org.graalvm.visualvm.core;
2927

3028
import org.graalvm.visualvm.core.datasource.DataSourceRepository;
3129
import org.graalvm.visualvm.core.datasource.Storage;
@@ -38,22 +36,30 @@
3836
*
3937
* @author Jiri Sedlacek
4038
*/
41-
// Class implementing logic on VisualVM module install
42-
public class Install extends ModuleInstall {
43-
44-
public void restored() {
45-
// NOTE: this has to be called before any of DataSourceProviders initializes
46-
cleanupPreviousSession();
47-
48-
DataSourceRepository.sharedInstance();
39+
class Install {
40+
// Class implementing logic on VisualVM module install
41+
public static class Impl extends ModuleInstall {
4942

50-
// Initialize snapshots
51-
SnapshotsSupport.getInstance();
52-
}
53-
54-
private void cleanupPreviousSession() {
55-
File temporaryStorage = new File(Storage.getTemporaryStorageDirectoryString());
56-
Utils.delete(temporaryStorage, false);
43+
@Override
44+
public boolean closing() {
45+
return VisualVM.getInstance().closing();
46+
}
47+
48+
@Override
49+
public void restored() {
50+
// NOTE: this has to be called before any of DataSourceProviders initializes
51+
cleanupPreviousSession();
52+
53+
DataSourceRepository.sharedInstance();
54+
55+
// Initialize snapshots
56+
SnapshotsSupport.getInstance();
57+
}
58+
59+
private void cleanupPreviousSession() {
60+
File temporaryStorage = new File(Storage.getTemporaryStorageDirectoryString());
61+
Utils.delete(temporaryStorage, false);
62+
}
63+
5764
}
58-
5965
}

visualvm/core/src/org/graalvm/visualvm/core/VisualVM.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525
package org.graalvm.visualvm.core;
2626

27+
import java.lang.ref.WeakReference;
28+
import java.util.ArrayList;
29+
import java.util.Collection;
30+
import org.graalvm.visualvm.core.datasupport.ComparableWeakReference;
2731
import org.openide.util.RequestProcessor;
2832

2933
/**
@@ -42,6 +46,8 @@ public final class VisualVM {
4246

4347
private final RequestProcessor taskProcessor;
4448

49+
private Collection<ComparableWeakReference<Runnable>> closingHandlers;
50+
4551

4652
private VisualVM() {
4753
taskProcessor = new RequestProcessor("VisualVM Shared RequestProcessor", TASK_PROCESSOR_THROUGHPUT); // NOI18N
@@ -59,4 +65,30 @@ public final void runTask(Runnable task, int timeToWait) {
5965
taskProcessor.post(task, timeToWait);
6066
}
6167

68+
69+
/**
70+
* Adds a Runnable instance to be notified when the host VisualVM is closing.
71+
* Note that the Runnable cannot be explicitly unregistered, it's weakly referenced and will
72+
* be notified up to once and then unregistered automatically.
73+
*
74+
* @param handler Runnable instance to be notified when the host VisualVM is closing.
75+
*/
76+
public synchronized final void notifyWhenClosing(Runnable handler) {
77+
if (closingHandlers == null) closingHandlers = new ArrayList();
78+
closingHandlers.add(new ComparableWeakReference(handler));
79+
}
80+
81+
82+
synchronized boolean closing() {
83+
if (closingHandlers != null)
84+
for (WeakReference<Runnable> handlerR : closingHandlers) {
85+
Runnable handler = handlerR.get();
86+
if (handler != null)
87+
try { handler.run(); }
88+
catch (Exception e) { System.err.println("Exception handling VisualVM.closing(): " + e); } // NOI18N
89+
}
90+
91+
return true;
92+
}
93+
6294
}

visualvm/core/src/org/graalvm/visualvm/core/boot/layer.xml renamed to visualvm/core/src/org/graalvm/visualvm/core/layer.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
4+
Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
55
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
77
This code is free software; you can redistribute it and/or modify it
@@ -80,7 +80,7 @@
8080
</folder>
8181

8282
<folder name="Applications">
83-
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.boot.Bundle"/>
83+
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.Bundle"/>
8484
<attr name="position" intvalue="200"/>
8585
<file name="org-graalvm-visualvm-core-ui-actions-OpenDataSourceAction.shadow">
8686
<attr name="originalFile" stringvalue="VisualVM/Actions/org-graalvm-visualvm-core-ui-actions-OpenDataSourceAction.instance"/>
@@ -123,7 +123,7 @@
123123
<folder name="Toolbars">
124124
<folder name="Snapshot">
125125
<attr name="position" intvalue="100"/>
126-
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.boot.Bundle"/>
126+
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.Bundle"/>
127127
<file name="org-graalvm-visualvm-core-ui-actions-LoadSnapshotAction.shadow">
128128
<attr name="originalFile" stringvalue="Actions/Other/org-graalvm-visualvm-core-ui-actions-LoadSnapshotAction.instance"/>
129129
<attr name="position" intvalue="100"/>
@@ -136,7 +136,7 @@
136136

137137
<folder name="DataSource">
138138
<attr name="position" intvalue="200"/>
139-
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.boot.Bundle"/>
139+
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.graalvm.visualvm.core.Bundle"/>
140140
</folder>
141141
</folder>
142142

@@ -221,11 +221,11 @@
221221
<folder name="Services">
222222
<folder name="AutoupdateType">
223223
<file name="com_sun_tools_visualvm_core_update_center.instance">
224-
<attr name="displayName" bundlevalue="org.graalvm.visualvm.core.boot.Bundle#Services/AutoupdateType/com_sun_tools_visualvm_core_update_center.instance"/>
224+
<attr name="displayName" bundlevalue="org.graalvm.visualvm.core.Bundle#Services/AutoupdateType/com_sun_tools_visualvm_core_update_center.instance"/>
225225
<attr name="enabled" boolvalue="true"/>
226226
<attr name="instanceCreate" methodvalue="org.netbeans.modules.autoupdate.updateprovider.AutoupdateCatalogFactory.createUpdateProvider"/>
227227
<attr name="instanceOf" stringvalue="org.netbeans.spi.autoupdate.UpdateProvider"/>
228-
<attr name="url" bundlevalue="org.graalvm.visualvm.core.boot.Bundle#com_sun_tools_visualvm_core_update_center"/>
228+
<attr name="url" bundlevalue="org.graalvm.visualvm.core.Bundle#com_sun_tools_visualvm_core_update_center"/>
229229
</file>
230230
</folder>
231231
</folder>

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceView.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -160,6 +160,16 @@ protected void willBeAdded() {
160160
protected void added() {
161161
}
162162

163+
/**
164+
* Notification when the view is about to be either programmatically removed from tabbed pane or closed by the user by clicking the X.
165+
* This notification comes from the EDT thread and its main intention is to
166+
* provide a possibility to save any view data before the view is closed, if
167+
* needed. Long-running operations should use a separate thread to not block
168+
* EDT closing the view.
169+
*/
170+
protected void willBeRemoved() {
171+
}
172+
163173
/**
164174
* Notification when the view has been either programatically removed from tabbed pane or closed by the user by clicking the X.
165175
* This notification comes from a thread other than EDT
@@ -198,6 +208,15 @@ void viewAdded() {
198208
controller.viewAdded(this);
199209
}
200210

211+
private boolean willBeRemovedNotified;
212+
void viewWillBeRemoved() {
213+
if (willBeRemovedNotified) return;
214+
215+
willBeRemoved();
216+
willBeRemovedNotified = true;
217+
controller.viewWillBeRemoved(this);
218+
}
219+
201220
void viewRemoved() {
202221
removed();
203222
controller.viewRemoved(this);

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceViewProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -122,6 +122,9 @@ void viewWillBeAdded(DataSourceView view) {
122122
void viewAdded(DataSourceView view) {
123123
}
124124

125+
void viewWillBeRemoved(DataSourceView view) {
126+
}
127+
125128
void viewRemoved(DataSourceView view) {
126129
synchronized(viewsCache) {
127130
viewsCache.remove((X)view.getDataSource());

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceWindow.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -130,13 +130,15 @@ public void selectView(DataSourceView view) {
130130
public void removeView(final DataSourceView view) {
131131
if (viewsCount == 1) {
132132
if (view != singleViewContainer.getView()) throw new RuntimeException("View " + view + " not present in DataSourceWindow " + this); // NOI18N
133+
view.viewWillBeRemoved();
133134
remove(singleViewContainer);
134135
singleViewContainer.getCaption().finish();
135136
singleViewContainer = null;
136137
} else {
137138
int viewIndex = indexOf(view);
138139
if (viewIndex == -1) throw new RuntimeException("View " + view + " not present in DataSourceWindow " + this); // NOI18N
139-
else tabbedContainer.removeView(viewIndex);
140+
view.viewWillBeRemoved();
141+
tabbedContainer.removeView(viewIndex);
140142

141143
if (viewsCount == 2) {
142144
DataSourceView remaining = tabbedContainer.getViews().get(0);
@@ -162,6 +164,7 @@ public void run() {
162164

163165
void clearView(final DataSourceView view, RequestProcessor notificationProcessor) {
164166
if (viewsCount == 1 && Objects.equals(singleViewContainer.getName(), view.getName())) {
167+
view.viewWillBeRemoved();
165168
singleViewContainer.removeAll();
166169
if (singleViewContainer.getCaption() != null) singleViewContainer.getCaption().finish();
167170
singleViewContainer.setReloading();
@@ -171,6 +174,7 @@ void clearView(final DataSourceView view, RequestProcessor notificationProcessor
171174
int viewIndex = indexOf(view);
172175
if (viewIndex == -1) return;
173176

177+
view.viewWillBeRemoved();
174178
tabbedContainer.clearView(viewIndex);
175179
}
176180

@@ -268,6 +272,12 @@ protected final void componentActivated() {
268272
else if (getComponentCount() > 0) getComponent(0).requestFocusInWindow();
269273
}
270274

275+
public final boolean canClose() {
276+
for (DataSourceView view : getViews()) view.viewWillBeRemoved();
277+
278+
return true;
279+
}
280+
271281
protected final void componentClosed() {
272282
dataSourceDescriptor.removePropertyChangeListener(this);
273283
removeAllViews();

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceWindowManager.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
2828
import org.graalvm.visualvm.core.datasource.DataSource;
2929
import org.graalvm.visualvm.core.datasource.descriptor.DataSourceDescriptorFactory;
3030
import java.util.ArrayList;
31+
import java.util.Collection;
3132
import java.util.Collections;
3233
import java.util.HashMap;
3334
import java.util.HashSet;
@@ -38,6 +39,7 @@
3839
import java.util.logging.Level;
3940
import java.util.logging.Logger;
4041
import javax.swing.SwingUtilities;
42+
import org.graalvm.visualvm.core.VisualVM;
4143
import org.netbeans.api.progress.ProgressHandle;
4244
import org.netbeans.api.progress.ProgressHandleFactory;
4345
import org.openide.util.NbBundle;
@@ -59,6 +61,8 @@ public final class DataSourceWindowManager {
5961

6062
private final Map<DataSource, DataSourceWindow> openedWindows = Collections.synchronizedMap(new HashMap());
6163
private final Map<DataSource, Set<DataSourceView>> openedViews = Collections.synchronizedMap(new HashMap());
64+
65+
private final Runnable closingHandler;
6266

6367

6468
/**
@@ -433,6 +437,16 @@ void unregisterClosedView(DataSourceView view) {
433437
}
434438

435439

436-
private DataSourceWindowManager() {}
440+
private DataSourceWindowManager() {
441+
closingHandler = new Runnable() {
442+
public void run() {
443+
Collection<Set<DataSourceView>> allViews = openedViews.values();
444+
for (Set<DataSourceView> dataSourceViews : allViews)
445+
for (DataSourceView view : dataSourceViews)
446+
view.viewWillBeRemoved();
447+
}
448+
};
449+
VisualVM.getInstance().notifyWhenClosing(closingHandler);
450+
}
437451

438452
}

0 commit comments

Comments
 (0)