View | Details | Raw Unified | Return to bug 242057 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/internal/net/Activator.java (-4 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 22-28 Link Here
22
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.IStatus;
24
import org.eclipse.core.runtime.Status;
24
import org.eclipse.core.runtime.Status;
25
import org.eclipse.core.runtime.preferences.ConfigurationScope;
26
import org.eclipse.osgi.framework.log.FrameworkLog;
25
import org.eclipse.osgi.framework.log.FrameworkLog;
27
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
26
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
28
import org.eclipse.osgi.service.datalocation.Location;
27
import org.eclipse.osgi.service.datalocation.Location;
Lines 61-66 Link Here
61
	
60
	
62
	private boolean debug = false;
61
	private boolean debug = false;
63
62
63
	private PreferenceManager preferenceManger;
64
64
	/**
65
	/**
65
	 * Constructor for use by the Eclipse platform only.
66
	 * Constructor for use by the Eclipse platform only.
66
	 */
67
	 */
Lines 140-147 Link Here
140
				status.getCode(), status.getMessage(), stackCode, t, children);
141
				status.getCode(), status.getMessage(), stackCode, t, children);
141
	}
142
	}
142
143
143
	public org.osgi.service.prefs.Preferences getPreferences() {
144
	public PreferenceManager getPreferenceManager() {
144
		return new ConfigurationScope().getNode(ID);
145
		return preferenceManger;
145
	}
146
	}
