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