Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c886d60c7e11f7769658baba1cfc30b333f6cf7c (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*******************************************************************************
 * Copyright (c) 2014, 2018 Red Hat.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0l
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.docker.core;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.linuxtools.docker.core.IDockerConnectionSettings.BindingType;
import org.eclipse.linuxtools.internal.docker.core.DefaultDockerConnectionSettingsFinder;
import org.eclipse.linuxtools.internal.docker.core.DefaultDockerConnectionStorageManager;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
import org.eclipse.linuxtools.internal.docker.core.DockerContainerRefreshManager;
import org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings;
import org.eclipse.linuxtools.internal.docker.core.UnixSocketConnectionSettings;

public class DockerConnectionManager {

	private static DockerConnectionManager instance;

	private List<IDockerConnection> connections;
	private ListenerList<IDockerConnectionManagerListener> connectionManagerListeners;

	private IDockerConnectionSettingsFinder connectionSettingsFinder = new DefaultDockerConnectionSettingsFinder();
	private IDockerConnectionStorageManager connectionStorageManager = new DefaultDockerConnectionStorageManager();

	public static DockerConnectionManager getInstance() {
		if (instance == null) {
			instance = new DockerConnectionManager();
		}
		return instance;
	}

	private DockerConnectionManager() {
		reloadConnections();
	}

	public void reloadConnections() {
		this.connections = connectionStorageManager.loadConnections();
		for (IDockerConnection connection : connections) {
				notifyListeners(connection,
						IDockerConnectionManagerListener.ADD_EVENT);
		}
		List<IDockerConnectionSettings> settings = connectionSettingsFinder
				.getKnownConnectionSettings();
		for (IDockerConnectionSettings setting : settings) {
			if (setting != null) {
				IDockerConnection conn;
				if (setting.getType().equals(BindingType.UNIX_SOCKET_CONNECTION)) {
					UnixSocketConnectionSettings usetting = (UnixSocketConnectionSettings) setting;
					conn = new DockerConnection.Builder().name(usetting.getName())
							.unixSocketConnection(usetting);
				} else {
					TCPConnectionSettings tsetting = (TCPConnectionSettings) setting;
					conn = new DockerConnection.Builder().name(tsetting.getName())
							.tcpConnection(tsetting);
				}
				// add the connection but do not notify the listeners to avoid
				// flickering on the Docker Explorer view for each entry
				addConnection(conn, false);
			}
		}
	}

	public void setConnectionSettingsFinder(
			final IDockerConnectionSettingsFinder connectionSettingsFinder) {
		this.connectionSettingsFinder = connectionSettingsFinder;
	}

	public void setConnectionStorageManager(
			final IDockerConnectionStorageManager connectionStorageManager) {
		this.connectionStorageManager = connectionStorageManager;
	}

	public void saveConnections() {
		this.connectionStorageManager.saveConnections(this.connections);
	}

	/**
	 * @return an unmodifiable and non-null array of {@link IDockerConnection}
	 */
	public IDockerConnection[] getConnections() {
		if (this.connections == null) {
			return new IDockerConnection[0];
		}
		return connections.toArray(new IDockerConnection[connections.size()]);
	}

	/**
	 * @return an unmodifiable and non-null list of {@link IDockerConnection}
	 */
	public List<IDockerConnection> getAllConnections() {
		if (this.connections == null) {
			return Collections.emptyList();
		}
		return Collections.unmodifiableList(this.connections);
	}

	/**
	 * @return the first {@link IDockerConnection} or <code>null</code> if none
	 *         exists yet.
	 */
	public IDockerConnection getFirstConnection() {
		if (!hasConnections()) {
			return null;
		}
		return this.connections.get(0);
	}

	/**
	 * @return <code>true</code> if there is at least one
	 *         {@link IDockerConnection} in this
	 *         {@link DockerConnectionManager}, <code>false</code> otherwise.
	 */
	public boolean hasConnections() {
		return connections != null && !connections.isEmpty();
	}

	/**
	 * Finds the {@link IDockerConnection} from the given {@code connectionName}
	 * 
	 * @param connectionName
	 *            the name of the connection to find
	 * @return the {@link IDockerConnection} or <code>null</code> if none
	 *         matched.
	 */
	public IDockerConnection getConnectionByName(final String connectionName) {
		return this.connections.stream().filter(
				connection -> connection.getName().equals(connectionName))
				.findFirst().orElse(null);
	}

	/**
	 * Finds the {@link IDockerConnection} from the given {@code connectionUri}
	 * 
	 * @param connectionUri
	 *            the URI of the connection to find
	 * @return the {@link IDockerConnection} or <code>null</code> if none
	 *         matched.
	 */
	public IDockerConnection getConnectionByUri(String connectionUri) {
		return DockerConnectionManager.getInstance().getAllConnections()
				.stream()
				.filter(c -> c.getUri().equals(connectionUri)).findFirst()
				.orElse(null);
	}

	/**
	 * @return an immutable {@link List} of the {@link IDockerConnection} names
	 */
	public List<String> getConnectionNames() {
		return Collections.unmodifiableList(getAllConnections().stream()
				.map(c -> c.getName())
				// making sure that no 'null' name is returned in the list of
				// connection names.
				.filter(n -> n != null)
				.collect(Collectors.toList()));
	}

	public IDockerConnection findConnection(final String name) {
		if (name != null) {
			for (IDockerConnection connection : connections) {
				if (connection.getName() != null
						&& connection.getName().equals(name))
					return connection;
			}
		}
		return null;
	}

	/**
	 * Adds the given connection and notifies all registered
	 * {@link IDockerConnectionManagerListener}
	 * 
	 * @param dockerConnection
	 *            the connection to add
	 */
	public void addConnection(final IDockerConnection dockerConnection) {
		addConnection(dockerConnection, true);
	}

	/**
	 * Adds the given connection and notifies optionally all registered
	 * {@link IDockerConnectionManagerListener}
	 * 
	 * @param dockerConnection
	 *            the connection to add
	 * @param notifyListeners
	 *            flag to indicate if registered
	 *            {@link IDockerConnectionManagerListener} should be notified
	 *            about the {@link IDockerConnection} addition.
	 */
	public void addConnection(final IDockerConnection dockerConnection,
			final boolean notifyListeners) {
		if (!connections.contains(dockerConnection)) {
			connections.add(dockerConnection);
			saveConnections();
			if (notifyListeners) {
				notifyListeners(dockerConnection,
						IDockerConnectionManagerListener.ADD_EVENT);
			}
		}
	}

	public void removeConnection(final IDockerConnection connection) {
		connections.remove(connection);
		saveConnections();
		notifyListeners(connection,
				IDockerConnectionManagerListener.REMOVE_EVENT);
		DockerContainerRefreshManager.getInstance()
				.removeContainerRefreshThread(connection);
	}

	public void addConnectionManagerListener(
			IDockerConnectionManagerListener listener) {
		if (connectionManagerListeners == null)
			connectionManagerListeners = new ListenerList<>(
					ListenerList.IDENTITY);
		connectionManagerListeners.add(listener);
	}

	public void removeConnectionManagerListener(
			IDockerConnectionManagerListener listener) {
		if (connectionManagerListeners != null)
			connectionManagerListeners.remove(listener);
	}

	/**
	 * Notifies all listeners that a change occurred on the given connection
	 * 
	 * @param connection
	 *            the connection that changed
	 * @param type
	 *            the type of change
	 */
	public void notifyListeners(final IDockerConnection connection,
			final int type) {
		if (connectionManagerListeners != null) {
			for (IDockerConnectionManagerListener listener : connectionManagerListeners) {
				listener.changeEvent(connection, type);
			}
		}
	}

	/**
	 * Notifies all listeners that a change occurred on the given connection but
	 * does nothing if DockerConnectionManager isn't instantiated
	 * 
	 * @param connection
	 *                       the connection that changed
	 * @param type
	 *                       the type of change
	 * 
	 * @since 4.0
	 */
	public static void instanceNotifyListeners(
			final IDockerConnection connection,
			final int type) {
		if (instance != null) {
			instance.notifyListeners(connection, type);
		}
	}

	/**
	 * Finds the default {@link IDockerConnectionSettings}
	 * 
	 * @return the default {@link IDockerConnectionSettings} or
	 *         <code>null</code> if nothing was found
	 */
	public IDockerConnectionSettings findDefaultConnectionSettings() {
		// delegate the call to a utility class.
		return connectionSettingsFinder.findDefaultConnectionSettings();
	}

	/**
	 * Resolves the name of the Docker instance, given the
	 * {@link IDockerConnectionSettings}
	 * 
	 * @param connectionSettings
	 *            the settings to use to connect
	 * @return the name retrieved from the Docker instance or <code>null</code>
	 *         if something went wrong.
	 */
	public String resolveConnectionName(
			IDockerConnectionSettings connectionSettings) {
		return connectionSettingsFinder
				.resolveConnectionName(connectionSettings);
	}

	/**
	 * Updates the given {@link IDockerConnection} with the given {@code name}
	 * and {@code connectionSettings}
	 * 
	 * @param connection
	 *            the {@link IDockerConnection} to update
	 * @param name
	 *            the (new) connection name
	 * @param connectionSettings
	 *            the (new) connection settings
	 * @return <code>true</code> if the connection name or settings changed,
	 *         <code>false</code> otherwise.
	 */
	public boolean updateConnection(final IDockerConnection connection,
			final String name,
			final IDockerConnectionSettings connectionSettings) {
		final boolean nameChanged = connection.setName(name);
		final boolean settingsChanged = connection
				.setSettings(connectionSettings);
		if (nameChanged) {
			notifyListeners(connection,
					IDockerConnectionManagerListener.RENAME_EVENT);
		}
		if (settingsChanged) {
			notifyListeners(connection,
					IDockerConnectionManagerListener.UPDATE_SETTINGS_EVENT);
		}
		if (nameChanged || settingsChanged) {
			saveConnections();
			return true;
		}
		return false;
	}


}

Back to the top