146
147
147
	public boolean instanceLocationAvailable() {
148
	public boolean instanceLocationAvailable() {
Lines 152-157 Link Here
152
153
153
	public void start(BundleContext context) throws Exception {
154
	public void start(BundleContext context) throws Exception {
154
		this.bundleContext = context;
155
		this.bundleContext = context;
156
		this.preferenceManger = PreferenceManager.createConfigurationManager(ID);
155
		Filter filter = null;
157
		Filter filter = null;
156
		try {
158
		try {
157
			filter = context.createFilter(Location.INSTANCE_FILTER);
159
			filter = context.createFilter(Location.INSTANCE_FILTER);
(-)src/org/eclipse/core/internal/net/PreferenceInitializer.java (-3 / +33 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-26 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net;
11
package org.eclipse.core.internal.net;
12
12
13
import org.eclipse.core.net.proxy.IProxyData;
13
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
14
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
15
import org.eclipse.core.runtime.preferences.DefaultScope;
16
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
17
import org.osgi.service.prefs.BackingStoreException;
18
import org.osgi.service.prefs.Preferences;
14
19
15
public class PreferenceInitializer extends AbstractPreferenceInitializer {
20
public class PreferenceInitializer extends AbstractPreferenceInitializer {
16
	
21
	
22
	private static final int DEFAULT_PREF_PROXY_PORT = -1;
23
	private static final boolean DEFAULT_PREF_PROXY_HAS_AUTH = false;
24
	private static final boolean DEFAULT_PREF_ENABLED = true;
25
	private static final boolean DEFAULT_PREF_OS = true;
26
	private static final String DEFAULT_PREF_NON_PROXIED_HOSTS = "localhost|127.0.0.1"; //$NON-NLS-1$
17
	public PreferenceInitializer() {
27
	public PreferenceInitializer() {
18
		super();
28
		super();
19
	}
29
	}
20
30
21
	public void initializeDefaultPreferences() {
31
	public void initializeDefaultPreferences() {
22
		// TODO: We should set defaults in the default scope
32
		IEclipsePreferences node = new DefaultScope().getNode(Activator.ID);
23
		//((ProxyManager)ProxyManager.getProxyManager()).initialize();
33
		node.put(ProxyManager.PREF_NON_PROXIED_HOSTS, DEFAULT_PREF_NON_PROXIED_HOSTS);
34
		node.putBoolean(ProxyManager.PREF_ENABLED, DEFAULT_PREF_ENABLED);
35
		node.putBoolean(ProxyManager.PREF_OS, DEFAULT_PREF_OS);
36
		
37
		Preferences type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.HTTP_PROXY_TYPE);
38
		type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT);
39
		type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH);
40
		
41
		type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.HTTPS_PROXY_TYPE);
42
		type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT); 
43
		type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH);
44
		
45
		type = node.node(ProxyType.PREF_PROXY_DATA_NODE).node(IProxyData.SOCKS_PROXY_TYPE);
46
		type.putInt(ProxyType.PREF_PROXY_PORT, DEFAULT_PREF_PROXY_PORT); 
47
		type.putBoolean(ProxyType.PREF_PROXY_HAS_AUTH, DEFAULT_PREF_PROXY_HAS_AUTH);
48
	
49
		try {
50
			node.flush();
51
		} catch (BackingStoreException e) {
52
			Activator.logInfo("Could not store default preferences", e); //$NON-NLS-1$
53
		}
24
	}
54
	}
25
55
26
}
56
}
(-)src/org/eclipse/core/internal/net/PreferenceManager.java (+383 lines)
Added Link Here
1
/*******************************************************************************
2
* Copyright (c) 2010 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
7
*
8
* Contributors:
9
*     IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.core.internal.net;
12
13
import org.eclipse.core.net.proxy.IProxyData;
14
import org.eclipse.core.runtime.preferences.ConfigurationScope;
15
import org.eclipse.core.runtime.preferences.DefaultScope;
16
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
17
import org.eclipse.core.runtime.preferences.InstanceScope;
18
import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener;
19
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
20
import org.osgi.service.prefs.BackingStoreException;
21
import org.osgi.service.prefs.Preferences;
22
23
24
/**
25
 * Provides set of methods to operate on preferences
26
 */
27
public class PreferenceManager {
28
	
29
	public static final String ROOT = ""; //$NON-NLS-1$
30
	
31
	private static final String PREF_HAS_MIGRATED = "org.eclipse.core.net.hasMigrated"; //$NON-NLS-1$
32
33
	/**
34
	 * Preference constants used by Update to record the HTTP proxy
35
	 */
36
	private static String HTTP_PROXY_HOST = "org.eclipse.update.core.proxy.host"; //$NON-NLS-1$
37
	private static String HTTP_PROXY_PORT = "org.eclipse.update.core.proxy.port"; //$NON-NLS-1$
38
	private static String HTTP_PROXY_ENABLE = "org.eclipse.update.core.proxy.enable"; //$NON-NLS-1$
39
	
40
	private static final int DEFAULT_INT = -1;
41
	private static final String DEFAULT_STRING = null;
42
	private static final boolean DEFAULT_BOOLEAN = false;
43
44
	private static boolean migrated = false;
45
46
	private IEclipsePreferences defaultScope;
47
	private IEclipsePreferences instanceScope;
48
	private IEclipsePreferences currentScope;
49
50
	private PreferenceManager(String id) {
51
		this.defaultScope = new DefaultScope().getNode(id);
52
		this.instanceScope = new InstanceScope().getNode(id);
53
	}
54
	
55
	/**
56
	 * Creates the preferences manager for the node defined by id
57
	 * in configuration scope.
58
	 * @param id node name for which node should be created in configuration scope 
59
	 * @return {@link PreferenceManager}
60
	 */
61
	public static PreferenceManager createConfigurationManager(String id) {
62
		PreferenceManager manager = new PreferenceManager(id);
63
		manager.currentScope = new ConfigurationScope().getNode(id);
64
		return manager;
65
	}
66
	
67
	/**
68
	 * Checks if preference migration was already performed.
69
	 * @return <code>boolean</code>
70
	 */
71
	public boolean isMigrated() {
72
		return migrated;
73
	}
74
	
75
	/**
76
	 * Returns the <code>boolean</code> value associated with the specified <code>key</code> 
77
	 * for specified <code>node</code> in current scope. 
78
	 * 
79
	 * <p>
80
	 * Returns the value specified in the default scope if there is no value associated with the
81
	 * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated
82
	 * value is something that can not be parsed as an integer value.
83
	 * Use {@link #putBoolean(String, String, boolean)} to set the value of this preference key.
84
	 * </p>
85
	 * @param node node
86
	 * @param key key whose associated value is to be returned as an <code>boolean</code>.
87
	 * @return the <code>boolean</code> value associated with <code>key</code>, or
88
	 *         <code>false</code> if the associated value does not exist in either scope or cannot
89
	 *         be interpreted as an <code>boolean</code>.
90
	 * @see #putBoolean(String, String, boolean)
91
	 */
92
	public boolean getBoolean(String node, String key) {
93
		return currentScope.node(node).getBoolean(key, defaultScope.node(node).getBoolean(key, DEFAULT_BOOLEAN));
94
	}
95
96
	/**
97
	 * Returns the <code>int</code> value associated with the specified <code>key</code> 
98
	 * for specified <code>node</code> in current scope. 
99
	 * 
100
	 * <p>
101
	 * Returns the value specified in the default scope if there is no value associated with the
102
	 * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated
103
	 * value is something that can not be parsed as an integer value.
104
	 * Use {@link #putInt(String, String, int)} to set the value of this preference key.
105
	 * </p>
106
	 * @param node node
107
	 * @param key key whose associated value is to be returned as an <code>int</code>.
108
	 * @return the <code>int</code> value associated with <code>key</code>, or
109
	 *         <code>-1</code> if the associated value does not exist in either scope or cannot
110
	 *         be interpreted as an <code>int</code>.
111
	 * @see #putInt(String, String, int)
112
	 */
113
	public int getInt(String node, String key) {
114
		return currentScope.node(node).getInt(key, defaultScope.node(node).getInt(key, DEFAULT_INT));
115
	}
116
117
	/**
118
	 * Returns the <code>String</code> value associated with the specified <code>key</code> 
119
	 * for specified <code>node</code> in current scope. 
120
	 * 
121
	 * <p>
122
	 * Returns the value specified in the default scope if there is no value associated with the
123
	 * <code>key</code> in the current scope, the backing store is inaccessible, or if the associated
124
	 * value is something that can not be parsed as an integer value.
125
	 * Use {@link #putString(String, String, String)} to set the value of this preference key.
126
	 * </p>
127
	 * @param node node
128
	 * @param key key whose associated value is to be returned as an <code>String</code>.
129
	 * @return the <code>String</code> value associated with <code>key</code>, or
130
	 *         <code>null</code> if the associated value does not exist in either scope or cannot
131
	 *         be interpreted as an <code>String</code>.
132
	 * @see #putString(String, String, String)
133
	 */
134
	public String getString(String node, String key) {
135
		return currentScope.node(node).get(key, defaultScope.node(node).get(key, DEFAULT_STRING));
136
	}
137
	
138
	/**
139
	 * Associates the specified <code>int</code> value with the specified key 
140
	 * for specified <code>node</code> in current scope.
141
	 * 
142
	 * @param node node 
143
	 * @param key <code>key</code> with which the string form of value is to be associated.
144
	 * @param value <code>value</code> to be associated with <code>key</code>.
145
	 * @see #getInt(String, String)
146
	 */
147
	public void putInt(String node, String key, int value) {
148
		currentScope.node(node).putInt(key, value);
149
	}
150
151
	/**
152
	 * Associates the specified <code>boolean</code> value with the specified key
153
	 * for specified <code>node</code> in current scope.
154
	 * 
155
	 * @param node node 
156
	 * @param key <code>key</code> with which the string form of value is to be associated.
157
	 * @param value <code>value</code> to be associated with <code>key</code>.
158
	 * @see #getBoolean(String, String)
159
	 */
160
	public void putBoolean(String node, String key, boolean value) {
161
		currentScope.node(node).putBoolean(key, value);
162
	}
163
164
	/**
165
	 * Associates the specified <code>String</code> value with the specified key
166
	 * for specified <code>node</code> in current scope.
167
	 * 
168
	 * @param node node 
169
	 * @param key <code>key</code> with which the string form of value is to be associated.
170
	 * @param value <code>value</code> to be associated with <code>key</code>.
171
	 * @see #getString(String, String)
172
	 */
173
	public void putString(String node, String key, String value) {
174
		currentScope.node(node).put(key, value);
175
	}
176
	
177
	/**
178
	 * Register the given listener for notification of preference changes.
179
	 * Calling this method multiple times with the same listener has no effect. The
180
	 * given listener argument must not be <code>null</code>.
181
	 * 
182
	 * @param node node
183
	 * @param listener the preference change listener to register
184
	 * @see #removePreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener)
185
	 * @see IEclipsePreferences.IPreferenceChangeListener
186
	 */
187
	public void addPreferenceChangeListener(String node, IPreferenceChangeListener listener) {
188
		((IEclipsePreferences)currentScope.node(node)).addPreferenceChangeListener(listener);
189
	}
190
191
	/**
192
	 * De-register the given listener from receiving notification of preference changes
193
	 * Calling this method multiple times with the same listener has no
194
	 * effect. The given listener argument must not be <code>null</code>.
195
	 * @param node node
196
	 * @param listener the preference change listener to remove
197
	 * @see #addPreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener)
198
	 * @see IEclipsePreferences.IPreferenceChangeListener
199
	 */
200
	public void removePreferenceChangeListener(String node, IPreferenceChangeListener listener) {
201
		((IEclipsePreferences)currentScope.node(node)).removePreferenceChangeListener(listener);
202
	}
203
	
204
	/**
205
	 * Register the given listener for changes to this node.
206
	 * Calling this method multiple times with the same listener has no effect. The
207
	 * given listener argument must not be <code>null</code>.
208
	 * 
209
	 * @param node node
210
	 * @param listener the preference change listener to register
211
	 * @see #removeNodeChangeListener(String, IEclipsePreferences.INodeChangeListener)
212
	 * @see IEclipsePreferences.IPreferenceChangeListener
213
	 */
214
	public void addNodeChangeListener(String node, INodeChangeListener listener) {
215
		((IEclipsePreferences)currentScope.node(node)).addNodeChangeListener(listener);
216
	}
217
218
	/**
219
	 * De-register the given listener from receiving event change notifications for this node.
220
	 * Calling this method multiple times with the same listener has no
221
	 * effect. The given listener argument must not be <code>null</code>.
222
	 * @param node node
223
	 * @param listener the preference change listener to remove
224
	 * @see #addPreferenceChangeListener(String, IEclipsePreferences.IPreferenceChangeListener)
225
	 * @see IEclipsePreferences.IPreferenceChangeListener
226
	 */
227
	public void removeNodeChangeListener(String node, INodeChangeListener listener) {
228
		((IEclipsePreferences)currentScope.node(node)).removeNodeChangeListener(listener);
229
	}
230
	
231
	/**
232
	 * Removes this node and all of its descendants, 
233
	 * invalidating any properties contained in the removed nodes. 
234
	 * @param node name of a node which should be removed
235
	 * @throws BackingStoreException - if this operation cannot be completed 
236
	 * due to a failure in the backing store, or inability to communicate with it.
237
	 */
238
	public void removeNode(String node) throws BackingStoreException {
239
		currentScope.node(node).removeNode();
240
	}
241
242
	/**
243
	 * Forces any changes in the contents of current scope
244
	 * and its descendants to the persistent store. 
245
	 * @throws BackingStoreException - if this operation cannot be completed
246
	 * due to a failure in the backing store, or inability to communicate with it.
247
	 */
248
	public void flush() throws BackingStoreException {
249
		currentScope.flush();
250
	}
251
252
	/**
253
	 * Migrate preferences from instance scope to current scope.
254
	 * @param proxies proxy types for which migration should be performed {@link ProxyType}
255
	 */
256
	public void migrate(ProxyType[] proxies) {
257
		migrated = true;
258
		if (currentScope.getBoolean(PREF_HAS_MIGRATED, false)
259
				|| currentScope.name().equals(InstanceScope.SCOPE)) {
260
			return;
261
		}
262
		currentScope.putBoolean(PREF_HAS_MIGRATED, true);
263
		migrateInstanceScopePreferences(proxies, true);
264
	}
265
266
	void migrateInstanceScopePreferences(ProxyType[] proxies, boolean isInitialize) {
267
		migrateUpdateHttpProxy(proxies, isInitialize);
268
269
		// migrate enabled status
270
		if (currentScope.get(ProxyManager.PREF_ENABLED, null) == null) {
271
			String instanceEnabled = instanceScope.get(ProxyManager.PREF_ENABLED, null);
272
			if (instanceEnabled != null)
273
				currentScope.put(ProxyManager.PREF_ENABLED, instanceEnabled);
274
		}
275
		
276
		// migrate enabled status
277
		if (currentScope.get(ProxyManager.PREF_OS, null) == null) {
278
			String instanceEnabled = instanceScope.get(ProxyManager.PREF_OS, null);
279
			if (instanceEnabled != null)
280
				currentScope.put(ProxyManager.PREF_OS, instanceEnabled);
281
		}
282
	
283
		// migrate non proxied hosts if not already set
284
		if (currentScope.get(ProxyManager.PREF_NON_PROXIED_HOSTS, null) == null) {
285
			String instanceNonProxiedHosts = instanceScope.get(ProxyManager.PREF_NON_PROXIED_HOSTS, null);
286
			if (instanceNonProxiedHosts != null) {
287
				currentScope.put(ProxyManager.PREF_NON_PROXIED_HOSTS, instanceNonProxiedHosts);
288
			}
289
		}
290
		
291
		// migrate proxy data
292
		PreferenceManager instanceManager = PreferenceManager.createInstanceManager(Activator.ID);
293
		for (int i = 0; i < proxies.length; i++) {
294
			ProxyType type = proxies[i];
295
			IProxyData data = type.getProxyData(ProxyType.DO_NOT_VERIFY);
296
			if (data.getHost() == null) {
297
				ProxyType instanceType = new ProxyType(type.getName(), instanceManager);
298
				IProxyData instanceData = instanceType.getProxyData(ProxyType.DO_NOT_VERIFY);
299
				if (instanceData.getHost() != null)
300
					type.setProxyData(instanceData);
301
			}
302
		}
303
		
304
		// if this an import we should remove the old node
305
		if (!isInitialize) {
306
			try {
307
				instanceScope.removeNode();
308
			} catch (BackingStoreException e) {
309
				// ignore
310
			}
311
		}			
312
	}
313
	
314
	private void migrateUpdateHttpProxy(ProxyType[] proxies, boolean isInitialize) {
315
		if (!instanceScope.getBoolean(PREF_HAS_MIGRATED, false)) {
316
			// Only set the migration bit when initializing
317
			if (isInitialize)
318
				instanceScope.putBoolean(PREF_HAS_MIGRATED, true);
319
			Preferences updatePrefs = instanceScope.parent().node("org.eclipse.update.core"); //$NON-NLS-1$
320
			String httpProxyHost = getHostToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
321
			int port = getPortToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
322
			boolean httpProxyEnable = getEnablementToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
323
			if (httpProxyHost != null) {
324
				ProxyData proxyData = new ProxyData(IProxyData.HTTP_PROXY_TYPE,
325
						httpProxyHost, port, false, null);
326
				for (int i = 0; i < proxies.length; i++) {
327
					ProxyType type = proxies[i];
328
					if (type.getName().equals(proxyData.getType())) {
329
						type.updatePreferencesIfMissing(proxyData);
330
					}
331
				}
332
				if (httpProxyEnable) {
333
					instanceScope.putBoolean(ProxyManager.PREF_ENABLED, true);
334
				}
335
			}
336
		}
337
	}
338
	
339
	private String getHostToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
340
		String httpProxyHost = updatePrefs.get(HTTP_PROXY_HOST, ""); //$NON-NLS-1$
341
		if (checkSystemProperties && "".equals(httpProxyHost)) { //$NON-NLS-1$
342
			httpProxyHost = System.getProperty("http.proxyHost", ""); //$NON-NLS-1$ //$NON-NLS-2$
343
		}
344
		if ("".equals(httpProxyHost)) //$NON-NLS-1$
345
			httpProxyHost = null;
346
		updatePrefs.remove(HTTP_PROXY_HOST);
347
		return httpProxyHost;
348
	}
349
	
350
	private int getPortToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
351
		String httpProxyPort = updatePrefs.get(HTTP_PROXY_PORT, ""); //$NON-NLS-1$
352
		if (checkSystemProperties && "".equals(httpProxyPort)) { //$NON-NLS-1$
353
			httpProxyPort = System.getProperty("http.proxyPort", ""); //$NON-NLS-1$ //$NON-NLS-2$
354
		}
355
		updatePrefs.remove(HTTP_PROXY_PORT);
356
		int port = -1;
357
		if (httpProxyPort != null && !"".equals(httpProxyPort)) //$NON-NLS-1$
358
			try {
359
				port = Integer.parseInt(httpProxyPort);
360
			} catch (NumberFormatException e) {
361
				// Ignore
362
			}
363
		return port;
364
	}
