32
32
import org .graalvm .visualvm .application .Application ;
33
33
import org .graalvm .visualvm .core .datasource .Storage ;
34
34
import org .graalvm .visualvm .core .datasupport .Utils ;
35
+ import org .graalvm .visualvm .jmx .impl .JmxApplication ;
36
+ import org .netbeans .api .keyring .Keyring ;
35
37
36
38
/**
37
39
* EnvironmentProvider adding the JMXConnector.CREDENTIALS property to the JMX
@@ -109,7 +111,7 @@ public Custom(String username, char[] password, boolean persistent) {
109
111
110
112
111
113
public Map <String , ?> getEnvironment (Application application , Storage storage ) {
112
- return createMap (user , pword == null ? null : Arrays . copyOf ( pword , pword . length ));
114
+ return createMap (user , getPassword ( storage ));
113
115
}
114
116
115
117
public String getEnvironmentId (Storage storage ) {
@@ -120,11 +122,24 @@ public String getEnvironmentId(Storage storage) {
120
122
public void saveEnvironment (Storage storage ) {
121
123
if (!persistent ) return ;
122
124
storage .setCustomProperty (PROPERTY_USER , user );
123
- storage .setCustomProperty (PROPERTY_PWORD , new String (pword ));
125
+ String keyringId = JmxApplication .createId (null , this , storage );
126
+ char [] pw = getPassword (storage );
127
+ if (pw != null ) {
128
+ Keyring .save (keyringId , pw , "VisualVM - JMX password for " +user ); // NOI18N
129
+ } else {
130
+ Keyring .delete (keyringId );
131
+ }
124
132
}
125
133
126
134
127
135
String getUsername (Storage storage ) { return user ; }
136
+
137
+ private char [] getPassword (Storage storage ) {
138
+ if (hasPassword (storage )) {
139
+ return decodePassword (Arrays .copyOf (pword , pword .length ));
140
+ }
141
+ return null ;
142
+ }
128
143
129
144
boolean hasPassword (Storage storage ) { return pword != null &&
130
145
pword .length > 0 ; }
@@ -143,8 +158,7 @@ public static class Persistent extends CredentialsProvider {
143
158
144
159
public Map <String , ?> getEnvironment (Application application , Storage storage ) {
145
160
String user = storage .getCustomProperty (PROPERTY_USER );
146
- String pword = storage .getCustomProperty (PROPERTY_PWORD );
147
- return createMap (user , pword == null ? null : pword .toCharArray ());
161
+ return createMap (user , getPassword (storage ));
148
162
}
149
163
150
164
public String getEnvironmentId (Storage storage ) {
@@ -159,9 +173,27 @@ public String getEnvironmentId(Storage storage) {
159
173
String getUsername (Storage storage ) { return storage .getCustomProperty (
160
174
PROPERTY_USER ); }
161
175
162
- boolean hasPassword (Storage storage ) {
176
+ private char [] getPassword (Storage storage ) {
177
+ String keyringId = JmxApplication .createId (null , this , storage );
178
+ char [] pw = Keyring .read (keyringId );
179
+ if (pw != null ) {
180
+ return pw ;
181
+ }
182
+ // read old settings
163
183
String pword = storage .getCustomProperty (PROPERTY_PWORD );
164
- return pword != null && !pword .isEmpty ();
184
+ if (pword != null && !pword .isEmpty ()) {
185
+ // migrate old settings to Keyring
186
+ // Keyring.save(keyringId, decodePassword(pword.toCharArray()), "VisualVM - JMX password for "+getUsername(storage)); // NOI18N
187
+ // storage.clearCustomProperty(PROPERTY_PWORD);
188
+ //return getPassword(storage);
189
+ return decodePassword (pword .toCharArray ());
190
+ }
191
+ return null ;
192
+ }
193
+
194
+ boolean hasPassword (Storage storage ) {
195
+ char [] pword = getPassword (storage );
196
+ return pword != null && pword .length >0 ;
165
197
}
166
198
167
199
boolean isPersistent (Storage storage ) {
@@ -178,10 +210,9 @@ boolean isPersistent(Storage storage) {
178
210
Map <String , Object > map = new HashMap <>();
179
211
180
212
if (username != null && !username .isEmpty ()) {
181
- map .put (JMXConnector .CREDENTIALS , new String [] { username , pword == null ? null : new String (decodePassword (pword )) });
182
- } else {
183
- if (pword != null ) Arrays .fill (pword , (char )0 );
213
+ map .put (JMXConnector .CREDENTIALS , new String [] { username , pword == null ? null : new String (pword ) });
184
214
}
215
+ if (pword != null ) Arrays .fill (pword , (char )0 );
185
216
186
217
return map ;
187
218
}
0 commit comments