View | Details | Raw Unified | Return to bug 31458
Collapse All | Expand All

(-)src/org/eclipse/core/internal/runtime/PreferenceExporter.java (-30 / +33 lines)
Lines 11-19 Link Here
11
package org.eclipse.core.internal.runtime;
11
package org.eclipse.core.internal.runtime;
12
12
13
import java.io.*;
13
import java.io.*;
14
import java.util.HashMap;
14
import java.util.*;
15
import java.util.Map;
16
17
import org.eclipse.core.runtime.*;
15
import org.eclipse.core.runtime.*;
18
16
19
/**
17
/**
Lines 40-47 Link Here
40
	 * @see Preferences#exportPreferences
38
	 * @see Preferences#exportPreferences
41
	 */
39
	 */
42
	public static void exportPreferences(IPath file) throws CoreException {
40
	public static void exportPreferences(IPath file) throws CoreException {
43
		//collect all plugin preferences into a single global preference object
41
		//collect all plugin preferences into a single global properties object
44
		Preferences globalPreferences = new Preferences();
42
		Properties globalPreferences = new Properties();
45
		IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors();
43
		IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors();
46
		for (int i = 0; i < descriptors.length; i++) {
44
		for (int i = 0; i < descriptors.length; i++) {
47
			IPluginDescriptor descriptor = descriptors[i];
45
			IPluginDescriptor descriptor = descriptors[i];
Lines 52-58 Link Here
52
			//now merge the plugin preferences into the global preference object
50
			//now merge the plugin preferences into the global preference object
53
			if (mergePluginPreferences(descriptor, globalPreferences)) {
51
			if (mergePluginPreferences(descriptor, globalPreferences)) {
54
				//write the property that indicates the plugin version
52
				//write the property that indicates the plugin version
55
				globalPreferences.setValue(descriptor.getUniqueIdentifier(), descriptor.getVersionIdentifier().toString());
53
				globalPreferences.put(descriptor.getUniqueIdentifier(), descriptor.getVersionIdentifier().toString());
56
			}
54
			}
57
55
58
		}
56
		}
Lines 66-76 Link Here
66
			String msg = Policy.bind("preferences.fileNotFound", file.toOSString()); //$NON-NLS-1$
64
			String msg = Policy.bind("preferences.fileNotFound", file.toOSString()); //$NON-NLS-1$
67
			throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, null));
65
			throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, null));
68
		}
66
		}
69
		Map idsToPreferences = splitPreferences(file);
67
		Map idsToProperties = splitPreferences(file);
70
		IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors();
68
		IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors();
71
		for (int i = 0; i < descriptors.length; i++) {
69
		for (int i = 0; i < descriptors.length; i++) {
72
			setPluginPreferences(descriptors[i],
70
			setPluginPreferences(descriptors[i],
73
				(Preferences) idsToPreferences.get(descriptors[i].getUniqueIdentifier()));
71
				(Properties) idsToProperties.get(descriptors[i].getUniqueIdentifier()));
74
		}
72
		}
75
	}
73
	}
76
	/**
74
	/**
Lines 108-114 Link Here
108
	 * preferences instance.  Returns true if any properties were added,
106
	 * preferences instance.  Returns true if any properties were added,
109
	 * and false otherwise.
107
	 * and false otherwise.
110
	 */
108
	 */
111
	private static boolean mergePluginPreferences(IPluginDescriptor descriptor, Preferences preferences) throws CoreException {
109
	private static boolean mergePluginPreferences(IPluginDescriptor descriptor, Properties globalProperties) throws CoreException {
112
		boolean found = false;
110
		boolean found = false;
113
		IPath propertiesFile = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false);
111
		IPath propertiesFile = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false);
114
		if (propertiesFile.toFile().exists()) {
112
		if (propertiesFile.toFile().exists()) {
Lines 118-124 Link Here
118
			found = keys.length > 0;
116
			found = keys.length > 0;
119
			for (int i = 0; i < keys.length; i++) {
117
			for (int i = 0; i < keys.length; i++) {
120
				String longKey = pluginId + PLUGIN_SEPARATOR + keys[i];
118
				String longKey = pluginId + PLUGIN_SEPARATOR + keys[i];
121
				preferences.setValue(longKey, pluginPreferences.getString(keys[i]));
119
				globalProperties.put(longKey, pluginPreferences.getString(keys[i]));
122
			}
120
			}
123
		}
121
		}
124
		return found;
122
		return found;
Lines 130-136 Link Here
130
	 * given descriptor, overwriting any previously defined preferences.  If
128
	 * given descriptor, overwriting any previously defined preferences.  If
131
	 * the given preferences is null, all values are returned to their default value.
129
	 * the given preferences is null, all values are returned to their default value.
132
	 */
130
	 */
133
	private static void setPluginPreferences(IPluginDescriptor descriptor, Preferences newPreferences) throws CoreException {
131
	private static void setPluginPreferences(IPluginDescriptor descriptor, Properties newProperties) throws CoreException {
134
		IPath location = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false);
132
		IPath location = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false);
135
		if (descriptor.isPluginActivated()) {
133
		if (descriptor.isPluginActivated()) {
136
			Plugin plugin = descriptor.getPlugin();
134
			Plugin plugin = descriptor.getPlugin();
Lines 143-153 Link Here
143
					oldPreferences.setToDefault(keys[i]);
141
					oldPreferences.setToDefault(keys[i]);
144
				}
142
				}