365
	
366
	private boolean getEnablementToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
367
		boolean httpProxyEnable = false;
368
		if (checkSystemProperties && updatePrefs.get(HTTP_PROXY_ENABLE, null) == null) {
369
			httpProxyEnable = Boolean.getBoolean("http.proxySet"); //$NON-NLS-1$
370
		} else {
371
			httpProxyEnable = updatePrefs.getBoolean(HTTP_PROXY_ENABLE, false);
372
			updatePrefs.remove(HTTP_PROXY_ENABLE);
373
		}
374
		return httpProxyEnable;
375
	}
376
	
377
	private static PreferenceManager createInstanceManager(String id) {
378
		PreferenceManager manager = new PreferenceManager(id);
379
		manager.currentScope = manager.instanceScope;
380
		return manager;
381
	}
382
	
383
}
(-)src/org/eclipse/core/internal/net/PreferenceModifyListener.java (-3 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 IBM Corporation and others.
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-16 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net;
11
package org.eclipse.core.internal.net;
12
12
13
import org.eclipse.core.runtime.preferences.ConfigurationScope;
14
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
13
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
15
import org.eclipse.core.runtime.preferences.InstanceScope;
14
import org.eclipse.core.runtime.preferences.InstanceScope;
16
import org.osgi.service.prefs.BackingStoreException;
15
import org.osgi.service.prefs.BackingStoreException;
Lines 25-31 Link Here
25
	public IEclipsePreferences preApply(IEclipsePreferences node) {
24
	public IEclipsePreferences preApply(IEclipsePreferences node) {
26
		try {
25
		try {
27
			if (node.nodeExists(InstanceScope.SCOPE)) {
26
			if (node.nodeExists(InstanceScope.SCOPE)) {
28
				((ProxyManager)ProxyManager.getProxyManager()).migrateInstanceScopePreferences(node.node(InstanceScope.SCOPE), node.node(ConfigurationScope.SCOPE), false);
27
				((ProxyManager)ProxyManager.getProxyManager()).migrateInstanceScopePreferences(false);
29
			}
28
			}
30
		} catch (BackingStoreException e) {
29
		} catch (BackingStoreException e) {
31
			Activator.logError("Could not access instance preferences", e); //$NON-NLS-1$
30
			Activator.logError("Could not access instance preferences", e); //$NON-NLS-1$
(-)src/org/eclipse/core/internal/net/ProxyManager.java (-148 / +25 lines)
Lines 14-19 Link Here
14
import java.net.URI;
14
import java.net.URI;
15
import java.net.URISyntaxException;
15
import java.net.URISyntaxException;
16
import java.util.ArrayList;
16
import java.util.ArrayList;
17
import java.util.Arrays;
17
import java.util.List;
18
import java.util.List;
18
import java.util.Properties;
19
import java.util.Properties;
19
20
Lines 30-63 Link Here
30
import org.eclipse.core.runtime.ListenerList;
31
import org.eclipse.core.runtime.ListenerList;
31
import org.eclipse.core.runtime.RegistryFactory;
32
import org.eclipse.core.runtime.RegistryFactory;
32
import org.eclipse.core.runtime.SafeRunner;
33
import org.eclipse.core.runtime.SafeRunner;
33
import org.eclipse.core.runtime.preferences.ConfigurationScope;
34
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
35
import org.eclipse.core.runtime.preferences.InstanceScope;
36
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
34
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
37
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
35
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
38
import org.eclipse.osgi.util.NLS;
36
import org.eclipse.osgi.util.NLS;
39
import org.osgi.service.prefs.BackingStoreException;
37
import org.osgi.service.prefs.BackingStoreException;
40
import org.osgi.service.prefs.Preferences;
41
38
42
public class ProxyManager implements IProxyService, IPreferenceChangeListener {
39
public class ProxyManager implements IProxyService, IPreferenceChangeListener {
43
44
	private static final String PREF_HAS_MIGRATED = "org.eclipse.core.net.hasMigrated"; //$NON-NLS-1$
45
	
40
	
46
	/**
41
	static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$
47
	 * Preference constants used by Update to record the HTTP proxy
42
	static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$
48
	 */
43
	static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$
49
	private static String HTTP_PROXY_HOST = "org.eclipse.update.core.proxy.host"; //$NON-NLS-1$
50
	private static String HTTP_PROXY_PORT = "org.eclipse.update.core.proxy.port"; //$NON-NLS-1$
51
	private static String HTTP_PROXY_ENABLE = "org.eclipse.update.core.proxy.enable"; //$NON-NLS-1$
52
	
53
	private static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$
54
	private static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$
55
	private static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$
56
	
44
	
57
	private static IProxyService proxyManager;
45
	private static IProxyService proxyManager;
58
	
46
	
59
	private AbstractProxyProvider nativeProxyProvider;
47
	private AbstractProxyProvider nativeProxyProvider;
60
	
48
	
49
	private PreferenceManager preferenceManager;
50
	
61
	ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
51
	ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
62
	private String[] nonProxiedHosts;
52
	private String[] nonProxiedHosts;
63
	private final ProxyType[] proxies = new ProxyType[] {
53
	private final ProxyType[] proxies = new ProxyType[] {
Lines 66-73 Link Here
66
			new ProxyType(IProxyData.SOCKS_PROXY_TYPE)
56
			new ProxyType(IProxyData.SOCKS_PROXY_TYPE)
67
		};
57
		};
68
58
69
	private boolean migrated = false;
70
71
	private ProxyManager() {
59
	private ProxyManager() {
72
		try {
60
		try {
73
			nativeProxyProvider = (AbstractProxyProvider) Class.forName(
61
			nativeProxyProvider = (AbstractProxyProvider) Class.forName(
Lines 77-82 Link Here
77
		} catch (Exception e) {
65
		} catch (Exception e) {
78
			Activator.logInfo("Problems occured during the proxy provider initialization.", e); //$NON-NLS-1$
66
			Activator.logInfo("Problems occured during the proxy provider initialization.", e); //$NON-NLS-1$
79
		}
67
		}
68
		preferenceManager = Activator.getInstance().getPreferenceManager();
80
	}
69
	}
81
70
82
	/**
71
	/**
Lines 124-130 Link Here
124
	public synchronized String[] getNonProxiedHosts() {
113
	public synchronized String[] getNonProxiedHosts() {
125
		checkMigrated();
114
		checkMigrated();
126
		if (nonProxiedHosts == null) {
115
		if (nonProxiedHosts == null) {
127
			String prop = Activator.getInstance().getPreferences().get(PREF_NON_PROXIED_HOSTS, "localhost|127.0.0.1"); //$NON-NLS-1$
116
			String prop = preferenceManager.getString(PreferenceManager.ROOT, PREF_NON_PROXIED_HOSTS);
128
			nonProxiedHosts = ProxyType.convertPropertyStringToHosts(prop);
117
			nonProxiedHosts = ProxyType.convertPropertyStringToHosts(prop);
129
		}
118
		}
130
		if (nonProxiedHosts.length == 0)
119
		if (nonProxiedHosts.length == 0)
Lines 153-162 Link Here
153
			Assert.isTrue(host.length() > 0);
142
			Assert.isTrue(host.length() > 0);
154
		}
143
		}
155
		String[] oldHosts = nonProxiedHosts;
144
		String[] oldHosts = nonProxiedHosts;
145
		if (Arrays.equals(oldHosts, hosts)) {
146
			return;
147
		}
156
		nonProxiedHosts = hosts;
148
		nonProxiedHosts = hosts;
157
		Activator.getInstance().getPreferences().put(PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts));
149
		preferenceManager.putString(PreferenceManager.ROOT, PREF_NON_PROXIED_HOSTS, ProxyType.convertHostsToPropertyString(nonProxiedHosts));
158
		try {
150
		try {
159
			Activator.getInstance().getPreferences().flush();
151
			preferenceManager.flush();
160
		} catch (BackingStoreException e) {
152
		} catch (BackingStoreException e) {
161
			Activator.logError(
153
			Activator.logError(
162
					"An error occurred while writing out the non-proxied hosts list", e); //$NON-NLS-1$
154
					"An error occurred while writing out the non-proxied hosts list", e); //$NON-NLS-1$
Lines 232-239 Link Here
232
	}
224
	}
233
225
234
	private boolean internalIsProxiesEnabled() {
226
	private boolean internalIsProxiesEnabled() {
235
		return Activator.getInstance().getPreferences().getBoolean(
227
		return preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED);
236
				PREF_ENABLED, true);
237
	}
228
	}
238
229
239
	/* (non-Javadoc)
230
	/* (non-Javadoc)
Lines 246-252 Link Here
246
			return;
237
			return;
247
		// Setting the preference will trigger the system property update
238
		// Setting the preference will trigger the system property update
248
		// (see preferenceChange)
239
		// (see preferenceChange)
249
		Activator.getInstance().getPreferences().putBoolean(PREF_ENABLED, enabled);
240
		preferenceManager.putBoolean(PreferenceManager.ROOT, PREF_ENABLED, enabled);
250
	}
241
	}
251
242
252
	private void internalSetEnabled(boolean enabled, boolean systemEnabled) {
243
	private void internalSetEnabled(boolean enabled, boolean systemEnabled) {
Lines 255-261 Link Here
255
		sysProps.put("systemProxySet", systemEnabled ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
246
		sysProps.put("systemProxySet", systemEnabled ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
256
		updateSystemProperties();
247
		updateSystemProperties();
257
		try {
248
		try {
258
			Activator.getInstance().getPreferences().flush();
249
			preferenceManager.flush();
259
		} catch (BackingStoreException e) {
250
		} catch (BackingStoreException e) {
260
			Activator.logError(
251
			Activator.logError(
261
					"An error occurred while writing out the enablement state", e); //$NON-NLS-1$
252
					"An error occurred while writing out the enablement state", e); //$NON-NLS-1$
Lines 275-281 Link Here
275
266
276
	public void initialize() {
267
	public void initialize() {
277
		checkMigrated();
268
		checkMigrated();
278
		((IEclipsePreferences)Activator.getInstance().getPreferences()).addPreferenceChangeListener(this);
269
		preferenceManager.addPreferenceChangeListener(PreferenceManager.ROOT, this);
279
		// Now initialize each proxy type
270
		// Now initialize each proxy type
280
		for (int i = 0; i < proxies.length; i++) {
271
		for (int i = 0; i < proxies.length; i++) {
281
			ProxyType type = proxies[i];
272
			ProxyType type = proxies[i];
Lines 404-539 Link Here
404
			return null;
395
			return null;
405
		}
396
		}
406
	}
397
	}
407
408
	
398
	
409
	private synchronized void checkMigrated() {
399
	private synchronized void checkMigrated() {
410
		if (migrated || !Activator.getInstance().instanceLocationAvailable())
400
		if (preferenceManager.isMigrated() || !Activator.getInstance().instanceLocationAvailable()) {
411
			return;
412
		
413
		migrated = true;
414
		if (Activator.getInstance().getPreferences().getBoolean(PREF_HAS_MIGRATED, false))
415
			return;
401
			return;
416
		
417
		Activator.getInstance().getPreferences().putBoolean(PREF_HAS_MIGRATED, true);
418
		migrateInstanceScopePreferences(new InstanceScope().getNode(""), new ConfigurationScope().getNode(""), true);  //$NON-NLS-1$//$NON-NLS-2$
419
	}
420
	
421
	void migrateInstanceScopePreferences(Preferences instanceNode, Preferences configurationNode, boolean isInitialize) {
422
		migrateUpdateHttpProxy(instanceNode, isInitialize);
423
		
424
		Preferences netInstancePrefs = instanceNode.node(Activator.ID);
425
		Preferences netConfigurationPrefs = configurationNode.node(Activator.ID);
426
		
427
		// migrate enabled status
428
		if (netConfigurationPrefs.get(PREF_ENABLED, null) == null) {
429
			String instanceEnabled = netInstancePrefs.get(PREF_ENABLED, null);
430
			if (instanceEnabled != null)
431
				netConfigurationPrefs.put(PREF_ENABLED, instanceEnabled);
432
		}
433
		
434
		// migrate enabled status
435
		if (netConfigurationPrefs.get(PREF_OS, null) == null) {
436
			String instanceEnabled = netInstancePrefs.get(PREF_OS, null);
437
			if (instanceEnabled != null)
438
				netConfigurationPrefs.put(PREF_OS, instanceEnabled);
439
		}
440
	
441
		// migrate non proxied hosts if not already set
442
		if (netConfigurationPrefs.get(PREF_NON_PROXIED_HOSTS, null) == null) {
443
			String instanceNonProxiedHosts = netInstancePrefs.get(PREF_NON_PROXIED_HOSTS, null);
444
			if (instanceNonProxiedHosts != null) {
445
				netConfigurationPrefs.put(PREF_NON_PROXIED_HOSTS, instanceNonProxiedHosts);
446
				nonProxiedHosts = null;
447
			}
448
		}
402
		}
449
		
403
		preferenceManager.migrate(proxies);
450
		// migrate proxy data
451
		for (int i = 0; i < proxies.length; i++) {
452
			ProxyType type = proxies[i];
453
			IProxyData data = type.getProxyData(ProxyType.DO_NOT_VERIFY);
454
			if (data.getHost() == null) {
455
				ProxyType instanceType = new ProxyType(type.getName(),netInstancePrefs);
456
				IProxyData instanceData = instanceType.getProxyData(ProxyType.DO_NOT_VERIFY);
457
				if (instanceData.getHost() != null)
458
					type.setProxyData(instanceData);
459
			}
460
		}
461
		
462
		// if this an import we should remove the old node
463
		if (! isInitialize) {
464
			try {
465
				netInstancePrefs.removeNode();
466
			} catch (BackingStoreException e) {
467
				// ignore
468
			}
469
		}			
470
	}
404
	}
471
	
405
	
472
	private void migrateUpdateHttpProxy(Preferences node, boolean isInitialize) {
406
	void migrateInstanceScopePreferences(boolean isInitialize) {
473
		Preferences netPrefs = node.node(Activator.ID);
407
		preferenceManager.migrateInstanceScopePreferences(proxies,  isInitialize);
474
		if (!netPrefs.getBoolean(PREF_HAS_MIGRATED, false)) {
475
			// Only set the migration bit when initializing
476
			if (isInitialize)
477
				netPrefs.putBoolean(PREF_HAS_MIGRATED, true);
478
			Preferences updatePrefs = node.node("org.eclipse.update.core"); //$NON-NLS-1$
479
			String httpProxyHost = getHostToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
480
			int port = getPortToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
481
			boolean httpProxyEnable = getEnablementToMigrate(updatePrefs, isInitialize /* checkSystemProperties */);
482
			if (httpProxyHost != null) {
483
				ProxyData proxyData = new ProxyData(IProxyData.HTTP_PROXY_TYPE,
484
						httpProxyHost, port, false, null);
485
				ProxyType type = getType(proxyData);
486
				type.updatePreferencesIfMissing(netPrefs, proxyData);
487
				if (httpProxyEnable) {
488
					netPrefs.putBoolean(ProxyManager.PREF_ENABLED, true);
489
				}
490
			}
491
		}
492
	}
493
494
	private boolean getEnablementToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
495
		boolean httpProxyEnable = false;
496
		if (checkSystemProperties && updatePrefs.get(HTTP_PROXY_ENABLE, null) == null) {
497
			httpProxyEnable = Boolean.getBoolean("http.proxySet"); //$NON-NLS-1$
498
		} else {
499
			httpProxyEnable = updatePrefs.getBoolean(HTTP_PROXY_ENABLE, false);
500
			updatePrefs.remove(HTTP_PROXY_ENABLE);
501
		}
502
		return httpProxyEnable;
503
	}
504
505
	private int getPortToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
506
		String httpProxyPort = updatePrefs.get(HTTP_PROXY_PORT, ""); //$NON-NLS-1$
507
		if (checkSystemProperties && "".equals(httpProxyPort)) { //$NON-NLS-1$
508
			httpProxyPort = System.getProperty("http.proxyPort", ""); //$NON-NLS-1$ //$NON-NLS-2$
509
		}
510
		updatePrefs.remove(HTTP_PROXY_PORT);
511
		int port = -1;
512
		if (httpProxyPort != null && !"".equals(httpProxyPort)) //$NON-NLS-1$
513
			try {
514
				port = Integer.parseInt(httpProxyPort);
515
			} catch (NumberFormatException e) {
516
				// Ignore
517
			}
518
		return port;
519
	}
520
521
	private String getHostToMigrate(Preferences updatePrefs, boolean checkSystemProperties) {
522
		String httpProxyHost = updatePrefs.get(HTTP_PROXY_HOST, ""); //$NON-NLS-1$
523
		if (checkSystemProperties && "".equals(httpProxyHost)) { //$NON-NLS-1$
524
			httpProxyHost = System.getProperty("http.proxyHost", ""); //$NON-NLS-1$ //$NON-NLS-2$
525
		}
526
		if ("".equals(httpProxyHost)) //$NON-NLS-1$
527
			httpProxyHost = null;
528
		updatePrefs.remove(HTTP_PROXY_HOST);
529
		return httpProxyHost;
530
	}
408
	}
531
409
532
	public void preferenceChange(PreferenceChangeEvent event) {
410
	public void preferenceChange(PreferenceChangeEvent event) {
533
		if (event.getKey().equals(PREF_ENABLED) || event.getKey().equals(PREF_OS)) {
411
		if (event.getKey().equals(PREF_ENABLED) || event.getKey().equals(PREF_OS)) {
534
			checkMigrated();
412
			checkMigrated();
535
			internalSetEnabled(Activator.getInstance().getPreferences().getBoolean(PREF_ENABLED, true),
413
			internalSetEnabled(preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED),
536
					Activator.getInstance().getPreferences().getBoolean(PREF_OS, true));
414
					preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS));
537
		}
415
		}
538
	}
416
	}
539
417
Lines 543-550 Link Here
543
421
544
	public boolean isSystemProxiesEnabled() {
422
	public boolean isSystemProxiesEnabled() {
545
		checkMigrated();
423
		checkMigrated();
546
		return Activator.getInstance().getPreferences().getBoolean(PREF_OS,
424
		return preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS);
547
				true);
548
	}
425
	}
