premek commented on a change in pull request #2906:
URL: https://github.com/apache/netbeans/pull/2906#discussion_r615458049



##########
File path: 
platform/keyring.impl/src/org/netbeans/modules/keyring/gnome/libsecret/GnomeLibSecretProvider.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.keyring.gnome.libsecret;
+
+import com.sun.jna.Pointer;
+import com.sun.jna.Structure;
+import com.sun.jna.ptr.PointerByReference;
+import java.text.MessageFormat;
+import java.util.MissingResourceException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.netbeans.modules.keyring.gnome.libsecret.Glib.GError;
+import org.netbeans.modules.keyring.gnome.libsecret.LibSecret.SecretSchema;
+import 
org.netbeans.modules.keyring.gnome.libsecret.LibSecret.SecretSchemaAttribute;
+import org.netbeans.spi.keyring.KeyringProvider;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+@ServiceProvider(service = KeyringProvider.class, position = 150)
+public class GnomeLibSecretProvider implements KeyringProvider {
+
+    private static final Logger LOG = 
Logger.getLogger(GnomeLibSecretProvider.class.getName());
+    private static final String KEY = "key"; // NOI18N
+
+    private final String appName;
+    private final LibSecret.SecretSchema schema;
+
+    public GnomeLibSecretProvider() {
+        appName = getAppName();
+
+        schema = new SecretSchema();
+        schema.name = appName;
+        schema.flags = LibSecret.SECRET_SCHEMA_NONE;
+        schema.attributes[0] = new SecretSchemaAttribute();
+        schema.attributes[0].name = KEY;
+        schema.attributes[0].type = LibSecret.SECRET_SCHEMA_ATTRIBUTE_STRING;
+    }
+
+    private String getAppName() {
+        try {
+            return MessageFormat.format(
+                    
NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").getString("CTL_MainWindow_Title_No_Project"),
+                    "…");
+        } catch (MissingResourceException x) {
+            return "NetBeans"; // NOI18N
+        }
+    }
+
+    @Override
+    public boolean enabled() {
+        if (Boolean.getBoolean("netbeans.keyring.no.native")) {
+            LOG.fine("native keyring integration disabled");
+            return false;
+        }
+
+        try {
+            read("NoNeXiStEnT"); // NOI18N
+            return true;
+        } catch (RuntimeException e) {
+            LOG.log(Level.FINE, null, e);
+            return false;
+        }
+    }
+
+    @Override
+    public char[] read(String key) {
+        PointerByReference gerrorBuffer = new PointerByReference();
+
+        Pointer pointer = 
LibSecret.INSTANCE.secret_password_lookup_sync(schema, null, gerrorBuffer, KEY, 
key);
+
+        if (gerrorBuffer.getValue() != null) {
+            processError(gerrorBuffer);
+            return null;
+        }
+
+        if (pointer == null) {
+            return null;
+        }
+
+        String secret = pointer.getString(0);
+        if (secret == null) {
+            LOG.warning("Secret is null");
+            return null;
+        }
+
+        return secret.toCharArray();
+    }
+
+    @Override
+    public void save(String key, char[] password, String description) {
+        PointerByReference gerrorBuffer = new PointerByReference();
+
+        String label = appName + " - " + (description != null ? description : 
key);
+        LibSecret.INSTANCE.secret_password_store_sync(schema, 
LibSecret.SECRET_COLLECTION_DEFAULT, label, new String(password), null, 
gerrorBuffer, KEY, key);
+
+        if (gerrorBuffer.getValue() != null) {
+            processError(gerrorBuffer);
+        }
+    }
+
+    @Override
+    public void delete(String key) {
+        PointerByReference gerrorBuffer = new PointerByReference();
+
+        LibSecret.INSTANCE.secret_password_clear_sync(schema, null, 
gerrorBuffer, KEY, key);
+
+        if (gerrorBuffer.getValue() != null) {
+            processError(gerrorBuffer);
+        }
+    }
+
+    private void processError(PointerByReference gerrorBuffer) throws 
IllegalArgumentException {

Review comment:
       the old GnomeProvider also does not throw exceptions when the native 
code returns an error, just returns null in case of `read` and just logs the 
error in case of `read`, `save` and `delete`. 
   It throws an exception when the native library is not available which is 
used in the `enabled` method.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to