Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Arthorne2009-02-20 22:44:00 +0000
committerJohn Arthorne2009-02-20 22:44:00 +0000
commit82de88aed4ba693f08a99509f6d366e11110cdf4 (patch)
treec4b4a1365eb094df591015e81af1c35ed0788621 /bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src
parent728d3af3bbaaae1d3a1fa69458159e040fd1aea7 (diff)
downloadrt.equinox.p2-82de88aed4ba693f08a99509f6d366e11110cdf4.tar.gz
rt.equinox.p2-82de88aed4ba693f08a99509f6d366e11110cdf4.tar.xz
rt.equinox.p2-82de88aed4ba693f08a99509f6d366e11110cdf4.zip
Bug 265315 Profile Scoped Preferences
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src')
-rw-r--r--bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/AddRepositoryAction.java16
-rw-r--r--bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RemoveRepositoryAction.java2
-rw-r--r--bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RepositoryAction.java174
3 files changed, 131 insertions, 61 deletions
diff --git a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/AddRepositoryAction.java b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/AddRepositoryAction.java
index fa95c15eb..bb8c6af1d 100644
--- a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/AddRepositoryAction.java
+++ b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/AddRepositoryAction.java
@@ -26,7 +26,7 @@ public class AddRepositoryAction extends RepositoryAction {
final RepositoryEvent event = createEvent(parameters);
Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
if (profile != null)
- addRepositoryToProfile(profile, event.getRepositoryLocation(), event.getRepositoryType());
+ addRepositoryToProfile(profile, event.getRepositoryLocation(), event.getRepositoryType(), event.isRepositoryEnabled());
//if provisioning into the current profile, broadcast an event to add this repository directly to the current repository managers.
if (isSelfProfile(profile))
addToSelf(event);
@@ -41,7 +41,17 @@ public class AddRepositoryAction extends RepositoryAction {
}
public IStatus undo(Map parameters) {
- //TODO: we don't know if the repository was already present
- return Status.OK_STATUS;
+ try {
+ final RepositoryEvent event = createEvent(parameters);
+ Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
+ if (profile != null)
+ removeRepositoryFromProfile(profile, event.getRepositoryLocation(), event.getRepositoryType());
+ //if provisioning into the current profile, broadcast an event to add this repository directly to the current repository managers.
+ if (isSelfProfile(profile))
+ removeFromSelf(event);
+ return Status.OK_STATUS;
+ } catch (CoreException e) {
+ return e.getStatus();
+ }
}
}
diff --git a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RemoveRepositoryAction.java b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RemoveRepositoryAction.java
index 77d20ee0e..9ab8cdefb 100644
--- a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RemoveRepositoryAction.java
+++ b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RemoveRepositoryAction.java
@@ -41,7 +41,7 @@ public class RemoveRepositoryAction extends RepositoryAction {
RepositoryEvent event = createEvent(parameters);
Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
if (profile != null)
- addRepositoryToProfile(profile, event.getRepositoryLocation(), event.getRepositoryType());
+ addRepositoryToProfile(profile, event.getRepositoryLocation(), event.getRepositoryType(), event.isRepositoryEnabled());
//if we are provisioning into the self profile, update the current set of repositories in this configuration
if (isSelfProfile(profile))
addToSelf(event);
diff --git a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RepositoryAction.java b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RepositoryAction.java
index 8ce1ce244..04071393a 100644
--- a/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RepositoryAction.java
+++ b/bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/touchpoint/eclipse/actions/RepositoryAction.java
@@ -13,8 +13,8 @@ package org.eclipse.equinox.internal.p2.touchpoint.eclipse.actions;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
-import java.util.StringTokenizer;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.internal.p2.engine.Profile;
import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Activator;
@@ -24,6 +24,8 @@ import org.eclipse.equinox.internal.provisional.p2.core.repository.*;
import org.eclipse.equinox.internal.provisional.p2.engine.*;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
import org.eclipse.osgi.util.NLS;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
/**
* Helper base class for dealing with repositories associated with profiles. Repositories
@@ -34,6 +36,14 @@ import org.eclipse.osgi.util.NLS;
*/
abstract class RepositoryAction extends ProvisioningAction {
+ private static final String METADATA_REPOSITORY = "org.eclipse.equinox.p2.metadata.repository"; //$NON-NLS-1$
+ private static final String ARTIFACT_REPOSITORY = "org.eclipse.equinox.p2.artifact.repository"; //$NON-NLS-1$
+
+ private static final String NODE_REPOSITORIES = "repositories"; //$NON-NLS-1$
+ private static final String REPOSITORY_COUNT = "count"; //$NON-NLS-1$
+ private static final String KEY_URI = "uri"; //$NON-NLS-1$
+ private static final String KEY_ENABLED = "enabled"; //$NON-NLS-1$
+
/**
* Returns the repository manager of the given type, or <code>null</code>
* if not available.
@@ -51,22 +61,25 @@ abstract class RepositoryAction extends ProvisioningAction {
* Associates the repository described by the given event with the given profile.
* Has no effect if the repository is already associated with this profile.
*/
- protected void addRepositoryToProfile(Profile profile, URI location, int type) {
- String key = type == IRepository.TYPE_METADATA ? IProfile.PROP_METADATA_REPOSITORIES : IProfile.PROP_ARTIFACT_REPOSITORIES;
- String encodedURI = encodeURI(location);
- String currentRepos = profile.getProperty(key);
- if (currentRepos == null) {
- currentRepos = encodedURI;
- } else {
- //if we already have the repository location, we are done
- StringTokenizer tokens = new StringTokenizer(currentRepos, ","); //$NON-NLS-1$
- while (tokens.hasMoreTokens())
- if (tokens.nextToken().equals(encodedURI))
- return;
- //add to comma-separated list
- currentRepos = currentRepos + ',' + encodedURI;
+ protected void addRepositoryToProfile(Profile profile, URI location, int type, boolean enabled) {
+ Preferences node = getRepositoryPreferenceNode(profile, location, type);
+ int count = 0;
+
+ if (repositoryExists(node)) {
+ count = getRepositoryCount(node);;
+ // If a user as added a repository we need to set the initial count manually
+ if (count == 0)
+ count = 1;
+ }
+ node.put(KEY_URI, location.toString());
+ node.put(KEY_ENABLED, Boolean.toString(enabled));
+ count++;
+ setRepositoryCount(node, count);
+ try {
+ node.flush();
+ } catch (BackingStoreException e) {
+ // TODO: perhaps an Exception should be passed backwards and associated with State
}
- profile.setProperty(key, currentRepos);
}
/**
@@ -74,8 +87,21 @@ abstract class RepositoryAction extends ProvisioningAction {
*/
protected void addToSelf(RepositoryEvent event) {
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType());
- if (manager != null)
- manager.addRepository(event.getRepositoryLocation());
+ Preferences node = getRepositoryPreferenceNode(null, event.getRepositoryLocation(), event.getRepositoryType());
+
+ int count = getRepositoryCount(node);
+ if (manager.contains(event.getRepositoryLocation())) {
+ // If a user as added a repository we need to set the initial count manually
+ if (count == 0)
+ count = 1;
+ } else {
+ if (manager != null)
+ manager.addRepository(event.getRepositoryLocation());
+ }
+ // increment the counter & send to preferences
+ count++;
+ setRepositoryCount(node, count);
+
if (!event.isRepositoryEnabled())
manager.setEnabled(event.getRepositoryLocation(), false);
}
@@ -106,22 +132,6 @@ abstract class RepositoryAction extends ProvisioningAction {
}
/**
- * Encodes a URI as a string, in a form suitable for storing in a comma-separated
- * list of location strings. Any comma character in the local string is encoded.
- */
- private String encodeURI(URI repositoryLocation) {
- char[] chars = repositoryLocation.toString().toCharArray();
- StringBuffer result = new StringBuffer(chars.length);
- for (int i = 0; i < chars.length; i++) {
- if (chars[i] == ',')
- result.append("${#").append(Integer.toString(chars[i])).append('}'); //$NON-NLS-1$
- else
- result.append(chars[i]);
- }
- return result.toString();
- }
-
- /**
* Returns the id of this action.
*/
protected abstract String getId();
@@ -149,8 +159,11 @@ abstract class RepositoryAction extends ProvisioningAction {
*/
protected void removeFromSelf(RepositoryEvent event) {
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType());
- if (manager != null)
+ Preferences node = getRepositoryPreferenceNode(null, event.getRepositoryLocation(), event.getRepositoryType());
+ int count = getRepositoryCount(node);
+ if ((--count < 1) && (manager != null))
manager.removeRepository(event.getRepositoryLocation());
+ setRepositoryCount(node, count);
}
/**
@@ -159,29 +172,76 @@ abstract class RepositoryAction extends ProvisioningAction {
* this profile.
*/
protected void removeRepositoryFromProfile(Profile profile, URI location, int type) {
- String key = type == IRepository.TYPE_METADATA ? IProfile.PROP_METADATA_REPOSITORIES : IProfile.PROP_ARTIFACT_REPOSITORIES;
- String encodedURI = encodeURI(location);
- String currentRepos = profile.getProperty(key);
- //if this profile has no associated repositories, we are done
- if (currentRepos == null)
- return;
- //find the matching location, if any
- StringTokenizer tokens = new StringTokenizer(currentRepos, ","); //$NON-NLS-1$
- StringBuffer result = new StringBuffer(currentRepos.length());
- boolean found = false;
- while (tokens.hasMoreTokens()) {
- final String nextLocation = tokens.nextToken();
- if (nextLocation.equals(encodedURI)) {
- found = true;
- } else {
- //add back any location not being removed
- result.append(nextLocation);
- if (tokens.hasMoreTokens())
- result.append(',');
+ Preferences node = getRepositoryPreferenceNode(profile, location, type);
+
+ int count = getRepositoryCount(node);
+ if (--count < 1) {
+ // TODO: Remove all associated values
+ try {
+ String[] keys = node.keys();
+
+ for (int i = 0; i < keys.length; i++)
+ node.remove(keys[i]);
+ } catch (BackingStoreException e) {
+ // TODO: Should this be passed back to be associated with State?
}
+
+ } else
+ setRepositoryCount(node, count);
+
+ try {
+ node.flush();
+ } catch (BackingStoreException e) {
+ // TODO: perhaps an Exception should be passed backwards and associated with State
}
- if (!found)
- return;
- profile.setProperty(key, result.toString());
+ }
+
+ /*
+ * Get the counter associated with a repository
+ */
+ protected int getRepositoryCount(Preferences node) {
+ return node.getInt(REPOSITORY_COUNT, 0);
+ }
+
+ /*
+ * Sets the counter associated with this repository to a specific value
+ */
+ protected void setRepositoryCount(Preferences node, int count) {
+ if (count < 1)
+ node.remove(REPOSITORY_COUNT);
+ else
+ node.putInt(REPOSITORY_COUNT, count);
+ }
+
+ /*
+ * Determine if a repository is already known
+ */
+ protected boolean repositoryExists(Preferences node) {
+ if (node.get(KEY_URI, null) == null)
+ return false;
+ return true;
+ }
+
+ /*
+ * Get the preference node associated with profile & location
+ */
+ protected Preferences getRepositoryPreferenceNode(Profile profile, URI location, int type) {
+ String key = type == IRepository.TYPE_METADATA ? METADATA_REPOSITORY : ARTIFACT_REPOSITORY;
+ IPreferencesService prefService = (IPreferencesService) ServiceHelper.getService(Activator.getContext(), IPreferencesService.class.getName());
+
+ if (profile != null)
+ return prefService.getRootNode().node("/profile/" + profile.getProfileId() + "/" + key + "/" + NODE_REPOSITORIES + "/" + getKey(location)); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
+ return prefService.getRootNode().node("/profile/_SELF_/" + key + "/" + NODE_REPOSITORIES + "/" + getKey(location)); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ }
+
+ /*
+ * Copied from AbstractRepositoryManager
+ */
+ private String getKey(URI location) {
+ String key = location.toString().replace('/', '_');
+ //remove trailing slash
+ if (key.endsWith("_")) //$NON-NLS-1$
+ key = key.substring(0, key.length() - 1);
+ return key;
}
}

Back to the top