549
426
550
	public void setSystemProxiesEnabled(boolean enabled) {
427
	public void setSystemProxiesEnabled(boolean enabled) {
Lines 554-560 Link Here
554
			return;
431
			return;
555
		// Setting the preference will trigger the system property update
432
		// Setting the preference will trigger the system property update
556
		// (see preferenceChange)
433
		// (see preferenceChange)
557
		Activator.getInstance().getPreferences().putBoolean(PREF_OS, enabled);
434
		preferenceManager.putBoolean(PreferenceManager.ROOT, PREF_OS, enabled);
558
	}
435
	}
559
436
560
	public IProxyData[] select(URI uri) {
437
	public IProxyData[] select(URI uri) {
(-)src/org/eclipse/core/internal/net/ProxyType.java (-45 / +53 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2009 IBM Corporation and others.
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-21 Link Here
16
16
17
import org.eclipse.core.net.proxy.IProxyData;
17
import org.eclipse.core.net.proxy.IProxyData;
18
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.core.runtime.IPath;
19
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20
import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener;
21
import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener;
21
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
22
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
Lines 26-42 Link Here
26
import org.eclipse.equinox.security.storage.StorageException;
27
import org.eclipse.equinox.security.storage.StorageException;
27
import org.eclipse.osgi.util.NLS;
28
import org.eclipse.osgi.util.NLS;
28
import org.osgi.service.prefs.BackingStoreException;
29
import org.osgi.service.prefs.BackingStoreException;
29
import org.osgi.service.prefs.Preferences;
30
30
31
public class ProxyType implements INodeChangeListener, IPreferenceChangeListener {
31
public class ProxyType implements INodeChangeListener, IPreferenceChangeListener {
32
32
33
	/**
33
	/**
34
	 * Preference keys
34
	 * Preference keys
35
	 */
35
	 */
36
	private static final String PREF_PROXY_DATA_NODE = "proxyData"; //$NON-NLS-1$
36
	static final String PREF_PROXY_DATA_NODE = "proxyData"; //$NON-NLS-1$
37
	private static final String PREF_PROXY_HOST = "host"; //$NON-NLS-1$
37
	static final String PREF_PROXY_HOST = "host"; //$NON-NLS-1$
38
	private static final String PREF_PROXY_PORT = "port"; //$NON-NLS-1$
38
	static final String PREF_PROXY_PORT = "port"; //$NON-NLS-1$
39
	private static final String PREF_PROXY_HAS_AUTH = "hasAuth"; //$NON-NLS-1$
39
	static final String PREF_PROXY_HAS_AUTH = "hasAuth"; //$NON-NLS-1$
40
40
41
	/**
41
	/**
42
	 * Verification tags used when creating a proxy data
42
	 * Verification tags used when creating a proxy data
Lines 78-84 Link Here
78
    
78
    
79
	private String name;
79
	private String name;
80
	private boolean updatingPreferences;
80
	private boolean updatingPreferences;
81
	private Preferences netPreferences;
81
	private PreferenceManager preferenceManager;
82
82
83
	public static String convertHostsToPropertyString(String[] value) {
83
	public static String convertHostsToPropertyString(String[] value) {
84
		StringBuffer buffer = new StringBuffer();
84
		StringBuffer buffer = new StringBuffer();
Lines 111-148 Link Here
111
111
112
	public ProxyType(String name) {
112
	public ProxyType(String name) {
113
		this.name = name;
113
		this.name = name;
114
		this.netPreferences = Activator.getInstance().getPreferences();
114
		this.preferenceManager = Activator.getInstance().getPreferenceManager();
115
	}	
115
	}	
116
	
116
	
117
	public ProxyType(String name, Preferences netPreferences) {
117
	public ProxyType(String name, PreferenceManager manager) {
118
		this.name = name;
118
		this.name = name;
119
		this.netPreferences = netPreferences;
119
		this.preferenceManager = manager;
120
	}
120
	}
121
121
122
	private Preferences getPreferenceNode() {
122
	private String getPreferenceNode() {
123
		return getParentPreferences().node(getName());
123
		return PREF_PROXY_DATA_NODE + IPath.SEPARATOR + getName();
124
	}
125
126
	/**
127
	 * Return the preferences node whose child nodes are the know proxy types
128
	 * 
129
	 * @return a preferences node
130
	 */
131
	private Preferences getParentPreferences() {
132
		return netPreferences.node(
133
				PREF_PROXY_DATA_NODE);
134
	}
124
	}
135
125
136
	public IProxyData getProxyData(int verifyFlag) {
126
	public IProxyData getProxyData(int verifyFlag) {
137
		return createProxyData(name, getPreferenceNode(), verifyFlag);
127
		return createProxyData(name, getPreferenceNode(), verifyFlag);
138
	}
128
	}
139
129
140
	private IProxyData createProxyData(String type, Preferences node, int verifyFlag) {
130
	private IProxyData createProxyData(String type, String node, int verifyFlag) {
141
		String host = node.get(PREF_PROXY_HOST, null);
131
		String host = preferenceManager.getString(node, PREF_PROXY_HOST);
142
		if (host != null && host.length() == 0)
132
		if (host != null && host.length() == 0)
143
			host = null;
133
			host = null;
144
		int port = node.getInt(PREF_PROXY_PORT, -1);
134
		int port = preferenceManager.getInt(node, PREF_PROXY_PORT);
145
		boolean requiresAuth = node.getBoolean(PREF_PROXY_HAS_AUTH, false);
135
		boolean requiresAuth = preferenceManager.getBoolean(node, PREF_PROXY_HAS_AUTH);
146
		ProxyData proxyData = new ProxyData(type, host, port, requiresAuth,
136
		ProxyData proxyData = new ProxyData(type, host, port, requiresAuth,
147
				null);
137
				null);
148
		loadProxyAuth(proxyData);
138
		loadProxyAuth(proxyData);
Lines 176-208 Link Here
176
		updatePreferences(getPreferenceNode(), proxyData);
166
		updatePreferences(getPreferenceNode(), proxyData);
177
	}
167
	}
178
	
168
	
179
	/* package */ void updatePreferencesIfMissing(Preferences node, IProxyData proxyData) {
169
	/*package*/  void updatePreferencesIfMissing(IProxyData proxyData) {
180
		Preferences proxyNode = node.node(PREF_PROXY_DATA_NODE).node(getName());
170
		String node = getPreferenceNode();
181
		if (node.get(PREF_PROXY_HOST, null) == null)
171
		if (preferenceManager.getString(node, PREF_PROXY_HOST) == null)
182
			updatePreferences(proxyNode, proxyData);
172
			updatePreferences(node, proxyData);
183
	}
173
	}
184
	
174
	
185
	private void updatePreferences(Preferences node, IProxyData proxyData) {
175
	private void updatePreferences(String node, IProxyData proxyData) {
176
		if (!hasPreferencesChanged(node, proxyData)) {
177
			return;
178
		}
186
		if (proxyData.getHost() == null) {
179
		if (proxyData.getHost() == null) {
187
			try {
180
			try {
188
				Preferences parent = node.parent();
181
				preferenceManager.removeNode(node);
189
				node.removeNode();
182
				preferenceManager.flush();
190
				parent.flush();
191
			} catch (BackingStoreException e) {
183
			} catch (BackingStoreException e) {
192
				Activator.logError(NLS.bind(
184
				Activator.logError(NLS.bind(
193
						"An error occurred removing the {0} proxy node from the preference store", proxyData.getType()), e); //$NON-NLS-1$
185
						"An error occurred removing the {0} proxy node from the preference store", proxyData.getType()), e); //$NON-NLS-1$
194
			}
186
			}
195
		} else {
187
			// Check if there is a value in default scope (e.g. set by -pluginCustomization).
196
			node.put(PREF_PROXY_HOST, proxyData.getHost());
188
			// If it is, update preferences even if host is empty.
197
			node.putInt(PREF_PROXY_PORT, proxyData.getPort());
189
			if (!hasPreferencesChanged(node, proxyData)) {
198
			node.putBoolean(PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null);
190
				return;
199
			try {
200
				node.flush();
201
			} catch (BackingStoreException e) {
202
				Activator.logError(NLS.bind(
203
					"The {0} proxy node could not be written", proxyData.getType()), e); //$NON-NLS-1$
204
			}
191
			}
205
		}
192
		}
193
		preferenceManager.putString(node, PREF_PROXY_HOST, proxyData.getHost() != null ? proxyData.getHost() : ""); //$NON-NLS-1$
194
		preferenceManager.putInt(node, PREF_PROXY_PORT, proxyData.getPort());
195
		preferenceManager.putBoolean(node, PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null);
196
		try {
197
			preferenceManager.flush();
198
		} catch (BackingStoreException e) {
199
			Activator.logError(NLS.bind(
200
				"The {0} proxy node could not be written", proxyData.getType()), e); //$NON-NLS-1$
201
		}
202
	}
203
204
	private boolean hasPreferencesChanged(String node, IProxyData proxyData) {
205
		String host = preferenceManager.getString(node, PREF_PROXY_HOST);
206
		if ((host != null && host.equals(proxyData.getHost())) || (host == null && proxyData.getHost() == null)) {
207
			if (preferenceManager.getInt(node, PREF_PROXY_PORT) == proxyData.getPort()) {
208
				if (preferenceManager.getBoolean(node, PREF_PROXY_HAS_AUTH) == proxyData.isRequiresAuthentication()) {
209
					return false;
210
				}
211
			}
212
		}
213
		return true;
206
	}
214
	}
207
	
215
	
208
	/* package */void updateSystemProperties(IProxyData proxyData) {
216
	/* package */void updateSystemProperties(IProxyData proxyData) {
Lines 507-514 Link Here
507
515
508
	public void initialize() {
516
	public void initialize() {
509
		updateSystemProperties(getProxyData(VERIFY_EMPTY));
517
		updateSystemProperties(getProxyData(VERIFY_EMPTY));
510
		((IEclipsePreferences)getParentPreferences()).addNodeChangeListener(this);
518
		preferenceManager.addNodeChangeListener(PREF_PROXY_DATA_NODE, this);
511
		((IEclipsePreferences)getPreferenceNode()).addPreferenceChangeListener(this);
519
		preferenceManager.addPreferenceChangeListener(getPreferenceNode(), this);
512
	}
520
	}
513
	
521
	
514
	private ISecurePreferences getNode() {
522
	private ISecurePreferences getNode() {

Return to bug 242057