Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDJ Houghton2006-05-04 09:59:07 +0000
committerDJ Houghton2006-05-04 09:59:07 +0000
commita53e8e56b1f2c8f03644c515e56c1224220e7d23 (patch)
tree31f0c55e12515841d01fb6b093932b2f2f61dafd
parent718bc3ca0785a02a8bac5981a8f8cdfd81701175 (diff)
downloadrt.equinox.bundles-a53e8e56b1f2c8f03644c515e56c1224220e7d23.tar.gz
rt.equinox.bundles-a53e8e56b1f2c8f03644c515e56c1224220e7d23.tar.xz
rt.equinox.bundles-a53e8e56b1f2c8f03644c515e56c1224220e7d23.zip
Bug 124176 - Store OSGi preferences in Eclipse Preferencesv20060504-0600
-rw-r--r--bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceImpl.java265
-rw-r--r--bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceManager.java117
2 files changed, 224 insertions, 158 deletions
diff --git a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceImpl.java b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceImpl.java
index 8a13cc0d4..30f558683 100644
--- a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceImpl.java
+++ b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceImpl.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004 IBM Corporation and others.
+ * Copyright (c) 2004, 2006 IBM Corporation and others.
* 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
@@ -10,13 +10,8 @@
*******************************************************************************/
package org.eclipse.core.internal.preferences;
-import java.io.File;
-import java.util.*;
-
-import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.osgi.service.prefs.*;
-import org.osgi.service.prefs.Preferences;
import org.osgi.service.prefs.PreferencesService;
/**
@@ -25,7 +20,7 @@ import org.osgi.service.prefs.PreferencesService;
* </p>
*
* <p>
- * Note: Eclipse preferences are not accessible through the OSGi Preferences API and vice
+ * Note: Eclipse preferences are accessible through the OSGi Preferences API and vice
* versa.
* </p>
*/
@@ -33,37 +28,44 @@ public class OSGiPreferencesServiceImpl implements PreferencesService {
/**
* Adaptor that implements OSGi Preferences interface on top of EclipsePreferences.
- *
+ * Creates a "local root" since OSGi preferences have lots of roots but eclipse
+ * only has one.
*/
- private static final class OSGiPreferences extends EclipsePreferences implements Preferences {
-
- private IPath location;
- private IEclipsePreferences loadLevel;
- private OSGiPreferencesServiceImpl prefsServiceImpl;
+ private static final class OSGiLocalRootPreferences implements Preferences {
- private OSGiPreferences(File prefsDir, OSGiPreferencesServiceImpl prefsServiceImpl) {
- super(null, ""); //$NON-NLS-1$
- this.prefsServiceImpl = prefsServiceImpl;
- this.location = new Path(prefsDir.getPath());
- this.loadLevel = this;
- }
+ //The "local" root of this preference tree (not the real Eclipse root)
+ private Preferences root;
- private OSGiPreferences(EclipsePreferences nodeParent, String nodeName, OSGiPreferencesServiceImpl prefsServiceImpl) {
- super(nodeParent, nodeName);
- this.loadLevel = nodeParent.getLoadLevel();
- this.prefsServiceImpl = prefsServiceImpl;
- }
+ //the node this node is wrappering
+ private Preferences wrapped;
- protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) {
- return new OSGiPreferences(nodeParent, nodeName, prefsServiceImpl);
+ private OSGiLocalRootPreferences(Preferences root) {
+ this(root, root);
}
- protected IPath getLocation() {
- return location;
+ private OSGiLocalRootPreferences(Preferences wrapped, Preferences root) {
+ this.root = root;
+ this.wrapped = wrapped;
}
- protected IEclipsePreferences getLoadLevel() {
- return loadLevel;
+ /**
+ * If pathName is absolute make it "absolute" with respect to this root.
+ * If pathName is relative, just return it
+ * @param pathName
+ * @return
+ */
+ private String fixPath(String pathName) {
+ if (pathName.startsWith("/")) {
+ if (pathName.equals("/")) {
+ return root.absolutePath();
+ } else {
+ //fix absolute path
+ return root.absolutePath().concat(pathName);
+ }
+ } else {
+ //pass-through relative path
+ return pathName;
+ }
}
/**
@@ -72,30 +74,13 @@ public class OSGiPreferencesServiceImpl implements PreferencesService {
* {@link IllegalArgumentException}.
*/
public Preferences node(String pathName) {
+ pathName = fixPath(pathName);
+
if ((pathName.length() > 1 && pathName.endsWith("/")) //$NON-NLS-1$
|| pathName.indexOf("//") != -1) { //$NON-NLS-1$
throw new IllegalArgumentException();
}
- return super.node(pathName);
- }
-
- /**
- * Override removeNode() to allow removal of root nodes. EclipsePreferences ignores
- * attempts to remove the root node, but in OSGi Preferences there are many root nodes
- * and removal is permitted.
- */
- public void removeNode() throws BackingStoreException {
- if (parent() == null) {
- flush();
- if (this == prefsServiceImpl.systemPreferences) {
- prefsServiceImpl.systemPreferences = null;
- } else {
- prefsServiceImpl.userPreferences.values().remove(this);
- }
- }
-
- super.removeNode();
- removed = true;
+ return new OSGiLocalRootPreferences(wrapped.node(pathName), root);
}
/**
@@ -111,7 +96,7 @@ public class OSGiPreferencesServiceImpl implements PreferencesService {
* </p>
*/
public byte[] getByteArray(String key, byte[] defaultValue) {
- String value = internalGet(key);
+ String value = wrapped.get(key, null);
byte[] byteArray = null;
if (value != null) {
byte[] encodedBytes = value.getBytes();
@@ -126,74 +111,146 @@ public class OSGiPreferencesServiceImpl implements PreferencesService {
return byteArray == null ? defaultValue : byteArray;
}
- }
+ public Preferences parent() {
+ if (wrapped == root) {
+ try {
+ if (!wrapped.nodeExists("")) {
+ throw new IllegalStateException();
+ }
+ } catch (BackingStoreException e) {
+ //best effort
+ }
+ return null;
+ } else {
+ return new OSGiLocalRootPreferences(wrapped.parent(), root);
+ }
+ }
- private File systemPrefsDir;
- private File userPrefsDir;
+ public boolean nodeExists(String pathName) throws BackingStoreException {
+ return wrapped.nodeExists(fixPath(pathName));
+ }
- Preferences systemPreferences;
+ public String absolutePath() {
+ if (wrapped == root) {
+ return "/";
+ } else {
+ return wrapped.absolutePath().substring(root.absolutePath().length(), wrapped.absolutePath().length());
+ }
+ }
- //Map of String user name -> Preferences
- Map userPreferences;
+ public String name() {
+ if (wrapped == root) {
+ return "";
+ } else {
+ return wrapped.name();
+ }
+ }
+
+ //delegate to wrapped preference
+ public void put(String key, String value) {
+ wrapped.put(key, value);
+ }
+
+ public String get(String key, String def) {
+ return wrapped.get(key, def);
+ }
+
+ public void remove(String key) {
+ wrapped.remove(key);
+ }
+
+ public void clear() throws BackingStoreException {
+ wrapped.clear();
+ }
+
+ public void putInt(String key, int value) {
+ wrapped.putInt(key, value);
+ }
+
+ public int getInt(String key, int def) {
+ return wrapped.getInt(key, def);
+ }
+
+ public void putLong(String key, long value) {
+ wrapped.putLong(key, value);
+ }
- OSGiPreferencesServiceImpl(File prefsLocation) {
- systemPrefsDir = new File(prefsLocation, "system"); //$NON-NLS-1$
- userPrefsDir = new File(prefsLocation, "user"); //$NON-NLS-1$
- userPreferences = new TreeMap(); //use TreeMap since keys are strings
+ public long getLong(String key, long def) {
+ return wrapped.getLong(key, def);
+ }
+
+ public void putBoolean(String key, boolean value) {
+ wrapped.putBoolean(key, value);
+ }
+
+ public boolean getBoolean(String key, boolean def) {
+ return wrapped.getBoolean(key, def);
+ }
+
+ public void putFloat(String key, float value) {
+ wrapped.putFloat(key, value);
+ }
+
+ public float getFloat(String key, float def) {
+ return wrapped.getFloat(key, def);
+ }
+
+ public void putDouble(String key, double value) {
+ wrapped.putDouble(key, value);
+ }
+
+ public double getDouble(String key, double def) {
+ return wrapped.getDouble(key, def);
+ }
+
+ public void putByteArray(String key, byte[] value) {
+ wrapped.putByteArray(key, value);
+ }
+
+ public String[] keys() throws BackingStoreException {
+ return wrapped.keys();
+ }
+
+ public String[] childrenNames() throws BackingStoreException {
+ return wrapped.childrenNames();
+ }
+
+ public void removeNode() throws BackingStoreException {
+ wrapped.removeNode();
+ }
+
+ public void flush() throws BackingStoreException {
+ wrapped.flush();
+ }
+
+ public void sync() throws BackingStoreException {
+ wrapped.sync();
+ }
+
+ } //end static inner class OSGiLocalRootPreferences
+
+ private IEclipsePreferences bundlePreferences;
+
+ OSGiPreferencesServiceImpl(IEclipsePreferences bundlePreferences) {
+ this.bundlePreferences = bundlePreferences;
}
public Preferences getSystemPreferences() {
- if (systemPreferences == null) {
- systemPreferences = new OSGiPreferences(systemPrefsDir, this);
- try {
- systemPreferences.sync();
- } catch (BackingStoreException e) {
- //nothing
- }
- }
- return systemPreferences;
+ return new OSGiLocalRootPreferences(bundlePreferences.node("system"));
}
public Preferences getUserPreferences(String name) {
- Preferences userPref = (Preferences) userPreferences.get(name);
- if (userPref == null) {
- userPref = new OSGiPreferences(new File(userPrefsDir, name), this);
- try {
- userPref.sync();
- } catch (BackingStoreException e) {
- //nothing
- }
- userPreferences.put(name, userPref);
- }
- return userPref;
+ return new OSGiLocalRootPreferences(bundlePreferences.node("user/" + name));
}
public String[] getUsers() {
- return userPrefsDir.list();
- }
-
- /**
- * Called when Bundle ungets Preferences Service - flushes all preferences to disk.
- */
- void destroy() {
+ String[] users = null;
try {
- if (systemPreferences != null && systemPreferences.nodeExists("")) { //$NON-NLS-1$
- systemPreferences.flush();
- }
+ users = bundlePreferences.node("user").childrenNames();
} catch (BackingStoreException e) {
- //nothing
- }
- Iterator it = userPreferences.values().iterator();
- while (it.hasNext()) {
- Preferences userPreference = (Preferences) it.next();
- try {
- if (userPreference.nodeExists("")) { //$NON-NLS-1$
- userPreference.flush();
- }
- } catch (BackingStoreException e) {
- //nothing
- }
+ //best effort
}
-
+ return users == null ? new String[0] : users;
}
+
}
diff --git a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceManager.java b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceManager.java
index 770e86c39..aec3a0632 100644
--- a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceManager.java
+++ b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/OSGiPreferencesServiceManager.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004 IBM Corporation and others.
+ * Copyright (c) 2004, 2006 IBM Corporation and others.
* 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
@@ -10,11 +10,12 @@
*******************************************************************************/
package org.eclipse.core.internal.preferences;
-import java.io.File;
import java.util.Set;
import java.util.TreeSet;
-
+import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.osgi.framework.*;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
/**
* <p>
@@ -25,84 +26,80 @@ import org.osgi.framework.*;
* <p>
* Also deletes saved preferences for bundles which are uninstalled.
* </p>
- * <p>
- * Preferences are saved in the Bundle Data area under the directory "OSGiPreferences".
- * </p>
*/
public class OSGiPreferencesServiceManager implements ServiceFactory, BundleListener {
- private static final String OSGI_PREFS_DIR = "OSGiPreferences"; //$NON-NLS-1$
+ private static final String ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI = "org.eclipse.core.internal.preferences.osgi"; //$NON-NLS-1$
- private File prefsDir;
+ //keys are bundles that use OSGi prefs
+ private Preferences prefBundles;
public OSGiPreferencesServiceManager(BundleContext context) {
- prefsDir = context.getDataFile(OSGI_PREFS_DIR);
-
context.addBundleListener(this);
+ //prefBundles = new InstanceScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI);
+ prefBundles = new ConfigurationScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI);
+
//clean up prefs for bundles that have been uninstalled
- Bundle[] allBundles = context.getBundles();
- Set bundleDirNames = new TreeSet();
- for (int i = 0; i < allBundles.length; i++) {
- bundleDirNames.add(getBundleDirName(allBundles[i]));
- }
- File[] prefsNodeDirs = prefsDir.listFiles();
- prefsNodeDirs = prefsNodeDirs == null ? new File[0] : prefsNodeDirs;
+ try {
- for (int i = 0; i < prefsNodeDirs.length; i++) {
- if (!bundleDirNames.contains(prefsNodeDirs[i].getName())) {
- rmdir(prefsNodeDirs[i]);
+ //get list of currently installed bundles
+ Bundle[] allBundles = context.getBundles();
+ Set bundleQualifiers = new TreeSet();
+ for (int i = 0; i < allBundles.length; i++) {
+ bundleQualifiers.add(getQualifier(allBundles[i]));
}
- }
- }
-
- /**
- * Recursively remove a file or a directory and all of it's children.
- */
- private void rmdir(File file) {
- if (!file.exists()) {
- return;
- }
- if (file.isDirectory()) {
- File[] children = file.listFiles();
+ //get list of bundles we created prefs for
+ String[] prefsBundles = prefBundles.keys();
- for (int i = 0; i < children.length; i++) {
- rmdir(children[i]);
+ //remove prefs nodes for bundles that are no longer installed
+ for (int i = 0; i < prefsBundles.length; i++) {
+ if (!bundleQualifiers.contains(prefsBundles[i])) {
+ removePrefs(prefsBundles[i]);
+ }
}
+
+ } catch (BackingStoreException e) {
+ //best effort
}
- file.delete();
}
/**
- * Bundle Preferences are saves in a directory with the same name as the bundle's
- * symbolic id. For backwards compatibility, preferences for bundles that do not
- * have a symbolic id are saved in a directory named
- * 'org.eclipse.core.internal.preferences.OSGiPreferences.bundleid.&lt;bundle id&gt;'.
+ * Creates a new OSGiPreferencesServiceImpl for each bundle.
*/
- private String getBundleDirName(Bundle bundle) {
- String bundleDirName = bundle.getSymbolicName();
-
- //backwards compatibility - if bundle does not have symbolic name
- if (bundleDirName == null) {
- bundleDirName = "org.eclipse.core.internal.preferences.OSGiPreferences.bundleid." + bundle.getBundleId(); //$NON-NLS-1$
+ public Object getService(Bundle bundle, ServiceRegistration registration) {
+ String qualifier = getQualifier(bundle);
+ //remember we created prefs for this bundle
+ prefBundles.put(qualifier, ""); //$NON-NLS-1$
+ try {
+ prefBundles.flush();
+ } catch (BackingStoreException e) {
+ //best effort
}
- return bundleDirName;
+ //return new OSGiPreferencesServiceImpl(new InstanceScope().getNode(getQualifier(bundle)));
+ return new OSGiPreferencesServiceImpl(new ConfigurationScope().getNode(getQualifier(bundle)));
}
-
+
/**
- * Creates a new OSGiPreferencesServiceImpl for each bundle.
+ * Store preferences per bundle id
*/
- public Object getService(Bundle bundle, ServiceRegistration registration) {
- return new OSGiPreferencesServiceImpl(new File(prefsDir, getBundleDirName(bundle)));
+ private String getQualifier(Bundle bundle) {
+ String qualifier = "org.eclipse.core.runtime.preferences.OSGiPreferences." + bundle.getBundleId(); //$NON-NLS-1$
+ return qualifier;
}
/**
- * Flush the bundle's preferences to disk.
+ * Flush the bundle's preferences.
*/
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
- ((OSGiPreferencesServiceImpl) service).destroy();
+ try {
+ //new InstanceScope().getNode(getQualifier(bundle)).flush();
+ new ConfigurationScope().getNode(getQualifier(bundle)).flush();
+ } catch (BackingStoreException e) {
+ //best effort
+ }
}
/**
@@ -110,10 +107,22 @@ public class OSGiPreferencesServiceManager implements ServiceFactory, BundleList
*/
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.UNINSTALLED) {
- File bundlePrefs = new File(prefsDir, getBundleDirName(event.getBundle()));
- rmdir(bundlePrefs);
+ try {
+ removePrefs(getQualifier(event.getBundle()));
+ } catch (BackingStoreException e) {
+ //best effort
+ }
}
}
+ protected void removePrefs(String qualifier) throws BackingStoreException {
+ //remove bundle's prefs
+ //new InstanceScope().getNode(qualifier).removeNode();
+ new ConfigurationScope().getNode(qualifier).removeNode();
+
+ //remove from our list of bundles with prefs
+ prefBundles.remove(qualifier);
+ prefBundles.flush();
+ }
}

Back to the top