Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * $Header: /cvshome/build/org.osgi.util.tracker/src/org/osgi/util/tracker/ServiceTracker.java,v 1.13 2005/05/13 20:33:35 hargrave Exp $ |
| 3 | * |
| 4 | * Copyright (c) OSGi Alliance (2000, 2005). All Rights Reserved. |
| 5 | * |
| 6 | * This program and the accompanying materials are made available under the |
| 7 | * terms of the Eclipse Public License v1.0 which accompanies this |
| 8 | * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html. |
| 9 | */ |
| 10 | |
| 11 | package org.osgi.util.tracker; |
| 12 | |
| 13 | import java.util.*; |
| 14 | |
| 15 | import org.osgi.framework.*; |
| 16 | |
| 17 | /** |
| 18 | * The <code>ServiceTracker</code> class simplifies using services from the |
| 19 | * Framework's service registry. |
| 20 | * <p> |
| 21 | * A <code>ServiceTracker</code> object is constructed with search criteria |
| 22 | * and a <code>ServiceTrackerCustomizer</code> object. A |
| 23 | * <code>ServiceTracker</code> object can use the |
| 24 | * <code>ServiceTrackerCustomizer</code> object to customize the service |
| 25 | * objects to be tracked. The <code>ServiceTracker</code> object can then be |
| 26 | * opened to begin tracking all services in the Framework's service registry |
| 27 | * that match the specified search criteria. The <code>ServiceTracker</code> |
| 28 | * object correctly handles all of the details of listening to |
| 29 | * <code>ServiceEvent</code> objects and getting and ungetting services. |
| 30 | * <p> |
| 31 | * The <code>getServiceReferences</code> method can be called to get |
| 32 | * references to the services being tracked. The <code>getService</code> and |
| 33 | * <code>getServices</code> methods can be called to get the service objects |
| 34 | * for the tracked service. |
| 35 | * |
| 36 | * @version $Revision: 1.13 $ |
| 37 | */ |
| 38 | public class ServiceTracker implements ServiceTrackerCustomizer { |
| 39 | /* set this to true to compile in debug messages */ |
| 40 | static final boolean DEBUG = false; |
| 41 | /** |
| 42 | * Bundle context this <code>ServiceTracker</code> object is tracking |
| 43 | * against. |
| 44 | */ |
| 45 | protected final BundleContext context; |
| 46 | /** |
| 47 | * Filter specifying search criteria for the services to track. |
| 48 | * |
| 49 | * @since 1.1 |
| 50 | */ |
| 51 | protected final Filter filter; |
| 52 | /** |
| 53 | * <code>ServiceTrackerCustomizer</code> object for this tracker. |
| 54 | */ |
| 55 | private final ServiceTrackerCustomizer customizer; |
| 56 | /** |
| 57 | * Filter string for use when adding the ServiceListener. If this field is |
| 58 | * set, then certain optimizations can be taken since we don't have a user |
| 59 | * supplied filter. |
| 60 | */ |
| 61 | private final String listenerFilter; |
| 62 | /** |
| 63 | * Class name to be tracked. If this field is set, then we are tracking by |
| 64 | * class name. |
| 65 | */ |
| 66 | private final String trackClass; |
| 67 | /** |
| 68 | * Reference to be tracked. If this field is set, then we are tracking a |
| 69 | * single ServiceReference. |
| 70 | */ |
| 71 | private final ServiceReference trackReference; |
| 72 | /** |
| 73 | * Tracked services: <code>ServiceReference</code> object -> customized |
| 74 | * Object and <code>ServiceListener</code> object |
| 75 | */ |
| 76 | private Tracked tracked; |
| 77 | /** |
| 78 | * Modification count. This field is initialized to zero by open, set to -1 |
| 79 | * by close and incremented by modified. This field is volatile since it is |
| 80 | * accessed by multiple threads. |
| 81 | */ |
| 82 | private volatile int trackingCount = -1; |
| 83 | /** |
| 84 | * Cached ServiceReference for getServiceReference. This field is volatile |
| 85 | * since it is accessed by multiple threads. |
| 86 | */ |
| 87 | private volatile ServiceReference cachedReference; |
| 88 | /** |
| 89 | * Cached service object for getService. This field is volatile since it is |
| 90 | * accessed by multiple threads. |
| 91 | */ |
| 92 | private volatile Object cachedService; |
| 93 | |
| 94 | /** |
| 95 | * Create a <code>ServiceTracker</code> object on the specified |
| 96 | * <code>ServiceReference</code> object. |
| 97 | * |
| 98 | * <p> |
| 99 | * The service referenced by the specified <code>ServiceReference</code> |
| 100 | * object will be tracked by this <code>ServiceTracker</code> object. |
| 101 | * |
| 102 | * @param context <code>BundleContext</code> object against which the |
| 103 | * tracking is done. |
| 104 | * @param reference <code>ServiceReference</code> object for the service |
| 105 | * to be tracked. |
| 106 | * @param customizer The customizer object to call when services are added, |
| 107 | * modified, or removed in this <code>ServiceTracker</code> object. |
| 108 | * If customizer is <code>null</code>, then this |
| 109 | * <code>ServiceTracker</code> object will be used as the |
| 110 | * <code>ServiceTrackerCustomizer</code> object and the |
| 111 | * <code>ServiceTracker</code> object will call the |
| 112 | * <code>ServiceTrackerCustomizer</code> methods on itself. |
| 113 | */ |
| 114 | public ServiceTracker(BundleContext context, ServiceReference reference, |
| 115 | ServiceTrackerCustomizer customizer) { |
| 116 | this.context = context; |
| 117 | this.trackReference = reference; |
| 118 | this.trackClass = null; |
| 119 | this.customizer = (customizer == null) ? this : customizer; |
| 120 | this.listenerFilter = "(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 121 | try { |
| 122 | this.filter = context.createFilter(listenerFilter); |
| 123 | } |
| 124 | catch (InvalidSyntaxException e) { // we could only get this exception |
| 125 | // if the ServiceReference was |
| 126 | // invalid |
| 127 | throw new IllegalArgumentException( |
| 128 | "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$ |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Create a <code>ServiceTracker</code> object on the specified class |
| 134 | * name. |
| 135 | * |
| 136 | * <p> |
| 137 | * Services registered under the specified class name will be tracked by |
| 138 | * this <code>ServiceTracker</code> object. |
| 139 | * |
| 140 | * @param context <code>BundleContext</code> object against which the |
| 141 | * tracking is done. |
| 142 | * @param clazz Class name of the services to be tracked. |
| 143 | * @param customizer The customizer object to call when services are added, |
| 144 | * modified, or removed in this <code>ServiceTracker</code> object. |
| 145 | * If customizer is <code>null</code>, then this |
| 146 | * <code>ServiceTracker</code> object will be used as the |
| 147 | * <code>ServiceTrackerCustomizer</code> object and the |
| 148 | * <code>ServiceTracker</code> object will call the |
| 149 | * <code>ServiceTrackerCustomizer</code> methods on itself. |
| 150 | */ |
| 151 | public ServiceTracker(BundleContext context, String clazz, |
| 152 | ServiceTrackerCustomizer customizer) { |
| 153 | this.context = context; |
| 154 | this.trackReference = null; |
| 155 | this.trackClass = clazz; |
| 156 | this.customizer = (customizer == null) ? this : customizer; |
| 157 | this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz.toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 158 | try { |
| 159 | this.filter = context.createFilter(listenerFilter); |
| 160 | } |
| 161 | catch (InvalidSyntaxException e) { // we could only get this exception |
| 162 | // if the clazz argument was |
| 163 | // malformed |
| 164 | throw new IllegalArgumentException( |
| 165 | "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$ |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Create a <code>ServiceTracker</code> object on the specified |
| 171 | * <code>Filter</code> object. |
| 172 | * |
| 173 | * <p> |
| 174 | * Services which match the specified <code>Filter</code> object will be |
| 175 | * tracked by this <code>ServiceTracker</code> object. |
| 176 | * |
| 177 | * @param context <code>BundleContext</code> object against which the |
| 178 | * tracking is done. |
| 179 | * @param filter <code>Filter</code> object to select the services to be |
| 180 | * tracked. |
| 181 | * @param customizer The customizer object to call when services are added, |
| 182 | * modified, or removed in this <code>ServiceTracker</code> object. |
| 183 | * If customizer is null, then this <code>ServiceTracker</code> |
| 184 | * object will be used as the <code>ServiceTrackerCustomizer</code> |
| 185 | * object and the <code>ServiceTracker</code> object will call the |
| 186 | * <code>ServiceTrackerCustomizer</code> methods on itself. |
| 187 | * @since 1.1 |
| 188 | */ |
| 189 | public ServiceTracker(BundleContext context, Filter filter, |
| 190 | ServiceTrackerCustomizer customizer) { |
| 191 | this.context = context; |
| 192 | this.trackReference = null; |
| 193 | this.trackClass = null; |
| 194 | this.listenerFilter = null; |
| 195 | this.filter = filter; |
| 196 | this.customizer = (customizer == null) ? this : customizer; |
| 197 | if ((context == null) || (filter == null)) { // we throw a NPE here |
| 198 | // to |
| 199 | // be consistent with the |
| 200 | // other constructors |
| 201 | throw new NullPointerException(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Open this <code>ServiceTracker</code> object and begin tracking |
| 207 | * services. |
| 208 | * |
| 209 | * <p> |
| 210 | * This method calls <code>open(false)</code>. |
| 211 | * |
| 212 | * @throws java.lang.IllegalStateException if the <code>BundleContext</code> |
| 213 | * object with which this <code>ServiceTracker</code> object was |
| 214 | * created is no longer valid. |
| 215 | * @see #open(boolean) |
| 216 | */ |
| 217 | public void open() { |
| 218 | open(false); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Open this <code>ServiceTracker</code> object and begin tracking |
| 223 | * services. |
| 224 | * |
| 225 | * <p> |
| 226 | * Services which match the search criteria specified when this |
| 227 | * <code>ServiceTracker</code> object was created are now tracked by this |
| 228 | * <code>ServiceTracker</code> object. |
| 229 | * |
| 230 | * @param trackAllServices If <code>true</code>, then this |
| 231 | * <code>ServiceTracker</code> will track all matching services |
| 232 | * regardless of class loader accessibility. If <code>false</code>, |
| 233 | * then this <code>ServiceTracker</code> will only track matching |
| 234 | * services which are class loader accessibile to the bundle whose |
| 235 | * <code>BundleContext</code> is used by this |
| 236 | * <code>ServiceTracker</code>. |
| 237 | * @throws java.lang.IllegalStateException if the <code>BundleContext</code> |
| 238 | * object with which this <code>ServiceTracker</code> object was |
| 239 | * created is no longer valid. |
| 240 | * @since 1.3 |
| 241 | */ |
| 242 | public synchronized void open(boolean trackAllServices) { |
| 243 | if (tracked != null) { |
| 244 | return; |
| 245 | } |
| 246 | if (DEBUG) { |
| 247 | System.out.println("ServiceTracker.open: " + filter); //$NON-NLS-1$ |
| 248 | } |
| 249 | tracked = trackAllServices ? new AllTracked() : new Tracked(); |
| 250 | trackingCount = 0; |
| 251 | ServiceReference[] references; |
| 252 | synchronized (tracked) { |
| 253 | try { |
| 254 | context.addServiceListener(tracked, listenerFilter); |
| 255 | if (listenerFilter == null) { // user supplied filter |
| 256 | references = getInitialReferences(trackAllServices, null, |
| 257 | filter.toString()); |
| 258 | } |
| 259 | else { // constructor supplied filter |
| 260 | if (trackClass == null) { |
| 261 | references = new ServiceReference[] {trackReference}; |
| 262 | } |
| 263 | else { |
| 264 | references = getInitialReferences(trackAllServices, |
| 265 | trackClass, null); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | catch (InvalidSyntaxException e) { |
| 270 | throw new RuntimeException( |
| 271 | "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$ |
| 272 | } |
| 273 | } |
| 274 | /* Call tracked outside of synchronized region */ |
| 275 | if (references != null) { |
| 276 | int length = references.length; |
| 277 | for (int i = 0; i < length; i++) { |
| 278 | ServiceReference reference = references[i]; |
| 279 | /* if the service is still registered */ |
| 280 | if (reference.getBundle() != null) { |
| 281 | tracked.track(reference); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns the list of initial <code>ServiceReference</code> objects that |
| 289 | * will be tracked by this <code>ServiceTracker</code> object. |
| 290 | * |
| 291 | * @param trackAllServices If true, use getAllServiceReferences. |
| 292 | * @param trackClass the class name with which the service was registered, |
| 293 | * or null for all services. |
| 294 | * @param filterString the filter criteria or null for all services. |
| 295 | * @return the list of initial <code>ServiceReference</code> objects. |
| 296 | * @throws InvalidSyntaxException if the filter uses an invalid syntax. |
| 297 | */ |
| 298 | private ServiceReference[] getInitialReferences(boolean trackAllServices, |
| 299 | String trackClass, String filterString) |
| 300 | throws InvalidSyntaxException { |
| 301 | if (trackAllServices) { |
| 302 | return context.getAllServiceReferences(trackClass, filterString); |
| 303 | } |
| 304 | else { |
| 305 | return context.getServiceReferences(trackClass, filterString); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Close this <code>ServiceTracker</code> object. |
| 311 | * |
| 312 | * <p> |
| 313 | * This method should be called when this <code>ServiceTracker</code> |
| 314 | * object should end the tracking of services. |
| 315 | */ |
| 316 | public synchronized void close() { |
| 317 | if (tracked == null) { |
| 318 | return; |
| 319 | } |
| 320 | if (DEBUG) { |
| 321 | System.out.println("ServiceTracker.close: " + filter); //$NON-NLS-1$ |
| 322 | } |
| 323 | tracked.close(); |
| 324 | ServiceReference[] references = getServiceReferences(); |
| 325 | Tracked outgoing = tracked; |
| 326 | tracked = null; |
| 327 | try { |
| 328 | context.removeServiceListener(outgoing); |
| 329 | } |
| 330 | catch (IllegalStateException e) { |
| 331 | /* In case the context was stopped. */ |
| 332 | } |
| 333 | if (references != null) { |
| 334 | for (int i = 0; i < references.length; i++) { |
| 335 | outgoing.untrack(references[i]); |
| 336 | } |
| 337 | } |
| 338 | trackingCount = -1; |
| 339 | if (DEBUG) { |
| 340 | if ((cachedReference == null) && (cachedService == null)) { |
| 341 | System.out |
| 342 | .println("ServiceTracker.close[cached cleared]: " + filter); //$NON-NLS-1$ |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Default implementation of the |
| 349 | * <code>ServiceTrackerCustomizer.addingService</code> method. |
| 350 | * |
| 351 | * <p> |
| 352 | * This method is only called when this <code>ServiceTracker</code> object |
| 353 | * has been constructed with a <code>null ServiceTrackerCustomizer</code> |
| 354 | * argument. |
| 355 | * |
| 356 | * The default implementation returns the result of calling |
| 357 | * <code>getService</code>, on the <code>BundleContext</code> object |
| 358 | * with which this <code>ServiceTracker</code> object was created, passing |
| 359 | * the specified <code>ServiceReference</code> object. |
| 360 | * <p> |
| 361 | * This method can be overridden in a subclass to customize the service |
| 362 | * object to be tracked for the service being added. In that case, take care |
| 363 | * not to rely on the default implementation of removedService that will |
| 364 | * unget the service. |
| 365 | * |
| 366 | * @param reference Reference to service being added to this |
| 367 | * <code>ServiceTracker</code> object. |
| 368 | * @return The service object to be tracked for the service added to this |
| 369 | * <code>ServiceTracker</code> object. |
| 370 | * @see ServiceTrackerCustomizer |
| 371 | */ |
| 372 | public Object addingService(ServiceReference reference) { |
| 373 | return context.getService(reference); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Default implementation of the |
| 378 | * <code>ServiceTrackerCustomizer.modifiedService</code> method. |
| 379 | * |
| 380 | * <p> |
| 381 | * This method is only called when this <code>ServiceTracker</code> object |
| 382 | * has been constructed with a <code>null ServiceTrackerCustomizer</code> |
| 383 | * argument. |
| 384 | * |
| 385 | * The default implementation does nothing. |
| 386 | * |
| 387 | * @param reference Reference to modified service. |
| 388 | * @param service The service object for the modified service. |
| 389 | * @see ServiceTrackerCustomizer |
| 390 | */ |
| 391 | public void modifiedService(ServiceReference reference, Object service) { |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Default implementation of the |
| 396 | * <code>ServiceTrackerCustomizer.removedService</code> method. |
| 397 | * |
| 398 | * <p> |
| 399 | * This method is only called when this <code>ServiceTracker</code> object |
| 400 | * has been constructed with a <code>null ServiceTrackerCustomizer</code> |
| 401 | * argument. |
| 402 | * |
| 403 | * The default implementation calls <code>ungetService</code>, on the |
| 404 | * <code>BundleContext</code> object with which this |
| 405 | * <code>ServiceTracker</code> object was created, passing the specified |
| 406 | * <code>ServiceReference</code> object. |
| 407 | * <p> |
| 408 | * This method can be overridden in a subclass. If the default |
| 409 | * implementation of <code>addingService</code> method was used, this |
| 410 | * method must unget the service. |
| 411 | * |
| 412 | * @param reference Reference to removed service. |
| 413 | * @param service The service object for the removed service. |
| 414 | * @see ServiceTrackerCustomizer |
| 415 | */ |
| 416 | public void removedService(ServiceReference reference, Object service) { |
| 417 | context.ungetService(reference); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Wait for at least one service to be tracked by this |
| 422 | * <code>ServiceTracker</code> object. |
| 423 | * <p> |
| 424 | * It is strongly recommended that <code>waitForService</code> is not used |
| 425 | * during the calling of the <code>BundleActivator</code> methods. |
| 426 | * <code>BundleActivator</code> methods are expected to complete in a |
| 427 | * short period of time. |
| 428 | * |
| 429 | * @param timeout time interval in milliseconds to wait. If zero, the method |
| 430 | * will wait indefinately. |
| 431 | * @return Returns the result of <code>getService()</code>. |
| 432 | * @throws IllegalArgumentException If the value of timeout is negative. |
| 433 | */ |
| 434 | public Object waitForService(long timeout) throws InterruptedException { |
| 435 | if (timeout < 0) { |
| 436 | throw new IllegalArgumentException("timeout value is negative"); //$NON-NLS-1$ |
| 437 | } |
| 438 | Object object = getService(); |
| 439 | while (object == null) { |
| 440 | Tracked tracked = this.tracked; /* |
| 441 | * use local var since we are not |
| 442 | * synchronized |
| 443 | */ |
| 444 | if (tracked == null) /* if ServiceTracker is not open */ |
| 445 | { |
| 446 | return null; |
| 447 | } |
| 448 | synchronized (tracked) { |
| 449 | if (tracked.size() == 0) { |
| 450 | tracked.wait(timeout); |
| 451 | } |
| 452 | } |
| 453 | object = getService(); |
| 454 | if (timeout > 0) { |
| 455 | return object; |
| 456 | } |
| 457 | } |
| 458 | return object; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Return an array of <code>ServiceReference</code> objects for all |
| 463 | * services being tracked by this <code>ServiceTracker</code> object. |
| 464 | * |
| 465 | * @return Array of <code>ServiceReference</code> objects or |
| 466 | * <code>null</code> if no service are being tracked. |
| 467 | */ |
| 468 | public ServiceReference[] getServiceReferences() { |
| 469 | Tracked tracked = this.tracked; /* |
| 470 | * use local var since we are not |
| 471 | * synchronized |
| 472 | */ |
| 473 | if (tracked == null) /* if ServiceTracker is not open */ |
| 474 | { |
| 475 | return null; |
| 476 | } |
| 477 | synchronized (tracked) { |
| 478 | int length = tracked.size(); |
| 479 | if (length == 0) { |
| 480 | return null; |
| 481 | } |
| 482 | ServiceReference[] references = new ServiceReference[length]; |
| 483 | Enumeration keys = tracked.keys(); |
| 484 | for (int i = 0; i < length; i++) { |
| 485 | references[i] = (ServiceReference) keys.nextElement(); |
| 486 | } |
| 487 | return references; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Returns a <code>ServiceReference</code> object for one of the services |
| 493 | * being tracked by this <code>ServiceTracker</code> object. |
| 494 | * |
| 495 | * <p> |
| 496 | * If multiple services are being tracked, the service with the highest |
| 497 | * ranking (as specified in its <code>service.ranking</code> property) is |
| 498 | * returned. |
| 499 | * |
| 500 | * <p> |
| 501 | * If there is a tie in ranking, the service with the lowest service ID (as |
| 502 | * specified in its <code>service.id</code> property); that is, the |
| 503 | * service that was registered first is returned. |
| 504 | * <p> |
| 505 | * This is the same algorithm used by |
| 506 | * <code>BundleContext.getServiceReference</code>. |
| 507 | * |
| 508 | * @return <code>ServiceReference</code> object or <code>null</code> if |
| 509 | * no service is being tracked. |
| 510 | * @since 1.1 |
| 511 | */ |
| 512 | public ServiceReference getServiceReference() { |
| 513 | ServiceReference reference = cachedReference; |
| 514 | if (reference != null) { |
| 515 | if (DEBUG) { |
| 516 | System.out |
| 517 | .println("ServiceTracker.getServiceReference[cached]: " + filter); //$NON-NLS-1$ |
| 518 | } |
| 519 | return reference; |
| 520 | } |
| 521 | if (DEBUG) { |
| 522 | System.out.println("ServiceTracker.getServiceReference: " + filter); //$NON-NLS-1$ |
| 523 | } |
| 524 | ServiceReference[] references = getServiceReferences(); |
| 525 | int length = (references == null) ? 0 : references.length; |
| 526 | if (length == 0) /* if no service is being tracked */ |
| 527 | { |
| 528 | return null; |
| 529 | } |
| 530 | int index = 0; |
| 531 | if (length > 1) /* if more than one service, select highest ranking */ |
| 532 | { |
| 533 | int rankings[] = new int[length]; |
| 534 | int count = 0; |
| 535 | int maxRanking = Integer.MIN_VALUE; |
| 536 | for (int i = 0; i < length; i++) { |
| 537 | Object property = references[i] |
| 538 | .getProperty(Constants.SERVICE_RANKING); |
| 539 | int ranking = (property instanceof Integer) ? ((Integer) property) |
| 540 | .intValue() |
| 541 | : 0; |
| 542 | rankings[i] = ranking; |
| 543 | if (ranking > maxRanking) { |
| 544 | index = i; |
| 545 | maxRanking = ranking; |
| 546 | count = 1; |
| 547 | } |
| 548 | else { |
| 549 | if (ranking == maxRanking) { |
| 550 | count++; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | if (count > 1) /* if still more than one service, select lowest id */ |
| 555 | { |
| 556 | long minId = Long.MAX_VALUE; |
| 557 | for (int i = 0; i < length; i++) { |
| 558 | if (rankings[i] == maxRanking) { |
| 559 | long id = ((Long) (references[i] |
| 560 | .getProperty(Constants.SERVICE_ID))) |
| 561 | .longValue(); |
| 562 | if (id < minId) { |
| 563 | index = i; |
| 564 | minId = id; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | return cachedReference = references[index]; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Returns the service object for the specified |
| 575 | * <code>ServiceReference</code> object if the referenced service is being |
| 576 | * tracked by this <code>ServiceTracker</code> object. |
| 577 | * |
| 578 | * @param reference Reference to the desired service. |
| 579 | * @return Service object or <code>null</code> if the service referenced |
| 580 | * by the specified <code>ServiceReference</code> object is not |
| 581 | * being tracked. |
| 582 | */ |
| 583 | public Object getService(ServiceReference reference) { |
| 584 | Tracked tracked = this.tracked; /* |
| 585 | * use local var since we are not |
| 586 | * synchronized |
| 587 | */ |
| 588 | if (tracked == null) /* if ServiceTracker is not open */ |
| 589 | { |
| 590 | return null; |
| 591 | } |
| 592 | synchronized (tracked) { |
| 593 | return tracked.get(reference); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Return an array of service objects for all services being tracked by this |
| 599 | * <code>ServiceTracker</code> object. |
| 600 | * |
| 601 | * @return Array of service objects or <code>null</code> if no service are |
| 602 | * being tracked. |
| 603 | */ |
| 604 | public Object[] getServices() { |
| 605 | Tracked tracked = this.tracked; /* |
| 606 | * use local var since we are not |
| 607 | * synchronized |
| 608 | */ |
| 609 | if (tracked == null) /* if ServiceTracker is not open */ |
| 610 | { |
| 611 | return null; |
| 612 | } |
| 613 | synchronized (tracked) { |
| 614 | ServiceReference[] references = getServiceReferences(); |
| 615 | int length = (references == null) ? 0 : references.length; |
| 616 | if (length == 0) { |
| 617 | return null; |
| 618 | } |
| 619 | Object[] objects = new Object[length]; |
| 620 | for (int i = 0; i < length; i++) { |
| 621 | objects[i] = getService(references[i]); |
| 622 | } |
| 623 | return objects; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Returns a service object for one of the services being tracked by this |
| 629 | * <code>ServiceTracker</code> object. |
| 630 | * |
| 631 | * <p> |
| 632 | * If any services are being tracked, this method returns the result of |
| 633 | * calling <code>getService(getServiceReference())</code>. |
| 634 | * |
| 635 | * @return Service object or <code>null</code> if no service is being |
| 636 | * tracked. |
| 637 | */ |
| 638 | public Object getService() { |
| 639 | Object service = cachedService; |
| 640 | if (service != null) { |
| 641 | if (DEBUG) { |
| 642 | System.out |
| 643 | .println("ServiceTracker.getService[cached]: " + filter); //$NON-NLS-1$ |
| 644 | } |
| 645 | return service; |
| 646 | } |
| 647 | if (DEBUG) { |
| 648 | System.out.println("ServiceTracker.getService: " + filter); //$NON-NLS-1$ |
| 649 | } |
| 650 | ServiceReference reference = getServiceReference(); |
| 651 | if (reference == null) { |
| 652 | return null; |
| 653 | } |
| 654 | return cachedService = getService(reference); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Remove a service from this <code>ServiceTracker</code> object. |
| 659 | * |
| 660 | * The specified service will be removed from this |
| 661 | * <code>ServiceTracker</code> object. If the specified service was being |
| 662 | * tracked then the <code>ServiceTrackerCustomizer.removedService</code> |
| 663 | * method will be called for that service. |
| 664 | * |
| 665 | * @param reference Reference to the service to be removed. |
| 666 | */ |
| 667 | public void remove(ServiceReference reference) { |
| 668 | Tracked tracked = this.tracked; /* |
| 669 | * use local var since we are not |
| 670 | * synchronized |
| 671 | */ |
| 672 | if (tracked == null) /* if ServiceTracker is not open */ |
| 673 | { |
| 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 | { |
| 692 | return 0; |
| 693 | } |
| 694 | return tracked.size(); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Returns the tracking count for this <code>ServiceTracker</code> object. |
| 699 | * |
| 700 | * The tracking count is initialized to 0 when this |
| 701 | * <code>ServiceTracker</code> object is opened. Every time a service is |
| 702 | * added or removed from this <code>ServiceTracker</code> object the |
| 703 | * tracking count is incremented. |
| 704 | * |
| 705 | * <p> |
| 706 | * The tracking count can be used to determine if this |
| 707 | * <code>ServiceTracker</code> object has added or removed a service by |
| 708 | * comparing a tracking count value previously collected with the current |
| 709 | * tracking count value. If the value has not changed, then no service has |
| 710 | * been added or removed from this <code>ServiceTracker</code> object |
| 711 | * since the previous tracking count 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 | /* |
| 726 | * This method must not be synchronized since it is called by Tracked while |
| 727 | * Tracked is synchronized. We don't want synchronization interactions |
| 728 | * between the ServiceListener thread and the user thread. |
| 729 | */ |
| 730 | private void modified() { |
| 731 | trackingCount++; /* increment modification count */ |
| 732 | cachedReference = null; /* clear cached value */ |
| 733 | cachedService = null; /* clear cached value */ |
| 734 | if (DEBUG) { |
| 735 | System.out.println("ServiceTracker.modified: " + filter); //$NON-NLS-1$ |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Inner class to track services. If a <code>ServiceTracker</code> object |
| 741 | * is reused (closed then reopened), then a new Tracked object is used. This |
| 742 | * class is a hashtable mapping <code>ServiceReference</code> object -> |
| 743 | * customized Object. This class is the <code>ServiceListener</code> |
| 744 | * object for the tracker. This class is used to synchronize access to the |
| 745 | * tracked services. This is not a public class. It is only for use by the |
| 746 | * implementation of the <code>ServiceTracker</code> class. |
| 747 | * |
| 748 | */ |
| 749 | class Tracked extends Hashtable implements ServiceListener { |
| 750 | static final long serialVersionUID = -7420065199791006079L; |
| 751 | /** |
| 752 | * List of ServiceReferences in the process of being added. |
| 753 | */ |
| 754 | private ArrayList adding; |
| 755 | /** |
| 756 | * true if the tracked object is closed. This field is volatile because |
| 757 | * it is set by one thread and read by another. |
| 758 | */ |
| 759 | private volatile boolean closed; |
| 760 | |
| 761 | /** |
| 762 | * Tracked constructor. |
| 763 | */ |
| 764 | protected Tracked() { |
| 765 | super(); |
| 766 | closed = false; |
| 767 | adding = new ArrayList(6); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Called by the owning <code>ServiceTracker</code> object when it is |
| 772 | * closed. |
| 773 | */ |
| 774 | protected void close() { |
| 775 | closed = true; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * <code>ServiceListener</code> method for the |
| 780 | * <code>ServiceTracker</code> class. This method must NOT be |
| 781 | * synchronized to avoid deadlock potential. |
| 782 | * |
| 783 | * @param event <code>ServiceEvent</code> object from the framework. |
| 784 | */ |
| 785 | public void serviceChanged(ServiceEvent event) { |
| 786 | /* |
| 787 | * Check if we had a delayed call (which could happen when we |
| 788 | * close). |
| 789 | */ |
| 790 | if (closed) { |
| 791 | return; |
| 792 | } |
| 793 | ServiceReference reference = event.getServiceReference(); |
| 794 | switch (event.getType()) { |
| 795 | case ServiceEvent.REGISTERED : |
| 796 | case ServiceEvent.MODIFIED : |
| 797 | if (listenerFilter != null) { // constructor supplied |
| 798 | // filter |
| 799 | track(reference); |
| 800 | /* |
| 801 | * If the customizer throws an unchecked exception, it |
| 802 | * is safe to let it propagate |
| 803 | */ |
| 804 | } |
| 805 | else { // user supplied filter |
| 806 | if (filter.match(reference)) { |
| 807 | track(reference); |
| 808 | /* |
| 809 | * If the customizer throws an unchecked exception, |
| 810 | * it is safe to let it propagate |
| 811 | */ |
| 812 | } |
| 813 | else { |
| 814 | untrack(reference); |
| 815 | /* |
| 816 | * If the customizer throws an unchecked exception, |
| 817 | * it is safe to let it propagate |
| 818 | */ |
| 819 | } |
| 820 | } |
| 821 | break; |
| 822 | case ServiceEvent.UNREGISTERING : |
| 823 | untrack(reference); |
| 824 | /* |
| 825 | * If the customizer throws an unchecked exception, it is |
| 826 | * safe to let it propagate |
| 827 | */ |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Begin to track the referenced service. |
| 834 | * |
| 835 | * @param reference Reference to a service to be tracked. |
| 836 | */ |
| 837 | protected void track(ServiceReference reference) { |
| 838 | Object object; |
| 839 | synchronized (this) { |
| 840 | object = this.get(reference); |
| 841 | } |
| 842 | if (object != null) /* we are already tracking the service */ |
| 843 | { |
| 844 | if (DEBUG) { |
| 845 | System.out |
| 846 | .println("ServiceTracker.Tracked.track[modified]: " + reference); //$NON-NLS-1$ |
| 847 | } |
| 848 | /* Call customizer outside of synchronized region */ |
| 849 | customizer.modifiedService(reference, object); |
| 850 | /* |
| 851 | * If the customizer throws an unchecked exception, it is safe |
| 852 | * to let it propagate |
| 853 | */ |
| 854 | return; |
| 855 | } |
| 856 | synchronized (this) { |
| 857 | if (adding.contains(reference)) /* |
| 858 | * if this service is already in |
| 859 | * the process of being added. |
| 860 | */ |
| 861 | { |
| 862 | if (DEBUG) { |
| 863 | System.out |
| 864 | .println("ServiceTracker.Tracked.track[already adding]: " + reference); //$NON-NLS-1$ |
| 865 | } |
| 866 | return; |
| 867 | } |
| 868 | adding.add(reference); /* mark this service is being added */ |
| 869 | } |
| 870 | if (DEBUG) { |
| 871 | System.out |
| 872 | .println("ServiceTracker.Tracked.track[adding]: " + reference); //$NON-NLS-1$ |
| 873 | } |
| 874 | boolean becameUntracked = false; |
| 875 | /* Call customizer outside of synchronized region */ |
| 876 | try { |
| 877 | object = customizer.addingService(reference); |
| 878 | /* |
| 879 | * If the customizer throws an unchecked exception, it will |
| 880 | * propagate after the finally |
| 881 | */ |
| 882 | } |
| 883 | finally { |
| 884 | synchronized (this) { |
| 885 | if (adding.remove(reference)) /* |
| 886 | * if the service was not |
| 887 | * untracked during the |
| 888 | * customizer callback |
| 889 | */ |
| 890 | { |
| 891 | if (object != null) { |
| 892 | this.put(reference, object); |
| 893 | modified(); /* increment modification count */ |
| 894 | notifyAll(); |
| 895 | } |
| 896 | } |
| 897 | else { |
| 898 | becameUntracked = true; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | /* |
| 903 | * The service became untracked during the customizer callback. |
| 904 | */ |
| 905 | if (becameUntracked) { |
| 906 | if (DEBUG) { |
| 907 | System.out |
| 908 | .println("ServiceTracker.Tracked.track[removed]: " + reference); //$NON-NLS-1$ |
| 909 | } |
| 910 | /* Call customizer outside of synchronized region */ |
| 911 | customizer.removedService(reference, object); |
| 912 | /* |
| 913 | * If the customizer throws an unchecked exception, it is safe |
| 914 | * to let it propagate |
| 915 | */ |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Discontinue tracking the referenced service. |
| 921 | * |
| 922 | * @param reference Reference to the tracked service. |
| 923 | */ |
| 924 | protected void untrack(ServiceReference reference) { |
| 925 | Object object; |
| 926 | synchronized (this) { |
| 927 | if (adding.remove(reference)) /* |
| 928 | * if the service is in the |
| 929 | * process of being added |
| 930 | */ |
| 931 | { |
| 932 | if (DEBUG) { |
| 933 | System.out |
| 934 | .println("ServiceTracker.Tracked.untrack[being added]: " + reference); //$NON-NLS-1$ |
| 935 | } |
| 936 | return; /* |
| 937 | * in case the service is untracked while in the |
| 938 | * process of adding |
| 939 | */ |
| 940 | } |
| 941 | object = this.remove(reference); /* |
| 942 | * must remove from tracker |
| 943 | * before calling customizer |
| 944 | * callback |
| 945 | */ |
| 946 | if (object == null) /* are we actually tracking the service */ |
| 947 | { |
| 948 | return; |
| 949 | } |
| 950 | modified(); /* increment modification count */ |
| 951 | } |
| 952 | if (DEBUG) { |
| 953 | System.out |
| 954 | .println("ServiceTracker.Tracked.untrack[removed]: " + reference); //$NON-NLS-1$ |
| 955 | } |
| 956 | /* Call customizer outside of synchronized region */ |
| 957 | customizer.removedService(reference, object); |
| 958 | /* |
| 959 | * If the customizer throws an unchecked exception, it is safe to |
| 960 | * let it propagate |
| 961 | */ |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Subclass of Tracked which implements the AllServiceListener interface. |
| 967 | * This class is used by the ServiceTracker if isAllServiceTracker returns |
| 968 | * true. |
| 969 | * |
| 970 | * @since 1.3 |
| 971 | */ |
| 972 | class AllTracked extends Tracked implements AllServiceListener { |
| 973 | static final long serialVersionUID = 4050764875305137716L; |
| 974 | |
| 975 | /** |
| 976 | * Tracked constructor. |
| 977 | */ |
| 978 | protected AllTracked() { |
| 979 | super(); |
| 980 | } |
| 981 | } |
| 982 | } |