[jbosstools-commits] JBoss Tools SVN: r24186 - in trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core: META-INF and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Aug 16 15:59:46 EDT 2010


Author: jjohnstn
Date: 2010-08-16 15:59:46 -0400 (Mon, 16 Aug 2010)
New Revision: 24186

Modified:
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java
Log:
2010-08-16  Jeff Johnston  <jjohnstn at redhat.com>

	* META-INF/MANIFEST.MF: Add dependency on org.eclipse.equinox.security.
	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java (DeltaCloud): Store
	the password for the cloud using Secure Preferences using the cloud's url
	and username to form the key. 
	(getPreferencesKey): New static method to formulate a preferences key used
	to store and retrieve a password for a cloud.
	* src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java (loadClouds): 
	New method to get persisted clouds. 
	(getClouds): Remove test stuff that creates a sample connection to the
	mock cloud.
	(saveClouds): New method to persist clouds minus passwords.
	(DeltaCloudManager): Add call to loadClouds at construction.
	(addCloud): Call saveClouds after modifying list.
	(removeCloud): Ditto.



Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-08-16 18:58:09 UTC (rev 24185)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-08-16 19:59:46 UTC (rev 24186)
@@ -1,3 +1,20 @@
+2010-08-16  Jeff Johnston  <jjohnstn at redhat.com>
+
+	* META-INF/MANIFEST.MF: Add dependency on org.eclipse.equinox.security.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java (DeltaCloud): Store
+	the password for the cloud using Secure Preferences using the cloud's url
+	and username to form the key. 
+	(getPreferencesKey): New static method to formulate a preferences key used
+	to store and retrieve a password for a cloud.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java (loadClouds): 
+	New method to get persisted clouds. 
+	(getClouds): Remove test stuff that creates a sample connection to the
+	mock cloud.
+	(saveClouds): New method to persist clouds minus passwords.
+	(DeltaCloudManager): Add call to loadClouds at construction.
+	(addCloud): Call saveClouds after modifying list.
+	(removeCloud): Ditto.
+
 2010-08-13  Jeff Johnston  <jjohnstn at redhat.com>
 
 	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java (DeltaCloud): Take the

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF	2010-08-16 18:58:09 UTC (rev 24185)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF	2010-08-16 19:59:46 UTC (rev 24186)
@@ -6,7 +6,8 @@
 Bundle-Activator: org.jboss.tools.deltacloud.core.Activator
 Bundle-Vendor: Red Hat Inc.
 Require-Bundle: org.eclipse.core.runtime,
- org.apache.log4j;bundle-version="1.2.13"
+ org.apache.log4j;bundle-version="1.2.13",
+ org.eclipse.equinox.security;bundle-version="1.0.100"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ActivationPolicy: lazy
 Export-Package: org.jboss.tools.deltacloud.core;x-friends:="org.jboss.tools.deltacloud.ui"

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-08-16 18:58:09 UTC (rev 24185)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-08-16 19:59:46 UTC (rev 24186)
@@ -7,6 +7,10 @@
 import java.util.List;
 
 import org.eclipse.core.runtime.ListenerList;
+import org.eclipse.equinox.security.storage.EncodingUtils;
+import org.eclipse.equinox.security.storage.ISecurePreferences;
+import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
+import org.eclipse.equinox.security.storage.StorageException;
 import org.jboss.tools.deltacloud.core.client.DeltaCloudAuthException;
 import org.jboss.tools.deltacloud.core.client.DeltaCloudClient;
 import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
@@ -27,12 +31,32 @@
 	ListenerList imageListeners = new ListenerList();
 	
 	public DeltaCloud(String name, String url, String username, String passwd) throws MalformedURLException {
+		this(name, url, username, passwd, false);
+	}
+
+	public DeltaCloud(String name, String url, String username, String passwd, boolean persistent) throws MalformedURLException {
 		this.client = new DeltaCloudClient(new URL(url + "/api"), username, passwd); //$NON-NLS-1$
 		this.url = url;
 		this.name = name;
 		this.username = username;
+		if (persistent) {
+			ISecurePreferences root = SecurePreferencesFactory.getDefault();
+			String key = DeltaCloud.getPreferencesKey(url, username);
+			ISecurePreferences node = root.node(key);
+			try {
+				node.put("password", passwd, true /*encrypt*/);
+			} catch (StorageException e) {
+				e.printStackTrace();
+			}
+		}
 	}
 
+	public static String getPreferencesKey(String url, String username) {
+		String key = "/org/jboss/tools/deltacloud/core/"; //$NON-NLS-1$
+		key += url + "/" + username; //$NON-NLS-1$
+		return EncodingUtils.encodeSlashes(key);
+	}
+	
 	public String getName() {
 		return name;
 	}

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java	2010-08-16 18:58:09 UTC (rev 24185)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java	2010-08-16 19:59:46 UTC (rev 24186)
@@ -1,20 +1,106 @@
 package org.jboss.tools.deltacloud.core;
 
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.MalformedURLException;
-import java.net.URL;
 import java.util.ArrayList;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.ListenerList;
