Skip to main content
summaryrefslogtreecommitdiffstats
blob: abf5d74060d9cae3af9418c44d915a371882281f (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package org.eclipse.ecf.osgi.services.remoteserviceadmin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.discovery.IServiceProperties;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.Activator;

public abstract class AbstractMetadataFactory {

	protected static final String COLLECTION_SEPARATOR = ",";
	protected static final List standardProperties = Arrays
	.asList(new String[] {
			// OSGi properties
			org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID,
			org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_SERVICE_ID,
			org.osgi.framework.Constants.OBJECTCLASS,
			org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID,
			org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS,
			org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_INTENTS,
			// ECF properties
			RemoteConstants.ENDPOINT_ID,
			RemoteConstants.ENDPOINT_ID_NAMESPACE,
			RemoteConstants.ENDPOINT_TARGET_ID,
			RemoteConstants.ENDPOINT_TARGET_ID_NAMESPACE,
			RemoteConstants.ENDPOINT_IDFILTER_IDS,
			RemoteConstants.ENDPOINT_IDFILTER_NAMESPACES,
			RemoteConstants.ENDPOINT_REMOTESERVICE_ID,
			RemoteConstants.ENDPOINT_REMOTESERVICEFILTER
	});


	protected String[] getStringArrayWithDefault(
			Map<String, Object> properties, String key, String[] def) {
		if (properties == null)
			return def;
		Object o = properties.get(key);
		if (o instanceof String) {
			return new String[] { (String) o };
		} else if (o instanceof String[]) {
			return (String[]) o;
		} else
			return def;
	}

	protected String getStringWithDefault(Map props, String key, String def) {
		if (props == null)
			return def;
		Object o = props.get(key);
		if (o == null || (!(o instanceof String)))
			return def;
		return (String) o;
	}

	protected void encodeString(IServiceProperties props, String name,
			String value) {
		byte[] bytes = value.getBytes();
		props.setPropertyBytes(name, bytes);
	}

	protected String decodeString(IServiceProperties props, String name) {
		byte[] bytes = props.getPropertyBytes(name);
		if (bytes == null)
			return null;
		return new String(bytes);
	}

	protected void encodeLong(IServiceProperties result, String name, Long value) {
		result.setPropertyString(name, value.toString());
	}

	protected Long decodeLong(IServiceProperties props, String name) {
		String longAsString = props.getPropertyString(name);
		if (longAsString == null)
			return new Long(0);
		return new Long(longAsString);
	}

	protected void encodeList(IServiceProperties props, String name,
			List<String> value) {
		final StringBuffer result = new StringBuffer();
		for (final Iterator<String> i = value.iterator(); i.hasNext();) {
			final String item = (String) i.next();
			result.append(item);
			if (i.hasNext()) {
				result.append(COLLECTION_SEPARATOR);
			}
		}
		// Now add to props
		props.setPropertyString(name, result.toString());
	}

	protected List decodeList(IServiceProperties props, String name) {
		String value = props.getPropertyString(name);
		if (value == null)
			return Collections.EMPTY_LIST;
		List result = new ArrayList();
		final StringTokenizer t = new StringTokenizer(value,
				COLLECTION_SEPARATOR);
		while (t.hasMoreTokens()) {
			result.add(t.nextToken());
		}
		return result;
	}

	protected void encodeIDArray(IServiceProperties result, String idsname,
			String idnamespaces, ID[] idFilter) {
		List<String> idef = new ArrayList<String>();
		List<String> idns = new ArrayList<String>();
		for (int i = 0; i < idFilter.length; i++) {
			idef.add(idFilter[i].toExternalForm());
			idns.add(idFilter[i].getNamespace().getName());
		}
		encodeList(result, idsname, idef);
		encodeList(result, idnamespaces, idns);
	}

	protected ID[] decodeIDArray(IServiceProperties props, String idsname,
			String idnamespaces) {
		String idef = props.getPropertyString(idsname);
		if (idef == null)
			return null;
		String idns = props.getPropertyString(idnamespaces);
		if (idns == null)
			return null;
		List<String> idsl = decodeList(props, idef);
		List<String> idnsl = decodeList(props, idns);
		List result = new ArrayList();
		for (int i = 0; i < idsl.size(); i++) {
			ID id = createID(idnsl.get(i), idsl.get(i));
			if (id != null)
				result.add(id);
		}
		return (ID[]) result.toArray(new ID[] {});
	}

	protected ID createID(String namespace, String name) {
		try {
			return IDFactory.getDefault().createID(namespace, name);
		} catch (IDCreateException e) {
			logError("createID", "Exception creating id for namespace="
					+ namespace + ",name=" + name, e);
			return null;
		}
	}

	protected Map decodeServiceProperties(IServiceProperties props) {
		Map result = new HashMap();
		// OSGI
		// endpoint.id
		String endpointId = decodeString(props,org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID);
		result.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID, endpointId);
		// endpoint.service.id
		Long endpointServiceId = decodeLong(props, org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_SERVICE_ID);
		result.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_SERVICE_ID, endpointServiceId);
		// objectClass
		List<String> interfaces = decodeList(props,org.osgi.framework.Constants.OBJECTCLASS);
		result.put(org.osgi.framework.Constants.OBJECTCLASS, (String[]) interfaces.toArray(new String[] {}));
		// framework uuid
		String fwkuuid = decodeString(props,org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID);
		result.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID, fwkuuid);
		// configuration types
		List<String> configTypes = decodeList(props,org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS);
		if (configTypes != null) result.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS, (String[]) configTypes.toArray(new String[] {}));
		// service intents
		List<String> intents = decodeList(props,org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_INTENTS);
		if (configTypes != null) result.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_INTENTS, (String[]) intents.toArray(new String[] {}));

		// endpoint ID
		String endpointName = decodeString(props,RemoteConstants.ENDPOINT_ID);
		String endpointNamespace = decodeString(props,RemoteConstants.ENDPOINT_ID_NAMESPACE);
		ID endpointID = createID(endpointName,endpointNamespace);
		if (endpointID != null) {
			result.put(RemoteConstants.ENDPOINT_ID,endpointID);
		}
		// remote service id
		Long remoteServiceId = decodeLong(props,RemoteConstants.ENDPOINT_REMOTESERVICE_ID);
		result.put(RemoteConstants.ENDPOINT_REMOTESERVICE_ID, remoteServiceId);
		// target ID
		String targetName = decodeString(props,RemoteConstants.ENDPOINT_TARGET_ID);
		String targetNamespace = decodeString(props,RemoteConstants.ENDPOINT_ID_NAMESPACE);
		ID targetID = createID(targetName,targetNamespace);
		if (targetID != null) {
			result.put(RemoteConstants.ENDPOINT_TARGET_ID,targetID);
		}
		// ID filter
		ID[] idFilter = decodeIDArray(props, RemoteConstants.ENDPOINT_IDFILTER_IDS, RemoteConstants.ENDPOINT_IDFILTER_NAMESPACES);
		if (idFilter != null) {
			result.put(RemoteConstants.ENDPOINT_IDFILTER_IDS, idFilter);
		}
		// remote service filter
		String remoteServiceFilter = decodeString(props,RemoteConstants.ENDPOINT_REMOTESERVICEFILTER);
		if (remoteServiceFilter != null) {
			result.put(RemoteConstants.ENDPOINT_REMOTESERVICEFILTER, remoteServiceFilter);
		}
		decodeNonStandardServiceProperties(props,result);
		return result;
	}
	
	protected void encodeServiceProperties(
			EndpointDescription endpointDescription, IServiceProperties result) {
		// OSGi service properties
		// endpoint.id == endpointDescription.getId()
		String endpointId = endpointDescription.getId();
		encodeString(
				result,
				org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID,
				endpointId);

		// OSGi endpoint.service.id = endpointDescription.getServiceId()
		long endpointServiceId = endpointDescription.getServiceId();
		encodeLong(
				result,
				org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_SERVICE_ID,
				new Long(endpointServiceId));

		// OSGi objectClass = endpointDescription.getInterfaces();
		List<String> interfaces = endpointDescription.getInterfaces();
		encodeList(result, org.osgi.framework.Constants.OBJECTCLASS, interfaces);

		// OSGi frameworkUUID = endpointDescription.getFrameworkUUID()
		String frameworkUUID = endpointDescription.getFrameworkUUID();
		if (frameworkUUID == null) {
			frameworkUUID = Activator.getDefault().getFrameworkUUID();
		}
		if (frameworkUUID != null)
			encodeString(
					result,
					org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID,
					frameworkUUID);
		// OSGi configuration types =
		// endpointDescription.getConfigurationTypes();
		List<String> configurationTypes = endpointDescription
				.getConfigurationTypes();
		if (configurationTypes.size() > 0) {
			encodeList(
					result,
					org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS,
					configurationTypes);
		}
		// OSGI service intents = endpointDescription.getIntents()
		List<String> serviceIntents = endpointDescription.getIntents();
		if (serviceIntents.size() > 0) {
			encodeList(
					result,
					org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_INTENTS,
					serviceIntents);
		}

		// ECF endpoint ID = endpointDescription.getID()
		ID endpointID = endpointDescription.getID();
		// external form of ID
		encodeString(result, RemoteConstants.ENDPOINT_ID,
				endpointID.toExternalForm());
		// namespace
		encodeString(result, RemoteConstants.ENDPOINT_ID_NAMESPACE, endpointID
				.getNamespace().getName());
		// ECF remote service id = endpointDescription.getRemoteServiceId()
		long remoteServiceId = endpointDescription.getRemoteServiceId();
		encodeLong(result, RemoteConstants.ENDPOINT_REMOTESERVICE_ID, new Long(
				remoteServiceId));
		// ECF connectTargetID = endpointDescription.getConnectTargetID()
		ID connectTargetID = endpointDescription.getTargetID();
		if (connectTargetID != null) {
			// external form of ID
			encodeString(result, RemoteConstants.ENDPOINT_TARGET_ID,
					connectTargetID.toExternalForm());
			// namespace
			encodeString(result, RemoteConstants.ENDPOINT_TARGET_ID_NAMESPACE,
					connectTargetID.getNamespace().getName());
		}
		// ECF idFilter = endpointDescription.getIDFilter();
		ID[] idFilter = endpointDescription.getIDFilter();
		if (idFilter != null && idFilter.length > 0) {
			encodeIDArray(result, RemoteConstants.ENDPOINT_IDFILTER_IDS,
					RemoteConstants.ENDPOINT_IDFILTER_NAMESPACES, idFilter);
		}

		// ECF remote service filter =
		// endpointDescription.getRemoteServiceFilter()
		String remoteFilter = endpointDescription.getRemoteServiceFilter();
		if (remoteFilter != null) {
			encodeString(result, RemoteConstants.ENDPOINT_REMOTESERVICEFILTER,
					remoteFilter);
		}
		// encode non standar properties
		encodeNonStandardServiceProperties(endpointDescription.getProperties(),
				result);
	}

	protected void encodeNonStandardServiceProperties(
			Map<String, Object> properties, IServiceProperties result) {
		for (String key : properties.keySet()) {
			if (!standardProperties.contains(key)) {
				Object val = properties.get(key);
				if (val instanceof byte[]) {
					result.setPropertyBytes(key, (byte[]) val);
				} else if (val instanceof String) {
					result.setPropertyString(key, (String) val);
				} else {
					result.setProperty(key, val);
				}
			}
		}
	}

	protected void decodeNonStandardServiceProperties(IServiceProperties props, Map<String, Object> result) {
		for(Enumeration keys=props.getPropertyNames(); keys.hasMoreElements(); ) {
			String key = (String) keys.nextElement();
			if (!standardProperties.contains(key)) {
				byte[] bytes = props.getPropertyBytes(key);
				if (bytes != null) {
					result.put(key, bytes);
					continue;
				}
				String str = props.getPropertyString(key);
				if (str != null) {
					result.put(key, str);
					continue;
				}
				Object obj = props.getProperty(key);
				if (obj != null) {
					result.put(key,obj);
					continue;
				}
			}
		}
	}

	protected void logInfo(String methodName, String message, Throwable t) {
		// XXX todo
	}

	protected void logError(String methodName, String message, Throwable t) {
		// XXX todo
	}

	public void close() {
		// nothing to do
	}
}

Back to the top