Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b84f99c20e495c8c09d79642f4c9873ca923ff99 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.activator.CoreBundleActivator;

/**
 * Abstract service manager implementation.
 */
public abstract class AbstractServiceManager<V extends IService> {

	// map for all services per id
	private Map<String, List<ServiceProxy>> services = new HashMap<String, List<ServiceProxy>>();

	/**
	 * Proxy to provide lazy loading of contributing plug-ins.
	 */
	protected class ServiceProxy {

		private IConfigurationElement configElement = null;
		public String clazz;
		private V service = null;
		private List<Class<? extends V>> serviceTypes = new ArrayList<Class<? extends V>>();

		/**
		 * Constructor.
		 */
		protected ServiceProxy(IConfigurationElement configElement) {
			Assert.isNotNull(configElement);
			this.configElement = configElement;

			// Read the class attribute. If null, check for the class sub element
			clazz = configElement.getAttribute("class"); //$NON-NLS-1$
			if (clazz == null) {
				IConfigurationElement[] children = configElement.getChildren("class"); //$NON-NLS-1$
				// Single element definition assumed (see extension point schema)
				if (children.length > 0) {
					clazz = children[0].getAttribute("class"); //$NON-NLS-1$
				}
			}
		}

		/**
		 * Add a type to the proxy. Types are used unless the proxy is instantiated to provide lazy
		 * loading of services. After instantiated, a service will be identified only by its type
		 * and implementing or extending interfaces or super-types.
		 *
		 * @param serviceType The type to add.
		 */
		public void addType(Class<? extends V> serviceType) {
			Assert.isNotNull(serviceType);
			if (service == null && serviceTypes != null && !serviceTypes.contains(serviceType)) {
				serviceTypes.add(serviceType);
			}
		}

