86
|
1 /*
|
|
2 * $Header: /cvshome/build/org.osgi.util.tracker/src/org/osgi/util/tracker/ServiceTracker.java,v 1.22 2006/07/20 16:14:43 hargrave Exp $
|
105
|
3 *
|
86
|
4 * Copyright (c) OSGi Alliance (2000, 2007). All Rights Reserved.
|
105
|
5 *
|
86
|
6 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
7 * you may not use this file except in compliance with the License.
|
|
8 * You may obtain a copy of the License at
|
|
9 *
|
|
10 * http://www.apache.org/licenses/LICENSE-2.0
|
|
11 *
|
|
12 * Unless required by applicable law or agreed to in writing, software
|
|
13 * distributed under the License is distributed on an "AS IS" BASIS,
|
|
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15 * See the License for the specific language governing permissions and
|
|
16 * limitations under the License.
|
|
17 */
|
|
18
|
105
|
19 // Port to the D programming language:
|
|
20 // Frank Benoit <benoit@tionex.de>
|
86
|
21 module org.osgi.util.tracker.ServiceTracker;
|
|
22
|
|
23 import java.lang.all;
|
105
|
24
|
|
25 import org.osgi.util.tracker.ServiceTrackerCustomizer; // packageimport
|
|
26
|
86
|
27 import java.util.ArrayList;
|
|
28 import java.util.Enumeration;
|
|
29 import java.util.Hashtable;
|
|
30 import java.util.LinkedList;
|
|
31
|
105
|
32 import org.eclipse.osgi.framework.internal.core.FilterImpl;
|
|
33 import org.osgi.framework.AllServiceListener;
|
86
|
34 import org.osgi.framework.BundleContext;
|
105
|
35 import org.osgi.framework.Constants;
|
86
|
36 import org.osgi.framework.Filter;
|
105
|
37 import org.osgi.framework.InvalidSyntaxException;
|
86
|
38 import org.osgi.framework.ServiceEvent;
|
|
39 import org.osgi.framework.ServiceListener;
|
|
40 import org.osgi.framework.ServiceReference;
|
|
41
|
|
42 /**
|
|
43 * The <code>ServiceTracker</code> class simplifies using services from the
|
|
44 * Framework's service registry.
|
|
45 * <p>
|
|
46 * A <code>ServiceTracker</code> object is constructed with search criteria
|
|
47 * and a <code>ServiceTrackerCustomizer</code> object. A
|
|
48 * <code>ServiceTracker</code> object can use the
|
|
49 * <code>ServiceTrackerCustomizer</code> object to customize the service
|
|
50 * objects to be tracked. The <code>ServiceTracker</code> object can then be
|
|
51 * opened to begin tracking all services in the Framework's service registry
|
|
52 * that match the specified search criteria. The <code>ServiceTracker</code>
|
|
53 * object correctly handles all of the details of listening to
|
|
54 * <code>ServiceEvent</code> objects and getting and ungetting services.
|
|
55 * <p>
|
|
56 * The <code>getServiceReferences</code> method can be called to get
|
|
57 * references to the services being tracked. The <code>getService</code> and
|
|
58 * <code>getServices</code> methods can be called to get the service objects
|
|
59 * for the tracked service.
|
|
60 * <p>
|
|
61 * The <code>ServiceTracker</code> class is thread-safe. It does not call a
|
|
62 * <code>ServiceTrackerCustomizer</code> object while holding any locks.
|
|
63 * <code>ServiceTrackerCustomizer</code> implementations must also be
|
|
64 * thread-safe.
|
105
|
65 *
|
86
|
66 * @ThreadSafe
|
|
67 * @version $Revision: 1.29 $
|
|
68 */
|
|
69
|
|
70 /*
|
|
71 * This implementation has been customized to only work with the Equinox OSGi framework.
|
|
72 * In all cases a filter string containing objectClass is passed to BundleContext.addServiceListener
|
|
73 * to take advantage of the ServiceEvent delivery optimizations.
|
|
74 */
|
|
75
|
|
76 public class ServiceTracker : ServiceTrackerCustomizer {
|
105
|
77 /* set this to true to compile in_ debug_ messages */
|
|
78 static final bool DEBUG = false;
|
86
|
79 /**
|
|
80 * Bundle context against which this <code>ServiceTracker</code> object is
|
|
81 * tracking.
|
|
82 */
|
|
83 protected final BundleContext context;
|
|
84 /**
|
|
85 * Filter specifying search criteria for the services to track.
|
105
|
86 *
|
86
|
87 * @since 1.1
|
|
88 */
|
|
89 protected final Filter filter;
|
|
90 /**
|
|
91 * <code>ServiceTrackerCustomizer</code> object for this tracker.
|
|
92 */
|
|
93 final ServiceTrackerCustomizer customizer;
|
|
94 /**
|
|
95 * Filter string for use when adding the ServiceListener.
|
|
96 */
|
|
97 private final String listenerFilter;
|
|
98 /**
|
|
99 * Class name to be tracked. If this field is set, then we are tracking by
|
|
100 * class name.
|
|
101 */
|
|
102 private final String trackClass;
|
|
103 /**
|
|
104 * Reference to be tracked. If this field is set, then we are tracking a
|
|
105 * single ServiceReference.
|
|
106 */
|
|
107 private final ServiceReference trackReference;
|
|
108 /**
|
|
109 * True if no Filter object was supplied in a constructor or we are not
|
|
110 * using the supplied filter.
|
|
111 */
|
105
|
112 final bool noUserFilter;
|
86
|
113 /**
|
|
114 * Tracked services: <code>ServiceReference</code> object -> customized
|
|
115 * Object and <code>ServiceListener</code> object
|
|
116 */
|
105
|
117 private /+volatile+/ Tracked tracked;
|
86
|
118 /**
|
|
119 * Modification count. This field is initialized to zero by open, set to -1
|
|
120 * by close and incremented by modified.
|
105
|
121 *
|
86
|
122 * This field is volatile since it is accessed by multiple threads.
|
|
123 */
|
105
|
124 private /+volatile+/ int trackingCount = -1;
|
86
|
125 /**
|
|
126 * Cached ServiceReference for getServiceReference.
|
105
|
127 *
|
86
|
128 * This field is volatile since it is accessed by multiple threads.
|
|
129 */
|
105
|
130 private /+volatile+/ ServiceReference cachedReference;
|
86
|
131 /**
|
|
132 * Cached service object for getService.
|
105
|
133 *
|
86
|
134 * This field is volatile since it is accessed by multiple threads.
|
|
135 */
|
105
|
136 private /+volatile+/ Object cachedService;
|
86
|
137
|
|
138 /**
|
|
139 * Create a <code>ServiceTracker</code> object on the specified
|
|
140 * <code>ServiceReference</code> object.
|
105
|
141 *
|
86
|
142 * <p>
|
|
143 * The service referenced by the specified <code>ServiceReference</code>
|
|
144 * object will be tracked by this <code>ServiceTracker</code> object.
|
105
|
145 *
|
86
|
146 * @param context <code>BundleContext</code> object against which the
|
|
147 * tracking is done.
|
|
148 * @param reference <code>ServiceReference</code> object for the service
|
|
149 * to be tracked.
|
|
150 * @param customizer The customizer object to call when services are added,
|
|
151 * modified, or removed in this <code>ServiceTracker</code> object.
|
|
152 * If customizer is <code>null</code>, then this
|
|
153 * <code>ServiceTracker</code> object will be used as the
|
|
154 * <code>ServiceTrackerCustomizer</code> object and the
|
|
155 * <code>ServiceTracker</code> object will call the
|
|
156 * <code>ServiceTrackerCustomizer</code> methods on itself.
|
|
157 */
|
|
158 public this(BundleContext context, ServiceReference reference,
|
|
159 ServiceTrackerCustomizer customizer) {
|
|
160 this.context = context;
|
|
161 this.trackReference = reference;
|
|
162 this.trackClass = null;
|
105
|
163 this.customizer = (customizer is null) ? this : customizer;
|
|
164 this.listenerFilter = "(&(" + Constants.OBJECTCLASS + "=" + (stringcast( reference.getProperty(Constants.OBJECTCLASS)))[0] //$NON-NLS-1$ //$NON-NLS-2$
|
86
|
165 + ")(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + "))" ; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
166 this.noUserFilter = true;
|
|
167 try {
|
|
168 this.filter = context.createFilter(listenerFilter);
|
|
169 }
|
|
170 catch (InvalidSyntaxException e) { // we could only get this exception
|
|
171 // if the ServiceReference was
|
|
172 // invalid
|
|
173 throw new IllegalArgumentException(
|
|
174 "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
|
|
175 }
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * Create a <code>ServiceTracker</code> object on the specified class
|
|
180 * name.
|
105
|
181 *
|
86
|
182 * <p>
|
|
183 * Services registered under the specified class name will be tracked by
|
|
184 * this <code>ServiceTracker</code> object.
|
105
|
185 *
|
86
|
186 * @param context <code>BundleContext</code> object against which the
|
|
187 * tracking is done.
|
|
188 * @param clazz Class name of the services to be tracked.
|
|
189 * @param customizer The customizer object to call when services are added,
|
|
190 * modified, or removed in this <code>ServiceTracker</code> object.
|
|
191 * If customizer is <code>null</code>, then this
|
|
192 * <code>ServiceTracker</code> object will be used as the
|
|
193 * <code>ServiceTrackerCustomizer</code> object and the
|
|
194 * <code>ServiceTracker</code> object will call the
|
|
195 * <code>ServiceTrackerCustomizer</code> methods on itself.
|
|
196 */
|
|
197 public this(BundleContext context, String clazz,
|
|
198 ServiceTrackerCustomizer customizer) {
|
|
199 this.context = context;
|
|
200 this.trackReference = null;
|
|
201 this.trackClass = clazz;
|
105
|
202 this.customizer = (customizer is null) ? this : customizer;
|
86
|
203 this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz.toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
204 this.noUserFilter = true;
|
|
205 try {
|
|
206 this.filter = context.createFilter(listenerFilter);
|
|
207 }
|
|
208 catch (InvalidSyntaxException e) { // we could only get this exception
|
|
209 // if the clazz argument was
|
|
210 // malformed
|
|
211 throw new IllegalArgumentException(
|
|
212 "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
|
|
213 }
|
|
214 }
|
|
215
|
|
216 /**
|
|
217 * Create a <code>ServiceTracker</code> object on the specified
|
|
218 * <code>Filter</code> object.
|
105
|
219 *
|
86
|
220 * <p>
|
|
221 * Services which match the specified <code>Filter</code> object will be
|
|
222 * tracked by this <code>ServiceTracker</code> object.
|
105
|
223 *
|
86
|
224 * @param context <code>BundleContext</code> object against which the
|
|
225 * tracking is done.
|
|
226 * @param filter <code>Filter</code> object to select the services to be
|
|
227 * tracked.
|
|
228 * @param customizer The customizer object to call when services are added,
|
|
229 * modified, or removed in this <code>ServiceTracker</code> object.
|
|
230 * If customizer is null, then this <code>ServiceTracker</code>
|
|
231 * object will be used as the <code>ServiceTrackerCustomizer</code>
|
|
232 * object and the <code>ServiceTracker</code> object will call the
|
|
233 * <code>ServiceTrackerCustomizer</code> methods on itself.
|
|
234 * @since 1.1
|
|
235 */
|
|
236 public this(BundleContext context, Filter filter,
|
|
237 ServiceTrackerCustomizer customizer) {
|
|
238 this.context = context;
|
|
239 this.trackReference = null;
|
|
240
|
|
241 // obtain a required objectClass from the user supplied filter
|
105
|
242 if ( null !is cast(FilterImpl)filter ) {
|
|
243 this.trackClass = (cast(FilterImpl)filter).getRequiredObjectClass();
|
86
|
244 } else {
|
|
245 this.trackClass = null;
|
|
246 }
|
105
|
247
|
|
248 if (this.trackClass !is null) {
|
86
|
249 this.listenerFilter = FilterImpl.getObjectClassFilterString(this.trackClass);
|
105
|
250 //convert to track by class instead of filter if the user filter is in the form (objectClass=some.Clazz)
|
|
251 this.noUserFilter = this.listenerFilter.opEquals(filter.toString());
|
86
|
252 } else {
|
|
253 this.listenerFilter = null;
|
|
254 this.noUserFilter = false;
|
|
255 }
|
|
256
|
|
257 this.filter = filter;
|
105
|
258 this.customizer = (customizer is null) ? this : customizer;
|
|
259 if ((context is null) || (filter is null)) { // we throw a NPE here
|
86
|
260 // to
|
|
261 // be consistent with the
|
|
262 // other constructors
|
|
263 throw new NullPointerException();
|
|
264 }
|
|
265 }
|
|
266
|
|
267 /**
|
|
268 * Open this <code>ServiceTracker</code> object and begin tracking
|
|
269 * services.
|
105
|
270 *
|
86
|
271 * <p>
|
|
272 * This method calls <code>open(false)</code>.
|
105
|
273 *
|
86
|
274 * @throws java.lang.IllegalStateException if the <code>BundleContext</code>
|
|
275 * object with which this <code>ServiceTracker</code> object was
|
|
276 * created is no longer valid.
|
105
|
277 * @see #open(boolean)
|
86
|
278 */
|
|
279 public void open() {
|
|
280 open(false);
|
|
281 }
|
|
282
|
|
283 /**
|
|
284 * Open this <code>ServiceTracker</code> object and begin tracking
|
|
285 * services.
|
105
|
286 *
|
86
|
287 * <p>
|
|
288 * Services which match the search criteria specified when this
|
|
289 * <code>ServiceTracker</code> object was created are now tracked by this
|
|
290 * <code>ServiceTracker</code> object.
|
105
|
291 *
|
86
|
292 * @param trackAllServices If <code>true</code>, then this
|
|
293 * <code>ServiceTracker</code> will track all matching services
|
|
294 * regardless of class loader accessibility. If <code>false</code>,
|
|
295 * then this <code>ServiceTracker</code> will only track matching
|
|
296 * services which are class loader accessibile to the bundle whose
|
|
297 * <code>BundleContext</code> is used by this
|
|
298 * <code>ServiceTracker</code>.
|
|
299 * @throws java.lang.IllegalStateException if the <code>BundleContext</code>
|
|
300 * object with which this <code>ServiceTracker</code> object was
|
|
301 * created is no longer valid.
|
|
302 * @since 1.3
|
|
303 */
|
|
304 public synchronized void open(bool trackAllServices) {
|
105
|
305 if (tracked !is null) {
|
86
|
306 return;
|
|
307 }
|
|
308 if (DEBUG) {
|
105
|
309 System.out_.println("ServiceTracker.open: " + filter); //$NON-NLS-1$
|
86
|
310 }
|
|
311 tracked = trackAllServices ? new AllTracked() : new Tracked();
|
|
312 trackingCount = 0;
|
|
313 synchronized (tracked) {
|
|
314 try {
|
|
315 context.addServiceListener(tracked, listenerFilter);
|
|
316 ServiceReference[] references;
|
105
|
317
|
|
318 if (trackReference !is null) { // tracking a single reference
|
|
319 references = [ cast(ServiceReference)trackReference];
|
86
|
320 }
|
|
321 else { // tracking a set of references
|
|
322 references = getInitialReferences(trackAllServices, trackClass,
|
|
323 noUserFilter ? null: filter.toString());
|
|
324 }
|
|
325
|
105
|
326 tracked.setInitialServices(references); // set tracked with_
|
86
|
327 // the initial
|
|
328 // references
|
|
329 }
|
|
330 catch (InvalidSyntaxException e) {
|
|
331 throw new RuntimeException(
|
|
332 "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
|
|
333 }
|
|
334 }
|
|
335 /* Call tracked outside of synchronized region */
|
|
336 tracked.trackInitialServices(); // process the initial references
|
|
337 }
|
|
338
|
|
339 /**
|
|
340 * Returns the list of initial <code>ServiceReference</code> objects that
|
|
341 * will be tracked by this <code>ServiceTracker</code> object.
|
105
|
342 *
|
86
|
343 * @param trackAllServices If true, use getAllServiceReferences.
|
|
344 * @param trackClass the class name with which the service was registered,
|
|
345 * or null for all services.
|
|
346 * @param filterString the filter criteria or null for all services.
|
|
347 * @return the list of initial <code>ServiceReference</code> objects.
|
|
348 * @throws InvalidSyntaxException if the filter uses an invalid syntax.
|
|
349 */
|
|
350 private ServiceReference[] getInitialReferences(bool trackAllServices,
|
105
|
351 String trackClass, String filterString) {
|
86
|
352 if (trackAllServices) {
|
|
353 return context.getAllServiceReferences(trackClass, filterString);
|
|
354 }
|
|
355 else {
|
|
356 return context.getServiceReferences(trackClass, filterString);
|
|
357 }
|
|
358 }
|
|
359
|
|
360 /**
|
|
361 * Close this <code>ServiceTracker</code> object.
|
105
|
362 *
|
86
|
363 * <p>
|
|
364 * This method should be called when this <code>ServiceTracker</code>
|
|
365 * object should end the tracking of services.
|
|
366 */
|
|
367 public synchronized void close() {
|
105
|
368 if (tracked is null) {
|
86
|
369 return;
|
|
370 }
|
|
371 if (DEBUG) {
|
105
|
372 System.out_.println("ServiceTracker.close: " + filter); //$NON-NLS-1$
|
86
|
373 }
|
|
374 tracked.close();
|
|
375 ServiceReference[] references = getServiceReferences();
|
|
376 Tracked outgoing = tracked;
|
|
377 tracked = null;
|
|
378 try {
|
|
379 context.removeServiceListener(outgoing);
|
|
380 }
|
|
381 catch (IllegalStateException e) {
|
|
382 /* In case the context was stopped. */
|
|
383 }
|
105
|
384 if (references !is null) {
|
|
385 for (int i = 0; i < references.length_; i++) {
|
86
|
386 outgoing.untrack(references[i]);
|
|
387 }
|
|
388 }
|
|
389 trackingCount = -1;
|
|
390 if (DEBUG) {
|
105
|
391 if ((cachedReference is null) && (cachedService is null)) {
|
|
392 System.out_
|
86
|
393 .println("ServiceTracker.close[cached cleared]: " + filter); //$NON-NLS-1$
|
|
394 }
|
|
395 }
|
|
396 }
|
|
397
|
|
398 /**
|
|
399 * Default implementation of the
|
|
400 * <code>ServiceTrackerCustomizer.addingService</code> method.
|
105
|
401 *
|
86
|
402 * <p>
|
|
403 * This method is only called when this <code>ServiceTracker</code> object
|
|
404 * has been constructed with a <code>null ServiceTrackerCustomizer</code>
|
|
405 * argument.
|
105
|
406 *
|
86
|
407 * The default implementation returns the result of calling
|
|
408 * <code>getService</code>, on the <code>BundleContext</code> object
|
|
409 * with which this <code>ServiceTracker</code> object was created, passing
|
|
410 * the specified <code>ServiceReference</code> object.
|
|
411 * <p>
|
|
412 * This method can be overridden in a subclass to customize the service
|
|
413 * object to be tracked for the service being added. In that case, take care
|
|
414 * not to rely on the default implementation of removedService that will
|
|
415 * unget the service.
|
105
|
416 *
|
86
|
417 * @param reference Reference to service being added to this
|
|
418 * <code>ServiceTracker</code> object.
|
|
419 * @return The service object to be tracked for the service added to this
|
|
420 * <code>ServiceTracker</code> object.
|
|
421 * @see ServiceTrackerCustomizer
|
|
422 */
|
|
423 public Object addingService(ServiceReference reference) {
|
|
424 return context.getService(reference);
|
|
425 }
|
|
426
|
|
427 /**
|
|
428 * Default implementation of the
|
|
429 * <code>ServiceTrackerCustomizer.modifiedService</code> method.
|
105
|
430 *
|
86
|
431 * <p>
|
|
432 * This method is only called when this <code>ServiceTracker</code> object
|
|
433 * has been constructed with a <code>null ServiceTrackerCustomizer</code>
|
|
434 * argument.
|
105
|
435 *
|
86
|
436 * The default implementation does nothing.
|
105
|
437 *
|
86
|
438 * @param reference Reference to modified service.
|
|
439 * @param service The service object for the modified service.
|
|
440 * @see ServiceTrackerCustomizer
|
|
441 */
|
|
442 public void modifiedService(ServiceReference reference, Object service) {
|
|
443 }
|
|
444
|
|
445 /**
|
|
446 * Default implementation of the
|
|
447 * <code>ServiceTrackerCustomizer.removedService</code> method.
|
105
|
448 *
|
86
|
449 * <p>
|
|
450 * This method is only called when this <code>ServiceTracker</code> object
|
|
451 * has been constructed with a <code>null ServiceTrackerCustomizer</code>
|
|
452 * argument.
|
105
|
453 *
|
86
|
454 * The default implementation calls <code>ungetService</code>, on the
|
|
455 * <code>BundleContext</code> object with which this
|
|
456 * <code>ServiceTracker</code> object was created, passing the specified
|
|
457 * <code>ServiceReference</code> object.
|
|
458 * <p>
|
|
459 * This method can be overridden in a subclass. If the default
|
|
460 * implementation of <code>addingService</code> method was used, this
|
|
461 * method must unget the service.
|
105
|
462 *
|
86
|
463 * @param reference Reference to removed service.
|
|
464 * @param service The service object for the removed service.
|
|
465 * @see ServiceTrackerCustomizer
|
|
466 */
|
|
467 public void removedService(ServiceReference reference, Object service) {
|
|
468 context.ungetService(reference);
|
|
469 }
|
|
470
|
|
471 /**
|
|
472 * Wait for at least one service to be tracked by this
|
|
473 * <code>ServiceTracker</code> object.
|
|
474 * <p>
|
|
475 * It is strongly recommended that <code>waitForService</code> is not used
|
|
476 * during the calling of the <code>BundleActivator</code> methods.
|
|
477 * <code>BundleActivator</code> methods are expected to complete in a
|
|
478 * short period of time.
|
105
|
479 *
|
86
|
480 * @param timeout time interval in milliseconds to wait. If zero, the method
|
|
481 * will wait indefinately.
|
|
482 * @return Returns the result of <code>getService()</code>.
|
|
483 * @throws InterruptedException If another thread has interrupted the
|
|
484 * current thread.
|
|
485 * @throws IllegalArgumentException If the value of timeout is negative.
|
|
486 */
|
105
|
487 public Object waitForService(long timeout) {
|
86
|
488 if (timeout < 0) {
|
105
|
489 throw new IllegalArgumentException("timeout value is_ negative"); //$NON-NLS-1$
|
86
|
490 }
|
|
491 Object object = getService();
|
105
|
492 while (object is null) {
|
86
|
493 Tracked tracked = this.tracked; /*
|
|
494 * use local var since we are not
|
|
495 * synchronized
|
|
496 */
|
105
|
497 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
498 return null;
|
|
499 }
|
|
500 synchronized (tracked) {
|
105
|
501 if (tracked.size() is 0) {
|
86
|
502 tracked.wait(timeout);
|
|
503 }
|
|
504 }
|
|
505 object = getService();
|
|
506 if (timeout > 0) {
|
|
507 return object;
|
|
508 }
|
|
509 }
|
|
510 return object;
|
|
511 }
|
|
512
|
|
513 /**
|
|
514 * Return an array of <code>ServiceReference</code> objects for all
|
|
515 * services being tracked by this <code>ServiceTracker</code> object.
|
105
|
516 *
|
86
|
517 * @return Array of <code>ServiceReference</code> objects or
|
|
518 * <code>null</code> if no service are being tracked.
|
|
519 */
|
|
520 public ServiceReference[] getServiceReferences() {
|
|
521 Tracked tracked = this.tracked; /*
|
|
522 * use local var since we are not
|
|
523 * synchronized
|
|
524 */
|
105
|
525 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
526 return null;
|
|
527 }
|
|
528 synchronized (tracked) {
|
105
|
529 int length_ = tracked.size();
|
|
530 if (length_ is 0) {
|
86
|
531 return null;
|
|
532 }
|
105
|
533 ServiceReference[] references = new ServiceReference[length_];
|
86
|
534 Enumeration keys = tracked.keys();
|
105
|
535 for (int i = 0; i < length_; i++) {
|
|
536 references[i] = cast(ServiceReference) keys.nextElement();
|
86
|
537 }
|
|
538 return references;
|
|
539 }
|
|
540 }
|
|
541
|
|
542 /**
|
|
543 * Returns a <code>ServiceReference</code> object for one of the services
|
|
544 * being tracked by this <code>ServiceTracker</code> object.
|
105
|
545 *
|
86
|
546 * <p>
|
|
547 * If multiple services are being tracked, the service with the highest
|
|
548 * ranking (as specified in its <code>service.ranking</code> property) is
|
|
549 * returned.
|
105
|
550 *
|
86
|
551 * <p>
|
|
552 * If there is a tie in ranking, the service with the lowest service ID (as
|
|
553 * specified in its <code>service.id</code> property); that is, the
|
|
554 * service that was registered first is returned.
|
|
555 * <p>
|
|
556 * This is the same algorithm used by
|
|
557 * <code>BundleContext.getServiceReference</code>.
|
105
|
558 *
|
86
|
559 * @return <code>ServiceReference</code> object or <code>null</code> if
|
|
560 * no service is being tracked.
|
|
561 * @since 1.1
|
|
562 */
|
|
563 public ServiceReference getServiceReference() {
|
|
564 ServiceReference reference = cachedReference;
|
105
|
565 if (reference !is null) {
|
86
|
566 if (DEBUG) {
|
105
|
567 System.out_
|
86
|
568 .println("ServiceTracker.getServiceReference[cached]: " + filter); //$NON-NLS-1$
|
|
569 }
|
|
570 return reference;
|
|
571 }
|
|
572 if (DEBUG) {
|
105
|
573 System.out_.println("ServiceTracker.getServiceReference: " + filter); //$NON-NLS-1$
|
86
|
574 }
|
|
575 ServiceReference[] references = getServiceReferences();
|
105
|
576 int length_ = (references is null) ? 0 : references.length_;
|
|
577 if (length_ is 0) /* if no service is_ being tracked */
|
86
|
578 {
|
|
579 return null;
|
|
580 }
|
|
581 int index = 0;
|
105
|
582 if (length_ > 1) /* if more than one service, select highest ranking */
|
86
|
583 {
|
105
|
584 int rankings[] = new int[length_];
|
86
|
585 int count = 0;
|
|
586 int maxRanking = Integer.MIN_VALUE;
|
105
|
587 for (int i = 0; i < length_; i++) {
|
86
|
588 Object property = references[i]
|
|
589 .getProperty(Constants.SERVICE_RANKING);
|
105
|
590 int ranking = ( null !is cast(Integer)property ) ? (cast(Integer) property)
|
86
|
591 .intValue()
|
|
592 : 0;
|
|
593 rankings[i] = ranking;
|
|
594 if (ranking > maxRanking) {
|
|
595 index = i;
|
|
596 maxRanking = ranking;
|
|
597 count = 1;
|
|
598 }
|
|
599 else {
|
105
|
600 if (ranking is maxRanking) {
|
86
|
601 count++;
|
|
602 }
|
|
603 }
|
|
604 }
|
|
605 if (count > 1) /* if still more than one service, select lowest id */
|
|
606 {
|
|
607 long minId = Long.MAX_VALUE;
|
105
|
608 for (int i = 0; i < length_; i++) {
|
|
609 if (rankings[i] is maxRanking) {
|
|
610 long id = (cast(Long) (references[i]
|
86
|
611 .getProperty(Constants.SERVICE_ID)))
|
|
612 .longValue();
|
|
613 if (id < minId) {
|
|
614 index = i;
|
|
615 minId = id;
|
|
616 }
|
|
617 }
|
|
618 }
|
|
619 }
|
|
620 }
|
|
621 return cachedReference = references[index];
|
|
622 }
|
|
623
|
|
624 /**
|
|
625 * Returns the service object for the specified
|
|
626 * <code>ServiceReference</code> object if the referenced service is being
|
|
627 * tracked by this <code>ServiceTracker</code> object.
|
105
|
628 *
|
86
|
629 * @param reference Reference to the desired service.
|
|
630 * @return Service object or <code>null</code> if the service referenced
|
|
631 * by the specified <code>ServiceReference</code> object is not
|
|
632 * being tracked.
|
|
633 */
|
|
634 public Object getService(ServiceReference reference) {
|
|
635 Tracked tracked = this.tracked; /*
|
|
636 * use local var since we are not
|
|
637 * synchronized
|
|
638 */
|
105
|
639 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
640 return null;
|
|
641 }
|
|
642 synchronized (tracked) {
|
|
643 return tracked.get(reference);
|
|
644 }
|
|
645 }
|
|
646
|
|
647 /**
|
|
648 * Return an array of service objects for all services being tracked by this
|
|
649 * <code>ServiceTracker</code> object.
|
105
|
650 *
|
86
|
651 * @return Array of service objects or <code>null</code> if no service are
|
|
652 * being tracked.
|
|
653 */
|
|
654 public Object[] getServices() {
|
|
655 Tracked tracked = this.tracked; /*
|
|
656 * use local var since we are not
|
|
657 * synchronized
|
|
658 */
|
105
|
659 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
660 return null;
|
|
661 }
|
|
662 synchronized (tracked) {
|
|
663 ServiceReference[] references = getServiceReferences();
|
105
|
664 int length_ = (references is null) ? 0 : references.length_;
|
|
665 if (length_ is 0) {
|
86
|
666 return null;
|
|
667 }
|
105
|
668 Object[] objects = new Object[length_];
|
|
669 for (int i = 0; i < length_; i++) {
|
86
|
670 objects[i] = getService(references[i]);
|
|
671 }
|
|
672 return objects;
|
|
673 }
|
|
674 }
|
|
675
|
|
676 /**
|
|
677 * Returns a service object for one of the services being tracked by this
|
|
678 * <code>ServiceTracker</code> object.
|
105
|
679 *
|
86
|
680 * <p>
|
|
681 * If any services are being tracked, this method returns the result of
|
|
682 * calling <code>getService(getServiceReference())</code>.
|
105
|
683 *
|
86
|
684 * @return Service object or <code>null</code> if no service is being
|
|
685 * tracked.
|
|
686 */
|
|
687 public Object getService() {
|
|
688 Object service = cachedService;
|
105
|
689 if (service !is null) {
|
86
|
690 if (DEBUG) {
|
105
|
691 System.out_
|
86
|
692 .println("ServiceTracker.getService[cached]: " + filter); //$NON-NLS-1$
|
|
693 }
|
|
694 return service;
|
|
695 }
|
|
696 if (DEBUG) {
|
105
|
697 System.out_.println("ServiceTracker.getService: " + filter); //$NON-NLS-1$
|
86
|
698 }
|
|
699 ServiceReference reference = getServiceReference();
|
105
|
700 if (reference is null) {
|
86
|
701 return null;
|
|
702 }
|
|
703 return cachedService = getService(reference);
|
|
704 }
|
|
705
|
|
706 /**
|
|
707 * Remove a service from this <code>ServiceTracker</code> object.
|
105
|
708 *
|
86
|
709 * The specified service will be removed from this
|
|
710 * <code>ServiceTracker</code> object. If the specified service was being
|
|
711 * tracked then the <code>ServiceTrackerCustomizer.removedService</code>
|
|
712 * method will be called for that service.
|
105
|
713 *
|
86
|
714 * @param reference Reference to the service to be removed.
|
|
715 */
|
|
716 public void remove(ServiceReference reference) {
|
|
717 Tracked tracked = this.tracked; /*
|
|
718 * use local var since we are not
|
|
719 * synchronized
|
|
720 */
|
105
|
721 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
722 return;
|
|
723 }
|
|
724 tracked.untrack(reference);
|
|
725 }
|
|
726
|
|
727 /**
|
|
728 * Return the number of services being tracked by this
|
|
729 * <code>ServiceTracker</code> object.
|
105
|
730 *
|
86
|
731 * @return Number of services being tracked.
|
|
732 */
|
|
733 public int size() {
|
|
734 Tracked tracked = this.tracked; /*
|
|
735 * use local var since we are not
|
|
736 * synchronized
|
|
737 */
|
105
|
738 if (tracked is null) { /* if ServiceTracker is_ not open */
|
86
|
739 return 0;
|
|
740 }
|
|
741 return tracked.size();
|
|
742 }
|
|
743
|
|
744 /**
|
|
745 * Returns the tracking count for this <code>ServiceTracker</code> object.
|
105
|
746 *
|
86
|
747 * The tracking count is initialized to 0 when this
|
|
748 * <code>ServiceTracker</code> object is opened. Every time a service is
|
|
749 * added, modified or removed from this <code>ServiceTracker</code> object
|
|
750 * the tracking count is incremented.
|
105
|
751 *
|
86
|
752 * <p>
|
|
753 * The tracking count can be used to determine if this
|
|
754 * <code>ServiceTracker</code> object has added, modified or removed a
|
|
755 * service by comparing a tracking count value previously collected with the
|
|
756 * current tracking count value. If the value has not changed, then no
|
|
757 * service has been added, modified or removed from this
|
|
758 * <code>ServiceTracker</code> object since the previous tracking count
|
|
759 * was collected.
|
105
|
760 *
|
86
|
761 * @since 1.2
|
|
762 * @return The tracking count for this <code>ServiceTracker</code> object
|
|
763 * or -1 if this <code>ServiceTracker</code> object is not open.
|
|
764 */
|
|
765 public int getTrackingCount() {
|
|
766 return trackingCount;
|
|
767 }
|
|
768
|
|
769 /**
|
|
770 * Called by the Tracked object whenever the set of tracked services is
|
|
771 * modified. Increments the tracking count and clears the cache.
|
105
|
772 *
|
86
|
773 * @GuardedBy tracked
|
|
774 */
|
|
775 /*
|
|
776 * This method must not be synchronized since it is called by Tracked while
|
|
777 * Tracked is synchronized. We don't want synchronization interactions
|
|
778 * between the ServiceListener thread and the user thread.
|
|
779 */
|
|
780 void modified() {
|
|
781 trackingCount++; /* increment modification count */
|
|
782 cachedReference = null; /* clear cached value */
|
|
783 cachedService = null; /* clear cached value */
|
|
784 if (DEBUG) {
|
105
|
785 System.out_.println("ServiceTracker.modified: " + filter); //$NON-NLS-1$
|
86
|
786 }
|
|
787 }
|
|
788
|
|
789
|
|
790 /**
|
|
791 * Inner class to track services. If a <code>ServiceTracker</code> object
|
|
792 * is reused (closed then reopened), then a new Tracked object is used. This
|
|
793 * class is a hashtable mapping <code>ServiceReference</code> object ->
|
|
794 * customized Object. This class is the <code>ServiceListener</code>
|
|
795 * object for the tracker. This class is used to synchronize access to the
|
|
796 * tracked services. This is not a public class. It is only for use by the
|
|
797 * implementation of the <code>ServiceTracker</code> class.
|
105
|
798 *
|
86
|
799 * @ThreadSafe
|
|
800 */
|
105
|
801 class Tracked : Hashtable , ServiceListener {
|
86
|
802 static final long serialVersionUID = -7420065199791006079L;
|
|
803 /**
|
|
804 * List of ServiceReferences in the process of being added. This is used
|
|
805 * to deal with nesting of ServiceEvents. Since ServiceEvents are
|
|
806 * synchronously delivered, ServiceEvents can be nested. For example,
|
|
807 * when processing the adding of a service and the customizer causes the
|
|
808 * service to be unregistered, notification to the nested call to
|
|
809 * untrack that the service was unregistered can be made to the track
|
|
810 * method.
|
105
|
811 *
|
86
|
812 * Since the ArrayList implementation is not synchronized, all access to
|
|
813 * this list must be protected by the same synchronized object for
|
|
814 * thread-safety.
|
105
|
815 *
|
86
|
816 * @GuardedBy this
|
|
817 */
|
|
818 private final ArrayList adding;
|
|
819
|
|
820 /**
|
|
821 * true if the tracked object is closed.
|
105
|
822 *
|
86
|
823 * This field is volatile because it is set by one thread and read by
|
|
824 * another.
|
|
825 */
|
105
|
826 private /+volatile+/ bool closed;
|
86
|
827
|
|
828 /**
|
|
829 * Initial list of ServiceReferences for the tracker. This is used to
|
|
830 * correctly process the initial services which could become
|
|
831 * unregistered before they are tracked. This is necessary since the
|
|
832 * initial set of tracked services are not "announced" by ServiceEvents
|
|
833 * and therefore the ServiceEvent for unregistration could be delivered
|
|
834 * before we track the service.
|
105
|
835 *
|
86
|
836 * A service must not be in both the initial and adding lists at the
|
|
837 * same time. A service must be moved from the initial list to the
|
|
838 * adding list "atomically" before we begin tracking it.
|
105
|
839 *
|
86
|
840 * Since the LinkedList implementation is not synchronized, all access
|
|
841 * to this list must be protected by the same synchronized object for
|
|
842 * thread-safety.
|
105
|
843 *
|
86
|
844 * @GuardedBy this
|
|
845 */
|
|
846 private final LinkedList initial;
|
|
847
|
|
848 /**
|
|
849 * Tracked constructor.
|
|
850 */
|
105
|
851 protected this() {
|
86
|
852 super();
|
|
853 closed = false;
|
|
854 adding = new ArrayList(6);
|
|
855 initial = new LinkedList();
|
|
856 }
|
|
857
|
|
858 /**
|
|
859 * Set initial list of services into tracker before ServiceEvents begin
|
|
860 * to be received.
|
105
|
861 *
|
86
|
862 * This method must be called from ServiceTracker.open while
|
|
863 * synchronized on this object in the same synchronized block as the
|
|
864 * addServiceListener call.
|
105
|
865 *
|
86
|
866 * @param references The initial list of services to be tracked.
|
|
867 * @GuardedBy this
|
|
868 */
|
|
869 protected void setInitialServices(ServiceReference[] references) {
|
105
|
870 if (references is null) {
|
86
|
871 return;
|
|
872 }
|
105
|
873 int size = references.length_;
|
86
|
874 for (int i = 0; i < size; i++) {
|
|
875 if (DEBUG) {
|
105
|
876 System.out_
|
86
|
877 .println("ServiceTracker.Tracked.setInitialServices: " + references[i]); //$NON-NLS-1$
|
|
878 }
|
|
879 initial.add(references[i]);
|
|
880 }
|
|
881 }
|
|
882
|
|
883 /**
|
|
884 * Track the initial list of services. This is called after
|
|
885 * ServiceEvents can begin to be received.
|
105
|
886 *
|
86
|
887 * This method must be called from ServiceTracker.open while not
|
|
888 * synchronized on this object after the addServiceListener call.
|
105
|
889 *
|
86
|
890 */
|
|
891 protected void trackInitialServices() {
|
|
892 while (true) {
|
|
893 ServiceReference reference;
|
|
894 synchronized (this) {
|
105
|
895 if (initial.size() is 0) {
|
86
|
896 /*
|
|
897 * if there are no more inital services
|
|
898 */
|
|
899 return; /* we are done */
|
|
900 }
|
|
901 /*
|
|
902 * move the first service from the initial list to the
|
|
903 * adding list within this synchronized block.
|
|
904 */
|
105
|
905 reference = cast(ServiceReference) initial.removeFirst();
|
|
906 if (this.get(reference) !is null) {
|
86
|
907 /* if we are already tracking this service */
|
|
908 if (DEBUG) {
|
105
|
909 System.out_
|
86
|
910 .println("ServiceTracker.Tracked.trackInitialServices[already tracked]: " + reference); //$NON-NLS-1$
|
|
911 }
|
|
912 continue; /* skip this service */
|
|
913 }
|
|
914 if (adding.contains(reference)) {
|
|
915 /*
|
|
916 * if this service is already in the process of being
|
|
917 * added.
|
|
918 */
|
|
919 if (DEBUG) {
|
105
|
920 System.out_
|
86
|
921 .println("ServiceTracker.Tracked.trackInitialServices[already adding]: " + reference); //$NON-NLS-1$
|
|
922 }
|
|
923 continue; /* skip this service */
|
|
924 }
|
|
925 adding.add(reference);
|
|
926 }
|
|
927 if (DEBUG) {
|
105
|
928 System.out_
|
86
|
929 .println("ServiceTracker.Tracked.trackInitialServices: " + reference); //$NON-NLS-1$
|
|
930 }
|
|
931 trackAdding(reference); /*
|
|
932 * Begin tracking it. We call
|
|
933 * trackAdding since we have already put
|
|
934 * the reference in the adding list.
|
|
935 */
|
|
936 }
|
|
937 }
|
|
938
|
|
939 /**
|
|
940 * Called by the owning <code>ServiceTracker</code> object when it is
|
|
941 * closed.
|
|
942 */
|
|
943 protected void close() {
|
|
944 closed = true;
|
|
945 }
|
|
946
|
|
947 /**
|
|
948 * <code>ServiceListener</code> method for the
|
|
949 * <code>ServiceTracker</code> class. This method must NOT be
|
|
950 * synchronized to avoid deadlock potential.
|
105
|
951 *
|
86
|
952 * @param event <code>ServiceEvent</code> object from the framework.
|
|
953 */
|
|
954 public void serviceChanged(ServiceEvent event) {
|
|
955 /*
|
|
956 * Check if we had a delayed call (which could happen when we
|
|
957 * close).
|
|
958 */
|
|
959 if (closed) {
|
|
960 return;
|
|
961 }
|
|
962 ServiceReference reference = event.getServiceReference();
|
|
963 if (DEBUG) {
|
105
|
964 System.out_
|
86
|
965 .println("ServiceTracker.Tracked.serviceChanged[" + event.getType() + "]: " + reference); //$NON-NLS-1$ //$NON-NLS-2$
|
|
966 }
|
|
967
|
|
968 switch (event.getType()) {
|
|
969 case ServiceEvent.REGISTERED :
|
|
970 case ServiceEvent.MODIFIED :
|
|
971 if (noUserFilter) { // no user supplied filter to be checked
|
|
972 track(reference);
|
|
973 /*
|
|
974 * If the customizer throws an unchecked exception, it
|
|
975 * is safe to let it propagate
|
|
976 */
|
|
977 }
|
|
978 else { // filter supplied by user must be checked
|
|
979 if (filter.match(reference)) {
|
|
980 track(reference);
|
|
981 /*
|
|
982 * If the customizer throws an unchecked exception,
|
|
983 * it is safe to let it propagate
|
|
984 */
|
|
985 }
|
|
986 else {
|
|
987 untrack(reference);
|
|
988 /*
|
|
989 * If the customizer throws an unchecked exception,
|
|
990 * it is safe to let it propagate
|
|
991 */
|
|
992 }
|
|
993 }
|
|
994 break;
|
|
995 case ServiceEvent.UNREGISTERING :
|
|
996 untrack(reference);
|
|
997 /*
|
|
998 * If the customizer throws an unchecked exception, it is
|
|
999 * safe to let it propagate
|
|
1000 */
|
|
1001 break;
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005 /**
|
|
1006 * Begin to track the referenced service.
|
105
|
1007 *
|
86
|
1008 * @param reference Reference to a service to be tracked.
|
|
1009 */
|
|
1010 private void track(ServiceReference reference) {
|
|
1011 Object object;
|
|
1012 synchronized (this) {
|
|
1013 object = this.get(reference);
|
|
1014 }
|
105
|
1015 if (object !is null) /* we are already tracking the service */
|
86
|
1016 {
|
|
1017 if (DEBUG) {
|
105
|
1018 System.out_
|
86
|
1019 .println("ServiceTracker.Tracked.track[modified]: " + reference); //$NON-NLS-1$
|
|
1020 }
|
|
1021 synchronized (this) {
|
|
1022 modified(); /* increment modification count */
|
|
1023 }
|
|
1024 /* Call customizer outside of synchronized region */
|
|
1025 customizer.modifiedService(reference, object);
|
|
1026 /*
|
|
1027 * If the customizer throws an unchecked exception, it is safe
|
|
1028 * to let it propagate
|
|
1029 */
|
|
1030 return;
|
|
1031 }
|
|
1032 synchronized (this) {
|
|
1033 if (adding.contains(reference)) { /*
|
|
1034 * if this service is
|
|
1035 * already in the process of
|
|
1036 * being added.
|
|
1037 */
|
|
1038 if (DEBUG) {
|
105
|
1039 System.out_
|
86
|
1040 .println("ServiceTracker.Tracked.track[already adding]: " + reference); //$NON-NLS-1$
|
|
1041 }
|
|
1042 return;
|
|
1043 }
|
105
|
1044 adding.add(reference); /* mark this service is_ being added */
|
86
|
1045 }
|
|
1046
|
|
1047 trackAdding(reference); /*
|
|
1048 * call trackAdding now that we have put the
|
|
1049 * reference in the adding list
|
|
1050 */
|
|
1051 }
|
|
1052
|
|
1053 /**
|
|
1054 * Common logic to add a service to the tracker used by track and
|
|
1055 * trackInitialServices. The specified reference must have been placed
|
|
1056 * in the adding list before calling this method.
|
105
|
1057 *
|
86
|
1058 * @param reference Reference to a service to be tracked.
|
|
1059 */
|
|
1060 private void trackAdding(ServiceReference reference) {
|
|
1061 if (DEBUG) {
|
105
|
1062 System.out_
|
86
|
1063 .println("ServiceTracker.Tracked.trackAdding: " + reference); //$NON-NLS-1$
|
|
1064 }
|
|
1065 Object object = null;
|
|
1066 bool becameUntracked = false;
|
|
1067 /* Call customizer outside of synchronized region */
|
|
1068 try {
|
|
1069 object = customizer.addingService(reference);
|
|
1070 /*
|
|
1071 * If the customizer throws an unchecked exception, it will
|
|
1072 * propagate after the finally
|
|
1073 */
|
|
1074 }
|
|
1075 finally {
|
|
1076 synchronized (this) {
|
|
1077 if (adding.remove(reference)) { /*
|
|
1078 * if the service was not
|
|
1079 * untracked during the
|
|
1080 * customizer callback
|
|
1081 */
|
105
|
1082 if (object !is null) {
|
86
|
1083 this.put(reference, object);
|
|
1084 modified(); /* increment modification count */
|
|
1085 notifyAll(); /*
|
|
1086 * notify any waiters in
|
|
1087 * waitForService
|
|
1088 */
|
|
1089 }
|
|
1090 }
|
|
1091 else {
|
|
1092 becameUntracked = true;
|
|
1093 }
|
|
1094 }
|
|
1095 }
|
|
1096 /*
|
|
1097 * The service became untracked during the customizer callback.
|
|
1098 */
|
|
1099 if (becameUntracked) {
|
|
1100 if (DEBUG) {
|
105
|
1101 System.out_
|
86
|
1102 .println("ServiceTracker.Tracked.trackAdding[removed]: " + reference); //$NON-NLS-1$
|
|
1103 }
|
|
1104 /* Call customizer outside of synchronized region */
|
|
1105 customizer.removedService(reference, object);
|
|
1106 /*
|
|
1107 * If the customizer throws an unchecked exception, it is safe
|
|
1108 * to let it propagate
|
|
1109 */
|
|
1110 }
|
|
1111 }
|
|
1112
|
|
1113 /**
|
|
1114 * Discontinue tracking the referenced service.
|
105
|
1115 *
|
86
|
1116 * @param reference Reference to the tracked service.
|
|
1117 */
|
|
1118 protected void untrack(ServiceReference reference) {
|
|
1119 Object object;
|
|
1120 synchronized (this) {
|
|
1121 if (initial.remove(reference)) { /*
|
|
1122 * if this service is
|
|
1123 * already in the list of
|
|
1124 * initial references to
|
|
1125 * process
|
|
1126 */
|
|
1127 if (DEBUG) {
|
105
|
1128 System.out_
|
86
|
1129 .println("ServiceTracker.Tracked.untrack[removed from initial]: " + reference); //$NON-NLS-1$
|
|
1130 }
|
|
1131 return; /*
|
|
1132 * we have removed it from the list and it will not
|
|
1133 * be processed
|
|
1134 */
|
|
1135 }
|
|
1136
|
|
1137 if (adding.remove(reference)) { /*
|
|
1138 * if the service is in the
|
|
1139 * process of being added
|
|
1140 */
|
|
1141 if (DEBUG) {
|
105
|
1142 System.out_
|
86
|
1143 .println("ServiceTracker.Tracked.untrack[being added]: " + reference); //$NON-NLS-1$
|
|
1144 }
|
|
1145 return; /*
|
|
1146 * in case the service is untracked while in the
|
|
1147 * process of adding
|
|
1148 */
|
|
1149 }
|
|
1150 object = this.remove(reference); /*
|
|
1151 * must remove from tracker
|
|
1152 * before calling customizer
|
|
1153 * callback
|
|
1154 */
|
105
|
1155 if (object is null) { /* are we actually tracking the service */
|
86
|
1156 return;
|
|
1157 }
|
|
1158 modified(); /* increment modification count */
|
|
1159 }
|
|
1160 if (DEBUG) {
|
105
|
1161 System.out_
|
86
|
1162 .println("ServiceTracker.Tracked.untrack[removed]: " + reference); //$NON-NLS-1$
|
|
1163 }
|
|
1164 /* Call customizer outside of synchronized region */
|
|
1165 customizer.removedService(reference, object);
|
|
1166 /*
|
|
1167 * If the customizer throws an unchecked exception, it is safe to
|
|
1168 * let it propagate
|
|
1169 */
|
|
1170 }
|
|
1171 }
|
|
1172
|
|
1173 /**
|
|
1174 * Subclass of Tracked which implements the AllServiceListener interface.
|
|
1175 * This class is used by the ServiceTracker if open is called with true.
|
105
|
1176 *
|
86
|
1177 * @since 1.3
|
|
1178 * @ThreadSafe
|
|
1179 */
|
105
|
1180 class AllTracked : Tracked , AllServiceListener {
|
86
|
1181 static final long serialVersionUID = 4050764875305137716L;
|
|
1182
|
|
1183 /**
|
|
1184 * AllTracked constructor.
|
|
1185 */
|
105
|
1186 protected this() {
|
86
|
1187 super();
|
|
1188 }
|
|
1189 }
|
|
1190 }
|