Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97380c6b0e6cc124f98281605524de14432ee6ed (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.device;

import org.eclipse.osgi.util.NLS;
import org.osgi.framework.*;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * DeviceManager bundle. This bundle implements the OSGi Device Access 1.1
 * specification.
 *
 * This implementation does not include the optimizations in section
 * 8.7.4 of the OSGi SP R2 spec.
 *
 */
public class Activator implements BundleActivator, ServiceTrackerCustomizer, FrameworkListener, Runnable {
	protected final static boolean DEBUG = false;

	/** DeviceManager BundleContext */
	protected BundleContext context;

	/** LogTracker object */
	protected LogTracker log;

	/** if false the thread must terminate */
	protected volatile boolean running;

	/** DeviceManager thread */
	protected Thread thread;

	/** DriverTracker for Driver services. */
	protected DriverTracker drivers;

	/** Tracker for DriverLocator services */
	protected DriverLocatorTracker locators;

	/** Tracker for DriverSelector services */
	protected DriverSelectorTracker selectors;

	/** ServiceTracker object for device services */
	protected ServiceTracker devices;

	/** filter for Device services */
	protected Filter deviceFilter;

	/** filter for Driver services */
	protected Filter driverFilter;

	/**
	 * Linked List item
	 */
	static class DeviceService {
		/** object for this item */
		final DeviceTracker device;
		/** next item in event queue */
		DeviceService next;

		/**
		 * Constructor for work queue item
		 *
		 * @param o Object for this event
		 */
		DeviceService(DeviceTracker device) {
			this.device = device;
			next = null;
		}
	}

	/** item at the head of the event queue */
	private DeviceService head;
	/** item at the tail of the event queue */
	private DeviceService tail;

	/** number of milliseconds to wait before refining idle Device services */
	protected long updatewait;

	/** set to true by DriverTracker when a Driver Service is registered */
	protected volatile boolean driverServiceRegistered;

	/**
	 * Create a DeviceManager object.
	 *
	 */

	public Activator() {
		super();
	}

	/**
	 * Start the Device Manager.
	 *
	 * @param context The device manager's bundle context
	 */

	public void start(BundleContext contxt) throws Exception {
		this.context = contxt;
		running = false;

		log = new LogTracker(context, System.err);

		try {
			deviceFilter = context.createFilter("(|(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + DeviceTracker.clazz + ////-1$ ////-2$ //$NON-NLS-1$ //$NON-NLS-2$
					")(" + org.osgi.service.device.Constants.DEVICE_CATEGORY + "=*))"); //$NON-NLS-1$ //$NON-NLS-2$

			driverFilter = context.createFilter("(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + DriverTracker.clazz + ")"); ////-1$ ////-2$ ////-3$ //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		} catch (InvalidSyntaxException e) {
			log.log(LogService.LOG_ERROR, NLS.bind(DeviceMsg.Unable_to_create_Filter_for_DeviceManager, e)); ////-1$
			throw e;
		}

		updatewait = 5 * 1000L;

		String prop = context.getProperty("org.eclipse.equinox.device.updatewait"); //$NON-NLS-1$

		if (prop != null) {
			try {
				updatewait = Long.parseLong(prop) * 1000L;
			} catch (NumberFormatException e) {
				//do nothing
			}
		}

		Bundle systemBundle = context.getBundle(0);

		if ((systemBundle != null) && ((systemBundle.getState() & Bundle.STARTING) != 0)) { /* if the system bundle is starting */
			context.addFrameworkListener(this);
		} else {
			startDeviceManager();
		}

		log.log(LogService.LOG_INFO, DeviceMsg.DeviceManager_started);
	}

	/**
	 * Receive notification of a general framework event.
	 *
	 * @param event The FrameworkEvent.
	 */
	public void frameworkEvent(FrameworkEvent event) {
		switch (event.getType()) {
			case FrameworkEvent.STARTED : {
				context.removeFrameworkListener(this);

				try {
					startDeviceManager();
				} catch (Throwable t) {
					log.log(LogService.LOG_ERROR, NLS.bind(DeviceMsg.DeviceManager_has_thrown_an_error, t)); ////-1$
				}

				break;
			}
		}
	}

	/**
	 * Start the DeviceManager thread.
	 *
	 */
	public void startDeviceManager() {
		if (!running) {
			head = null;
			tail = null;

			locators = new DriverLocatorTracker(this);

			selectors = new DriverSelectorTracker(this);

			drivers = new DriverTracker(this);

			devices = new ServiceTracker(context, deviceFilter, this);
			devices.open();

			running = true;
			driverServiceRegistered = false;

			thread = (new SecureAction()).createThread(this, "DeviceManager"); //$NON-NLS-1$
			thread.start(); /* Start DeviceManager thread */
		}
	}

	/**
	 * Stop the Device Manager bundle.
	 *
	 * @param context The device manager's bundle context
	 */

	public void stop(BundleContext contxt) throws Exception {
		context.removeFrameworkListener(this);

		if (running) {
			Thread t = thread;

			running = false; /* request thread to stop */

			if (t != null) {
				t.interrupt();

				synchronized (t) {
					while (t.isAlive()) /* wait for thread to complete */
					{
						try {
							t.wait(0);
						} catch (InterruptedException e) {
							//	do nothing
						}
					}
				}
			}
		}

		if (drivers != null) {
			drivers.close();
			drivers = null;
		}

		if (devices != null) {
			devices.close();
			devices = null;
		}

		if (locators != null) {
			locators.close();
			locators = null;
		}

		if (selectors != null) {
			selectors.close();
			selectors = null;
		}

		if (log != null) {
			log.close();
			log = null;
		}

		this.context = null;
	}

	/**
	 * A service is being added to the ServiceTracker.
	 *
	 * <p>This method is called before a service which matched
	 * the search parameters of the ServiceTracker is
	 * added to the ServiceTracker. This method should return the
	 * service object to be tracked for this ServiceReference.
	 * The returned service object is stored in the ServiceTracker
	 * and is available from the getService and getServices
	 * methods.
	 *
	 * @param reference Reference to service being added to the ServiceTracker.
	 * @return The service object to be tracked for the
	 * ServiceReference or <tt>null</tt> if the ServiceReference should not
	 * be tracked.
	 */
	public Object addingService(ServiceReference reference) {
		if (Activator.DEBUG) {
			this.log.log(reference, LogService.LOG_DEBUG, "DeviceManager device service registered"); //$NON-NLS-1$
		}

		enqueue(new DeviceTracker(this, reference));

		return (reference);
	}

	/**
	 * A service tracked by the ServiceTracker has been modified.
	 *
	 * <p>This method is called when a service being tracked
	 * by the ServiceTracker has had it properties modified.
	 *
	 * @param reference Reference to service that has been modified.
	 * @param service The service object for the modified service.
	 */
	public void modifiedService(ServiceReference reference, Object service) {
		// do nothing
	}

	/**
	 * A service tracked by the ServiceTracker is being removed.
	 *
	 * <p>This method is called after a service is no longer being tracked
	 * by the ServiceTracker.
	 *
	 * @param reference Reference to service that has been removed.
	 * @param service The service object for the removed service.
	 */
	public void removedService(ServiceReference reference, Object object) {
		if (Activator.DEBUG) {
			log.log(reference, LogService.LOG_DEBUG, "DeviceManager device service unregistered"); //$NON-NLS-1$
		}

		/* We do not implement optional driver reclamation.
		 * Thus we take no specific action upon Device service unregistration .
		 */
	}

	public void refineIdleDevices() {
		if (Activator.DEBUG) {
			log.log(LogService.LOG_DEBUG, "DeviceManager refining idle device services"); //$NON-NLS-1$
		}

		ServiceReference[] references = devices.getServiceReferences();

		if (references != null) {
			int size = references.length;

			for (int i = 0; i < size; i++) {
				ServiceReference device = references[i];

				enqueue(new DeviceTracker(this, device));
			}
		}
	}

	/**
	 * Main thread for DeviceManager.
	 *
	 * Attempt to refine all Device services that are not in use
	 * by a driver bundle.
	 */
	public void run() {
		while (running) {
			DeviceTracker device;

			try {
				device = dequeue();
			} catch (InterruptedException e) {
				continue;
			}

			try {
				device.refine();
			} catch (Throwable t) {
				log.log(LogService.LOG_ERROR, NLS.bind(DeviceMsg.DeviceManager_has_thrown_an_error, t)); ////-1$
			}
		}
	}

	/**
	 * Queue the object to be processed on the work thread.
	 * The thread is notified.
	 *
	 * @param device Work item.
	 */
	public synchronized void enqueue(DeviceTracker device) {
		if (device != null) {
			if (Activator.DEBUG) {
				log.log(LogService.LOG_DEBUG, "DeviceManager queuing DeviceTracker"); //$NON-NLS-1$
			}

			DeviceService item = new DeviceService(device);

			if (head == null) /* if the queue was empty */
			{
				head = item;
				tail = item;
			} else /* else add to end of queue */
			{
				tail.next = item;
				tail = item;
			}
		}

		notify();
	}

	/**
	 * Dequeue an object from the work thread.
	 * If the queue is empty, this method blocks.
	 *
	 * @return Dequeue object from the work thread.
	 * @throws InterruptedException If the queue has been stopped.
	 */
	private synchronized DeviceTracker dequeue() throws InterruptedException {
		while (running && (head == null)) {
			// TODO need to determine if this code is needed (bug 261197)
			/* This should be included per Section 8.7.7 of the OSGi SP R2
			 * spec, but it causes the OSGi SP R2 Test Suite to fail.
			 * We should turn this on for R3.

			 if (driverServiceRegistered)
			 */
			//			if (false) {
			//				driverServiceRegistered = false;
			//
			//				refineIdleDevices();
			//			} else {
			locators.uninstallDriverBundles();

			try {
				if (Activator.DEBUG) {
					log.log(LogService.LOG_DEBUG, "DeviceManager waiting on queue"); //$NON-NLS-1$
				}

				wait();
			} catch (InterruptedException e) {
				// do nothing
			}
			//			}
		}

		if (!running) /* if we are stopping */
		{
			throw new InterruptedException(); /* throw an exception */
		}

		DeviceService item = head;
		head = item.next;
		if (head == null) {
			tail = null;
		}

		return (item.device);
	}
}

Back to the top