Author: adietish
Date: 2010-11-26 10:37:40 -0500 (Fri, 26 Nov 2010)
New Revision: 26981
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
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:
[JBIDE-7731] extracted password storage to its own class (removed duplicate code in
EditCloudConnectionWizard, NewCloudConnectionWizard and DeltaCloud)
[JBIDE-7694] removed duplicate loadImage(id) / getImage(id) methods
[JBIDE-7694] corrected lazy initialisation (to signal correctly that no instances had been
loaded so far)
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-11-26 15:36:30
UTC (rev 26980)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-11-26 15:37:40
UTC (rev 26981)
@@ -1,3 +1,27 @@
+2010-11-26 André Dietisheim <adietish(a)redhat.com>
+
+ * src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java:
+ * src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java:
+ * src/org/jboss/tools/deltacloud/core/DeltaCloud.java:
+ (editCloud):
+ (getPassword):
+ (createClient):
+ (dispose):
+ (loadImage):
+ * src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java (removeCloud):
+ [JBIDE-7731] extracted password storage to its own class (removed duplicate code in
EditCloudConnectionWizard, NewCloudConnectionWizard and DeltaCloud)
+ * src/org/jboss/tools/deltacloud/core/DeltaCloud.java:
+ (loadImage):
+ [JBIDE-7694] removed duplicate loadImage(id) / getImage(id) methods
+ * src/org/jboss/tools/deltacloud/core/DeltaCloud.java
+ (images):
+ (instances)
+ (getImages):
+ (getInstances):
+ (loadImages):
+ (loadInstances):
+ [JBIDE-7694] corrected lazy initialisation (to signal correctly that no instances had
been loaded so far)
+
2010-11-25 André Dietisheim <adietish(a)redhat.com>
* src/org/jboss/tools/deltacloud/core/DeltaCloud.java:
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-11-26
15:36:30 UTC (rev 26980)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java 2010-11-26
15:37:40 UTC (rev 26981)
@@ -22,10 +22,6 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.jobs.Job;
-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.DeltaCloudClientException;
import org.jboss.tools.deltacloud.core.client.DeltaCloudClientImpl;
@@ -54,8 +50,8 @@
private InternalDeltaCloudClient client;
- private DeltaCloudImagesRepository images = new DeltaCloudImagesRepository();
- private DeltaCloudInstancesRepository instances = new DeltaCloudInstancesRepository();
+ private DeltaCloudImagesRepository images;
+ private DeltaCloudInstancesRepository instances;
private IImageFilter imageFilter;
private IInstanceFilter instanceFilter;
@@ -63,10 +59,10 @@
private Map<String, Job> actionJobs;
private Object actionLock = new Object();
- // TODO: switch to readwrite lock
ListenerList instanceListeners = new ListenerList();
ListenerList imageListeners = new ListenerList();
+ private SecurePasswordStore passwordStore;
public static interface IInstanceStateMatcher {
public boolean matchesState(DeltaCloudInstance instance, String instanceState);
@@ -92,8 +88,8 @@
this.name = name;
this.username = username;
this.type = type;
- storePassword(name, username, password);
- this.client = createClient(name, url, username, password);
+ this.passwordStore = new SecurePasswordStore(new DeltaCloudPasswordStorageKey(name,
username), password);
+ this.client = createClient(name, url, username, passwordStore.getPassword());
imageFilter = createImageFilter(imageFilterRules);
instanceFilter = createInstanceFilter(instanceFilterRules);
}
@@ -104,69 +100,20 @@
this.name = name;
this.username = username;
this.type = type;
- removePassword(name, username);
- storePassword(name, username, password);
- client = createClient(name, url, username, password);
+ this.passwordStore.update(new DeltaCloudPasswordStorageKey(name, username), password);
+ client = createClient(name, url, username, passwordStore.getPassword());
loadChildren();
}
private InternalDeltaCloudClient createClient(String name, String url, String username,
String password)
throws DeltaCloudException {
try {
- if (password == null) {
- password = getPasswordFromPreferences(name, username);
- }
return new DeltaCloudClientImpl(url, username, password);
} catch (MalformedURLException e) {
throw new DeltaCloudException(MessageFormat.format("Could not access cloud at
{0}", url), e);
- } catch (StorageException e) {
- throw new DeltaCloudException(MessageFormat.format(
- "Could not get password for user {0} on cloud at {1} in the preferences",
username, url), e);
}
}
- private String getPasswordFromPreferences(String cloudName, String username) throws
StorageException {
- String key = getPreferencesKey(cloudName, username); // $NON-NLS-1$
- ISecurePreferences root = SecurePreferencesFactory.getDefault();
- ISecurePreferences node = root.node(key);
- String password = node.get("password", null); //$NON-NLS-1$
- return password;
- }
-
- private void storePassword(String cloudName, String username, String passwd) throws
DeltaCloudException {
- if (passwd != null) {
- ISecurePreferences root = SecurePreferencesFactory.getDefault();
- String key = getPreferencesKey(cloudName, username);
- ISecurePreferences node = root.node(key);
- try {
- node.put("password", passwd, true /* encrypt */); //$NON-NLS-1$
- } catch (StorageException e) {
- // TODO: internationalize string
- throw new DeltaCloudException("Could not store password", e);
- }
- }
- }
-
- public void removePassword(String cloudName, String userName) throws DeltaCloudException
{
- String key = getPreferencesKey(cloudName, userName);
- ISecurePreferences root = SecurePreferencesFactory.getDefault();
- ISecurePreferences node = root.node(key);
- if (node == null) {
- throw new DeltaCloudException(MessageFormat.format(
- "Could not remove password for cloud {0} from secure preferences store",
cloudName));
- }
- node.clear();
- }
-
- public static String getPreferencesKey(String cloudName, String username) {
- String key = new StringBuilder("/org/jboss/tools/deltacloud/core/")
//$NON-NLS-1$
- .append(cloudName)
- .append('/') //$NON-NLS-1$
- .append(username)
- .toString();
- return EncodingUtils.encodeSlashes(key);
- }
-
public String getName() {
return name;
}
@@ -179,6 +126,10 @@
return username;
}
+ public String getPassword() throws DeltaCloudException {
+ return passwordStore.getPassword();
+ }
+
public String getType() {
return type;
}
@@ -257,6 +208,13 @@
return imageFilter;
}
+ /**
+ * Loads all children of this delta cloud instance (regardless if things
+ * have already been loaded before). Catched and collects individual errors
+ * that may occur and throws a multi exception.
+ *
+ * @throws DeltaCloudException
+ */
public void loadChildren() throws DeltaCloudException {
DeltaCloudMultiException multiException = new
DeltaCloudMultiException(MessageFormat.format(
"Could not load children of cloud {0}", getName()));
@@ -307,14 +265,6 @@
imageListeners.remove(listener);
}
- private DeltaCloudInstance[] cloneInstancesArray() {
- return instances.get();
- }
-
- private DeltaCloudImage[] cloneImagesArray() {
- return images.get();
- }
-
private DeltaCloudImage[] notifyImageListListeners(DeltaCloudImage[] array) {
Object[] listeners = imageListeners.getListeners();
for (int i = 0; i < listeners.length; ++i) {
@@ -397,9 +347,12 @@
*
* @see #notifyInstanceListListeners(DeltaCloudInstance[])
*/
- public DeltaCloudInstance[] loadInstances() throws DeltaCloudException {
+ private DeltaCloudInstance[] loadInstances() throws DeltaCloudException {
try {
clearInstances();
+ if (instances == null) {
+ instances = new DeltaCloudInstancesRepository();
+ }
instances.add(client.listInstances(), this);
// TODO: remove notification with all instances, replace by
// notifying the changed instance
@@ -411,28 +364,31 @@
}
private void clearInstances() {
- instances.clear();
- notifyInstanceListListeners(instances.get());
+ if (instances != null) {
+ instances.clear();
+ notifyInstanceListListeners(instances.get());
+ }
}
private void clearImages() {
- images.clear();
- notifyImageListListeners(images.get());
+ if (images != null) {
+ images.clear();
+ notifyImageListListeners(images.get());
+ }
}
public DeltaCloudInstance[] getInstances() throws DeltaCloudException {
if (instances == null) {
- instances = new DeltaCloudInstancesRepository();
return loadInstances();
}
- return cloneInstancesArray();
+ return instances.get();
}
public DeltaCloudImage[] getImages() throws DeltaCloudException {
if (images == null) {
return loadImages();
}
- return cloneImagesArray();
+ return images.get();
}
public void createKey(String keyname, String keystoreLocation) throws
DeltaCloudException {
@@ -538,15 +494,6 @@
return profileArray;
}
- public DeltaCloudImage loadImage(String imageId) throws DeltaCloudException {
- try {
- Image image = client.listImages(imageId);
- return images.add(image, this);
- } catch (DeltaCloudClientException e) {
- throw new DeltaCloudException(e);
- }
- }
-
/**
* Loads the available images from the server and stores them locally.
* Furthermore listeners get informed.
@@ -556,9 +503,12 @@
*
* @see #notifyImageListListeners(DeltaCloudImage[])
*/
- public DeltaCloudImage[] loadImages() throws DeltaCloudException {
+ private DeltaCloudImage[] loadImages() throws DeltaCloudException {
try {
clearImages();
+ if (images == null) {
+ images = new DeltaCloudImagesRepository();
+ }
images.add(client.listImages(), this);
return notifyImageListListeners(images.get());
} catch (DeltaCloudClientException e) {
@@ -568,19 +518,15 @@
}
- public DeltaCloudImage getImage(String imageId) {
- DeltaCloudImage deltaCloudImage = null;
+ public DeltaCloudImage loadImage(String imageId) throws DeltaCloudException {
try {
Image image = client.listImages(imageId);
- deltaCloudImage = new DeltaCloudImage(image, this);
- } catch (Exception e) {
- // TODO: implement proper logging / error reporting / ignore
- e.printStackTrace();
- // do nothing and return null
+ return images.add(image, this);
+ } catch (DeltaCloudClientException e) {
+ throw new DeltaCloudException(e);
}
- return deltaCloudImage;
}
-
+
public boolean testConnection() throws DeltaCloudException {
String instanceId = "nonexistingInstance"; //$NON-NLS-1$
try {
@@ -630,4 +576,8 @@
}
return null;
}
+
+ public void dispose() throws DeltaCloudException {
+ passwordStore.remove();
+ }
}
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-11-26
15:36:30 UTC (rev 26980)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java 2010-11-26
15:37:40 UTC (rev 26981)
@@ -194,7 +194,7 @@
public void removeCloud(DeltaCloud d) throws DeltaCloudException {
doGetClouds().remove(d);
- d.removePassword(d.getName(), d.getName());
+ d.dispose();
saveClouds();
notifyListeners(ICloudManagerListener.REMOVE_EVENT);
}
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java 2010-11-26
15:37:40 UTC (rev 26981)
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core;
+
+import org.eclipse.equinox.security.storage.EncodingUtils;
+import org.jboss.tools.deltacloud.core.SecurePasswordStore.IStorageKey;
+
+/**
+ * Implements a key to be used to store keywords with.
+ *
+ * @author Andre Dietisheim
+ *
+ */
+public class DeltaCloudPasswordStorageKey implements IStorageKey {
+
+ private static final String PREFERNCES_BASEKEY =
Activator.PLUGIN_ID.replace('.', '/');
+ private String cloudName;
+ private String userName;
+
+ public DeltaCloudPasswordStorageKey(String cloudName, String userName) {
+ this.userName = userName;
+ this.cloudName = cloudName;
+ }
+
+ @Override
+ public String getKey() {
+ String key = new StringBuilder(PREFERNCES_BASEKEY)
+ .append(cloudName)
+ .append('/') //$NON-NLS-1$
+ .append(userName)
+ .toString();
+ return EncodingUtils.encodeSlashes(key);
+ }
+
+ @Override
+ public boolean equals(IStorageKey key) {
+ if (!key.getClass().isAssignableFrom(DeltaCloudPasswordStorageKey.class)) {
+ return false;
+ }
+ DeltaCloudPasswordStorageKey deltaCloudKey = (DeltaCloudPasswordStorageKey) key;
+ return userName.equals(deltaCloudKey.userName)
+ && cloudName.equals(deltaCloudKey.cloudName);
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudPasswordStorageKey.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java 2010-11-26
15:37:40 UTC (rev 26981)
@@ -0,0 +1,100 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core;
+
+import org.eclipse.equinox.security.storage.ISecurePreferences;
+import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
+import org.eclipse.equinox.security.storage.StorageException;
+
+/**
+ * @author Andre Dietisheim
+ *
+ * TODO: remove DeltaCloudException
+ */
+public class SecurePasswordStore {
+
+ public static interface IStorageKey {
+ public String getKey();
+
+ public boolean equals(IStorageKey key);
+ }
+
+ private String password;
+ private boolean stored;
+ private IStorageKey storageKey;
+
+ public SecurePasswordStore(IStorageKey key, String password) {
+ this.storageKey = key;
+ this.password = password;
+ }
+
+ public String getPassword() throws DeltaCloudException {
+ if (password != null) {
+ if (!stored) {
+ storeInPreferences(password, storageKey);
+ stored = true;
+ }
+ return password;
+ } else {
+ try {
+ return this.password = getFromPreferences(storageKey);
+ } catch (StorageException e) {
+ throw new DeltaCloudException("Could get password", e);
+ }
+ }
+ }
+
+ public void update(IStorageKey key, String password) throws DeltaCloudException {
+ if (!storageKey.equals(key)
+ || hasPasswordChanged(password)) {
+ storeInPreferences(this.password = password, this.storageKey = key);
+ }
+ }
+
+ private boolean hasPasswordChanged(String password) {
+ if (this.password == null && password == null) {
+ return false;
+ } else {
+ return (this.password == null && password != null)
+ || (this.password != null && password == null)
+ || !password.equals(this.password);
+ }
+ }
+
+ public void remove() throws DeltaCloudException {
+ ISecurePreferences node = getNode(storageKey);
+ if (node == null) {
+ throw new DeltaCloudException("Could not remove password");
+ }
+ node.clear();
+ }
+
+ private String getFromPreferences(IStorageKey key) throws StorageException {
+ ISecurePreferences node = getNode(key);
+ String password = node.get("password", null); //$NON-NLS-1$
+ return password;
+ }
+
+ private void storeInPreferences(String password, IStorageKey key) throws
DeltaCloudException {
+ try {
+ ISecurePreferences node = getNode(key);
+ node.put("password", password, true /* encrypt */); //$NON-NLS-1$
+ } catch (StorageException e) {
+ // TODO: internationalize string
+ throw new DeltaCloudException("Could not store password", e);
+ }
+ }
+
+ private ISecurePreferences getNode(IStorageKey key) {
+ ISecurePreferences root = SecurePreferencesFactory.getDefault();
+ return root.node(key.getKey());
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/SecurePasswordStore.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain