Skip to main content
summaryrefslogtreecommitdiffstats
blob: e491f8646c99810ec9541a465f02d20af05b57b2 (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
package org.eclipse.ecf.osgi.services.remoteserviceadmin;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.discovery.IServiceProperties;
import org.eclipse.ecf.discovery.ServiceInfo;
import org.eclipse.ecf.discovery.ServiceProperties;
import org.eclipse.ecf.discovery.identity.IServiceTypeID;
import org.eclipse.ecf.discovery.identity.ServiceIDFactory;

public abstract class AbstractServiceInfoFactory extends
		AbstractMetadataFactory implements IServiceInfoFactory {

	protected Map<ServiceInfoKey, IServiceInfo> serviceInfos = new HashMap();

	protected class ServiceInfoKey {
		private EndpointDescription endpointDescription;
		private Namespace discoveryNamespace;
		private int hashCode = 7;

		public ServiceInfoKey(EndpointDescription endpointDescription,
				Namespace discoveryNamespace) {
			this.endpointDescription = endpointDescription;
			this.discoveryNamespace = discoveryNamespace;
			this.hashCode = 31 * this.hashCode + endpointDescription.hashCode();
			this.hashCode = 31 * this.hashCode + discoveryNamespace.hashCode();
		}

		public boolean equals(Object other) {
			if (other == null)
				return false;
			if (!(other instanceof ServiceInfoKey))
				return false;
			ServiceInfoKey otherKey = (ServiceInfoKey) other;
			return (this.endpointDescription
					.equals(otherKey.endpointDescription) && this.discoveryNamespace
					.equals(otherKey.discoveryNamespace));
		}

		public int hashCode() {
			return hashCode;
		}
	}

	public IServiceInfo createServiceInfoForDiscovery(
			IDiscoveryAdvertiser advertiser,
			EndpointDescription endpointDescription) {
		try {
			Namespace advertiserNamespace = advertiser.getServicesNamespace();
			ServiceInfoKey key = new ServiceInfoKey(endpointDescription,
					advertiserNamespace);
			IServiceInfo existingServiceInfo = null;
			synchronized (serviceInfos) {
				existingServiceInfo = serviceInfos.get(key);
				// If it's already there, then we return null
				if (existingServiceInfo != null)
					return null;
				IServiceTypeID serviceTypeID = createServiceTypeID(
						endpointDescription, advertiser);
				String serviceName = createServiceName(endpointDescription,
						advertiser, serviceTypeID);
				URI uri = createURI(endpointDescription, advertiser,
						serviceTypeID, serviceName);
				IServiceProperties serviceProperties = createServiceProperties(
						endpointDescription, advertiser, serviceTypeID,
						serviceName, uri);
				IServiceInfo newServiceInfo = createServiceInfo(uri, serviceName, serviceTypeID, serviceProperties);
				// put into map using key
				serviceInfos.put(key, newServiceInfo);
				return newServiceInfo;
			}
		} catch (Exception e) {
			logError(
					"createServiceInfoForDiscovery",
					"Exception creating service info for endpointDescription="
							+ endpointDescription + ",advertiser=" + advertiser,
					e);
			return null;
		}
	}

	protected IServiceInfo createServiceInfo(URI uri, String serviceName, IServiceTypeID serviceTypeID, IServiceProperties serviceProperties) {
		return new ServiceInfo(uri, serviceName,
				serviceTypeID, serviceProperties);
	}
	
	protected IServiceProperties createServiceProperties(
			EndpointDescription endpointDescription,
			IDiscoveryAdvertiser advertiser, IServiceTypeID serviceTypeID,
			String serviceName, URI uri) {
		ServiceProperties result = new ServiceProperties();
		encodeServiceProperties(endpointDescription, result);
		return result;
	}

	protected URI createURI(EndpointDescription endpointDescription,
			IDiscoveryAdvertiser advertiser, IServiceTypeID serviceTypeID,
			String serviceName) throws URISyntaxException {
		String path = "/" + serviceName;
		String str = endpointDescription.getID().getName();
		URI uri = null;
		while (true) {
			try {
				uri = new URI(str);
				if (uri.getHost() != null) {
					break;
				} else {
					final String rawSchemeSpecificPart = uri
							.getRawSchemeSpecificPart();
					// make sure we break eventually
					if (str.equals(rawSchemeSpecificPart)) {
						uri = null;
						break;
					} else {
						str = rawSchemeSpecificPart;
					}
				}
			} catch (URISyntaxException e) {
				uri = null;
				break;
			}
		}
		String scheme = RemoteConstants.SERVICE_TYPE;
		int port = 32565;
		if (uri != null) {
			port = uri.getPort();
			if (port == -1)
				port = 32565;
		}
		String host = null;
		if (uri != null) {
			host = uri.getHost();
		} else {
			try {
				host = InetAddress.getLocalHost().getHostAddress();
			} catch (Exception e) {
				logInfo("createURI", //$NON-NLS-1$
						"failed to get local host adress, falling back to \'localhost\'.", e); //$NON-NLS-1$
				host = "localhost"; //$NON-NLS-1$
			}
		}
		return new URI(scheme, null, host, port, path, null, null);
	}

	protected String createServiceName(EndpointDescription endpointDescription,
			IDiscoveryAdvertiser advertiser, IServiceTypeID serviceTypeID) {
		// First create unique default name
		String defaultServiceName = createDefaultServiceName(
				endpointDescription, advertiser, serviceTypeID);
		// Look for service name that was explicitly set
		String serviceName = getStringWithDefault(
				endpointDescription.getProperties(),
				RemoteConstants.DISCOVERY_SERVICE_NAME, defaultServiceName);
		return serviceName;
	}

	protected String createDefaultServiceName(
			EndpointDescription endpointDescription,
			IDiscoveryAdvertiser advertiser, IServiceTypeID serviceTypeID) {
		return RemoteConstants.DISCOVERY_DEFAULT_SERVICE_NAME_PREFIX
				+ IDFactory.getDefault().createGUID().getName();
	}

	protected IServiceTypeID createServiceTypeID(
			EndpointDescription endpointDescription,
			IDiscoveryAdvertiser advertiser) {
		Map props = endpointDescription.getProperties();
		String[] scopes = getStringArrayWithDefault(props,
				RemoteConstants.DISCOVERY_SCOPE, IServiceTypeID.DEFAULT_SCOPE);
		String[] protocols = getStringArrayWithDefault(props,
				RemoteConstants.DISCOVERY_PROTOCOLS,
				IServiceTypeID.DEFAULT_SCOPE);
		String namingAuthority = getStringWithDefault(props,
				RemoteConstants.DISCOVERY_NAMING_AUTHORITY,
				IServiceTypeID.DEFAULT_NA);
		return ServiceIDFactory.getDefault().createServiceTypeID(
				advertiser.getServicesNamespace(),
				new String[] { RemoteConstants.SERVICE_TYPE }, scopes,
				protocols, namingAuthority);
	}

	public IServiceInfo removeServiceInfoForUndiscovery(
			IDiscoveryAdvertiser advertiser,
			EndpointDescription endpointDescription) {
		Namespace advertiserNamespace = advertiser.getServicesNamespace();
		ServiceInfoKey key = new ServiceInfoKey(endpointDescription,
				advertiserNamespace);
		synchronized (serviceInfos) {
			return serviceInfos.remove(key);
		}
	}

	public void close() {
		synchronized (serviceInfos) {
			serviceInfos.clear();
		}
		super.close();
	}
}

Back to the top