Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5e3889d963325814792318fb3c32a36d3441a90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.preferences;

import java.util.*;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.preferences.AbstractPreferenceStorage;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.osgi.util.NLS;
import org.osgi.service.prefs.BackingStoreException;

public class ScopeDescriptor {

	String name;
	AbstractPreferenceStorage storage;
	Set loadedNodes = Collections.synchronizedSet(new HashSet());

	public ScopeDescriptor(AbstractPreferenceStorage storage) {
		super();
		this.storage = storage;
	}

	String getName() {
		return name;
	}

	/*
	 * For now the default behaviour is that we flush/load at the second level.
	 */
	IEclipsePreferences getLoadLevel(IEclipsePreferences node) {
		String path = node.absolutePath();
		int count = EclipsePreferences.getSegmentCount(path);
		// root or scope root
		if (count == 1 || count == 0)
			return null;
		// the load level we want
		if (count == 2)
			return node;
		for (int i = count; i > 2 && node.parent() != null; i--)
			node = (IEclipsePreferences) node.parent();
		return node;
	}

	String[] childrenNames(final String path) throws BackingStoreException {
		if (storage == null)
			return new String[0];
		final String[][] result = new String[1][];
		final BackingStoreException[] bse = new BackingStoreException[1];
		ISafeRunnable code = new ISafeRunnable() {
			public void run() throws Exception {
				result[0] = storage.childrenNames(path);
			}

			public void handleException(Throwable exception) {
				if (exception instanceof BackingStoreException)
					bse[0] = (BackingStoreException) exception;
				else
					bse[0] = new BackingStoreException(NLS.bind(PrefsMessages.childrenNames2, path), exception);
			}
		};
		SafeRunner.run(code);
		if (bse[0] != null)
			throw bse[0];
		return result[0] == null ? new String[0] : result[0];
	}

	Properties load(final String path) throws BackingStoreException {
		if (storage == null)
			return null;
		final Properties[] result = new Properties[1];
		final BackingStoreException[] bse = new BackingStoreException[1];
		ISafeRunnable code = new ISafeRunnable() {
			public void run() throws Exception {
				result[0] = storage.load(path);
			}

			public void handleException(Throwable exception) {
				if (exception instanceof BackingStoreException)
					bse[0] = (BackingStoreException) exception;
				else
					bse[0] = new BackingStoreException(NLS.bind(PrefsMessages.preferences_loadException, path), exception);
			}
		};
		SafeRunner.run(code);
		if (bse[0] != null)
			throw bse[0];
		return result[0] == null ? null : result[0];
	}

	void save(final String path, final Properties properties) throws BackingStoreException {
		if (storage == null)
			return;
		final BackingStoreException[] bse = new BackingStoreException[1];
		ISafeRunnable code = new ISafeRunnable() {
			public void run() throws Exception {
				storage.save(path, properties);
			}

			public void handleException(Throwable exception) {
				if (exception instanceof BackingStoreException)
					bse[0] = (BackingStoreException) exception;
				else
					bse[0] = new BackingStoreException(NLS.bind(PrefsMessages.preferences_saveException, path), exception);
			}
		};
		SafeRunner.run(code);
		if (bse[0] != null)
			throw bse[0];
	}

	boolean isAlreadyLoaded(String node) {
		return loadedNodes.contains(node);
	}

	void loaded(String node) {
		loadedNodes.add(node);
	}

	void removed(final String path) {
		if (storage == null)
			return;
		SafeRunner.run(new ISafeRunnable() {
			public void run() throws Exception {
				storage.removed(path);
			}

			public void handleException(Throwable exception) {
				// ignore here, error will be logged in saferunner
			}
		});
	}
}

Back to the top