Author: rob.stryker(a)jboss.com
Date: 2012-02-09 06:59:04 -0500 (Thu, 09 Feb 2012)
New Revision: 38544
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/console/UserModel.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/OpenShiftUIActivator.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/OpenShiftExpressConsoleContentProvider.java
Log:
Additions to usermodel class to organize stuff in a nice api
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/console/UserModel.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/console/UserModel.java 2012-02-09
11:54:01 UTC (rev 38543)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/console/UserModel.java 2012-02-09
11:59:04 UTC (rev 38544)
@@ -11,15 +11,20 @@
package org.jboss.tools.openshift.express.internal.core.console;
import java.io.IOException;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
+import org.jboss.tools.common.ui.preferencevalue.StringPreferenceValue;
import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
+import org.jboss.tools.openshift.express.internal.ui.utils.Logger;
+import org.jboss.tools.openshift.express.internal.ui.utils.OpenShiftPasswordStorageKey;
+import org.jboss.tools.openshift.express.internal.ui.utils.SecurePasswordStore;
+import org.jboss.tools.openshift.express.internal.ui.utils.SecurePasswordStoreException;
import com.openshift.express.client.IUser;
import com.openshift.express.client.OpenShiftException;
import com.openshift.express.client.User;
+import com.openshift.express.client.configuration.OpenShiftConfiguration;
public class UserModel {
private static UserModel model;
@@ -62,6 +67,10 @@
return recentUser;
}
+ public void setRecentUser(IUser user) {
+ this.recentUser = user;
+ }
+
public IUser findUser(String username) {
try {
for( int i = 0; i < allUsers.size(); i++ ) {
@@ -80,13 +89,107 @@
return rets;
}
+ /**
+ * Load the user list from preferences and secure storage
+ */
public void load() {
// TODO
}
+ /**
+ * Save the user list to preferences and secure storage
+ */
public void save() {
// TODO
// save the passwords in secure storage, save the username list somewhere else
}
+ private static final String RHLOGIN_PREFS_KEY =
"org.jboss.tools.openshift.express.internal.ui.wizard.CredentialsWizardModel_RHLOGIN";
+
+ /**
+ * Get the username stored in preferences as most recently used
+ * @return
+ */
+ public String getDefaultUsername() {
+ StringPreferenceValue pref = new StringPreferenceValue(RHLOGIN_PREFS_KEY,
OpenShiftUIActivator.PLUGIN_ID);
+ String rhLogin = null;
+ rhLogin = pref.get();
+ if (rhLogin == null || rhLogin.length() == 0) {
+ String configuredUsername = null;
+ try {
+ configuredUsername = new OpenShiftConfiguration().getRhlogin();
+ } catch (Exception e) {
+ // do nothing
+ }
+ rhLogin = configuredUsername;
+ }
+ return rhLogin;
+ }
+
+ /*
+ * Return true if the value is updated, false otherwise
+ */
+ public boolean setDefaultUsername(String rhLogin) {
+ String prev = getDefaultUsername();
+ StringPreferenceValue preference = new StringPreferenceValue(RHLOGIN_PREFS_KEY,
OpenShiftUIActivator.PLUGIN_ID);
+ if (rhLogin != null && !rhLogin.equals(prev)) {
+ preference.store(rhLogin);
+ return true;
+ }
+ return false;
+ }
+
+ /*
+ * Return a password from secure storage, or
+ * null if platform not found, or password not stored
+ */
+ public String getPasswordFromSecureStorage(final String rhLogin) {
+ SecurePasswordStore store = getSecureStore(rhLogin);
+ if( store != null && rhLogin != null && !rhLogin.isEmpty() ) {
+ try {
+ return store.getPassword();
+ } catch (SecurePasswordStoreException e) {
+ Logger.error("Failed to retrieve OpenShift user's password from Secured
Store", e);
+ }
+ }
+ return null;
+ }
+
+ public void setPasswordInSecureStorage(final String rhLogin, String password) {
+ SecurePasswordStore store = getSecureStore(rhLogin);
+ if( store != null && rhLogin != null && !rhLogin.isEmpty() ) {
+ try {
+ store.setPassword(password);
+ } catch (SecurePasswordStoreException e) {
+ Logger.error(e.getMessage(), e);
+ }
+ }
+ }
+
+
+ private SecurePasswordStore getSecureStore(final String rhLogin) {
+ return getSecureStore(initLibraServer(), rhLogin);
+ }
+
+ /*
+ * Return a secure store or null if platform is not found
+ */
+ private SecurePasswordStore getSecureStore(final String platform, final String username)
{
+ if( platform == null )
+ return null;
+ final OpenShiftPasswordStorageKey key = new OpenShiftPasswordStorageKey(platform,
username);
+ SecurePasswordStore store = new SecurePasswordStore(key);
+ return store;
+ }
+
+ private String initLibraServer() {
+ try {
+ return new OpenShiftConfiguration().getLibraServer();
+ } catch (Exception e) {
+ Logger.error("Failed to load OpenShift configuration from client library",
e);
+ }
+ return null;
+ }
+
+
}
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/OpenShiftUIActivator.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/OpenShiftUIActivator.java 2012-02-09
11:54:01 UTC (rev 38543)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/OpenShiftUIActivator.java 2012-02-09
11:59:04 UTC (rev 38544)
@@ -12,6 +12,7 @@
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.jboss.tools.openshift.express.internal.core.console.UserModel;
import org.osgi.framework.BundleContext;
import com.openshift.express.client.IUser;
@@ -29,9 +30,6 @@
// The shared instance
private static OpenShiftUIActivator plugin;
- /** The user connected on OpenShift. */
- private IUser user = null;
-
/**
* The constructor
*/
@@ -96,16 +94,27 @@
}
+ /**
+ * Create a new user and add it to the model
+ * @param username
+ * @param password
+ * @return
+ * @throws OpenShiftException
+ * @throws IOException
+ */
public IUser createUser(String username, String password) throws OpenShiftException,
IOException {
- this.user = new User(username, password, PLUGIN_ID + " " +
getBundle().getVersion());
- return user;
+ IUser u = UserModel.getDefault().createUser(username, password);
+ UserModel.getDefault().addUser(u);
+ return u;
}
/**
+ * Get the most recently created or used user
+ *
* @return the user
*/
public final IUser getUser() {
- return user;
+ return UserModel.getDefault().getRecentUser();
}
/**
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/OpenShiftExpressConsoleContentProvider.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/OpenShiftExpressConsoleContentProvider.java 2012-02-09
11:54:01 UTC (rev 38543)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/OpenShiftExpressConsoleContentProvider.java 2012-02-09
11:59:04 UTC (rev 38544)
@@ -10,6 +10,7 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
+import org.jboss.tools.openshift.express.internal.core.console.UserModel;
import org.jboss.tools.openshift.express.internal.ui.utils.Logger;
import com.openshift.express.client.IApplication;
@@ -31,8 +32,12 @@
@Override
public Object[] getElements(final Object parentElement) {
if(parentElement instanceof IWorkspaceRoot) {
- return new Object[0];
+ return UserModel.getDefault().getUsers();
}
+ if( parentElement instanceof UserModel ) {
+ return ((UserModel)parentElement).getUsers();
+ }
+
final ArrayBlockingQueue<Object[]> queue = new
ArrayBlockingQueue<Object[]>(1);
try {
Job job = new Job("Loading OpenShift Express User information...") {
Show replies by date