]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/auth/AuthenticationUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / auth / AuthenticationUtils.java
index 4c17ae3c59bbc191702e1913f39c9f069a3deb48..01977a7d8a1f7395613532fa6a3e39263f879021 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.auth;\r
-\r
-import java.io.IOException;\r
-\r
-import org.eclipse.equinox.security.storage.ISecurePreferences;\r
-import org.eclipse.equinox.security.storage.SecurePreferencesFactory;\r
-import org.eclipse.equinox.security.storage.StorageException;\r
-import org.eclipse.jface.dialogs.Dialog;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.simantics.db.management.discovery.ServerInfo;\r
-import org.simantics.db.service.ServerInformation;\r
-import org.simantics.ui.auth.model.LoginModel;\r
-import org.simantics.ui.internal.Activator;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class AuthenticationUtils {\r
-\r
-    /**\r
-     * @param forServer\r
-     * @param forceQuery always present query dialog regardless of the remember\r
-     *        setting attribute\r
-     * @return\r
-     */\r
-    public static LoginModel queryAuthenticationInfo(Shell parentShell, ServerInfo forServer, boolean forceQuery) {\r
-        LoginModel model = loadAuthenticationInfo(forServer);\r
-        if (model == null)\r
-            model = new LoginModel(forServer);\r
-\r
-        boolean remember = model.isRemember();\r
-        if (remember) {\r
-            // Verify that the model data contains at least something before\r
-            // skipping the interactive query.\r
-            if (model.getName().trim().isEmpty())\r
-                remember = false;\r
-        }\r
-        if (!remember || forceQuery) {\r
-            LoginDialog dialog = new LoginDialog(parentShell, Activator.getDefault().getDialogSettings(), model);\r
-            if (dialog.open() != Dialog.OK)\r
-                return null;\r
-        }\r
-\r
-        return model;\r
-    }\r
-\r
-    private static LoginModel loadAuthenticationInfo(ServerInfo forServer) {\r
-        // Cannot store any info if the server cannot be identified uniquely.\r
-        ServerInformation info = forServer.getInfo();\r
-        if (info == null)\r
-            return null;\r
-        String id = formId(info);\r
-\r
-//        try {\r
-//            Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);\r
-//            if (!preferences.nodeExists(id))\r
-//                return null;\r
-//            Preferences server = preferences.node(id);\r
-//            \r
-//            String name = server.get("name", "");\r
-//            String pass = server.get("pass", "");\r
-//\r
-//            return new LoginModel(forServer, name, pass, true, true);\r
-//        } catch (BackingStoreException e) {\r
-//            // Don't pass this forward, just log it.\r
-//            ExceptionUtils.logAndShowError(e);\r
-//            return null;\r
-//        }\r
-\r
-        try {\r
-            ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");\r
-            if (!preferences.nodeExists(id))\r
-                return null;\r
-            ISecurePreferences server = preferences.node(id);\r
-            \r
-            String name = server.get("name", "");\r
-            String pass = server.get("pass", "");\r
-\r
-            return new LoginModel(forServer, name, pass, true, true);\r
-        } catch (StorageException e) {\r
-            // Don't pass this forward, just log it.\r
-            ExceptionUtils.logAndShowError(e);\r
-            return null;\r
-        }\r
-    }\r
-    \r
-    public static void storeAuthenticationInfo(ServerInfo forServer, LoginModel model) throws IOException {\r
-        ServerInformation info = forServer.getInfo();\r
-        if (info == null)\r
-            throw new IllegalArgumentException("server information not available for uniquely identifying the server");\r
-        String id = formId(info);\r
-\r
-//        Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);\r
-//        Preferences server = preferences.node(id);\r
-//        server.put("name", model.getName());\r
-//        server.put("hash", hashData);\r
-//        try {\r
-//            server.flush();\r
-//        } catch (BackingStoreException e) {\r
-//            throw new IOException(e);\r
-//        }\r
-\r
-        ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");\r
-        ISecurePreferences server = preferences.node(id);\r
-        try {\r
-            server.put("name", model.getName(), false);\r
-            server.put("pass", model.getPassword(), true);\r
-            server.flush();\r
-        } catch (StorageException e) {\r
-            throw new IOException(e);\r
-        }\r
-    }\r
-\r
-    private static String formId(ServerInformation info) {\r
-        return info.getDatabaseId();\r
-    }\r
-    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.auth;
+
+import java.io.IOException;
+
+import org.eclipse.equinox.security.storage.ISecurePreferences;
+import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
+import org.eclipse.equinox.security.storage.StorageException;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.db.management.discovery.ServerInfo;
+import org.simantics.db.service.ServerInformation;
+import org.simantics.ui.auth.model.LoginModel;
+import org.simantics.ui.internal.Activator;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class AuthenticationUtils {
+
+    /**
+     * @param forServer
+     * @param forceQuery always present query dialog regardless of the remember
+     *        setting attribute
+     * @return
+     */
+    public static LoginModel queryAuthenticationInfo(Shell parentShell, ServerInfo forServer, boolean forceQuery) {
+        LoginModel model = loadAuthenticationInfo(forServer);
+        if (model == null)
+            model = new LoginModel(forServer);
+
+        boolean remember = model.isRemember();
+        if (remember) {
+            // Verify that the model data contains at least something before
+            // skipping the interactive query.
+            if (model.getName().trim().isEmpty())
+                remember = false;
+        }
+        if (!remember || forceQuery) {
+            LoginDialog dialog = new LoginDialog(parentShell, Activator.getDefault().getDialogSettings(), model);
+            if (dialog.open() != Dialog.OK)
+                return null;
+        }
+
+        return model;
+    }
+
+    private static LoginModel loadAuthenticationInfo(ServerInfo forServer) {
+        // Cannot store any info if the server cannot be identified uniquely.
+        ServerInformation info = forServer.getInfo();
+        if (info == null)
+            return null;
+        String id = formId(info);
+
+//        try {
+//            Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);
+//            if (!preferences.nodeExists(id))
+//                return null;
+//            Preferences server = preferences.node(id);
+//            
+//            String name = server.get("name", "");
+//            String pass = server.get("pass", "");
+//
+//            return new LoginModel(forServer, name, pass, true, true);
+//        } catch (BackingStoreException e) {
+//            // Don't pass this forward, just log it.
+//            ExceptionUtils.logAndShowError(e);
+//            return null;
+//        }
+
+        try {
+            ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
+            if (!preferences.nodeExists(id))
+                return null;
+            ISecurePreferences server = preferences.node(id);
+            
+            String name = server.get("name", "");
+            String pass = server.get("pass", "");
+
+            return new LoginModel(forServer, name, pass, true, true);
+        } catch (StorageException e) {
+            // Don't pass this forward, just log it.
+            ExceptionUtils.logAndShowError(e);
+            return null;
+        }
+    }
+    
+    public static void storeAuthenticationInfo(ServerInfo forServer, LoginModel model) throws IOException {
+        ServerInformation info = forServer.getInfo();
+        if (info == null)
+            throw new IllegalArgumentException("server information not available for uniquely identifying the server");
+        String id = formId(info);
+
+//        Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);
+//        Preferences server = preferences.node(id);
+//        server.put("name", model.getName());
+//        server.put("hash", hashData);
+//        try {
+//            server.flush();
+//        } catch (BackingStoreException e) {
+//            throw new IOException(e);
+//        }
+
+        ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
+        ISecurePreferences server = preferences.node(id);
+        try {
+            server.put("name", model.getName(), false);
+            server.put("pass", model.getPassword(), true);
+            server.flush();
+        } catch (StorageException e) {
+            throw new IOException(e);
+        }
+    }
+
+    private static String formId(ServerInformation info) {
+        return info.getDatabaseId();
+    }
+    
+}