Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 92d4397046f2de94490f8ff65eae37fda9e7f086 (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
/*******************************************************************************
* Copyright (c) 2013 Composent, Inc. 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:
*   Composent, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.server.generic;

import java.io.IOException;
import java.security.PermissionCollection;
import java.util.*;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.*;
import org.eclipse.ecf.core.security.IConnectHandlerPolicy;
import org.eclipse.ecf.internal.server.generic.Activator;
import org.eclipse.ecf.provider.generic.*;

/**
 * @since 6.0
 */
public abstract class SSLAbstractGenericServer {

	protected SSLServerSOContainerGroup serverGroup;

	public SSLAbstractGenericServer(String host, int port) {
		this.serverGroup = new SSLServerSOContainerGroup(host, port);
	}

	/**
	 * @since 2.0
	 */
	public SSLGenericServerContainer getFirstServerContainer() {
		return getServerContainer(0);
	}

	public List /* GenericServerContainer */getServerContainers() {
		List result = new ArrayList();
		for (Iterator i = serverGroup.elements(); i.hasNext();) {
			result.add(i.next());
		}
		return result;
	}

	/**
	 * @since 2.0
	 */
	public SSLGenericServerContainer getServerContainer(int index) {
		return (SSLGenericServerContainer) getServerContainers().get(index);
	}

	protected void putOnTheAir() throws IOException {
		if (!serverGroup.isOnTheAir())
			serverGroup.putOnTheAir();
	}

	protected void takeOffTheAir() {
		if (serverGroup.isOnTheAir())
			serverGroup.takeOffTheAir();
	}

	public synchronized void start(String path, int keepAlive) throws Exception {
		createAndInitializeServer(path, keepAlive);
		putOnTheAir();
	}

	public synchronized void stop() {
		if (serverGroup != null) {
			serverGroup.takeOffTheAir();
		}
		List servers = getServerContainers();
		for (Iterator i = servers.iterator(); i.hasNext();) {
			SSLGenericServerContainer s = (SSLGenericServerContainer) i.next();
			s.ejectAllGroupMembers("Shutting down immediately"); //$NON-NLS-1$
			s.dispose();
		}
	}

	protected void createAndInitializeServer(String path) throws IDCreateException {
		createAndInitializeServer(path, TCPServerSOContainer.DEFAULT_KEEPALIVE);
	}

	protected void createAndInitializeServer(String path, int keepAlive) throws IDCreateException {
		if (path == null || path.equals("")) //$NON-NLS-1$
			throw new NullPointerException("Cannot create ID with null or empty path"); //$NON-NLS-1$
		SSLGenericServerContainer s = new SSLGenericServerContainer(this, createServerConfig(path), serverGroup, path, keepAlive);
		IContainerManager containerManager = Activator.getDefault().getContainerManager();
		if (containerManager != null) {
			ContainerTypeDescription ctd = containerManager.getContainerFactory().getDescriptionByName("ecf.generic.ssl.server"); //$NON-NLS-1$
			containerManager.addContainer(s, ctd);
		}
		IConnectHandlerPolicy policy = createConnectHandlerPolicy(s, path);
		if (policy != null)
			s.setConnectPolicy(policy);
	}

	protected PermissionCollection checkConnect(Object address, ID fromID, ID targetID, String targetGroup, Object connectData) throws Exception {
		return null;
	}

	protected abstract void handleDisconnect(ID targetID);

	protected abstract void handleEject(ID targetID);

	/**
	 * @since 2.0
	 */
	protected IConnectHandlerPolicy createConnectHandlerPolicy(SSLGenericServerContainer s, String path) {
		return new IConnectHandlerPolicy() {
			public PermissionCollection checkConnect(Object address, ID fromID, ID targetID, String targetGroup, Object connectData) throws Exception {
				return SSLAbstractGenericServer.this.checkConnect(address, fromID, targetID, targetGroup, connectData);
			}

			public void refresh() {
				// do nothing
			}
		};
	}

	protected ID createServerIDFromPath(String path) throws IDCreateException {
		if (!path.startsWith("/"))path = "/" + path; //$NON-NLS-1$//$NON-NLS-2$
		String id = SSLServerSOContainer.DEFAULT_PROTOCOL + "://" //$NON-NLS-1$
				+ getHost() + ":" + getPort() + path; //$NON-NLS-1$
		return IDFactory.getDefault().createStringID(id);
	}

	protected SOContainerConfig createServerConfig(String path) throws IDCreateException {
		return new SOContainerConfig(createServerIDFromPath(path));
	}

	protected String getHost() {
		return this.serverGroup.getName();
	}

	protected int getPort() {
		return this.serverGroup.getPort();
	}

}

Back to the top