+import org.eclipse.equinox.security.storage.ISecurePreferences;
+import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 public class DeltaCloudManager {
 	
+	public final static String CLOUDFILE_NAME = "clouds.xml"; //$NON-NLS-1$
+	
 	private static DeltaCloudManager cloudManager;
 	private ArrayList<DeltaCloud> clouds = new ArrayList<DeltaCloud>();
 	private ListenerList cloudManagerListeners;
 	
 	private DeltaCloudManager() {
+		loadClouds();
 	}
 	
+	private void loadClouds() {
+		IPath stateLocation = Activator.getDefault().getStateLocation();
+		File cloudFile = stateLocation.append(CLOUDFILE_NAME).toFile();
+		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+		try {
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			if (cloudFile.exists()) {
+				Document d = db.parse(cloudFile);
+				Element e = d.getDocumentElement();
+				// Get the stored configuration data
+				NodeList cloudNodes = e.getElementsByTagName("cloud"); // $NON-NLS-1$
+				for (int x = 0; x < cloudNodes.getLength(); ++x) {
+					Node n = cloudNodes.item(x);
+					NamedNodeMap attrs = n.getAttributes();
+					Node nameNode = attrs.getNamedItem("name"); // $NON-NLS-1$
+					Node urlNode = attrs.getNamedItem("url"); // $NON-NLS-1$
+					Node usernameNode = attrs.getNamedItem("username"); // $NON-NLS-1$
+					String name = nameNode.getNodeValue();
+					String url = urlNode.getNodeValue();
+					String username = usernameNode.getNodeValue();
+					String key = DeltaCloud.getPreferencesKey(url, username);
+					ISecurePreferences root = SecurePreferencesFactory.getDefault();
+					ISecurePreferences node = root.node(key);
+					String password;
+					try {
+						password = node.get("password", null); //$NON-NLS-1$
+						DeltaCloud cloud = new DeltaCloud(name, url, username, password);
+						clouds.add(cloud);
+					} catch (Exception e1) {
+						Activator.log(e1);
+						continue; // skip cloud
+					}
+				}
+			} 
+		} catch (ParserConfigurationException e) {
+			Activator.log(e);
+		} catch (SAXException e) {
+			Activator.log(e);
+		} catch (IOException e) {
+			Activator.log(e);
+		}
+	}
+
+	private void saveClouds() {
+		try {
+			IPath stateLocation = Activator.getDefault().getStateLocation();
+			File cloudFile = stateLocation.append(CLOUDFILE_NAME).toFile();
+			if (!cloudFile.exists())
+				cloudFile.createNewFile();
+			if (cloudFile.exists()) {
+				PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter(cloudFile)));
+				p.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
+				p.println("<clouds>"); // $NON-NLS-1$
+				for (DeltaCloud d : clouds) {
+					p.println("<cloud name=\"" + d.getName() + "\" url=\"" //$NON-NLS-1$ //$NON-NLS-2$ 
+							+ d.getURL() +
+							"\" username=\"" + d.getUsername() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
+				}
+				p.println("</clouds>"); //$NON-NLS-1$
+				p.close();
+			}
+		} catch (Exception e) {
+			Activator.log(e);
+		}
+	}
+	
 	public static DeltaCloudManager getDefault() {
 		if (cloudManager == null)
 			cloudManager = new DeltaCloudManager();
@@ -22,16 +108,6 @@
 	}
 	
 	public DeltaCloud[] getClouds() {
-		// FIXME: testing only
-//		if (clouds.size() == 0) {
-//			try {
-//				DeltaCloud x = new DeltaCloud("Red Hat Cloud", new URL("http://localhost:3001/api"), "mockuser", "mockpassword");
-//				addCloud(x);
-//			} catch (MalformedURLException e) {
-//				// TODO Auto-generated catch block
-//				e.printStackTrace();
-//			}
-//		}
 		return clouds.toArray(new DeltaCloud[clouds.size()]);
 	}
 
@@ -45,11 +121,33 @@
 	
 	public void addCloud(DeltaCloud d) {
 		clouds.add(d);
+		saveClouds();
 		notifyListeners(ICloudManagerListener.ADD_EVENT);
 	}
 	
 	public void removeCloud(DeltaCloud d) {
 		clouds.remove(d);
+		String url = d.getURL();
+		String userName = d.getUsername();
+		// check if we have a duplicate cloud connection using the same
+		// url/username combo.
+		boolean found = false;
+		for (DeltaCloud cloud : clouds) {
+			if (cloud.getURL().equals(url) && cloud.getUsername().equals(userName)) {
+				found = true;
+				break;
+			}
+		}
+		// if we have removed a cloud and no other cloud shares the
+		// url/username combo, then we should clear the node out which
+		// includes the password.
+		if (!found) {
+			ISecurePreferences root = SecurePreferencesFactory.getDefault();
+			String key = DeltaCloud.getPreferencesKey(d.getURL(), d.getUsername());
+			ISecurePreferences node = root.node(key);
+			node.clear();
+		}
+		saveClouds();
 		notifyListeners(ICloudManagerListener.REMOVE_EVENT);
 	}
 	



More information about the jbosstools-commits mailing list