From 8c37bfc7a0e4098b6813873c161a99a66a18b9b2 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 3 Dec 2023 17:25:24 +0100 Subject: [PATCH] Introduce 'user' preference scope located in /.eclipse --- .../preferences/PreferencesService.java | 2 + .../internal/preferences/UserPreferences.java | 48 ++++++++++++++++ .../core/runtime/preferences/UserScope.java | 57 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/UserPreferences.java create mode 100644 bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/runtime/preferences/UserScope.java diff --git a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/PreferencesService.java b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/PreferencesService.java index 35049cb84a2..b00cdb8f804 100644 --- a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/PreferencesService.java +++ b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/PreferencesService.java @@ -39,6 +39,7 @@ public class PreferencesService implements IPreferencesService { private static List DEFAULT_DEFAULT_LOOKUP_ORDER = List.of( // InstanceScope.SCOPE, // ConfigurationScope.SCOPE, // + UserScope.SCOPE, // DefaultScope.SCOPE); private static final char EXPORT_ROOT_PREFIX = '!'; private static final char BUNDLE_VERSION_PREFIX = '@'; @@ -67,6 +68,7 @@ private PreferencesService() { initializeDefaultScope(DefaultScope.SCOPE, new DefaultPreferences()); initializeDefaultScope(InstanceScope.SCOPE, new InstancePreferences()); initializeDefaultScope(ConfigurationScope.SCOPE, new ConfigurationPreferences()); + initializeDefaultScope(UserScope.SCOPE, new UserPreferences()); } @Override diff --git a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/UserPreferences.java b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/UserPreferences.java new file mode 100644 index 00000000000..6c4011a3804 --- /dev/null +++ b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/internal/preferences/UserPreferences.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2023, 2023 Hannes Wellmann and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Hannes Wellmann - initial API and implementation + *******************************************************************************/ +package org.eclipse.core.internal.preferences; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.preferences.UserScope; + +public class UserPreferences extends SingletonEclipsePreferences { + + // cache which nodes have been loaded from disk + private static final Set LOADED_NODES = ConcurrentHashMap.newKeySet(); + private static final AtomicBoolean INITIALIZED = new AtomicBoolean(); + + /** + * Default constructor. Should only be called by #createExecutableExtension. + */ + public UserPreferences() { + this(null, null); + } + + private UserPreferences(EclipsePreferences parent, String name) { + super(parent, name, LOADED_NODES, INITIALIZED); + } + + @Override + IPath getBaseLocation() { + return UserScope.INSTANCE.getLocation(); + } + + @Override + protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) { + return new UserPreferences(nodeParent, nodeName); + } +} diff --git a/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/runtime/preferences/UserScope.java b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/runtime/preferences/UserScope.java new file mode 100644 index 00000000000..8cce11eabcc --- /dev/null +++ b/bundles/org.eclipse.equinox.preferences/src/org/eclipse/core/runtime/preferences/UserScope.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2023, 2023 Hannes Wellmann and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Hannes Wellmann - initial API and implementation + *******************************************************************************/ +package org.eclipse.core.runtime.preferences; + +import org.eclipse.core.internal.preferences.AbstractScope; +import org.eclipse.core.runtime.IPath; + +/** + * @since 3.11 + */ +public class UserScope extends AbstractScope { + + /** + * String constant (value of "user") used for the scope name for + * the user preference scope. + */ + public static final String SCOPE = "user"; //$NON-NLS-1$ + + private static final IPath USER_HOME_PREFERENCE_LOCATION; + static { + String userHome = System.getProperty("user.home"); //$NON-NLS-1$ + USER_HOME_PREFERENCE_LOCATION = IPath.forWindows(userHome).append(".eclipse"); //$NON-NLS-1$ + } + + /** + * Singleton instance of a User Scope object. Typical usage is: + * UserScope.INSTANCE.getNode(...); + * + * @since 3.4 + */ + public static final IScopeContext INSTANCE = new UserScope(); + + private UserScope() { // static use only via INSTANCE + } + + @Override + public String getName() { + return SCOPE; + } + + @Override + public IPath getLocation() { + return USER_HOME_PREFERENCE_LOCATION; + } + +}