diff options
author | Szymon Brandys | 2010-10-07 11:24:26 +0000 |
---|---|---|
committer | Szymon Brandys | 2010-10-07 11:24:26 +0000 |
commit | 02a98a5efe94cc82a3a027c25b0a2d6e5a2c0466 (patch) | |
tree | 980851edcfa1ff5fcf877e3c9bc8dc6cb4bf881f /bundles/org.eclipse.core.net | |
parent | d18025202892b1e0406d5d8340d09ddf8bf303a9 (diff) | |
download | eclipse.platform.team-02a98a5efe94cc82a3a027c25b0a2d6e5a2c0466.tar.gz eclipse.platform.team-02a98a5efe94cc82a3a027c25b0a2d6e5a2c0466.tar.xz eclipse.platform.team-02a98a5efe94cc82a3a027c25b0a2d6e5a2c0466.zip |
bug 242057 - [Net] Default preference gets ignored
Diffstat (limited to 'bundles/org.eclipse.core.net')
6 files changed, 501 insertions, 203 deletions
diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/Activator.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/Activator.java index 5ed05a291..419bd090f 100644 --- a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/Activator.java +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/Activator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2010 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 @@ -22,7 +22,6 @@ import org.eclipse.core.net.proxy.IProxyService; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.preferences.ConfigurationScope; import org.eclipse.osgi.framework.log.FrameworkLog; import org.eclipse.osgi.framework.log.FrameworkLogEntry; import org.eclipse.osgi.service.datalocation.Location; @@ -61,6 +60,8 @@ public class Activator implements BundleActivator { private boolean debug = false; + private PreferenceManager preferenceManger; + /** * Constructor for use by the Eclipse platform only. */ @@ -140,8 +141,8 @@ public class Activator implements BundleActivator { status.getCode(), status.getMessage(), stackCode, t, children); } - public org.osgi.service.prefs.Preferences getPreferences() { - return new ConfigurationScope().getNode(ID); + public PreferenceManager getPreferenceManager() { + return preferenceManger; } public boolean instanceLocationAvailable() { @@ -152,6 +153,7 @@ public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { this.bundleContext = context; + this.preferenceManger = PreferenceManager.createConfigurationManager(ID); Filter filter = null; try { filter = context.createFilter(Location.INSTANCE_FILTER); diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceInitializer.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceInitializer.java index d154f9419..1570a6272 100644 --- a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceInitializer.java +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceInitializer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 IBM Corporation and others. + * Copyright (c) 2007, 2010 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,17 +10,47 @@ *******************************************************************************/ package org.eclipse.core.internal.net; +import org.eclipse.core.net.proxy.IProxyData; import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.osgi.service.prefs.BackingStoreException; +import org.osgi.service.prefs.Preferences; public class PreferenceInitializer extends AbstractPreferenceInitializer { + private static final int DEFAULT_PREF_PROXY_PORT = -1; + private static final boolean DEFAULT_PREF_PROXY_HAS_AUTH = false; + private static final boolean DEFAULT_PREF_ENABLED = true; + private static final boolean DEFAULT_PREF_OS = true; + private static final String DEFAULT_PREF_NON_PROXIED_HOSTS = "localhost|127.0.0.1"; //$NON-NLS-1$ public PreferenceInitializer() { super(); } public void initializeDefaultPreferences() { - // TODO: We should set defaults in the default scope - //((ProxyManager)ProxyManager.getProxyManager()).initialize(); + IEclipsePreferences node = new DefaultScope().getNode(Activator.ID); + node.put(ProxyManager.PREF_NON_PROXIED_HOSTS, DEFAULT_PREF_NON_PROXIED_HOSTS); + node.putBoolean(ProxyManager.PREF_ENABLED, DEFAULT_PREF_ENABLED); + node.putBoolean(ProxyManager.PREF_OS, DEFAULT_PREF_OS); + + Preferences type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.HTTP_PROXY_TYPE); + type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT); + type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH); + + type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.HTTPS_PROXY_TYPE); + type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT); + type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH); + + type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.SOCKS_PROXY_TYPE); + type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT); + type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH); + + try { + node.flush(); + } catch (BackingStoreException e) { + Activator.logInfo("Could not store default preferences", e); //$NON-NLS-1$ + } } } diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceManager.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceManager.java new file mode 100644 index 000000000..0d79ac7b0 --- /dev/null +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceManager.java @@ -0,0 +1,382 @@ +/******************************************************************************* +* Copyright (c) 2010 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 +* http://www.eclipse.org/legal/epl-v10.html +* +* Contributors: +* IBM Corporation - initial API and implementation +*******************************************************************************/ +package org.eclipse.core.internal.net; + +import org.eclipse.core.net.proxy.IProxyData; +import org.eclipse.core.runtime.preferences.ConfigurationScope; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; +import org.osgi.service.prefs.BackingStoreException; +import org.osgi.service.prefs.Preferences; + +/** + * Provides set of methods to operate on preferences + */ +public class PreferenceManager { + + public static final String ROOT = ""; //$NON-NLS-1$ + + private static final String PREF_HAS_MIGRATED = "org.eclipse.core.net.hasMigrated"; //$NON-NLS-1$ + + /** + * Preference constants used by Update to record the HTTP proxy + */ + private static String HTTP_PROXY_HOST = "org.eclipse.update.core.proxy.host"; //$NON-NLS-1$ + private static String HTTP_PROXY_PORT = "org.eclipse.update.core.proxy.port"; //$NON-NLS-1$ + private static String HTTP_PROXY_ENABLE = "org.eclipse.update.core.proxy.enable"; //$NON-NLS-1$ + + private static final int DEFAULT_INT = -1; + private static final String DEFAULT_STRING = null; + private static final boolean DEFAULT_BOOLEAN = false; + + private static boolean migrated = false; + + private IEclipsePreferences defaultScope; + private IEclipsePreferences instanceScope; + private IEclipsePreferences currentScope; + + private PreferenceManager(String id) { + this.defaultScope = new DefaultScope().getNode(id); + this.instanceScope = new InstanceScope().getNode(id); + } + + /** + * Creates the preferences manager for the node defined by id + * in configuration scope. + * @param id node name for which node should be created in configuration scope + * @return {@link PreferenceManager} + */ + public static PreferenceManager createConfigurationManager(String id) { + PreferenceManager manager = new PreferenceManager(id); + manager.currentScope = new ConfigurationScope().getNode(id); + return manager; + } + + /** + * Checks if preference migration was already performed. + * @return <code>boolean</code> + */ + public boolean isMigrated() { + return migrated; + } + + /** + * Returns the <code>boolean</code> value associated with the specified <code>key</code> + * for specified <code>node</code> in current scope. + * + * <p> + * Returns the value specified in the default scope if there is no value associated with the + * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated + * value is something that can not be parsed as an integer value. + * Use {@link #putBoolean(String, String, boolean)} to set the value of this preference key. + * </p> + * @param node node + * @param key key whose associated value is to be returned as an <code>boolean</code>. + * @return the <code>boolean</code> value associated with <code>key</code>, or + * <code>false</code> if the associated value does not exist in either scope or cannot + * be interpreted as an <code>boolean</code>. + * @see #putBoolean(String, String, boolean) + */ + public boolean getBoolean(String node, String key) { + return currentScope.node(node).getBoolean(key, defaultScope.node(node).getBoolean(key, DEFAULT_BOOLEAN)); + } + + /** + * Returns the <code>int</code> value associated with the specified <code>key</code> + * for specified <code>node</code> in current scope. + * + * <p> + * Returns the value specified in the default scope if there is no value associated with the + * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated + * value is something that can not be parsed as an integer value. + * Use {@link #putInt(String, String, int)} to set the value of this preference key. + * </p> + * @param node node + * @param key key whose associated value is to be returned as an <code>int</code>. + * @return the <code>int</code> value associated with <code>key</code>, or + * <code>-1</code> if the associated value does not exist in either scope or cannot + * be interpreted as an <code>int</code>. + * @see #putInt(String, String, int) + */ + public int getInt(String node, String key) { + return currentScope.node(node).getInt(key, defaultScope.node(node).getInt(key, DEFAULT_INT)); + } + + /** + * Returns the <code>String</code> value associated with the specified <code>key</code> + * for specified <code>node</code> in current scope. + * + * <p> + * Returns the value specified in the default scope if there is no value associated with the + * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated + * value is something that can not be parsed as an integer value. + * Use {@link #putString(String, String, String)} to set the value of this preference key. + * </p> + * @param node node + * @param key key whose associated value is to be returned as an <code>String</code>. + * @return the <code>String</code> value associated with <code>key</code>, or + * <code>null</code> if the associated value does not exist in either scope or cannot + * be interpreted as an <code>String</code>. + * @see #putString(String, String, String) + */ + public String getString(String node, String key) { + return currentScope.node(node).get(key, defaultScope.node(node).get(key, DEFAULT_STRING)); + } + + /** + * Associates the specified <code>int</code> value with the specified key + * for specified <code>node</code> in current scope. + * + * @param node node + * @param key <code>key</code> with which the string form of value is to be associated. + * @param value <code>value</code> to be associated with <code>key</code>. + * @see #getInt(String, String) + */ + public void putInt(String node, String key, int value) { + currentScope.node(node).putInt(key, value); + } + + /** + * Associates the specified <code>boolean</code> value with the specified key + * for specified <code>node</code> in current scope. + * + * @param node node + * @param key <code>key</code> with which the string form of value is to be associated. + * @param value <code>value</code> to be associated with <code>key</code>. + * @see #getBoolean(String, String) + */ + public void putBoolean(String node, String key, boolean value) { + currentScope.node(node).putBoolean(key, value); + } + + /** + * Associates the specified <code>String</code> value with the specified key + * for specified <code>node</code> in current scope. + * + * @param node node + * @param key <code>key</code> with which the string form of value is to be associated. + * @param value <code>value</code> to be associated with <code>key</code>. + * @see #getString(String, String) + */ + public void putString(String node, String key, String value) { + currentScope.node(node).put(key, value); + } + + /** + * Register the given listener for notification of preference changes. + * Calling this method multiple times with the same listener has no effect. The + * given listener argument must not be <code>null</code>. + * + * @param node node + * @param listener the preference change listener to register + * @see #removePreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener) + * @see IEclipsePreferences.IPreferenceChangeListener + */ + public void addPreferenceChangeListener(String node, IPreferenceChangeListener listener) { + ((IEclipsePreferences)currentScope.node(node)).addPreferenceChangeListener(listener); + } + + /** + * De-register the given listener from receiving notification of preference changes + * Calling this method multiple times with the same listener has no + * effect. The given listener argument must not be <code>null</code>. + * @param node node + * @param listener the preference change listener to remove + * @see #addPreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener) + * @see IEclipsePreferences.IPreferenceChangeListener + */ + public void removePreferenceChangeListener(String node, IPreferenceChangeListener listener) { + ((IEclipsePreferences)currentScope.node(node)).removePreferenceChangeListener(listener); + } + + /** + * Register the given listener for changes to this node. + * Calling this method multiple times with the same listener has no effect. The + * given listener argument must not be <code>null</code>. + * + * @param node node + * @param listener the preference change listener to register + * @see #removeNodeChangeListener(String, IEclipsePreferences.INodeChangeListener) + * @see IEclipsePreferences.IPreferenceChangeListener + */ + public void addNodeChangeListener(String node, INodeChangeListener listener) { + ((IEclipsePreferences)currentScope.node(node)).addNodeChangeListener(listener); + } + + /** + * De-register the given listener from receiving event change notifications for this node. + * Calling this method multiple times with the same listener has no + * effect. The given listener argument must not be <code>null</code>. + * @param node node + * @param listener the preference change listener to remove + * @see #addPreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener) + * @see IEclipsePreferences.IPreferenceChangeListener + */ + public void removeNodeChangeListener(String node, INodeChangeListener listener) { + ((IEclipsePreferences)currentScope.node(node)).removeNodeChangeListener(listener); + } + + /** + * Removes this node and all of its descendants, + * invalidating any properties contained in the removed nodes. + * @param node name of a node which should be removed + * @throws BackingStoreException - if this operation cannot be completed + * due to a failure in the backing store, or inability to communicate with it. + */ + public void removeNode(String node) throws BackingStoreException { + currentScope.node(node).removeNode(); + } + + /** + * Forces any changes in the contents of current scope + * and its descendants to the persistent store. + * @throws BackingStoreException - if this operation cannot be completed + * due to a failure in the backing store, or inability to communicate with it. + */ + public void flush() throws BackingStoreException { + currentScope.flush(); + } + + /** + * Migrate preferences from instance scope to current scope. + * @param proxies proxy types for which migration should be performed {@link ProxyType} + */ + public void migrate(ProxyType[] proxies) { + migrated = true; + if (currentScope.getBoolean(PREF_HAS_MIGRATED, false) + || currentScope.name().equals(InstanceScope.SCOPE)) { + return; + } + currentScope.putBoolean(PREF_HAS_MIGRATED, true); + migrateInstanceScopePreferences(proxies, true); + } + + void migrateInstanceScopePreferences(ProxyType[] proxies, boolean isInitialize) { + migrateUpdateHttpProxy(proxies, isInitialize); + + // migrate enabled status + if (currentScope.get(ProxyManager.PREF_ENABLED, null) == null) { + String instanceEnabled = instanceScope.get(ProxyManager.PREF_ENABLED, null); + if (instanceEnabled != null) + currentScope.put(ProxyManager.PREF_ENABLED, instanceEnabled); + } + + // migrate enabled status + if (currentScope.get(ProxyManager.PREF_OS, null) == null) { + String instanceEnabled = instanceScope.get(ProxyManager.PREF_OS, null); + if (instanceEnabled != null) + currentScope.put(ProxyManager.PREF_OS, instanceEnabled); + } + + // migrate non proxied hosts if not already set + if (currentScope.get(ProxyManager.PREF_NON_PROXIED_HOSTS, null) == null) { + String instanceNonProxiedHosts = instanceScope.get(ProxyManager.PREF_NON_PROXIED_HOSTS, null); + if (instanceNonProxiedHosts != null) { + currentScope.put(ProxyManager.PREF_NON_PROXIED_HOSTS, instanceNonProxiedHosts); + } + } + + // migrate proxy data + PreferenceManager instanceManager = PreferenceManager.createInstanceManager(Activator.ID); + for (int i = 0; i < proxies.length; i++) { + ProxyType type = proxies[i]; + IProxyData data = type.getProxyData(ProxyType.DO_NOT_VERIFY); + if (data.getHost() == null) { + ProxyType instanceType = new ProxyType(type.getName(), instanceManager); + IProxyData instanceData = instanceType.getProxyData(ProxyType.DO_NOT_VERIFY); + if (instanceData.getHost() != null) + type.setProxyData(instanceData); + } + } + + // if this an import we should remove the old node + if (!isInitialize) { + try { + instanceScope.removeNode(); + } catch (BackingStoreException e) { + // ignore + } + } + } + + private void migrateUpdateHttpProxy(ProxyType[] proxies, boolean isInitialize) { + if (!instanceScope.getBoolean(PREF_HAS_MIGRATED, false)) { + // Only set the migration bit when initializing + if (isInitialize) + instanceScope.putBoolean(PREF_HAS_MIGRATED, true); + Preferences updatePrefs = instanceScope.parent().node("org.eclipse.update.core"); //$NON-NLS-1$ + String httpProxyHost = getHostToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); + int port = getPortToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); + boolean httpProxyEnable = getEnablementToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); + if (httpProxyHost != null) { + ProxyData proxyData = new ProxyData(IProxyData.HTTP_PROXY_TYPE, + httpProxyHost, port, false, null); + for (int i = 0; i < proxies.length; i++) { + ProxyType type = proxies[i]; + if (type.getName().equals(proxyData.getType())) { + type.updatePreferencesIfMissing(proxyData); + } + } + if (httpProxyEnable) { + instanceScope.putBoolean(ProxyManager.PREF_ENABLED, true); + } + } + } + } + + private String getHostToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { + String httpProxyHost = updatePrefs.get(HTTP_PROXY_HOST, ""); //$NON-NLS-1$ + if (checkSystemProperties && "".equals(httpProxyHost)) { //$NON-NLS-1$ + httpProxyHost = System.getProperty("http.proxyHost", ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + if ("".equals(httpProxyHost)) //$NON-NLS-1$ + httpProxyHost = null; + updatePrefs.remove(HTTP_PROXY_HOST); + return httpProxyHost; + } + + private int getPortToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { + String httpProxyPort = updatePrefs.get(HTTP_PROXY_PORT, ""); //$NON-NLS-1$ + if (checkSystemProperties && "".equals(httpProxyPort)) { //$NON-NLS-1$ + httpProxyPort = System.getProperty("http.proxyPort", ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + updatePrefs.remove(HTTP_PROXY_PORT); + int port = -1; + if (httpProxyPort != null && !"".equals(httpProxyPort)) //$NON-NLS-1$ + try { + port = Integer.parseInt(httpProxyPort); + } catch (NumberFormatException e) { + // Ignore + } + return port; + } + + private boolean getEnablementToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { + boolean httpProxyEnable = false; + if (checkSystemProperties && updatePrefs.get(HTTP_PROXY_ENABLE, null) == null) { + httpProxyEnable = Boolean.getBoolean("http.proxySet"); //$NON-NLS-1$ + } else { + httpProxyEnable = updatePrefs.getBoolean(HTTP_PROXY_ENABLE, false); + updatePrefs.remove(HTTP_PROXY_ENABLE); + } + return httpProxyEnable; + } + + private static PreferenceManager createInstanceManager(String id) { + PreferenceManager manager = new PreferenceManager(id); + manager.currentScope = manager.instanceScope; + return manager; + } + +} diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceModifyListener.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceModifyListener.java index a9181c4c4..dc8f86cbb 100644 --- a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceModifyListener.java +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/PreferenceModifyListener.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 IBM Corporation and others. + * Copyright (c) 2007, 2010 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,7 +10,6 @@ *******************************************************************************/ package org.eclipse.core.internal.net; -import org.eclipse.core.runtime.preferences.ConfigurationScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.osgi.service.prefs.BackingStoreException; @@ -25,7 +24,7 @@ public class PreferenceModifyListener extends public IEclipsePreferences preApply(IEclipsePreferences node) { try { if (node.nodeExists(InstanceScope.SCOPE)) { - ((ProxyManager)ProxyManager.getProxyManager()).migrateInstanceScopePreferences(node.node(InstanceScope.SCOPE), node.node(ConfigurationScope.SCOPE), false); + ((ProxyManager)ProxyManager.getProxyManager()).migrateInstanceScopePreferences(false); } } catch (BackingStoreException e) { Activator.logError("Could not access instance preferences", e); //$NON-NLS-1$ diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyManager.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyManager.java index d94862316..789bcd332 100644 --- a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyManager.java +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyManager.java @@ -14,6 +14,7 @@ import java.net.Authenticator; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Properties; @@ -30,34 +31,23 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.RegistryFactory; import org.eclipse.core.runtime.SafeRunner; -import org.eclipse.core.runtime.preferences.ConfigurationScope; -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; import org.eclipse.osgi.util.NLS; import org.osgi.service.prefs.BackingStoreException; -import org.osgi.service.prefs.Preferences; public class ProxyManager implements IProxyService, IPreferenceChangeListener { - - private static final String PREF_HAS_MIGRATED = "org.eclipse.core.net.hasMigrated"; //$NON-NLS-1$ - /** - * Preference constants used by Update to record the HTTP proxy - */ - private static String HTTP_PROXY_HOST = "org.eclipse.update.core.proxy.host"; //$NON-NLS-1$ - private static String HTTP_PROXY_PORT = "org.eclipse.update.core.proxy.port"; //$NON-NLS-1$ - private static String HTTP_PROXY_ENABLE = "org.eclipse.update.core.proxy.enable"; //$NON-NLS-1$ - - private static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$ - private static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$ - private static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$ + static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$ + static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$ + static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$ private static IProxyService proxyManager; private AbstractProxyProvider nativeProxyProvider; + private PreferenceManager preferenceManager; + ListenerList listeners = new ListenerList(ListenerList.IDENTITY); private String[] nonProxiedHosts; private final ProxyType[] proxies = new ProxyType[] { @@ -66,8 +56,6 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { new ProxyType(IProxyData.SOCKS_PROXY_TYPE) }; - private boolean migrated = false; - private ProxyManager() { try { nativeProxyProvider = (AbstractProxyProvider) Class.forName( @@ -77,6 +65,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { } catch (Exception e) { Activator.logInfo("Problems occured during the proxy provider initialization.", e); //$NON-NLS-1$ } + preferenceManager = Activator.getInstance().getPreferenceManager(); } /** @@ -124,7 +113,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { public synchronized String[] getNonProxiedHosts() { checkMigrated(); if (nonProxiedHosts == null) { - String prop = Activator.getInstance().getPreferences().get(PREF_NON_PROXIED_HOSTS, "localhost|127.0.0.1"); //$NON-NLS-1$ + String prop = preferenceManager.getString(PreferenceManager.ROOT, PREF_NON_PROXIED_HOSTS); nonProxiedHosts = ProxyType.convertPropertyStringToHosts(prop); } if (nonProxiedHosts.length == 0) @@ -153,10 +142,13 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { Assert.isTrue(host.length() > 0); } String[] oldHosts = nonProxiedHosts; + if (Arrays.equals(oldHosts, hosts)) { + return; + } nonProxiedHosts = hosts; - Activator.getInstance().getPreferences().put(PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts)); + preferenceManager.putString(PreferenceManager.ROOT, PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts)); try { - Activator.getInstance().getPreferences().flush(); + preferenceManager.flush(); } catch (BackingStoreException e) { Activator.logError( "An error occurred while writing out the non-proxied hosts list", e); //$NON-NLS-1$ @@ -232,8 +224,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { } private boolean internalIsProxiesEnabled() { - return Activator.getInstance().getPreferences().getBoolean( - PREF_ENABLED, true); + return preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED); } /* (non-Javadoc) @@ -246,7 +237,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { return; // Setting the preference will trigger the system property update // (see preferenceChange) - Activator.getInstance().getPreferences().putBoolean(PREF_ENABLED, enabled); + preferenceManager.putBoolean(PreferenceManager.ROOT, PREF_ENABLED, enabled); } private void internalSetEnabled(boolean enabled, boolean systemEnabled) { @@ -255,7 +246,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { sysProps.put("systemProxySet", systemEnabled ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ updateSystemProperties(); try { - Activator.getInstance().getPreferences().flush(); + preferenceManager.flush(); } catch (BackingStoreException e) { Activator.logError( "An error occurred while writing out the enablement state", e); //$NON-NLS-1$ @@ -275,7 +266,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { public void initialize() { checkMigrated(); - ((IEclipsePreferences)Activator.getInstance().getPreferences()).addPreferenceChangeListener(this); + preferenceManager.addPreferenceChangeListener(PreferenceManager.ROOT, this); // Now initialize each proxy type for (int i = 0; i < proxies.length; i++) { ProxyType type = proxies[i]; @@ -404,136 +395,23 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { return null; } } - private synchronized void checkMigrated() { - if (migrated || !Activator.getInstance().instanceLocationAvailable()) + if (preferenceManager.isMigrated() || !Activator.getInstance().instanceLocationAvailable()) { return; - - migrated = true; - if (Activator.getInstance().getPreferences().getBoolean(PREF_HAS_MIGRATED, false)) - return; - - Activator.getInstance().getPreferences().putBoolean(PREF_HAS_MIGRATED, true); - migrateInstanceScopePreferences(new InstanceScope().getNode(""), new ConfigurationScope().getNode(""), true); //$NON-NLS-1$//$NON-NLS-2$ - } - - void migrateInstanceScopePreferences(Preferences instanceNode, Preferences configurationNode, boolean isInitialize) { - migrateUpdateHttpProxy(instanceNode, isInitialize); - - Preferences netInstancePrefs = instanceNode.node(Activator.ID); - Preferences netConfigurationPrefs = configurationNode.node(Activator.ID); - - // migrate enabled status - if (netConfigurationPrefs.get(PREF_ENABLED, null) == null) { - String instanceEnabled = netInstancePrefs.get(PREF_ENABLED, null); - if (instanceEnabled != null) - netConfigurationPrefs.put(PREF_ENABLED, instanceEnabled); - } - - // migrate enabled status - if (netConfigurationPrefs.get(PREF_OS, null) == null) { - String instanceEnabled = netInstancePrefs.get(PREF_OS, null); - if (instanceEnabled != null) - netConfigurationPrefs.put(PREF_OS, instanceEnabled); } - - // migrate non proxied hosts if not already set - if (netConfigurationPrefs.get(PREF_NON_PROXIED_HOSTS, null) == null) { - String instanceNonProxiedHosts = netInstancePrefs.get(PREF_NON_PROXIED_HOSTS, null); - if (instanceNonProxiedHosts != null) { - netConfigurationPrefs.put(PREF_NON_PROXIED_HOSTS, instanceNonProxiedHosts); - nonProxiedHosts = null; - } - } - - // migrate proxy data - for (int i = 0; i < proxies.length; i++) { - ProxyType type = proxies[i]; - IProxyData data = type.getProxyData(ProxyType.DO_NOT_VERIFY); - if (data.getHost() == null) { - ProxyType instanceType = new ProxyType(type.getName(),netInstancePrefs); - IProxyData instanceData = instanceType.getProxyData(ProxyType.DO_NOT_VERIFY); - if (instanceData.getHost() != null) - type.setProxyData(instanceData); - } - } - - // if this an import we should remove the old node - if (! isInitialize) { - try { - netInstancePrefs.removeNode(); - } catch (BackingStoreException e) { - // ignore - } - } + preferenceManager.migrate(proxies); } - private void migrateUpdateHttpProxy(Preferences node, boolean isInitialize) { - Preferences netPrefs = node.node(Activator.ID); - if (!netPrefs.getBoolean(PREF_HAS_MIGRATED, false)) { - // Only set the migration bit when initializing - if (isInitialize) - netPrefs.putBoolean(PREF_HAS_MIGRATED, true); - Preferences updatePrefs = node.node("org.eclipse.update.core"); //$NON-NLS-1$ - String httpProxyHost = getHostToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); - int port = getPortToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); - boolean httpProxyEnable = getEnablementToMigrate(updatePrefs, isInitialize /* checkSystemProperties */); - if (httpProxyHost != null) { - ProxyData proxyData = new ProxyData(IProxyData.HTTP_PROXY_TYPE, - httpProxyHost, port, false, null); - ProxyType type = getType(proxyData); - type.updatePreferencesIfMissing(netPrefs, proxyData); - if (httpProxyEnable) { - netPrefs.putBoolean(ProxyManager.PREF_ENABLED, true); - } - } - } - } - - private boolean getEnablementToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { - boolean httpProxyEnable = false; - if (checkSystemProperties && updatePrefs.get(HTTP_PROXY_ENABLE, null) == null) { - httpProxyEnable = Boolean.getBoolean("http.proxySet"); //$NON-NLS-1$ - } else { - httpProxyEnable = updatePrefs.getBoolean(HTTP_PROXY_ENABLE, false); - updatePrefs.remove(HTTP_PROXY_ENABLE); - } - return httpProxyEnable; - } - - private int getPortToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { - String httpProxyPort = updatePrefs.get(HTTP_PROXY_PORT, ""); //$NON-NLS-1$ - if (checkSystemProperties && "".equals(httpProxyPort)) { //$NON-NLS-1$ - httpProxyPort = System.getProperty("http.proxyPort", ""); //$NON-NLS-1$ //$NON-NLS-2$ - } - updatePrefs.remove(HTTP_PROXY_PORT); - int port = -1; - if (httpProxyPort != null && !"".equals(httpProxyPort)) //$NON-NLS-1$ - try { - port = Integer.parseInt(httpProxyPort); - } catch (NumberFormatException e) { - // Ignore - } - return port; - } - - private String getHostToMigrate(Preferences updatePrefs, boolean checkSystemProperties) { - String httpProxyHost = updatePrefs.get(HTTP_PROXY_HOST, ""); //$NON-NLS-1$ - if (checkSystemProperties && "".equals(httpProxyHost)) { //$NON-NLS-1$ - httpProxyHost = System.getProperty("http.proxyHost", ""); //$NON-NLS-1$ //$NON-NLS-2$ - } - if ("".equals(httpProxyHost)) //$NON-NLS-1$ - httpProxyHost = null; - updatePrefs.remove(HTTP_PROXY_HOST); - return httpProxyHost; + void migrateInstanceScopePreferences(boolean isInitialize) { + preferenceManager.migrateInstanceScopePreferences(proxies, isInitialize); } public void preferenceChange(PreferenceChangeEvent event) { if (event.getKey().equals(PREF_ENABLED) || event.getKey().equals(PREF_OS)) { checkMigrated(); - internalSetEnabled(Activator.getInstance().getPreferences().getBoolean(PREF_ENABLED, true), - Activator.getInstance().getPreferences().getBoolean(PREF_OS, true)); + internalSetEnabled(preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED), + preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS)); } } @@ -543,8 +421,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { public boolean isSystemProxiesEnabled() { checkMigrated(); - return Activator.getInstance().getPreferences().getBoolean(PREF_OS, - true); + return preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS); } public void setSystemProxiesEnabled(boolean enabled) { @@ -554,7 +431,7 @@ public class ProxyManager implements IProxyService, IPreferenceChangeListener { return; // Setting the preference will trigger the system property update // (see preferenceChange) - Activator.getInstance().getPreferences().putBoolean(PREF_OS, enabled); + preferenceManager.putBoolean(PreferenceManager.ROOT, PREF_OS, enabled); } public IProxyData[] select(URI uri) { diff --git a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyType.java b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyType.java index f1dcd6d9d..7416867d4 100644 --- a/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyType.java +++ b/bundles/org.eclipse.core.net/src/org/eclipse/core/internal/net/ProxyType.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 IBM Corporation and others. + * Copyright (c) 2007, 2010 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 @@ -16,6 +16,7 @@ import java.util.Properties; import org.eclipse.core.net.proxy.IProxyData; import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener; import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; @@ -26,17 +27,16 @@ import org.eclipse.equinox.security.storage.SecurePreferencesFactory; import org.eclipse.equinox.security.storage.StorageException; import org.eclipse.osgi.util.NLS; import org.osgi.service.prefs.BackingStoreException; -import org.osgi.service.prefs.Preferences; public class ProxyType implements INodeChangeListener, IPreferenceChangeListener { /** * Preference keys */ - private static final String PREF_PROXY_DATA_NODE = "proxyData"; //$NON-NLS-1$ - private static final String PREF_PROXY_HOST = "host"; //$NON-NLS-1$ - private static final String PREF_PROXY_PORT = "port"; //$NON-NLS-1$ - private static final String PREF_PROXY_HAS_AUTH = "hasAuth"; //$NON-NLS-1$ + static final String PREF_PROXY_DATA_NODE = "proxyData"; //$NON-NLS-1$ + static final String PREF_PROXY_HOST = "host"; //$NON-NLS-1$ + static final String PREF_PROXY_PORT = "port"; //$NON-NLS-1$ + static final String PREF_PROXY_HAS_AUTH = "hasAuth"; //$NON-NLS-1$ /** * Verification tags used when creating a proxy data @@ -78,7 +78,7 @@ public class ProxyType implements INodeChangeListener, IPreferenceChangeListener private String name; private boolean updatingPreferences; - private Preferences netPreferences; + private PreferenceManager preferenceManager; public static String convertHostsToPropertyString(String[] value) { StringBuffer buffer = new StringBuffer(); @@ -111,38 +111,28 @@ public class ProxyType implements INodeChangeListener, IPreferenceChangeListener public ProxyType(String name) { this.name = name; - this.netPreferences = Activator.getInstance().getPreferences(); + this.preferenceManager = Activator.getInstance().getPreferenceManager(); } - public ProxyType(String name, Preferences netPreferences) { + public ProxyType(String name, PreferenceManager manager) { this.name = name; - this.netPreferences = netPreferences; + this.preferenceManager = manager; } - private Preferences getPreferenceNode() { - return getParentPreferences().node(getName()); - } - - /** - * Return the preferences node whose child nodes are the know proxy types - * - * @return a preferences node - */ - private Preferences getParentPreferences() { - return netPreferences.node( - PREF_PROXY_DATA_NODE); + private String getPreferenceNode() { + return PREF_PROXY_DATA_NODE + IPath.SEPARATOR + getName(); } public IProxyData getProxyData(int verifyFlag) { return createProxyData(name, getPreferenceNode(), verifyFlag); } - private IProxyData createProxyData(String type, Preferences node, int verifyFlag) { - String host = node.get(PREF_PROXY_HOST, null); + private IProxyData createProxyData(String type, String node, int verifyFlag) { + String host = preferenceManager.getString(node, PREF_PROXY_HOST); if (host != null && host.length() == 0) host = null; - int port = node.getInt(PREF_PROXY_PORT, -1); - boolean requiresAuth = node.getBoolean(PREF_PROXY_HAS_AUTH, false); + int port = preferenceManager.getInt(node, PREF_PROXY_PORT); + boolean requiresAuth = preferenceManager.getBoolean(node, PREF_PROXY_HAS_AUTH); ProxyData proxyData = new ProxyData(type, host, port, requiresAuth, null); loadProxyAuth(proxyData); @@ -176,33 +166,51 @@ public class ProxyType implements INodeChangeListener, IPreferenceChangeListener updatePreferences(getPreferenceNode(), proxyData); } - /* package */ void updatePreferencesIfMissing(Preferences node, IProxyData proxyData) { - Preferences proxyNode = node.node(PREF_PROXY_DATA_NODE).node(getName()); - if (node.get(PREF_PROXY_HOST, null) == null) - updatePreferences(proxyNode, proxyData); + /*package*/ void updatePreferencesIfMissing(IProxyData proxyData) { + String node = getPreferenceNode(); + if (preferenceManager.getString(node, PREF_PROXY_HOST) == null) + updatePreferences(node, proxyData); } - private void updatePreferences(Preferences node, IProxyData proxyData) { + private void updatePreferences(String node, IProxyData proxyData) { + if (!hasPreferencesChanged(node, proxyData)) { + return; + } if (proxyData.getHost() == null) { try { - Preferences parent = node.parent(); - node.removeNode(); - parent.flush(); + preferenceManager.removeNode(node); + preferenceManager.flush(); } catch (BackingStoreException e) { Activator.logError(NLS.bind( "An error occurred removing the {0} proxy node from the preference store", proxyData.getType()), e); //$NON-NLS-1$ } - } else { - node.put(PREF_PROXY_HOST, proxyData.getHost()); - node.putInt(PREF_PROXY_PORT, proxyData.getPort()); - node.putBoolean(PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null); - try { - node.flush(); - } catch (BackingStoreException e) { - Activator.logError(NLS.bind( - "The {0} proxy node could not be written", proxyData.getType()), e); //$NON-NLS-1$ + // Check if there is a value in default scope (e.g. set by -pluginCustomization). + // If it is, update preferences even if host is empty. + if (!hasPreferencesChanged(node, proxyData)) { + return; } } + preferenceManager.putString(node, PREF_PROXY_HOST, proxyData.getHost() != null ? proxyData.getHost() : ""); //$NON-NLS-1$ + preferenceManager.putInt(node, PREF_PROXY_PORT, proxyData.getPort()); + preferenceManager.putBoolean(node, PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null); + try { + preferenceManager.flush(); + } catch (BackingStoreException e) { + Activator.logError(NLS.bind( + "The {0} proxy node could not be written", proxyData.getType()), e); //$NON-NLS-1$ + } + } + + private boolean hasPreferencesChanged(String node, IProxyData proxyData) { + String host = preferenceManager.getString(node, PREF_PROXY_HOST); + if ((host != null && host.equals(proxyData.getHost())) || (host == null && proxyData.getHost() == null)) { + if (preferenceManager.getInt(node, PREF_PROXY_PORT) == proxyData.getPort()) { + if (preferenceManager.getBoolean(node, PREF_PROXY_HAS_AUTH) == proxyData.isRequiresAuthentication()) { + return false; + } + } + } + return true; } /* package */void updateSystemProperties(IProxyData proxyData) { @@ -507,8 +515,8 @@ public class ProxyType implements INodeChangeListener, IPreferenceChangeListener public void initialize() { updateSystemProperties(getProxyData(VERIFY_EMPTY)); - ((IEclipsePreferences)getParentPreferences()).addNodeChangeListener(this); - ((IEclipsePreferences)getPreferenceNode()).addPreferenceChangeListener(this); + preferenceManager.addNodeChangeListener(PREF_PROXY_DATA_NODE, this); + preferenceManager.addPreferenceChangeListener(getPreferenceNode(), this); } private ISecurePreferences getNode() { |