49
49
* To test right from NetBeans:
50
50
*
51
51
* 1a/ Predefined import:
52
- * - add -J-Dnetbeans .import_userdir=... to visualvm/nbproject/project.properties
52
+ * - add -J-Dvisualvm .import_userdir=... to visualvm/nbproject/project.properties
53
53
*
54
54
* 1b/ Interactive import:
55
55
* - add -J-Dnetbeans.importclass=org.graalvm.visualvm.modules.startup.ImportSettings to visualvm/nbproject/project.properties
67
67
public class ImportSettings {
68
68
69
69
// List of userdirs supported by the settings importer
70
+ // SHOULD NOT BE EMPTY! At least the current release should be listed
71
+ //
72
+ // First supported release userdir is the first one
70
73
// Most recent release userdir is the last one
71
74
private static final String [] SUPPORTED_USERDIRS = new String [] {
72
75
"2.0" , // NOI18N
@@ -82,104 +85,133 @@ public class ImportSettings {
82
85
83
86
84
87
public static void main (String [] args ) throws Exception {
85
- String importUserdirS = System .getProperty ("netbeans.import_userdir" ); // NOI18N
88
+ // Check current VisualVM userdir
89
+ String userdirS = System .getProperty ("netbeans.user" ); // NOI18N
90
+ final File userdir = userdirS == null || userdirS .isEmpty () ? null : new File (userdirS );
91
+ if (userdir == null || !userdir .isDirectory ()) {
92
+ LOGGER .info ("Skipping import, could not resolve VisualVM userdir: " + userdirS ); // NOI18N
93
+ return ;
94
+ }
95
+
96
+ // Immediate import based on the provided system property
97
+ String importUserdirS = System .getProperty ("visualvm.import_userdir" ); // NOI18N
86
98
if (importUserdirS != null && !importUserdirS .isEmpty ()) {
87
- // immediate import based on the provided system property
88
99
File importUserdir = new File (importUserdirS );
89
- if (importUserdir .isDirectory ()) {
90
- copyToUserdir (importUserdir );
91
- } else {
92
- LOGGER .info ("Skipping import from netbeans.import_userdir, wrong userdir provided " + importUserdir ); // NOI18N
93
- }
94
- } else {
95
- // interactive selection & import of the userdir
96
- String userdirsRootS = System .getProperty ("netbeans.default_userdir_root" ); // NOI18
97
- ///*DEV*/ if (userdirsRootS == null || userdirsRootS.isEmpty()) userdirsRootS = System.getProperty("netbeans.default_userdir_root.dev"); // NOI18
98
-
99
- File userdirsRoot = userdirsRootS == null ? null : new File (userdirsRootS );
100
- if (userdirsRoot == null || !userdirsRoot .isDirectory ()) return ;
100
+
101
+ String msg ;
102
+ if (!importUserdir .isDirectory ()) msg = "not a directory" ; // NOI18N
103
+ else if (userdir .equals (importUserdir )) msg = "own userdir" ; // NOI18N
104
+ else msg = null ;
105
+
106
+ if (msg == null ) copyToUserdir (importUserdir , userdir );
107
+ else LOGGER .info ("Skipping import from visualvm.import_userdir, wrong directory provided (" + msg + "): " + importUserdirS ); // NOI18N
108
+
109
+ return ;
110
+ }
111
+
112
+ // Check VisualVM userdirs root
113
+ String userdirsRootS = System .getProperty ("netbeans.default_userdir_root" ); // NOI18
114
+ /*DEV*/ if (userdirsRootS == null || userdirsRootS .isEmpty ()) userdirsRootS = System .getProperty ("netbeans.default_userdir_root.dev" ); // NOI18
115
+ File userdirsRoot = userdirsRootS == null || userdirsRootS .isEmpty () ? null : new File (userdirsRootS );
116
+ if (userdirsRoot == null || !userdirsRoot .isDirectory ()) {
117
+ LOGGER .info ("Skipping import, could not resolve VisualVM userdirs root: " + userdirsRootS ); // NOI18N
118
+ return ;
119
+ }
101
120
102
- List <File > userdirs = availableUserdirs (userdirsRoot );
103
- if (userdirs .isEmpty ()) return ;
121
+ // Read available userdirs supported for import
122
+ List <File > userdirs = availableUserdirs (userdirsRoot , userdir );
123
+ if (userdirs .isEmpty ()) {
124
+ LOGGER .info ("Skipping import, no supported userdirs found in: " + userdirsRootS ); // NOI18N
125
+ return ;
126
+ }
127
+
104
128
105
- File latestRelease = latestReleaseUserdir (userdirs );
106
- File recentlyUsed = lastRecentlyUsedUserdir (userdirs );
129
+ File latestRelease = latestReleaseUserdir (userdirs );
130
+ File recentlyUsed = lastRecentlyUsedUserdir (userdirs );
107
131
108
132
109
- Utils .setSystemLaF ();
133
+ // Interactive selection & import of the userdir
134
+ Utils .setSystemLaF ();
110
135
111
- final JDialog d = StartupDialog .create (NbBundle .getMessage (
112
- ImportSettings .class , "ImportSettings_Caption" ), null , -1 ); // NOI18N
136
+ final JDialog d = StartupDialog .create (NbBundle .getMessage (
137
+ ImportSettings .class , "ImportSettings_Caption" ), null , -1 ); // NOI18N
113
138
114
- ImportPanel p = new ImportPanel (latestRelease , recentlyUsed , userdirsRoot ) {
115
- @ Override
116
- void contentsChanged () {
117
- SwingUtilities .invokeLater (new Runnable () {
118
- public void run () { d .pack (); }
119
- });
120
- }
139
+ ImportPanel p = new ImportPanel (latestRelease , recentlyUsed , userdirsRoot , SUPPORTED_USERDIRS [0 ]) {
140
+ @ Override
141
+ boolean isSupportedImport (File dir ) {
142
+ return isSupportedUserdir (dir , userdir );
143
+ }
144
+
145
+ @ Override
146
+ void contentsChanged () {
147
+ SwingUtilities .invokeLater (new Runnable () {
148
+ public void run () { d .pack (); }
149
+ });
150
+ }
121
151
122
- @ Override
123
- void beforeImport () {
124
- d .setDefaultCloseOperation (JDialog .DO_NOTHING_ON_CLOSE );
125
- }
152
+ @ Override
153
+ void beforeImport () {
154
+ d .setDefaultCloseOperation (JDialog .DO_NOTHING_ON_CLOSE );
155
+ }
126
156
127
- @ Override
128
- void doImport (File source ) throws Exception {
129
- copyToUserdir (source );
130
- }
157
+ @ Override
158
+ void doImport (File source ) throws Exception {
159
+ copyToUserdir (source , userdir );
160
+ }
131
161
132
- @ Override
133
- void afterImport () {
134
- d .setDefaultCloseOperation (JDialog .DISPOSE_ON_CLOSE );
135
- }
162
+ @ Override
163
+ void afterImport () {
164
+ d .setDefaultCloseOperation (JDialog .DISPOSE_ON_CLOSE );
165
+ }
136
166
137
- @ Override
138
- void close () {
139
- d .setVisible (false );
140
- d .dispose ();
141
- }
142
- };
143
- d .getContentPane ().add (p , BorderLayout .CENTER );
167
+ @ Override
168
+ void close () {
169
+ d .setVisible (false );
170
+ d .dispose ();
171
+ }
172
+ };
173
+ d .getContentPane ().add (p , BorderLayout .CENTER );
144
174
145
- d .getRootPane ().setDefaultButton (p .getDefaultButton ());
146
- d .getRootPane ().registerKeyboardAction (new ActionListener () {
147
- @ Override public void actionPerformed (ActionEvent e ) {
148
- if (d .getDefaultCloseOperation () == JDialog .DISPOSE_ON_CLOSE ) {
149
- d .setVisible (false );
150
- d .dispose ();
151
- }
175
+ d .getRootPane ().setDefaultButton (p .getDefaultButton ());
176
+ d .getRootPane ().registerKeyboardAction (new ActionListener () {
177
+ @ Override public void actionPerformed (ActionEvent e ) {
178
+ if (d .getDefaultCloseOperation () == JDialog .DISPOSE_ON_CLOSE ) {
179
+ d .setVisible (false );
180
+ d .dispose ();
152
181
}
153
- },
154
- KeyStroke .getKeyStroke (KeyEvent .VK_ESCAPE , 0 ),
155
- JComponent .WHEN_IN_FOCUSED_WINDOW );
182
+ }
183
+ },
184
+ KeyStroke .getKeyStroke (KeyEvent .VK_ESCAPE , 0 ),
185
+ JComponent .WHEN_IN_FOCUSED_WINDOW );
156
186
157
- // d.setResizable(true);
158
- d .pack ();
159
- d .setLocationRelativeTo (null );
187
+ // d.setResizable(true);
188
+ d .pack ();
189
+ d .setLocationRelativeTo (null );
160
190
161
- d .setVisible (true );
162
- }
191
+ d .setVisible (true );
163
192
}
164
193
165
194
166
- private static List <File > availableUserdirs (File userdirsRoot ) {
195
+ private static List <File > availableUserdirs (File userdirsRoot , File userdir ) {
167
196
List <File > userdirs = new ArrayList ();
168
197
169
198
for (String supported : SUPPORTED_USERDIRS ) {
170
- File userdir = availableUserdir (userdirsRoot , supported );
171
- if (userdir != null ) userdirs .add (userdir );
199
+ File available = availableUserdir (userdirsRoot , supported , userdir );
200
+ if (available != null ) userdirs .add (available );
172
201
}
173
202
174
203
return userdirs ;
175
204
}
176
205
177
- private static File availableUserdir (File userdirsFolder , String userdir ) {
178
- File file = new File (userdirsFolder , userdir );
179
-
180
- if (!file .isDirectory () || !new File (file , "config" ).isDirectory ()) return null ; // NOI18N
181
-
182
- return file ;
206
+ private static File availableUserdir (File userdirsFolder , String dir , File current ) {
207
+ File file = new File (userdirsFolder , dir );
208
+ return isSupportedUserdir (file , current ) ? file : null ;
209
+ }
210
+
211
+ private static boolean isSupportedUserdir (File userdir , File current ) {
212
+ return !current .equals (userdir ) &&
213
+ userdir .isDirectory () &&
214
+ new File (userdir , "config" ).isDirectory (); // NOI18N
183
215
}
184
216
185
217
private static File latestReleaseUserdir (List <File > userdirs ) {
@@ -204,17 +236,16 @@ private static File lastRecentlyUsedUserdir(List<File> userdirs) {
204
236
205
237
/* Copy files from source folder to current userdir according to include/exclude
206
238
* patterns in etc/netbeans.import file. */
207
- private static void copyToUserdir (File source ) throws IOException , PropertyVetoException {
208
- File userdir = new File (System .getProperty ("netbeans.user" , "" )); // NOI18N
239
+ private static void copyToUserdir (File source , File userdir ) throws IOException , PropertyVetoException {
209
240
File netBeansDir = InstalledFileLocator .getDefault ().locate ("modules" , null , false ).getParentFile ().getParentFile (); // NOI18N
210
241
211
242
File importFile = new File (netBeansDir , "etc/visualvm.import" ); // NOI18N
212
- /// *DEV*/ if (!importFile.isFile() && "testuserdir".equals(userdir.getName())) { // NOI18N
213
- /// *DEV*/ File parent = netBeansDir.getParentFile();
214
- /// *DEV*/ if (parent != null && parent.isDirectory()) {
215
- /// *DEV*/ importFile = new File(parent, "launcher/visualvm.import"); // NOI18N
216
- /// *DEV*/ }
217
- /// *DEV*/ }
243
+ /*DEV*/ if (!importFile .isFile () && "testuserdir" .equals (userdir .getName ())) { // NOI18N
244
+ /*DEV*/ File parent = netBeansDir .getParentFile ();
245
+ /*DEV*/ if (parent != null && parent .isDirectory ()) {
246
+ /*DEV*/ importFile = new File (parent , "launcher/visualvm.import" ); // NOI18N
247
+ /*DEV*/ }
248
+ /*DEV*/ }
218
249
219
250
LOGGER .fine ("Import file: " + importFile ); // NOI18N
220
251
LOGGER .info ("Importing from " + source + " to " + userdir ); // NOI18N
@@ -226,7 +257,7 @@ private static void copyToUserdir(File source) throws IOException, PropertyVetoE
226
257
try {
227
258
cleanupUserdir (userdir , files1 );
228
259
} catch (Exception ee ) {
229
- LOGGER .info ("Failed to cleanup after import failure " + ee ); // NOI18N
260
+ LOGGER .info ("Failed to cleanup after import failure: " + ee ); // NOI18N
230
261
}
231
262
throw e ;
232
263
}
0 commit comments