Skip to main content
summaryrefslogtreecommitdiffstats
blob: 694ae571760eff28555f95409e4f7d2c3573806e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.db.internal;

import java.util.Iterator;
import java.util.Vector;

import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.IProfileListener;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.jpt.db.ConnectionProfile;
import org.eclipse.jpt.db.ConnectionProfileRepository;
import org.eclipse.jpt.db.ConnectionProfileListener;
import org.eclipse.jpt.utility.internal.ClassTools;
import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 * Wrap the DTP ProfileManager.
 */
public final class DTPConnectionProfileRepository
	implements ConnectionProfileRepository
{
	private ProfileManager dtpProfileManager;

	private LocalProfileListener profileListener;

	private final Vector<DTPConnectionProfileWrapper> connectionProfiles = new Vector<DTPConnectionProfileWrapper>();


	// ********** singleton **********

	private static final DTPConnectionProfileRepository INSTANCE = new DTPConnectionProfileRepository();

	public static DTPConnectionProfileRepository instance() {
		return INSTANCE;
	}

	/**
	 * 'private' to ensure singleton
	 */
	private DTPConnectionProfileRepository() {
		super();
	}


	// ********** lifecycle **********

	/**
	 * called by plug-in
	 */
	public synchronized void start() {
		this.dtpProfileManager = ProfileManager.getInstance();
		this.profileListener = new LocalProfileListener();
		this.dtpProfileManager.addProfileListener(this.profileListener);
		for (IConnectionProfile dtpProfile : this.dtpProfileManager.getProfiles()) {
			this.connectionProfiles.add(new DTPConnectionProfileWrapper(dtpProfile));
		}
	}

	/**
	 * called by plug-in
	 */
	public synchronized void stop() {
		for (DTPConnectionProfileWrapper profile : this.connectionProfiles) {
			profile.dispose();
		}
		this.connectionProfiles.clear();
		this.dtpProfileManager.removeProfileListener(this.profileListener);
		this.profileListener = null;
		this.dtpProfileManager = null;
	}


	// ********** profiles **********

	public synchronized Iterator<ConnectionProfile> connectionProfiles() {
		return new CloneIterator<ConnectionProfile>(this.connectionProfiles);  // read-only
	}

	private synchronized Iterator<DTPConnectionProfileWrapper> connectionProfileWrappers() {
		return new CloneIterator<DTPConnectionProfileWrapper>(this.connectionProfiles);  // read-only
	}

	public int connectionProfilesSize() {
		return this.connectionProfiles.size();
	}

	public Iterator<String> connectionProfileNames() {
		return new TransformationIterator<DTPConnectionProfileWrapper, String>(this.connectionProfileWrappers()) {
			@Override
			protected String transform(DTPConnectionProfileWrapper profile) {
				 return profile.getName();
			}
		};
	}

	public boolean containsConnectionProfileNamed(String name) {
		return ! this.connectionProfileNamed(name).isNull();
	}

	public ConnectionProfile connectionProfileNamed(String name) {
		for (Iterator<DTPConnectionProfileWrapper> stream = this.connectionProfileWrappers(); stream.hasNext(); ) {
			DTPConnectionProfileWrapper profile = stream.next();
			if (profile.getName().equals(name)) {
				return profile;
			}
		}
		return NullConnectionProfile.instance();
	}

	synchronized DTPConnectionProfileWrapper addConnectionProfile(IConnectionProfile dtpConnectionProfile) {
		for (DTPConnectionProfileWrapper wrapper : this.connectionProfiles) {
			if (wrapper.wraps(dtpConnectionProfile)) {
				throw new IllegalStateException("duplicate connection profile: " + dtpConnectionProfile.getName());  //$NON-NLS-1$
			}
		}
		DTPConnectionProfileWrapper wrapper = new DTPConnectionProfileWrapper(dtpConnectionProfile);
		this.connectionProfiles.add(wrapper);
		return wrapper;
	}

	synchronized DTPConnectionProfileWrapper removeConnectionProfile(IConnectionProfile dtpConnectionProfile) {
		for (Iterator<DTPConnectionProfileWrapper> stream = this.connectionProfiles.iterator(); stream.hasNext(); ) {
			DTPConnectionProfileWrapper wrapper = stream.next();
			if (wrapper.wraps(dtpConnectionProfile)) {
				stream.remove();
				return wrapper;
			}
		}
		throw new IllegalStateException("invalid connection profile: " + dtpConnectionProfile.getName());  //$NON-NLS-1$
	}

	synchronized DTPConnectionProfileWrapper connectionProfile(IConnectionProfile dtpConnectionProfile) {
		for (DTPConnectionProfileWrapper wrapper : this.connectionProfiles) {
			if (wrapper.wraps(dtpConnectionProfile)) {
				return wrapper;
			}
		}
		throw new IllegalStateException("invalid connection profile: " + dtpConnectionProfile.getName());  //$NON-NLS-1$
	}


	// ********** Object overrides **********

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(ClassTools.toStringClassNameForObject(this));
		sb.append((this.connectionProfiles != null) ? this.connectionProfiles : "<'connectionProfiles' uninitialized>");  //$NON-NLS-1$
		return sb.toString();
	}


	// ********** listeners **********

	public void addConnectionProfileListener(ConnectionProfileListener listener) {
		this.profileListener.addConnectionProfileListener(listener);
	}

	public void removeConnectionProfileListener(ConnectionProfileListener listener) {
		this.profileListener.removeConnectionProfileListener(listener);
	}


	// ********** listener **********

	/**
	 * Keep the repository in synch with the DTP profile manager
	 * and forward events to the repository's listeners.
	 */
	private class LocalProfileListener implements IProfileListener {
		private Vector<ConnectionProfileListener> listeners = new Vector<ConnectionProfileListener>();

		LocalProfileListener() {
			super();
		}

		void addConnectionProfileListener(ConnectionProfileListener listener) {
			this.listeners.add(listener);
		}

		void removeConnectionProfileListener(ConnectionProfileListener listener) {
			this.listeners.remove(listener);
		}

		private Iterator<ConnectionProfileListener> listeners() {
			return new CloneIterator<ConnectionProfileListener>(this.listeners);
		}

		// ********** IProfileListener implementation **********

		public void profileAdded(IConnectionProfile dtpProfile) {
			// synch the repository then notify listeners
			DTPConnectionProfileWrapper profile = DTPConnectionProfileRepository.this.addConnectionProfile(dtpProfile);
			for (Iterator<ConnectionProfileListener> stream = this.listeners(); stream.hasNext(); ) {
				stream.next().connectionProfileReplaced(NullConnectionProfile.instance(), profile);
			}
		}

		public void profileChanged(IConnectionProfile dtpProfile) {
			DTPConnectionProfileWrapper profile = DTPConnectionProfileRepository.this.connectionProfile(dtpProfile);
			for (Iterator<ConnectionProfileListener> stream = this.listeners(); stream.hasNext(); ) {
				stream.next().connectionProfileChanged(profile);
			}
		}

		public void profileDeleted(IConnectionProfile dtpProfile) {
			// synch the repository then notify listeners
			DTPConnectionProfileWrapper profile = DTPConnectionProfileRepository.this.removeConnectionProfile(dtpProfile);
			for (Iterator<ConnectionProfileListener> stream = this.listeners(); stream.hasNext(); ) {
				stream.next().connectionProfileReplaced(profile, NullConnectionProfile.instance());
			}
		}

	}

}

Back to the top