Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e56e69ec29f572778e4a660b84acd32db3c817c (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
package org.eclipse.ecf.example.collab.start;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.ecf.example.collab.ClientPlugin;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

public class AccountStart {
	private static final String SAVED = "saved-connections";
	private static final int BACKING_STORE_SAVE_ERROR = 1001;
	private static final int BACKING_STORE_LOAD_ERROR = 1002;
	private Map connectionDetails = new HashMap();
	
	public ConnectionDetails addConnectionDetails(ConnectionDetails cd) {
		String targetURI = normalizeURI(cd.getTargetURI());
		return (ConnectionDetails) connectionDetails.put(targetURI, cd);
	}
	
	private String normalizeURI(String uri) {
		return uri.replace('/', '.');
	}
	public void removeConnectionDetails(ConnectionDetails cd) {
			try {
				Preferences preferences = new InstanceScope()
						.getNode(ClientPlugin.PLUGIN_ID);
				Preferences connections = preferences.node(SAVED);
				String[] targets = connections.childrenNames();
				for (int i = 0; i < targets.length; i++) {
					String target = targets[i];
					Preferences node = connections.node(target);
					String cdTarget = normalizeURI(cd.getTargetURI());
					if (node != null && target != null && target.equals(cdTarget)) {
						node.removeNode();
					}
				}
				connections.flush();
			} catch (BackingStoreException e) {
				ClientPlugin.getDefault().getLog().log(
						new Status(IStatus.ERROR, ClientPlugin.PLUGIN_ID,
								BACKING_STORE_LOAD_ERROR,
								"Exception loading connection details", e));
			}
	}
	public Collection getConnectionDetails() {
		return connectionDetails.values();
	}
	public void saveConnectionDetailsToPreferenceStore() {
		Preferences preferences = new InstanceScope()
				.getNode(ClientPlugin.PLUGIN_ID);
		Preferences connections = preferences.node(SAVED);
		for (Iterator i = connectionDetails.keySet().iterator(); i.hasNext();) {
			String target = (String) i.next();
			ConnectionDetails details = (ConnectionDetails) connectionDetails
					.get(target);
			Preferences p = connections.node(target);
			p.put(ConnectionDetails.CONTAINER_TYPE, details.getContainerType());
			p.put(ConnectionDetails.TARGET_URI, details.getTargetURI());
			p.put(ConnectionDetails.NICKNAME, details.getNickname());
			p.put(ConnectionDetails.PASSWORD, details.getPassword());
		}
		try {
			connections.flush();
		} catch (BackingStoreException e) {
			ClientPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR, ClientPlugin.PLUGIN_ID,
							BACKING_STORE_SAVE_ERROR,
							"Exception saving connection details", e));
		}
	}
	public void loadConnectionDetailsFromPreferenceStore() {
		try {
			Preferences preferences = new InstanceScope()
					.getNode(ClientPlugin.PLUGIN_ID);
			Preferences connections = preferences.node(SAVED);
			String[] targets = connections.childrenNames();
			for (int i = 0; i < targets.length; i++) {
				String target = targets[i];
				Preferences node = connections.node(target);
				if (node != null) {
					addConnectionDetails(new ConnectionDetails(node.get(
							ConnectionDetails.CONTAINER_TYPE, ""), node.get(
							ConnectionDetails.TARGET_URI, ""), node.get(
							ConnectionDetails.NICKNAME, ""), node.get(
							ConnectionDetails.PASSWORD, "")));
				}
			}
		} catch (BackingStoreException e) {
			ClientPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR, ClientPlugin.PLUGIN_ID,
							BACKING_STORE_LOAD_ERROR,
							"Exception loading connection details", e));
		}
	}
}

Back to the top