Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1bc65f34449bc1c984e7a43b2bdca6491bf3bd30 (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
/****************************************************************************
 * Copyright (c) 2013 Composent, Inc. and others.
 *
 * 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/
 *
 * Contributors:
 *   Composent, Inc. - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.server.generic;

import java.io.IOException;
import java.net.*;
import java.util.*;
import org.eclipse.ecf.core.*;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.sharedobject.ISharedObjectContainer;
import org.eclipse.ecf.internal.server.generic.Activator;
import org.eclipse.ecf.provider.generic.*;

/**
 * @since 6.0
 */
public class SSLGenericServerContainerGroup implements IGenericServerContainerGroup {

	private SSLGenericServerSOContainerGroup serverGroup;
	private Map defaultContainerProperties;

	class SSLGenericServerSOContainerGroup extends SSLServerSOContainerGroup {

		public SSLGenericServerSOContainerGroup(String name, int port, InetAddress bindAddress) {
			super(name, null, port, bindAddress);
		}

		public Map getMap() {
			return map;
		}
	}

	/**
	 * @since 7.0
	 */
	public SSLGenericServerContainerGroup(String hostname, int port, InetAddress bindAddress, Map defaultContainerProperties) {
		this.serverGroup = new SSLGenericServerSOContainerGroup(hostname, port, bindAddress);
		this.defaultContainerProperties = defaultContainerProperties;
	}

	public SSLGenericServerContainerGroup(String hostname, int port, Map defaultContainerProperties) {
		this(hostname, port, null, defaultContainerProperties);
	}

	private String getHost() {
		return serverGroup.getName();
	}

	private int getPort() {
		return serverGroup.getPort();
	}

	public Map getContainers() {
		Map result = new HashMap();
		Map lock = serverGroup.getMap();
		synchronized (lock) {
			for (Iterator i = lock.keySet().iterator(); i.hasNext();) {
				String key = (String) i.next();
				result.put(key, lock.get(key));
			}
		}
		return result;
	}

	public ISharedObjectContainer createContainer(String path, int keepAlive, Map properties) throws ContainerCreateException {
		if (path == null)
			throw new ContainerCreateException("Path for new container cannot be null"); //$NON-NLS-1$
		Map lock = serverGroup.getMap();
		ISharedObjectContainer newContainer = null;
		synchronized (lock) {
			SSLServerSOContainer existing = (SSLServerSOContainer) lock.get(path);
			if (existing != null)
				throw new ContainerCreateException("Container with path=" + path + " already exists"); //$NON-NLS-1$ //$NON-NLS-2$
			// create container
			newContainer = createGenericServerContainer(path, keepAlive, properties);
			// add To container manager
			addNewContainerToContainerManager(newContainer);
		}
		return newContainer;
	}

	public ISharedObjectContainer createContainer(String path, int keepAlive) throws ContainerCreateException {
		return createContainer(path, keepAlive, null);
	}

	private void addNewContainerToContainerManager(ISharedObjectContainer container) {
		IContainerManager containerManager = Activator.getDefault().getContainerManager();
		if (containerManager != null) {
			ContainerTypeDescription ctd = containerManager.getContainerFactory().getDescriptionByName("ecf.generic.server"); //$NON-NLS-1$
			containerManager.addContainer(container, ctd);
		}
	}

	private void removeContainerFromContainerManager(ISharedObjectContainer container) {
		IContainerManager containerManager = Activator.getDefault().getContainerManager();
		if (containerManager != null) {
			containerManager.removeContainer(container);
		}
	}

	public ISharedObjectContainer createContainer(String path) throws ContainerCreateException {
		return createContainer(path, IGenericServerContainerGroup.DEFAULT_KEEPALIVE);
	}

	/**
	 * @since 5.1
	 */
	protected SSLServerSOContainerGroup getServerGroup() {
		return serverGroup;
	}

	protected SSLServerSOContainer createGenericServerContainer(String path, int keepAlive, Map properties) throws ContainerCreateException {
		try {
			return new SSLServerSOContainer(new SOContainerConfig(createGenericServerID(path, properties), createGenericServerProperties(path, properties)), getServerGroup(), path, keepAlive);
		} catch (Exception e) {
			throw new ContainerCreateException("Unexpected exception creating generic server container path=" + path, e); //$NON-NLS-1$
		}
	}

	protected Map createGenericServerProperties(String path, Map properties) {
		return (properties == null) ? defaultContainerProperties : properties;
	}

	protected ID createGenericServerID(String path, Map properties) throws ContainerCreateException {
		if (!path.startsWith("/")) //$NON-NLS-1$
			throw new ContainerCreateException("Path must start with '/'"); //$NON-NLS-1$
		String serverIDPrefix = createGenericServerIDPrefix();
		return IDFactory.getDefault().createStringID(serverIDPrefix + path);
	}

	private String createGenericServerIDPrefix() {
		return SSLServerSOContainer.DEFAULT_PROTOCOL + "://" + getHost() + ":" + getPort(); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public ISharedObjectContainer getContainer(String path) {
		return serverGroup.get(path);
	}

	public ISharedObjectContainer removeContainer(String path) {
		return serverGroup.remove(path);
	}

	public void startListening() throws IOException {
		Map lock = serverGroup.getMap();
		synchronized (lock) {
			serverGroup.putOnTheAir();
		}
	}

	public boolean isListening() {
		return serverGroup.isOnTheAir();
	}

	public void stopListening() {
		Map lock = serverGroup.getMap();
		synchronized (lock) {
			serverGroup.takeOffTheAir();
		}
	}

	public void close() {
		Map lock = serverGroup.getMap();
		synchronized (lock) {
			for (Iterator i = lock.keySet().iterator(); i.hasNext();) {
				SSLServerSOContainer container = (SSLServerSOContainer) removeContainer((String) i.next());
				removeContainerFromContainerManager(container);
				if (container != null) {
					container.dispose();
				}
			}
			serverGroup.takeOffTheAir();
		}
	}

	public URI getGroupEndpoint() {
		try {
			return new URI(createGenericServerIDPrefix());
		} catch (URISyntaxException e) {
			// should not happen
			return null;
		}
	}

	public boolean isSSLTransport() {
		return true;
	}

}

Back to the top