Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 386fc08f4747b116120a0e9a44d283348240066c (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
/*******************************************************************************
 * Copyright (c) 2009 EclipseSource 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:
 *   EclipseSource - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.osgi.services.discovery;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.discovery.identity.IServiceID;
import org.eclipse.ecf.internal.osgi.services.discovery.Activator;
import org.eclipse.ecf.internal.osgi.services.discovery.ServicePropertyUtils;
import org.osgi.service.discovery.ServicePublication;

public abstract class ECFServiceEndpointDescription implements
		IServiceEndpointDescription {

	protected Map serviceProperties;

	public ECFServiceEndpointDescription(Map properties) {
		this.serviceProperties = properties;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getEndpointID()
	 */
	public String getEndpointID() {
		Object o = serviceProperties
				.get(ServicePublication.PROP_KEY_ENDPOINT_ID);
		if (o instanceof String) {
			return (String) o;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.osgi.service.discovery.ServiceEndpointDescription#
	 * getEndpointInterfaceName(java.lang.String)
	 */
	public String getEndpointInterfaceName(String interfaceName) {
		if (interfaceName == null)
			return null;
		Object o = serviceProperties
				.get(ServicePublication.PROP_KEY_ENDPOINT_INTERFACE_NAME);
		if (o == null || !(o instanceof String)) {
			return null;
		}
		String intfNames = (String) o;
		Collection c = ServicePropertyUtils
				.createCollectionFromString(intfNames);
		if (c == null)
			return null;
		for (Iterator i = c.iterator(); i.hasNext();) {
			String intfName = (String) i.next();
			if (intfName != null && intfName.startsWith(interfaceName)) {
				// return just endpointInterfaceName
				return intfName
						.substring(
								intfName.length()
										+ ServicePropertyUtils.ENDPOINT_INTERFACE_NAME_SEPARATOR
												.length()).trim();
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.service.discovery.ServiceEndpointDescription#getLocation()
	 */
	public URL getLocation() {
		Object o = serviceProperties
				.get(ServicePublication.PROP_KEY_ENDPOINT_LOCATION);
		if (o == null || !(o instanceof String)) {
			return null;
		}
		String urlExternalForm = (String) o;
		URL url = null;
		try {
			url = new URL(urlExternalForm);
		} catch (MalformedURLException e) {
			Activator.getDefault()
					.log(
							new Status(IStatus.ERROR, Activator.PLUGIN_ID,
									IStatus.ERROR,
									"Exception getting location URL", e));//$NON-NLS-1$
		}
		return url;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getProperties()
	 */
	public Map getProperties() {
		return serviceProperties;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getProperty(java
	 * .lang.String)
	 */
	public Object getProperty(String key) {
		return serviceProperties.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getPropertyKeys()
	 */
	public Collection getPropertyKeys() {
		return serviceProperties.keySet();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getProvidedInterfaces
	 * ()
	 */
	public Collection getProvidedInterfaces() {
		Object o = serviceProperties
				.get(ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME);
		if (o == null || !(o instanceof String)) {
			throw new NullPointerException();
		}
		final String providedInterfacesStr = (String) o;
		return ServicePropertyUtils
				.createCollectionFromString(providedInterfacesStr);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.service.discovery.ServiceEndpointDescription#getVersion(java
	 * .lang.String)
	 */
	public String getVersion(String interfaceName) {
		Collection c = getProvidedInterfaces();
		if (c == null) {
			return null;
		}
		for (Iterator i = c.iterator(); i.hasNext();) {
			String intfName = (String) i.next();
			if (intfName != null && intfName.startsWith(interfaceName)) {
				// return just version string
				return intfName
						.substring(
								intfName.length()
										+ ServicePropertyUtils.INTERFACE_VERSION_SEPARATOR
												.length()).trim();
			}
		}
		return null;
	}

	public long getRemoteServiceId() {
		Long rsId = (Long) getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_ID);
		if (rsId == null)
			return 0L;
		else
			return rsId.longValue();
	}

	public abstract ID getECFEndpointID();

	public abstract ID getECFTargetID();

	public abstract IServiceID getServiceID();

	public String getECFRemoteServicesFilter() {
		Object o = serviceProperties
				.get(IServicePublication.PROP_KEY_REMOTE_SERVICE_FILTER);
		if (o instanceof String)
			return (String) o;
		return null;
	}

	public void setProperties(Map props) {
		if (props != null)
			this.serviceProperties = props;
	}

}

Back to the top