145
				//add new values
143
				//add new values
146
				if (newPreferences != null) {
144
				if (newProperties != null) {
147
					keys = newPreferences.propertyNames();
145
					for (Enumeration e = newProperties.propertyNames(); e.hasMoreElements();) {
148
					for (int i = 0; i < keys.length; i++) {
146
						String key = ((String)e.nextElement()).intern();
149
						keys[i] = keys[i].intern();
147
						oldPreferences.setValue(key, newProperties.getProperty(key));
150
						oldPreferences.setValue(keys[i], newPreferences.getString(keys[i]));
151
					}
148
					}
152
				}
149
				}
153
				//save the preferences file
150
				//save the preferences file
Lines 155-161 Link Here
155
			}
152
			}
156
		} else {
153
		} else {
157
			//if the plugin isn't loaded, just save the preferences file directly
154
			//if the plugin isn't loaded, just save the preferences file directly
158
			storePreferences(location, newPreferences);
155
			storePreferences(location, newProperties);
159
		}
156
		}
160
	}
157
	}
161
	/**
158
	/**
Lines 166-172 Link Here
166
	private static Map splitPreferences(IPath file) throws CoreException {
163
	private static Map splitPreferences(IPath file) throws CoreException {
167
		Preferences globalPreferences = loadPreferences(file, new Preferences());
164
		Preferences globalPreferences = loadPreferences(file, new Preferences());
168
		validatePreferenceFileFormat(globalPreferences);
165
		validatePreferenceFileFormat(globalPreferences);
169
		Map idsToPreferences = new HashMap();
166
		Map idsToProperties = new HashMap();
170
		String[] keys = globalPreferences.propertyNames();
167
		String[] keys = globalPreferences.propertyNames();
171
		for (int i = 0; i < keys.length; i++) {
168
		for (int i = 0; i < keys.length; i++) {
172
			String longKey = keys[i];
169
			String longKey = keys[i];
Lines 174-188 Link Here
174
			if (index >= 0) {
171
			if (index >= 0) {
175
				String pluginId = longKey.substring(0, index);
172
				String pluginId = longKey.substring(0, index);
176
				String key = longKey.substring(index + 1);
173
				String key = longKey.substring(index + 1);
177
				Preferences preferences = (Preferences) idsToPreferences.get(pluginId);
174
				Properties properties = (Properties) idsToProperties.get(pluginId);
178
				if (preferences == null) {
175
				if (properties == null) {
179
					preferences = new Preferences();
176
					properties = new Properties();
180
					idsToPreferences.put(pluginId, preferences);
177
					idsToProperties.put(pluginId, properties);
181
				}
178
				}
182
				preferences.setValue(key, globalPreferences.getString(longKey));
179
				properties.put(key, globalPreferences.getString(longKey));
183
			}
180
			}
184
		}
181
		}
185
		return idsToPreferences;
182
		return idsToProperties;
186
	}
183
	}
187
184
188
185
Lines 191-211 Link Here
191
	/**
188
	/**
192
	 * Writes the given preferences to the given file.  If the preferences are
189
	 * Writes the given preferences to the given file.  If the preferences are
193
	 * null or empty, the file is deleted.
190
	 * null or empty, the file is deleted.
191
	 * @param preferences either a Properties or Preferences object
194
	 */
192
	 */
195
	private static void storePreferences(IPath destination, Preferences preferences) throws CoreException {
193
	private static void storePreferences(IPath destination, Object preferences) throws CoreException {
196
		File propertiesFile = destination.toFile();
194
		File propertiesFile = destination.toFile();
197
		if (preferences == null || preferences.propertyNames().length == 0) {
195
		//if the preferences is null or empty, delete the file and return
196
		if (preferences == null ||
197
			(preferences instanceof Properties && ((Properties)preferences).size() == 0) ||
198
			(preferences instanceof Preferences &&((Preferences)preferences).propertyNames().length == 0)) { 
198
			propertiesFile.delete();
199
			propertiesFile.delete();
199
			return;
200
			return;
200
		}
201
		}
201
		File parent = propertiesFile.getParentFile();
202
		File parent = propertiesFile.getParentFile();
202
		if (!parent.exists()) {
203
		if (!parent.exists())
203
			parent.mkdirs();
204
			parent.mkdirs();
204
		}
205
		OutputStream out = null;
205
		OutputStream out = null;
206
		try {
206
		try {
207
			out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
207
			out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
208
			preferences.store(out, null);
208
			if (preferences instanceof Properties)
209
				((Properties)preferences).store(out, null);
210
			else
211
				((Preferences)preferences).store(out, null);
209
		} catch (IOException e) {
212
		} catch (IOException e) {
210
			String msg = Policy.bind("preferences.errorWriting", propertiesFile.toString(), e.getMessage()); //$NON-NLS-1$
213
			String msg = Policy.bind("preferences.errorWriting", propertiesFile.toString(), e.getMessage()); //$NON-NLS-1$
211
			throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, e));
214
			throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, e));
Lines 218-224 Link Here
218
				}
221
				}
219
			}
222
			}
220
		}
223
		}
221
	}
224
	}	
222
	/**
225
	/**
223
	 * Compares two plugin version identifiers to see if their preferences
226
	 * Compares two plugin version identifiers to see if their preferences
224
	 * are compatible.  If they are not compatible, a warning message is 
227
	 * are compatible.  If they are not compatible, a warning message is 

Return to bug 31458