		/**
		 * Return the real service instance for this proxy.
		 */
		@SuppressWarnings("unchecked")
		protected V getService(boolean unique) {
			if ((service == null || unique) && configElement != null) {
				try {
					// Create the service class instance via the configuration element
					Object service = configElement.createExecutableExtension("class"); //$NON-NLS-1$
					if (service instanceof IService) {
						if (unique) {
							return (V) service;
						}
						else if (service instanceof IService) {
							this.service = (V)service;
						}
						else {
							IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), "Service '" + service.getClass().getName() + "' not of type IService."); //$NON-NLS-1$ //$NON-NLS-2$
							Platform.getLog(CoreBundleActivator.getContext().getBundle())
							                .log(status);
						}
					}
				}
				catch (CoreException e) {
					IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), "Cannot create service '" + clazz + "'.", e); //$NON-NLS-1$ //$NON-NLS-2$
					Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
				}
				if (serviceTypes != null) {
					serviceTypes.clear();
				}
				serviceTypes = null;
			}
			return service;
		}

		/**
		 * Check whether this proxy holds a service that is suitable for the given type.
		 *
		 * @param serviceType The service type
		 * @return
		 */
		protected boolean isMatching(Class<? extends V> serviceType) {
			if (service != null) {
				return serviceType.isInstance(service);
			}
			else if (configElement != null) {
				if (serviceType.getClass().getName().equals(clazz)) {
					return true;
				}
				for (Class<? extends V> type : serviceTypes) {
					if (type.equals(serviceType)) {
						return true;
					}
				}
			}
			return false;
		}

		public boolean equals(V service) {
			return clazz.equals(service.getClass());
		}

		public boolean equals(ServiceProxy proxy) {
			return clazz.equals(proxy.clazz);
		}
	}

	/**
	 * Constructor.
	 */
	protected AbstractServiceManager() {
		loadServices();
	}

	/**
	 * @param element
	 * @return
	 */
	protected ServiceProxy getServiceProxy(IConfigurationElement element) {
		return new ServiceProxy(element);
	}

	/**
	 * Returns all id's of the registered services.
	 *
	 * @return The list of id's of the registered services.
	 */
	public String[] getIds() {
		return services.keySet().toArray(new String[services.keySet().size()]);
	}

	/**
	 * Get a service for the id that implements at least the needed service type. If an interface
	 * type is given, the service with the highest implementation is returned. This may result in a
	 * random selection depending on the extension registration order, especially when a service
	 * interface is implemented two times in different hierarchy paths. If a class type is given, if
	 * available, the service of exactly that class is returned. Otherwise the highest
	 * implementation is returned.
	 *
	 * @param id The id for which a service is needed.
	 * @param serviceType The service type the service should at least implement or extend.
	 * @return The service or <code>null</code>.
	 */
	public V getService(String id, Class<? extends V> serviceType) {
		return getService(id, serviceType, false);
	}

	/**
	 * Get a service for the id that implements at least the needed service type. If an interface
	 * type is given, the service with the highest implementation is returned. This may result in a
	 * random selection depending on the extension registration order, especially when a service
	 * interface is implemented two times in different hierarchy paths. If a class type is given, if
	 * available, the service of exactly that class is returned. Otherwise the highest
	 * implementation is returned.
	 *
	 * @param id The id for which a service is needed.
	 * @param serviceType The service type the service should at least implement or extend.
	 * @param unique <code>true</code> if a new instance of the service is needed.
	 *
	 * @return The service or <code>null</code>.
	 */
	public V getService(String id, Class<? extends V> serviceType, boolean unique) {
		Assert.isNotNull(serviceType);
		if (id == null) {
			id = ""; //$NON-NLS-1$
		}
		List<ServiceProxy> proxies = services.get(id);
		if (proxies != null && !proxies.isEmpty()) {
			List<ServiceProxy> candidates = new ArrayList<ServiceProxy>();
			boolean isInterface = serviceType.isInterface();
			for (ServiceProxy proxy : proxies) {
				if (proxy.isMatching(serviceType)) {
					if (!isInterface && proxy.equals(serviceType)) {
						V service = proxy.getService(unique);
						service.setId(id);
						return service;
					}
					candidates.add(proxy);
				}
			}
			V service = null;
			if (!candidates.isEmpty()) {
				service = candidates.get(0).getService(unique);
				service.setId(id);
			}

			return service;
		}
		return null;
	}

	/**
	 * Get a service list for the id that implements at least the needed service type.
	 *
	 * @param id The id for which a service is needed.
	 * @param serviceType The service type the service should at least implement or extend.
	 * @param unique <code>true</code> if a new instance of the service is needed.
	 * @return The service list or empty list.
	 */
	public IService[] getServices(String id, Class<? extends V> serviceType, boolean unique) {
		Assert.isNotNull(serviceType);
		if (id == null) {
			id = ""; //$NON-NLS-1$
		}
		List<ServiceProxy> proxies = services.get(id);
		List<IService> services = new ArrayList<IService>();
		if (proxies != null && !proxies.isEmpty()) {
			List<ServiceProxy> candidates = new ArrayList<ServiceProxy>();
			for (ServiceProxy proxy : proxies) {
				if (proxy.isMatching(serviceType)) {
					candidates.add(proxy);
				}
			}
			for (ServiceProxy serviceProxy : candidates) {
				IService service = serviceProxy.getService(unique);
				service.setId(id);
				services.add(service);
			}
		}
		return services.toArray(new IService[services.size()]);
	}

	/*
	 * Add a service proxy to the list of available services.
	 */
	protected boolean addService(String id, ServiceProxy proxy) {
		Assert.isNotNull(services);
		Assert.isNotNull(id);
		Assert.isNotNull(proxy);

		List<ServiceProxy> proxies = services.get(id);
		if (proxies == null) {
			proxies = new ArrayList<ServiceProxy>();
			services.put(id, proxies);
		}
		Assert.isNotNull(proxies);
		if (proxies.isEmpty() || !proxies.contains(proxy)) {
			return proxies.add(proxy);
		}
		return false;
	}

	/**
	 * Loads the contributed services into proxies (lazy loading!!) and adds them to this manager;
	 */
	protected abstract void loadServices();
}

Back to the top