moving around some of the code in preparation for some extensions
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@883733 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTracker.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTracker.java
deleted file mode 100644
index 83c4b4b..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTracker.java
+++ /dev/null
@@ -1,1153 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.felix.dependencymanager;
-
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.LinkedList;
-
-import org.osgi.framework.AllServiceListener;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.Filter;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.ServiceReference;
-
-/**
- * A modified <code>ServiceTracker</code> class simplifies using services
- * from the Framework's service registry. This class is used internally
- * by the dependency manager. It is based on the OSGi R4.1 sources, which
- * are made available under the same ASF 2.0 license.
- *
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
-public class ServiceTracker implements ServiceTrackerCustomizer {
- /* set this to true to compile in debug messages */
- static final boolean DEBUG = false;
- /**
- * Bundle context against which this <code>ServiceTracker</code> object is
- * tracking.
- */
- protected final BundleContext context;
- /**
- * Filter specifying search criteria for the services to track.
- *
- * @since 1.1
- */
- protected final Filter filter;
- /**
- * <code>ServiceTrackerCustomizer</code> object for this tracker.
- */
- final ServiceTrackerCustomizer customizer;
- /**
- * Filter string for use when adding the ServiceListener. If this field is
- * set, then certain optimizations can be taken since we don't have a user
- * supplied filter.
- */
- final String listenerFilter;
- /**
- * Class name to be tracked. If this field is set, then we are tracking by
- * class name.
- */
- private final String trackClass;
- /**
- * Reference to be tracked. If this field is set, then we are tracking a
- * single ServiceReference.
- */
- private final ServiceReference trackReference;
- /**
- * Tracked services: <code>ServiceReference</code> object -> customized
- * Object and <code>ServiceListener</code> object
- */
- private volatile Tracked tracked;
- /**
- * Modification count. This field is initialized to zero by open, set to -1
- * by close and incremented by modified.
- *
- * This field is volatile since it is accessed by multiple threads.
- */
- private volatile int trackingCount = -1;
- /**
- * Cached ServiceReference for getServiceReference.
- *
- * This field is volatile since it is accessed by multiple threads.
- */
- private volatile ServiceReference cachedReference;
- /**
- * Cached service object for getService.
- *
- * This field is volatile since it is accessed by multiple threads.
- */
- private volatile Object cachedService;
-
- /**
- * Create a <code>ServiceTracker</code> object on the specified
- * <code>ServiceReference</code> object.
- *
- * <p>
- * The service referenced by the specified <code>ServiceReference</code>
- * object will be tracked by this <code>ServiceTracker</code> object.
- *
- * @param context <code>BundleContext</code> object against which the
- * tracking is done.
- * @param reference <code>ServiceReference</code> object for the service
- * to be tracked.
- * @param customizer The customizer object to call when services are added,
- * modified, or removed in this <code>ServiceTracker</code> object.
- * If customizer is <code>null</code>, then this
- * <code>ServiceTracker</code> object will be used as the
- * <code>ServiceTrackerCustomizer</code> object and the
- * <code>ServiceTracker</code> object will call the
- * <code>ServiceTrackerCustomizer</code> methods on itself.
- */
- public ServiceTracker(BundleContext context, ServiceReference reference,
- ServiceTrackerCustomizer customizer) {
- this.context = context;
- this.trackReference = reference;
- this.trackClass = null;
- this.customizer = (customizer == null) ? this : customizer;
- this.listenerFilter = "(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- try {
- this.filter = context.createFilter(listenerFilter);
- }
- catch (InvalidSyntaxException e) { // we could only get this exception
- // if the ServiceReference was
- // invalid
- throw new IllegalArgumentException(
- "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
- }
- }
-
- /**
- * Create a <code>ServiceTracker</code> object on the specified class
- * name.
- *
- * <p>
- * Services registered under the specified class name will be tracked by
- * this <code>ServiceTracker</code> object.
- *
- * @param context <code>BundleContext</code> object against which the
- * tracking is done.
- * @param clazz Class name of the services to be tracked.
- * @param customizer The customizer object to call when services are added,
- * modified, or removed in this <code>ServiceTracker</code> object.
- * If customizer is <code>null</code>, then this
- * <code>ServiceTracker</code> object will be used as the
- * <code>ServiceTrackerCustomizer</code> object and the
- * <code>ServiceTracker</code> object will call the
- * <code>ServiceTrackerCustomizer</code> methods on itself.
- */
- public ServiceTracker(BundleContext context, String clazz,
- ServiceTrackerCustomizer customizer) {
- this.context = context;
- this.trackReference = null;
- this.trackClass = clazz;
- this.customizer = (customizer == null) ? this : customizer;
- this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- try {
- this.filter = context.createFilter(listenerFilter);
- }
- catch (InvalidSyntaxException e) { // we could only get this exception
- // if the clazz argument was
- // malformed
- throw new IllegalArgumentException(
- "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
- }
- }
-
- /**
- * Create a <code>ServiceTracker</code> object on the specified
- * <code>Filter</code> object.
- *
- * <p>
- * Services which match the specified <code>Filter</code> object will be
- * tracked by this <code>ServiceTracker</code> object.
- *
- * @param context <code>BundleContext</code> object against which the
- * tracking is done.
- * @param filter <code>Filter</code> object to select the services to be
- * tracked.
- * @param customizer The customizer object to call when services are added,
- * modified, or removed in this <code>ServiceTracker</code> object.
- * If customizer is null, then this <code>ServiceTracker</code>
- * object will be used as the <code>ServiceTrackerCustomizer</code>
- * object and the <code>ServiceTracker</code> object will call the
- * <code>ServiceTrackerCustomizer</code> methods on itself.
- * @since 1.1
- */
- public ServiceTracker(BundleContext context, Filter filter,
- ServiceTrackerCustomizer customizer) {
- this.context = context;
- this.trackReference = null;
- this.trackClass = null;
- this.listenerFilter = null;
- this.filter = filter;
- this.customizer = (customizer == null) ? this : customizer;
- if ((context == null) || (filter == null)) { // we throw a NPE here
- // to
- // be consistent with the
- // other constructors
- throw new NullPointerException();
- }
- }
-
- /**
- * Open this <code>ServiceTracker</code> object and begin tracking
- * services.
- *
- * <p>
- * This method calls <code>open(false)</code>.
- *
- * @throws java.lang.IllegalStateException if the <code>BundleContext</code>
- * object with which this <code>ServiceTracker</code> object was
- * created is no longer valid.
- * @see #open(boolean)
- */
- public void open() {
- open(false);
- }
-
- /**
- * Open this <code>ServiceTracker</code> object and begin tracking
- * services.
- *
- * <p>
- * Services which match the search criteria specified when this
- * <code>ServiceTracker</code> object was created are now tracked by this
- * <code>ServiceTracker</code> object.
- *
- * @param trackAllServices If <code>true</code>, then this
- * <code>ServiceTracker</code> will track all matching services
- * regardless of class loader accessibility. If <code>false</code>,
- * then this <code>ServiceTracker</code> will only track matching
- * services which are class loader accessibile to the bundle whose
- * <code>BundleContext</code> is used by this
- * <code>ServiceTracker</code>.
- * @throws java.lang.IllegalStateException if the <code>BundleContext</code>
- * object with which this <code>ServiceTracker</code> object was
- * created is no longer valid.
- * @since 1.3
- */
- public synchronized void open(boolean trackAllServices) {
- if (tracked != null) {
- return;
- }
- if (DEBUG) {
- System.out.println("ServiceTracker.open: " + filter); //$NON-NLS-1$
- }
- tracked = trackAllServices ? new AllTracked() : new Tracked();
- trackingCount = 0;
- synchronized (tracked) {
- try {
- context.addServiceListener(tracked, listenerFilter);
- ServiceReference[] references;
- if (listenerFilter == null) { // user supplied filter
- references = getInitialReferences(trackAllServices, null,
- filter.toString());
- }
- else { // constructor supplied filter
- if (trackClass == null) {
- references = new ServiceReference[] {trackReference};
- }
- else {
- references = getInitialReferences(trackAllServices,
- trackClass, null);
- }
- }
-
- tracked.setInitialServices(references); // set tracked with
- // the initial
- // references
- }
- catch (InvalidSyntaxException e) {
- throw new RuntimeException(
- "unexpected InvalidSyntaxException: " + e.getMessage()); //$NON-NLS-1$
- }
- }
- /* Call tracked outside of synchronized region */
- tracked.trackInitialServices(); // process the initial references
- }
-
- /**
- * Returns the list of initial <code>ServiceReference</code> objects that
- * will be tracked by this <code>ServiceTracker</code> object.
- *
- * @param trackAllServices If true, use getAllServiceReferences.
- * @param trackClass the class name with which the service was registered,
- * or null for all services.
- * @param filterString the filter criteria or null for all services.
- * @return the list of initial <code>ServiceReference</code> objects.
- * @throws InvalidSyntaxException if the filter uses an invalid syntax.
- */
- private ServiceReference[] getInitialReferences(boolean trackAllServices,
- String trackClass, String filterString)
- throws InvalidSyntaxException {
- if (trackAllServices) {
- return context.getAllServiceReferences(trackClass, filterString);
- }
- else {
- return context.getServiceReferences(trackClass, filterString);
- }
- }
-
- /**
- * Close this <code>ServiceTracker</code> object.
- *
- * <p>
- * This method should be called when this <code>ServiceTracker</code>
- * object should end the tracking of services.
- */
- public synchronized void close() {
- if (tracked == null) {
- return;
- }
- if (DEBUG) {
- System.out.println("ServiceTracker.close: " + filter); //$NON-NLS-1$
- }
- tracked.close();
- ServiceReference[] references = getServiceReferences();
- Tracked outgoing = tracked;
- tracked = null;
- try {
- context.removeServiceListener(outgoing);
- }
- catch (IllegalStateException e) {
- /* In case the context was stopped. */
- }
- if (references != null) {
- for (int i = 0; i < references.length; i++) {
- outgoing.untrack(references[i]);
- }
- }
- trackingCount = -1;
- if (DEBUG) {
- if ((cachedReference == null) && (cachedService == null)) {
- System.out
- .println("ServiceTracker.close[cached cleared]: " + filter); //$NON-NLS-1$
- }
- }
- }
-
- /**
- * Default implementation of the
- * <code>ServiceTrackerCustomizer.addingService</code> method.
- *
- * <p>
- * This method is only called when this <code>ServiceTracker</code> object
- * has been constructed with a <code>null ServiceTrackerCustomizer</code>
- * argument.
- *
- * The default implementation returns the result of calling
- * <code>getService</code>, on the <code>BundleContext</code> object
- * with which this <code>ServiceTracker</code> object was created, passing
- * the specified <code>ServiceReference</code> object.
- * <p>
- * This method can be overridden in a subclass to customize the service
- * object to be tracked for the service being added. In that case, take care
- * not to rely on the default implementation of removedService that will
- * unget the service.
- *
- * @param reference Reference to service being added to this
- * <code>ServiceTracker</code> object.
- * @return The service object to be tracked for the service added to this
- * <code>ServiceTracker</code> object.
- * @see ServiceTrackerCustomizer
- */
- public Object addingService(ServiceReference reference) {
- return context.getService(reference);
- }
-
- /**
- * Default implementation of the
- * <code>ServiceTrackerCustomizer.modifiedService</code> method.
- *
- * <p>
- * This method is only called when this <code>ServiceTracker</code> object
- * has been constructed with a <code>null ServiceTrackerCustomizer</code>
- * argument.
- *
- * The default implementation does nothing.
- *
- * @param reference Reference to modified service.
- * @param service The service object for the modified service.
- * @see ServiceTrackerCustomizer
- */
- public void modifiedService(ServiceReference reference, Object service) {
- }
-
- /**
- * Default implementation of the
- * <code>ServiceTrackerCustomizer.removedService</code> method.
- *
- * <p>
- * This method is only called when this <code>ServiceTracker</code> object
- * has been constructed with a <code>null ServiceTrackerCustomizer</code>
- * argument.
- *
- * The default implementation calls <code>ungetService</code>, on the
- * <code>BundleContext</code> object with which this
- * <code>ServiceTracker</code> object was created, passing the specified
- * <code>ServiceReference</code> object.
- * <p>
- * This method can be overridden in a subclass. If the default
- * implementation of <code>addingService</code> method was used, this
- * method must unget the service.
- *
- * @param reference Reference to removed service.
- * @param service The service object for the removed service.
- * @see ServiceTrackerCustomizer
- */
- public void removedService(ServiceReference reference, Object service) {
- context.ungetService(reference);
- }
-
- /**
- * Wait for at least one service to be tracked by this
- * <code>ServiceTracker</code> object.
- * <p>
- * It is strongly recommended that <code>waitForService</code> is not used
- * during the calling of the <code>BundleActivator</code> methods.
- * <code>BundleActivator</code> methods are expected to complete in a
- * short period of time.
- *
- * @param timeout time interval in milliseconds to wait. If zero, the method
- * will wait indefinately.
- * @return Returns the result of <code>getService()</code>.
- * @throws InterruptedException If another thread has interrupted the
- * current thread.
- * @throws IllegalArgumentException If the value of timeout is negative.
- */
- public Object waitForService(long timeout) throws InterruptedException {
- if (timeout < 0) {
- throw new IllegalArgumentException("timeout value is negative"); //$NON-NLS-1$
- }
- Object object = getService();
- while (object == null) {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return null;
- }
- synchronized (tracked) {
- if (tracked.size() == 0) {
- tracked.wait(timeout);
- }
- }
- object = getService();
- if (timeout > 0) {
- return object;
- }
- }
- return object;
- }
-
- /**
- * Return an array of <code>ServiceReference</code> objects for all
- * services being tracked by this <code>ServiceTracker</code> object.
- *
- * @return Array of <code>ServiceReference</code> objects or
- * <code>null</code> if no service are being tracked.
- */
- public ServiceReference[] getServiceReferences() {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return null;
- }
- synchronized (tracked) {
- int length = tracked.size();
- if (length == 0) {
- return null;
- }
- ServiceReference[] references = new ServiceReference[length];
- Enumeration keys = tracked.keys();
- for (int i = 0; i < length; i++) {
- references[i] = (ServiceReference) keys.nextElement();
- }
- return references;
- }
- }
-
- /**
- * Returns a <code>ServiceReference</code> object for one of the services
- * being tracked by this <code>ServiceTracker</code> object.
- *
- * <p>
- * If multiple services are being tracked, the service with the highest
- * ranking (as specified in its <code>service.ranking</code> property) is
- * returned.
- *
- * <p>
- * If there is a tie in ranking, the service with the lowest service ID (as
- * specified in its <code>service.id</code> property); that is, the
- * service that was registered first is returned.
- * <p>
- * This is the same algorithm used by
- * <code>BundleContext.getServiceReference</code>.
- *
- * @return <code>ServiceReference</code> object or <code>null</code> if
- * no service is being tracked.
- * @since 1.1
- */
- public ServiceReference getServiceReference() {
- ServiceReference reference = cachedReference;
- if (reference != null) {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.getServiceReference[cached]: " + filter); //$NON-NLS-1$
- }
- return reference;
- }
- if (DEBUG) {
- System.out.println("ServiceTracker.getServiceReference: " + filter); //$NON-NLS-1$
- }
- ServiceReference[] references = getServiceReferences();
- int length = (references == null) ? 0 : references.length;
- if (length == 0) /* if no service is being tracked */
- {
- return null;
- }
- int index = 0;
- if (length > 1) /* if more than one service, select highest ranking */
- {
- int rankings[] = new int[length];
- int count = 0;
- int maxRanking = Integer.MIN_VALUE;
- for (int i = 0; i < length; i++) {
- Object property = references[i]
- .getProperty(Constants.SERVICE_RANKING);
- int ranking = (property instanceof Integer) ? ((Integer) property)
- .intValue()
- : 0;
- rankings[i] = ranking;
- if (ranking > maxRanking) {
- index = i;
- maxRanking = ranking;
- count = 1;
- }
- else {
- if (ranking == maxRanking) {
- count++;
- }
- }
- }
- if (count > 1) /* if still more than one service, select lowest id */
- {
- long minId = Long.MAX_VALUE;
- for (int i = 0; i < length; i++) {
- if (rankings[i] == maxRanking) {
- long id = ((Long) (references[i]
- .getProperty(Constants.SERVICE_ID)))
- .longValue();
- if (id < minId) {
- index = i;
- minId = id;
- }
- }
- }
- }
- }
- return cachedReference = references[index];
- }
-
- /**
- * Returns the service object for the specified
- * <code>ServiceReference</code> object if the referenced service is being
- * tracked by this <code>ServiceTracker</code> object.
- *
- * @param reference Reference to the desired service.
- * @return Service object or <code>null</code> if the service referenced
- * by the specified <code>ServiceReference</code> object is not
- * being tracked.
- */
- public Object getService(ServiceReference reference) {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return null;
- }
- synchronized (tracked) {
- return tracked.get(reference);
- }
- }
-
- /**
- * Return an array of service objects for all services being tracked by this
- * <code>ServiceTracker</code> object.
- *
- * @return Array of service objects or <code>null</code> if no service are
- * being tracked.
- */
- public Object[] getServices() {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return null;
- }
- synchronized (tracked) {
- ServiceReference[] references = getServiceReferences();
- int length = (references == null) ? 0 : references.length;
- if (length == 0) {
- return null;
- }
- Object[] objects = new Object[length];
- for (int i = 0; i < length; i++) {
- objects[i] = getService(references[i]);
- }
- return objects;
- }
- }
-
- /**
- * Returns a service object for one of the services being tracked by this
- * <code>ServiceTracker</code> object.
- *
- * <p>
- * If any services are being tracked, this method returns the result of
- * calling <code>getService(getServiceReference())</code>.
- *
- * @return Service object or <code>null</code> if no service is being
- * tracked.
- */
- public Object getService() {
- Object service = cachedService;
- if (service != null) {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.getService[cached]: " + filter); //$NON-NLS-1$
- }
- return service;
- }
- if (DEBUG) {
- System.out.println("ServiceTracker.getService: " + filter); //$NON-NLS-1$
- }
- ServiceReference reference = getServiceReference();
- if (reference == null) {
- return null;
- }
- return cachedService = getService(reference);
- }
-
- /**
- * Remove a service from this <code>ServiceTracker</code> object.
- *
- * The specified service will be removed from this
- * <code>ServiceTracker</code> object. If the specified service was being
- * tracked then the <code>ServiceTrackerCustomizer.removedService</code>
- * method will be called for that service.
- *
- * @param reference Reference to the service to be removed.
- */
- public void remove(ServiceReference reference) {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return;
- }
- tracked.untrack(reference);
- }
-
- /**
- * Return the number of services being tracked by this
- * <code>ServiceTracker</code> object.
- *
- * @return Number of services being tracked.
- */
- public int size() {
- Tracked tracked = this.tracked; /*
- * use local var since we are not
- * synchronized
- */
- if (tracked == null) { /* if ServiceTracker is not open */
- return 0;
- }
- return tracked.size();
- }
-
- /**
- * Returns the tracking count for this <code>ServiceTracker</code> object.
- *
- * The tracking count is initialized to 0 when this
- * <code>ServiceTracker</code> object is opened. Every time a service is
- * added, modified or removed from this <code>ServiceTracker</code> object
- * the tracking count is incremented.
- *
- * <p>
- * The tracking count can be used to determine if this
- * <code>ServiceTracker</code> object has added, modified or removed a
- * service by comparing a tracking count value previously collected with the
- * current tracking count value. If the value has not changed, then no
- * service has been added, modified or removed from this
- * <code>ServiceTracker</code> object since the previous tracking count
- * was collected.
- *
- * @since 1.2
- * @return The tracking count for this <code>ServiceTracker</code> object
- * or -1 if this <code>ServiceTracker</code> object is not open.
- */
- public int getTrackingCount() {
- return trackingCount;
- }
-
- /**
- * Called by the Tracked object whenever the set of tracked services is
- * modified. Increments the tracking count and clears the cache.
- *
- * @GuardedBy tracked
- */
- /*
- * This method must not be synchronized since it is called by Tracked while
- * Tracked is synchronized. We don't want synchronization interactions
- * between the ServiceListener thread and the user thread.
- */
- void modified() {
- trackingCount++; /* increment modification count */
- cachedReference = null; /* clear cached value */
- cachedService = null; /* clear cached value */
- if (DEBUG) {
- System.out.println("ServiceTracker.modified: " + filter); //$NON-NLS-1$
- }
- }
-
- /**
- * Inner class to track services. If a <code>ServiceTracker</code> object
- * is reused (closed then reopened), then a new Tracked object is used. This
- * class is a hashtable mapping <code>ServiceReference</code> object ->
- * customized Object. This class is the <code>ServiceListener</code>
- * object for the tracker. This class is used to synchronize access to the
- * tracked services. This is not a public class. It is only for use by the
- * implementation of the <code>ServiceTracker</code> class.
- *
- * @ThreadSafe
- */
- class Tracked extends Hashtable implements ServiceListener {
- static final long serialVersionUID = -7420065199791006079L;
- /**
- * List of ServiceReferences in the process of being added. This is used
- * to deal with nesting of ServiceEvents. Since ServiceEvents are
- * synchronously delivered, ServiceEvents can be nested. For example,
- * when processing the adding of a service and the customizer causes the
- * service to be unregistered, notification to the nested call to
- * untrack that the service was unregistered can be made to the track
- * method.
- *
- * Since the ArrayList implementation is not synchronized, all access to
- * this list must be protected by the same synchronized object for
- * thread-safety.
- *
- * @GuardedBy this
- */
- private final ArrayList adding;
-
- /**
- * true if the tracked object is closed.
- *
- * This field is volatile because it is set by one thread and read by
- * another.
- */
- private volatile boolean closed;
-
- /**
- * Initial list of ServiceReferences for the tracker. This is used to
- * correctly process the initial services which could become
- * unregistered before they are tracked. This is necessary since the
- * initial set of tracked services are not "announced" by ServiceEvents
- * and therefore the ServiceEvent for unregistration could be delivered
- * before we track the service.
- *
- * A service must not be in both the initial and adding lists at the
- * same time. A service must be moved from the initial list to the
- * adding list "atomically" before we begin tracking it.
- *
- * Since the LinkedList implementation is not synchronized, all access
- * to this list must be protected by the same synchronized object for
- * thread-safety.
- *
- * @GuardedBy this
- */
- private final LinkedList initial;
-
- /**
- * Tracked constructor.
- */
- protected Tracked() {
- super();
- closed = false;
- adding = new ArrayList(6);
- initial = new LinkedList();
- }
-
- /**
- * Set initial list of services into tracker before ServiceEvents begin
- * to be received.
- *
- * This method must be called from ServiceTracker.open while
- * synchronized on this object in the same synchronized block as the
- * addServiceListener call.
- *
- * @param references The initial list of services to be tracked.
- * @GuardedBy this
- */
- protected void setInitialServices(ServiceReference[] references) {
- if (references == null) {
- return;
- }
- int size = references.length;
- for (int i = 0; i < size; i++) {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.setInitialServices: " + references[i]); //$NON-NLS-1$
- }
- initial.add(references[i]);
- }
- }
-
- /**
- * Track the initial list of services. This is called after
- * ServiceEvents can begin to be received.
- *
- * This method must be called from ServiceTracker.open while not
- * synchronized on this object after the addServiceListener call.
- *
- */
- protected void trackInitialServices() {
- while (true) {
- ServiceReference reference;
- synchronized (this) {
- if (initial.size() == 0) {
- /*
- * if there are no more inital services
- */
- return; /* we are done */
- }
- /*
- * move the first service from the initial list to the
- * adding list within this synchronized block.
- */
- reference = (ServiceReference) initial.removeFirst();
- if (this.get(reference) != null) {
- /* if we are already tracking this service */
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.trackInitialServices[already tracked]: " + reference); //$NON-NLS-1$
- }
- continue; /* skip this service */
- }
- if (adding.contains(reference)) {
- /*
- * if this service is already in the process of being
- * added.
- */
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.trackInitialServices[already adding]: " + reference); //$NON-NLS-1$
- }
- continue; /* skip this service */
- }
- adding.add(reference);
- }
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.trackInitialServices: " + reference); //$NON-NLS-1$
- }
- trackAdding(reference); /*
- * Begin tracking it. We call
- * trackAdding since we have already put
- * the reference in the adding list.
- */
- }
- }
-
- /**
- * Called by the owning <code>ServiceTracker</code> object when it is
- * closed.
- */
- protected void close() {
- closed = true;
- }
-
- /**
- * <code>ServiceListener</code> method for the
- * <code>ServiceTracker</code> class. This method must NOT be
- * synchronized to avoid deadlock potential.
- *
- * @param event <code>ServiceEvent</code> object from the framework.
- */
- public void serviceChanged(ServiceEvent event) {
- /*
- * Check if we had a delayed call (which could happen when we
- * close).
- */
- if (closed) {
- return;
- }
- ServiceReference reference = event.getServiceReference();
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.serviceChanged[" + event.getType() + "]: " + reference); //$NON-NLS-1$ //$NON-NLS-2$
- }
-
- switch (event.getType()) {
- case ServiceEvent.REGISTERED :
- case ServiceEvent.MODIFIED :
- if (listenerFilter != null) { // constructor supplied
- // filter
- track(reference);
- /*
- * If the customizer throws an unchecked exception, it
- * is safe to let it propagate
- */
- }
- else { // user supplied filter
- if (filter.match(reference)) {
- track(reference);
- /*
- * If the customizer throws an unchecked exception,
- * it is safe to let it propagate
- */
- }
- else {
- untrack(reference);
- /*
- * If the customizer throws an unchecked exception,
- * it is safe to let it propagate
- */
- }
- }
- break;
- case ServiceEvent.UNREGISTERING :
- untrack(reference);
- /*
- * If the customizer throws an unchecked exception, it is
- * safe to let it propagate
- */
- break;
- }
- }
-
- /**
- * Begin to track the referenced service.
- *
- * @param reference Reference to a service to be tracked.
- */
- private void track(ServiceReference reference) {
- Object object;
- synchronized (this) {
- object = this.get(reference);
- }
- if (object != null) /* we are already tracking the service */
- {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.track[modified]: " + reference); //$NON-NLS-1$
- }
- synchronized (this) {
- modified(); /* increment modification count */
- }
- /* Call customizer outside of synchronized region */
- customizer.modifiedService(reference, object);
- /*
- * If the customizer throws an unchecked exception, it is safe
- * to let it propagate
- */
- return;
- }
- synchronized (this) {
- if (adding.contains(reference)) { /*
- * if this service is
- * already in the process of
- * being added.
- */
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.track[already adding]: " + reference); //$NON-NLS-1$
- }
- return;
- }
- adding.add(reference); /* mark this service is being added */
- }
-
- trackAdding(reference); /*
- * call trackAdding now that we have put the
- * reference in the adding list
- */
- }
-
- /**
- * Common logic to add a service to the tracker used by track and
- * trackInitialServices. The specified reference must have been placed
- * in the adding list before calling this method.
- *
- * @param reference Reference to a service to be tracked.
- */
- private void trackAdding(ServiceReference reference) {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.trackAdding: " + reference); //$NON-NLS-1$
- }
- Object object = null;
- boolean becameUntracked = false;
- /* Call customizer outside of synchronized region */
- try {
- object = customizer.addingService(reference);
- /*
- * If the customizer throws an unchecked exception, it will
- * propagate after the finally
- */
- }
- finally {
- boolean needToCallback = false;
- synchronized (this) {
- if (adding.remove(reference)) { /*
- * if the service was not
- * untracked during the
- * customizer callback
- */
- if (object != null) {
- this.put(reference, object);
- modified(); /* increment modification count */
- notifyAll(); /*
- * notify any waiters in
- * waitForService
- */
- // marrs: extra callback added, will be invoked after
- // the synchronized block
- needToCallback = true;
- }
- }
- else {
- becameUntracked = true;
- }
- }
- if (needToCallback) {
- customizer.addedService(reference, object);
- }
- }
- /*
- * The service became untracked during the customizer callback.
- */
- if (becameUntracked) {
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.trackAdding[removed]: " + reference); //$NON-NLS-1$
- }
- /* Call customizer outside of synchronized region */
- customizer.removedService(reference, object);
- /*
- * If the customizer throws an unchecked exception, it is safe
- * to let it propagate
- */
- }
- }
-
- /**
- * Discontinue tracking the referenced service.
- *
- * @param reference Reference to the tracked service.
- */
- protected void untrack(ServiceReference reference) {
- Object object;
- synchronized (this) {
- if (initial.remove(reference)) { /*
- * if this service is
- * already in the list of
- * initial references to
- * process
- */
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.untrack[removed from initial]: " + reference); //$NON-NLS-1$
- }
- return; /*
- * we have removed it from the list and it will not
- * be processed
- */
- }
-
- if (adding.remove(reference)) { /*
- * if the service is in the
- * process of being added
- */
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.untrack[being added]: " + reference); //$NON-NLS-1$
- }
- return; /*
- * in case the service is untracked while in the
- * process of adding
- */
- }
- object = this.remove(reference); /*
- * must remove from tracker
- * before calling customizer
- * callback
- */
- if (object == null) { /* are we actually tracking the service */
- return;
- }
- modified(); /* increment modification count */
- }
- if (DEBUG) {
- System.out
- .println("ServiceTracker.Tracked.untrack[removed]: " + reference); //$NON-NLS-1$
- }
- /* Call customizer outside of synchronized region */
- customizer.removedService(reference, object);
- /*
- * If the customizer throws an unchecked exception, it is safe to
- * let it propagate
- */
- }
- }
-
- /**
- * Subclass of Tracked which implements the AllServiceListener interface.
- * This class is used by the ServiceTracker if open is called with true.
- *
- * @since 1.3
- * @ThreadSafe
- */
- class AllTracked extends Tracked implements AllServiceListener {
- static final long serialVersionUID = 4050764875305137716L;
-
- /**
- * AllTracked constructor.
- */
- protected AllTracked() {
- super();
- }
- }
-
- public void addedService(ServiceReference ref, Object service) {
- // do nothing
- }
-}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTrackerCustomizer.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTrackerCustomizer.java
deleted file mode 100644
index 44f7590..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/ServiceTrackerCustomizer.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.felix.dependencymanager;
-
-import org.osgi.framework.ServiceReference;
-
-/**
- * A modified version of a normal service tracker customizer. This one has an
- * extra callback "addedservice" that is invoked after the service has been added
- * to the tracker (and therefore is accessible through the tracker API).
- *
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
-public interface ServiceTrackerCustomizer {
- public Object addingService(ServiceReference ref);
- public void addedService(ServiceReference ref, Object service);
- public void modifiedService(ServiceReference ref, Object service);
- public void removedService(ServiceReference ref, Object service);
-}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/AbstractTracked.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/AbstractTracked.java
new file mode 100644
index 0000000..013384b
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/AbstractTracked.java
@@ -0,0 +1,456 @@
+package org.apache.felix.dependencymanager4.tracker;
+/*
+ * Copyright (c) OSGi Alliance (2007, 2008). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Abstract class to track items. If a Tracker is reused (closed then reopened),
+ * then a new AbstractTracked object is used. This class acts a map of tracked
+ * item -> customized object. Subclasses of this class will act as the listener
+ * object for the tracker. This class is used to synchronize access to the
+ * tracked items. This is not a public class. It is only for use by the
+ * implementation of the Tracker class.
+ *
+ * @ThreadSafe
+ * @version $Revision: 5871 $
+ * @since 1.4
+ */
+abstract class AbstractTracked {
+ /* set this to true to compile in debug messages */
+ private static final boolean DEBUG = false;
+
+ /**
+ * Map of tracked items to customized objects.
+ *
+ * @GuardedBy this
+ */
+ private final Map tracked;
+
+ /**
+ * Modification count. This field is initialized to zero and incremented by
+ * modified.
+ *
+ * @GuardedBy this
+ */
+ private int trackingCount;
+
+ /**
+ * List of items in the process of being added. This is used to deal with
+ * nesting of events. Since events may be synchronously delivered, events
+ * can be nested. For example, when processing the adding of a service and
+ * the customizer causes the service to be unregistered, notification to the
+ * nested call to untrack that the service was unregistered can be made to
+ * the track method.
+ *
+ * Since the ArrayList implementation is not synchronized, all access to
+ * this list must be protected by the same synchronized object for
+ * thread-safety.
+ *
+ * @GuardedBy this
+ */
+ private final List adding;
+
+ /**
+ * true if the tracked object is closed.
+ *
+ * This field is volatile because it is set by one thread and read by
+ * another.
+ */
+ volatile boolean closed;
+
+ /**
+ * Initial list of items for the tracker. This is used to correctly process
+ * the initial items which could be modified before they are tracked. This
+ * is necessary since the initial set of tracked items are not "announced"
+ * by events and therefore the event which makes the item untracked could be
+ * delivered before we track the item.
+ *
+ * An item must not be in both the initial and adding lists at the same
+ * time. An item must be moved from the initial list to the adding list
+ * "atomically" before we begin tracking it.
+ *
+ * Since the LinkedList implementation is not synchronized, all access to
+ * this list must be protected by the same synchronized object for
+ * thread-safety.
+ *
+ * @GuardedBy this
+ */
+ private final LinkedList initial;
+
+ /**
+ * AbstractTracked constructor.
+ */
+ AbstractTracked() {
+ tracked = new HashMap();
+ trackingCount = 0;
+ adding = new ArrayList(6);
+ initial = new LinkedList();
+ closed = false;
+ }
+
+ /**
+ * Set initial list of items into tracker before events begin to be
+ * received.
+ *
+ * This method must be called from Tracker's open method while synchronized
+ * on this object in the same synchronized block as the add listener call.
+ *
+ * @param list The initial list of items to be tracked. <code>null</code>
+ * entries in the list are ignored.
+ * @GuardedBy this
+ */
+ void setInitial(Object[] list) {
+ if (list == null) {
+ return;
+ }
+ int size = list.length;
+ for (int i = 0; i < size; i++) {
+ Object item = list[i];
+ if (item == null) {
+ continue;
+ }
+ if (DEBUG) {
+ System.out.println("AbstractTracked.setInitial: " + item); //$NON-NLS-1$
+ }
+ initial.add(item);
+ }
+ }
+
+ /**
+ * Track the initial list of items. This is called after events can begin to
+ * be received.
+ *
+ * This method must be called from Tracker's open method while not
+ * synchronized on this object after the add listener call.
+ *
+ */
+ void trackInitial() {
+ while (true) {
+ Object item;
+ synchronized (this) {
+ if (closed || (initial.size() == 0)) {
+ /*
+ * if there are no more initial items
+ */
+ return; /* we are done */
+ }
+ /*
+ * move the first item from the initial list to the adding list
+ * within this synchronized block.
+ */
+ item = initial.removeFirst();
+ if (tracked.get(item) != null) {
+ /* if we are already tracking this item */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.trackInitial[already tracked]: " + item); //$NON-NLS-1$
+ }
+ continue; /* skip this item */
+ }
+ if (adding.contains(item)) {
+ /*
+ * if this item is already in the process of being added.
+ */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.trackInitial[already adding]: " + item); //$NON-NLS-1$
+ }
+ continue; /* skip this item */
+ }
+ adding.add(item);
+ }
+ if (DEBUG) {
+ System.out.println("AbstractTracked.trackInitial: " + item); //$NON-NLS-1$
+ }
+ trackAdding(item, null); /*
+ * Begin tracking it. We call trackAdding
+ * since we have already put the item in the
+ * adding list.
+ */
+ }
+ }
+
+ /**
+ * Called by the owning Tracker object when it is closed.
+ */
+ void close() {
+ closed = true;
+ }
+
+ /**
+ * Begin to track an item.
+ *
+ * @param item Item to be tracked.
+ * @param related Action related object.
+ */
+ void track(final Object item, final Object related) {
+ final Object object;
+ synchronized (this) {
+ if (closed) {
+ return;
+ }
+ object = tracked.get(item);
+ if (object == null) { /* we are not tracking the item */
+ if (adding.contains(item)) {
+ /* if this item is already in the process of being added. */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.track[already adding]: " + item); //$NON-NLS-1$
+ }
+ return;
+ }
+ adding.add(item); /* mark this item is being added */
+ }
+ else { /* we are currently tracking this item */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.track[modified]: " + item); //$NON-NLS-1$
+ }
+ modified(); /* increment modification count */
+ }
+ }
+
+ if (object == null) { /* we are not tracking the item */
+ trackAdding(item, related);
+ }
+ else {
+ /* Call customizer outside of synchronized region */
+ customizerModified(item, related, object);
+ /*
+ * If the customizer throws an unchecked exception, it is safe to
+ * let it propagate
+ */
+ }
+ }
+
+ /**
+ * Common logic to add an item to the tracker used by track and
+ * trackInitial. The specified item must have been placed in the adding list
+ * before calling this method.
+ *
+ * @param item Item to be tracked.
+ * @param related Action related object.
+ */
+ private void trackAdding(final Object item, final Object related) {
+ if (DEBUG) {
+ System.out.println("AbstractTracked.trackAdding: " + item); //$NON-NLS-1$
+ }
+ Object object = null;
+ boolean becameUntracked = false;
+ /* Call customizer outside of synchronized region */
+ try {
+ object = customizerAdding(item, related);
+ /*
+ * If the customizer throws an unchecked exception, it will
+ * propagate after the finally
+ */
+ }
+ finally {
+ boolean needToCallback = false;
+ synchronized (this) {
+ if (adding.remove(item) && !closed) {
+ /*
+ * if the item was not untracked during the customizer
+ * callback
+ */
+ if (object != null) {
+ tracked.put(item, object);
+ modified(); /* increment modification count */
+ notifyAll(); /* notify any waiters */
+ needToCallback = true; /* marrs: invoke added callback */
+ }
+ }
+ else {
+ becameUntracked = true;
+ }
+ }
+ if (needToCallback) {
+ customizerAdded(item, related, object);
+ }
+ }
+ /*
+ * The item became untracked during the customizer callback.
+ */
+ if (becameUntracked && (object != null)) {
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.trackAdding[removed]: " + item); //$NON-NLS-1$
+ }
+ /* Call customizer outside of synchronized region */
+ customizerRemoved(item, related, object);
+ /*
+ * If the customizer throws an unchecked exception, it is safe to
+ * let it propagate
+ */
+ }
+ }
+
+ /**
+ * Discontinue tracking the item.
+ *
+ * @param item Item to be untracked.
+ * @param related Action related object.
+ */
+ void untrack(final Object item, final Object related) {
+ final Object object;
+ synchronized (this) {
+ if (initial.remove(item)) { /*
+ * if this item is already in the list
+ * of initial references to process
+ */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.untrack[removed from initial]: " + item); //$NON-NLS-1$
+ }
+ return; /*
+ * we have removed it from the list and it will not be
+ * processed
+ */
+ }
+
+ if (adding.remove(item)) { /*
+ * if the item is in the process of
+ * being added
+ */
+ if (DEBUG) {
+ System.out
+ .println("AbstractTracked.untrack[being added]: " + item); //$NON-NLS-1$
+ }
+ return; /*
+ * in case the item is untracked while in the process of
+ * adding
+ */
+ }
+ object = tracked.remove(item); /*
+ * must remove from tracker before
+ * calling customizer callback
+ */
+ if (object == null) { /* are we actually tracking the item */
+ return;
+ }
+ modified(); /* increment modification count */
+ }
+ if (DEBUG) {
+ System.out.println("AbstractTracked.untrack[removed]: " + item); //$NON-NLS-1$
+ }
+ /* Call customizer outside of synchronized region */
+ customizerRemoved(item, related, object);
+ /*
+ * If the customizer throws an unchecked exception, it is safe to let it
+ * propagate
+ */
+ }
+
+ /**
+ * Returns the number of tracked items.
+ *
+ * @return The number of tracked items.
+ *
+ * @GuardedBy this
+ */
+ int size() {
+ return tracked.size();
+ }
+
+ /**
+ * Return the customized object for the specified item
+ *
+ * @param item The item to lookup in the map
+ * @return The customized object for the specified item.
+ *
+ * @GuardedBy this
+ */
+ Object getCustomizedObject(final Object item) {
+ return tracked.get(item);
+ }
+
+ /**
+ * Return the list of tracked items.
+ *
+ * @param list An array to contain the tracked items.
+ * @return The specified list if it is large enough to hold the tracked
+ * items or a new array large enough to hold the tracked items.
+ * @GuardedBy this
+ */
+ Object[] getTracked(final Object[] list) {
+ return tracked.keySet().toArray(list);
+ }
+
+ /**
+ * Increment the modification count. If this method is overridden, the
+ * overriding method MUST call this method to increment the tracking count.
+ *
+ * @GuardedBy this
+ */
+ void modified() {
+ trackingCount++;
+ }
+
+ /**
+ * Returns the tracking count for this <code>ServiceTracker</code> object.
+ *
+ * The tracking count is initialized to 0 when this object is opened. Every
+ * time an item is added, modified or removed from this object the tracking
+ * count is incremented.
+ *
+ * @GuardedBy this
+ * @return The tracking count for this object.
+ */
+ int getTrackingCount() {
+ return trackingCount;
+ }
+
+ /**
+ * Call the specific customizer adding method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Item to be tracked.
+ * @param related Action related object.
+ * @return Customized object for the tracked item or <code>null</code> if
+ * the item is not to be tracked.
+ */
+ abstract Object customizerAdding(final Object item, final Object related);
+
+ /** marrs: Call the specific customizer added method. */
+ abstract void customizerAdded(final Object item, final Object related, final Object object);
+
+ /**
+ * Call the specific customizer modified method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ abstract void customizerModified(final Object item, final Object related,
+ final Object object);
+
+ /**
+ * Call the specific customizer removed method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ abstract void customizerRemoved(final Object item, final Object related,
+ final Object object);
+}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTracker.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTracker.java
new file mode 100644
index 0000000..c407dc5
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTracker.java
@@ -0,0 +1,479 @@
+package org.apache.felix.dependencymanager4.tracker;
+
+/*
+ * Copyright (c) OSGi Alliance (2007, 2008). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.SynchronousBundleListener;
+
+/**
+ * The <code>BundleTracker</code> class simplifies tracking bundles much like
+ * the <code>ServiceTracker</code> simplifies tracking services.
+ * <p>
+ * A <code>BundleTracker</code> is constructed with state criteria and a
+ * <code>BundleTrackerCustomizer</code> object. A <code>BundleTracker</code> can
+ * use the <code>BundleTrackerCustomizer</code> to select which bundles are
+ * tracked and to create a customized object to be tracked with the bundle. The
+ * <code>BundleTracker</code> can then be opened to begin tracking all bundles
+ * whose state matches the specified state criteria.
+ * <p>
+ * The <code>getBundles</code> method can be called to get the
+ * <code>Bundle</code> objects of the bundles being tracked. The
+ * <code>getObject</code> method can be called to get the customized object for
+ * a tracked bundle.
+ * <p>
+ * The <code>BundleTracker</code> class is thread-safe. It does not call a
+ * <code>BundleTrackerCustomizer</code> while holding any locks.
+ * <code>BundleTrackerCustomizer</code> implementations must also be
+ * thread-safe.
+ *
+ * @ThreadSafe
+ * @version $Revision: 5894 $
+ * @since 1.4
+ */
+public class BundleTracker implements BundleTrackerCustomizer {
+ /* set this to true to compile in debug messages */
+ static final boolean DEBUG = false;
+
+ /**
+ * The Bundle Context used by this <code>BundleTracker</code>.
+ */
+ protected final BundleContext context;
+
+ /**
+ * The <code>BundleTrackerCustomizer</code> object for this tracker.
+ */
+ final BundleTrackerCustomizer customizer;
+
+ /**
+ * Tracked bundles: <code>Bundle</code> object -> customized Object and
+ * <code>BundleListener</code> object
+ */
+ private volatile Tracked tracked;
+
+ /**
+ * Accessor method for the current Tracked object. This method is only
+ * intended to be used by the unsynchronized methods which do not modify the
+ * tracked field.
+ *
+ * @return The current Tracked object.
+ */
+ private Tracked tracked() {
+ return tracked;
+ }
+
+ /**
+ * State mask for bundles being tracked. This field contains the ORed values
+ * of the bundle states being tracked.
+ */
+ final int mask;
+
+ /**
+ * Create a <code>BundleTracker</code> for bundles whose state is present in
+ * the specified state mask.
+ *
+ * <p>
+ * Bundles whose state is present on the specified state mask will be
+ * tracked by this <code>BundleTracker</code>.
+ *
+ * @param context The <code>BundleContext</code> against which the tracking
+ * is done.
+ * @param stateMask The bit mask of the <code>OR</code>ing of the bundle
+ * states to be tracked.
+ * @param customizer The customizer object to call when bundles are added,
+ * modified, or removed in this <code>BundleTracker</code>. If
+ * customizer is <code>null</code>, then this
+ * <code>BundleTracker</code> will be used as the
+ * <code>BundleTrackerCustomizer</code> and this
+ * <code>BundleTracker</code> will call the
+ * <code>BundleTrackerCustomizer</code> methods on itself.
+ * @see Bundle#getState()
+ */
+ public BundleTracker(BundleContext context, int stateMask,
+ BundleTrackerCustomizer customizer) {
+ this.context = context;
+ this.mask = stateMask;
+ this.customizer = (customizer == null) ? this : customizer;
+ }
+
+ /**
+ * Open this <code>BundleTracker</code> and begin tracking bundles.
+ *
+ * <p>
+ * Bundle which match the state criteria specified when this
+ * <code>BundleTracker</code> was created are now tracked by this
+ * <code>BundleTracker</code>.
+ *
+ * @throws java.lang.IllegalStateException If the <code>BundleContext</code>
+ * with which this <code>BundleTracker</code> was created is no
+ * longer valid.
+ * @throws java.lang.SecurityException If the caller and this class do not
+ * have the appropriate
+ * <code>AdminPermission[context bundle,LISTENER]</code>, and the
+ * Java Runtime Environment supports permissions.
+ */
+ public void open() {
+ final Tracked t;
+ synchronized (this) {
+ if (tracked != null) {
+ return;
+ }
+ if (DEBUG) {
+ System.out.println("BundleTracker.open"); //$NON-NLS-1$
+ }
+ t = new Tracked();
+ synchronized (t) {
+ context.addBundleListener(t);
+ Bundle[] bundles = context.getBundles();
+ if (bundles != null) {
+ int length = bundles.length;
+ for (int i = 0; i < length; i++) {
+ int state = bundles[i].getState();
+ if ((state & mask) == 0) {
+ /* null out bundles whose states are not interesting */
+ bundles[i] = null;
+ }
+ }
+ /* set tracked with the initial bundles */
+ t.setInitial(bundles);
+ }
+ }
+ tracked = t;
+ }
+ /* Call tracked outside of synchronized region */
+ t.trackInitial(); /* process the initial references */
+ }
+
+ /**
+ * Close this <code>BundleTracker</code>.
+ *
+ * <p>
+ * This method should be called when this <code>BundleTracker</code> should
+ * end the tracking of bundles.
+ *
+ * <p>
+ * This implementation calls {@link #getBundles()} to get the list of
+ * tracked bundles to remove.
+ */
+ public void close() {
+ final Bundle[] bundles;
+ final Tracked outgoing;
+ synchronized (this) {
+ outgoing = tracked;
+ if (outgoing == null) {
+ return;
+ }
+ if (DEBUG) {
+ System.out.println("BundleTracker.close"); //$NON-NLS-1$
+ }
+ outgoing.close();
+ bundles = getBundles();
+ tracked = null;
+ try {
+ context.removeBundleListener(outgoing);
+ }
+ catch (IllegalStateException e) {
+ /* In case the context was stopped. */
+ }
+ }
+ if (bundles != null) {
+ for (int i = 0; i < bundles.length; i++) {
+ outgoing.untrack(bundles[i], null);
+ }
+ }
+ }
+
+ /**
+ * Default implementation of the
+ * <code>BundleTrackerCustomizer.addingBundle</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>BundleTracker</code> has been
+ * constructed with a <code>null BundleTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation simply returns the specified <code>Bundle</code>.
+ *
+ * <p>
+ * This method can be overridden in a subclass to customize the object to be
+ * tracked for the bundle being added.
+ *
+ * @param bundle The <code>Bundle</code> being added to this
+ * <code>BundleTracker</code> object.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @return The specified bundle.
+ * @see BundleTrackerCustomizer#addingBundle(Bundle, BundleEvent)
+ */
+ public Object addingBundle(Bundle bundle, BundleEvent event) {
+ return bundle;
+ }
+
+ public void addedBundle(Bundle bundle, BundleEvent event, Object object) {
+ /* do nothing */
+ }
+
+ /**
+ * Default implementation of the
+ * <code>BundleTrackerCustomizer.modifiedBundle</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>BundleTracker</code> has been
+ * constructed with a <code>null BundleTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation does nothing.
+ *
+ * @param bundle The <code>Bundle</code> whose state has been modified.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @param object The customized object for the specified Bundle.
+ * @see BundleTrackerCustomizer#modifiedBundle(Bundle, BundleEvent, Object)
+ */
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ /* do nothing */
+ }
+
+ /**
+ * Default implementation of the
+ * <code>BundleTrackerCustomizer.removedBundle</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>BundleTracker</code> has been
+ * constructed with a <code>null BundleTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation does nothing.
+ *
+ * @param bundle The <code>Bundle</code> being removed.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @param object The customized object for the specified bundle.
+ * @see BundleTrackerCustomizer#removedBundle(Bundle, BundleEvent, Object)
+ */
+ public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+ /* do nothing */
+ }
+
+ /**
+ * Return an array of <code>Bundle</code>s for all bundles being tracked by
+ * this <code>BundleTracker</code>.
+ *
+ * @return An array of <code>Bundle</code>s or <code>null</code> if no
+ * bundles are being tracked.
+ */
+ public Bundle[] getBundles() {
+ final Tracked t = tracked();
+ if (t == null) { /* if BundleTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ int length = t.size();
+ if (length == 0) {
+ return null;
+ }
+ return (Bundle[]) t.getTracked(new Bundle[length]);
+ }
+ }
+
+ /**
+ * Returns the customized object for the specified <code>Bundle</code> if
+ * the specified bundle is being tracked by this <code>BundleTracker</code>.
+ *
+ * @param bundle The <code>Bundle</code> being tracked.
+ * @return The customized object for the specified <code>Bundle</code> or
+ * <code>null</code> if the specified <code>Bundle</code> is not
+ * being tracked.
+ */
+ public Object getObject(Bundle bundle) {
+ final Tracked t = tracked();
+ if (t == null) { /* if BundleTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ return t.getCustomizedObject(bundle);
+ }
+ }
+
+ /**
+ * Remove a bundle from this <code>BundleTracker</code>.
+ *
+ * The specified bundle will be removed from this <code>BundleTracker</code>
+ * . If the specified bundle was being tracked then the
+ * <code>BundleTrackerCustomizer.removedBundle</code> method will be called
+ * for that bundle.
+ *
+ * @param bundle The <code>Bundle</code> to be removed.
+ */
+ public void remove(Bundle bundle) {
+ final Tracked t = tracked();
+ if (t == null) { /* if BundleTracker is not open */
+ return;
+ }
+ t.untrack(bundle, null);
+ }
+
+ /**
+ * Return the number of bundles being tracked by this
+ * <code>BundleTracker</code>.
+ *
+ * @return The number of bundles being tracked.
+ */
+ public int size() {
+ final Tracked t = tracked();
+ if (t == null) { /* if BundleTracker is not open */
+ return 0;
+ }
+ synchronized (t) {
+ return t.size();
+ }
+ }
+
+ /**
+ * Returns the tracking count for this <code>BundleTracker</code>.
+ *
+ * The tracking count is initialized to 0 when this
+ * <code>BundleTracker</code> is opened. Every time a bundle is added,
+ * modified or removed from this <code>BundleTracker</code> the tracking
+ * count is incremented.
+ *
+ * <p>
+ * The tracking count can be used to determine if this
+ * <code>BundleTracker</code> has added, modified or removed a bundle by
+ * comparing a tracking count value previously collected with the current
+ * tracking count value. If the value has not changed, then no bundle has
+ * been added, modified or removed from this <code>BundleTracker</code>
+ * since the previous tracking count was collected.
+ *
+ * @return The tracking count for this <code>BundleTracker</code> or -1 if
+ * this <code>BundleTracker</code> is not open.
+ */
+ public int getTrackingCount() {
+ final Tracked t = tracked();
+ if (t == null) { /* if BundleTracker is not open */
+ return -1;
+ }
+ synchronized (t) {
+ return t.getTrackingCount();
+ }
+ }
+
+ /**
+ * Inner class which subclasses AbstractTracked. This class is the
+ * <code>SynchronousBundleListener</code> object for the tracker.
+ *
+ * @ThreadSafe
+ * @since 1.4
+ */
+ class Tracked extends AbstractTracked implements SynchronousBundleListener {
+ /**
+ * Tracked constructor.
+ */
+ Tracked() {
+ super();
+ }
+
+ /**
+ * <code>BundleListener</code> method for the <code>BundleTracker</code>
+ * class. This method must NOT be synchronized to avoid deadlock
+ * potential.
+ *
+ * @param event <code>BundleEvent</code> object from the framework.
+ */
+ public void bundleChanged(final BundleEvent event) {
+ /*
+ * Check if we had a delayed call (which could happen when we
+ * close).
+ */
+ if (closed) {
+ return;
+ }
+ final Bundle bundle = event.getBundle();
+ final int state = bundle.getState();
+ if (DEBUG) {
+ System.out
+ .println("BundleTracker.Tracked.bundleChanged[" + state + "]: " + bundle); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ if ((state & mask) != 0) {
+ track(bundle, event);
+ /*
+ * If the customizer throws an unchecked exception, it is safe
+ * to let it propagate
+ */
+ }
+ else {
+ untrack(bundle, event);
+ /*
+ * If the customizer throws an unchecked exception, it is safe
+ * to let it propagate
+ */
+ }
+ }
+
+ /**
+ * Call the specific customizer adding method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Item to be tracked.
+ * @param related Action related object.
+ * @return Customized object for the tracked item or <code>null</code>
+ * if the item is not to be tracked.
+ */
+ Object customizerAdding(final Object item,
+ final Object related) {
+ return customizer
+ .addingBundle((Bundle) item, (BundleEvent) related);
+ }
+
+ void customizerAdded(final Object item, final Object related, final Object object) {
+ customizer.addedBundle((Bundle) item, (BundleEvent) related, object);
+ }
+
+ /**
+ * Call the specific customizer modified method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ void customizerModified(final Object item,
+ final Object related, final Object object) {
+ customizer.modifiedBundle((Bundle) item, (BundleEvent) related,
+ object);
+ }
+
+ /**
+ * Call the specific customizer removed method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ void customizerRemoved(final Object item,
+ final Object related, final Object object) {
+ customizer.removedBundle((Bundle) item, (BundleEvent) related,
+ object);
+ }
+ }
+}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTrackerCustomizer.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTrackerCustomizer.java
new file mode 100644
index 0000000..94ac3c7
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/BundleTrackerCustomizer.java
@@ -0,0 +1,106 @@
+package org.apache.felix.dependencymanager4.tracker;
+/*
+ * Copyright (c) OSGi Alliance (2007, 2008). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+
+/**
+ * The <code>BundleTrackerCustomizer</code> interface allows a
+ * <code>BundleTracker</code> to customize the <code>Bundle</code>s that are
+ * tracked. A <code>BundleTrackerCustomizer</code> is called when a bundle is
+ * being added to a <code>BundleTracker</code>. The
+ * <code>BundleTrackerCustomizer</code> can then return an object for the
+ * tracked bundle. A <code>BundleTrackerCustomizer</code> is also called when a
+ * tracked bundle is modified or has been removed from a
+ * <code>BundleTracker</code>.
+ *
+ * <p>
+ * The methods in this interface may be called as the result of a
+ * <code>BundleEvent</code> being received by a <code>BundleTracker</code>.
+ * Since <code>BundleEvent</code>s are received synchronously by the
+ * <code>BundleTracker</code>, it is highly recommended that implementations of
+ * these methods do not alter bundle states while being synchronized on any
+ * object.
+ *
+ * <p>
+ * The <code>BundleTracker</code> class is thread-safe. It does not call a
+ * <code>BundleTrackerCustomizer</code> while holding any locks.
+ * <code>BundleTrackerCustomizer</code> implementations must also be
+ * thread-safe.
+ *
+ * @ThreadSafe
+ * @version $Revision: 5874 $
+ * @since 1.4
+ */
+public interface BundleTrackerCustomizer {
+ /**
+ * A bundle is being added to the <code>BundleTracker</code>.
+ *
+ * <p>
+ * This method is called before a bundle which matched the search parameters
+ * of the <code>BundleTracker</code> is added to the
+ * <code>BundleTracker</code>. This method should return the object to be
+ * tracked for the specified <code>Bundle</code>. The returned object is
+ * stored in the <code>BundleTracker</code> and is available from the
+ * {@link BundleTracker#getObject(Bundle) getObject} method.
+ *
+ * @param bundle The <code>Bundle</code> being added to the
+ * <code>BundleTracker</code>.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @return The object to be tracked for the specified <code>Bundle</code>
+ * object or <code>null</code> if the specified <code>Bundle</code>
+ * object should not be tracked.
+ */
+ public Object addingBundle(Bundle bundle, BundleEvent event);
+
+ /** marrs: A bundle has been added to the BundleTracker. */
+ public void addedBundle(Bundle bundle, BundleEvent event, Object object);
+
+ /**
+ * A bundle tracked by the <code>BundleTracker</code> has been modified.
+ *
+ * <p>
+ * This method is called when a bundle being tracked by the
+ * <code>BundleTracker</code> has had its state modified.
+ *
+ * @param bundle The <code>Bundle</code> whose state has been modified.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @param object The tracked object for the specified bundle.
+ */
+ public void modifiedBundle(Bundle bundle, BundleEvent event,
+ Object object);
+
+ /**
+ * A bundle tracked by the <code>BundleTracker</code> has been removed.
+ *
+ * <p>
+ * This method is called after a bundle is no longer being tracked by the
+ * <code>BundleTracker</code>.
+ *
+ * @param bundle The <code>Bundle</code> that has been removed.
+ * @param event The bundle event which caused this customizer method to be
+ * called or <code>null</code> if there is no bundle event associated
+ * with the call to this method.
+ * @param object The tracked object for the specified bundle.
+ */
+ public void removedBundle(Bundle bundle, BundleEvent event,
+ Object object);
+}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTracker.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTracker.java
new file mode 100644
index 0000000..c32a7d4
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTracker.java
@@ -0,0 +1,948 @@
+package org.apache.felix.dependencymanager4.tracker;
+/*
+ * Copyright (c) OSGi Alliance (2000, 2009). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.osgi.framework.AllServiceListener;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+
+/**
+ * The <code>ServiceTracker</code> class simplifies using services from the
+ * Framework's service registry.
+ * <p>
+ * A <code>ServiceTracker</code> object is constructed with search criteria and
+ * a <code>ServiceTrackerCustomizer</code> object. A <code>ServiceTracker</code>
+ * can use a <code>ServiceTrackerCustomizer</code> to customize the service
+ * objects to be tracked. The <code>ServiceTracker</code> can then be opened to
+ * begin tracking all services in the Framework's service registry that match
+ * the specified search criteria. The <code>ServiceTracker</code> correctly
+ * handles all of the details of listening to <code>ServiceEvent</code>s and
+ * getting and ungetting services.
+ * <p>
+ * The <code>getServiceReferences</code> method can be called to get references
+ * to the services being tracked. The <code>getService</code> and
+ * <code>getServices</code> methods can be called to get the service objects for
+ * the tracked service.
+ * <p>
+ * The <code>ServiceTracker</code> class is thread-safe. It does not call a
+ * <code>ServiceTrackerCustomizer</code> while holding any locks.
+ * <code>ServiceTrackerCustomizer</code> implementations must also be
+ * thread-safe.
+ *
+ * @ThreadSafe
+ * @version $Revision: 6386 $
+ */
+public class ServiceTracker implements ServiceTrackerCustomizer {
+ /* set this to true to compile in debug messages */
+ static final boolean DEBUG = false;
+ /**
+ * The Bundle Context used by this <code>ServiceTracker</code>.
+ */
+ protected final BundleContext context;
+ /**
+ * The Filter used by this <code>ServiceTracker</code> which specifies the
+ * search criteria for the services to track.
+ *
+ * @since 1.1
+ */
+ protected final Filter filter;
+ /**
+ * The <code>ServiceTrackerCustomizer</code> for this tracker.
+ */
+ final ServiceTrackerCustomizer customizer;
+ /**
+ * Filter string for use when adding the ServiceListener. If this field is
+ * set, then certain optimizations can be taken since we don't have a user
+ * supplied filter.
+ */
+ final String listenerFilter;
+ /**
+ * Class name to be tracked. If this field is set, then we are tracking by
+ * class name.
+ */
+ private final String trackClass;
+ /**
+ * Reference to be tracked. If this field is set, then we are tracking a
+ * single ServiceReference.
+ */
+ private final ServiceReference trackReference;
+ /**
+ * Tracked services: <code>ServiceReference</code> -> customized Object and
+ * <code>ServiceListener</code> object
+ */
+ private volatile Tracked tracked;
+
+ /**
+ * Accessor method for the current Tracked object. This method is only
+ * intended to be used by the unsynchronized methods which do not modify the
+ * tracked field.
+ *
+ * @return The current Tracked object.
+ */
+ private Tracked tracked() {
+ return tracked;
+ }
+
+ /**
+ * Cached ServiceReference for getServiceReference.
+ *
+ * This field is volatile since it is accessed by multiple threads.
+ */
+ private volatile ServiceReference cachedReference;
+ /**
+ * Cached service object for getService.
+ *
+ * This field is volatile since it is accessed by multiple threads.
+ */
+ private volatile Object cachedService;
+
+ /**
+ * org.osgi.framework package version which introduced
+ * {@link ServiceEvent#MODIFIED_ENDMATCH}
+ */
+ private static final Version endMatchVersion = new Version(1, 5, 0);
+
+ /**
+ * Create a <code>ServiceTracker</code> on the specified
+ * <code>ServiceReference</code>.
+ *
+ * <p>
+ * The service referenced by the specified <code>ServiceReference</code>
+ * will be tracked by this <code>ServiceTracker</code>.
+ *
+ * @param context The <code>BundleContext</code> against which the tracking
+ * is done.
+ * @param reference The <code>ServiceReference</code> for the service to be
+ * tracked.
+ * @param customizer The customizer object to call when services are added,
+ * modified, or removed in this <code>ServiceTracker</code>. If
+ * customizer is <code>null</code>, then this
+ * <code>ServiceTracker</code> will be used as the
+ * <code>ServiceTrackerCustomizer</code> and this
+ * <code>ServiceTracker</code> will call the
+ * <code>ServiceTrackerCustomizer</code> methods on itself.
+ */
+ public ServiceTracker(final BundleContext context,
+ final ServiceReference reference,
+ final ServiceTrackerCustomizer customizer) {
+ this.context = context;
+ this.trackReference = reference;
+ this.trackClass = null;
+ this.customizer = (customizer == null) ? this : customizer;
+ this.listenerFilter = "(" + Constants.SERVICE_ID + "="
+ + reference.getProperty(Constants.SERVICE_ID).toString() + ")";
+ try {
+ this.filter = context.createFilter(listenerFilter);
+ }
+ catch (InvalidSyntaxException e) {
+ /*
+ * we could only get this exception if the ServiceReference was
+ * invalid
+ */
+ IllegalArgumentException iae = new IllegalArgumentException(
+ "unexpected InvalidSyntaxException: " + e.getMessage());
+ iae.initCause(e);
+ throw iae;
+ }
+ }
+
+ /**
+ * Create a <code>ServiceTracker</code> on the specified class name.
+ *
+ * <p>
+ * Services registered under the specified class name will be tracked by
+ * this <code>ServiceTracker</code>.
+ *
+ * @param context The <code>BundleContext</code> against which the tracking
+ * is done.
+ * @param clazz The class name of the services to be tracked.
+ * @param customizer The customizer object to call when services are added,
+ * modified, or removed in this <code>ServiceTracker</code>. If
+ * customizer is <code>null</code>, then this
+ * <code>ServiceTracker</code> will be used as the
+ * <code>ServiceTrackerCustomizer</code> and this
+ * <code>ServiceTracker</code> will call the
+ * <code>ServiceTrackerCustomizer</code> methods on itself.
+ */
+ public ServiceTracker(final BundleContext context, final String clazz,
+ final ServiceTrackerCustomizer customizer) {
+ this.context = context;
+ this.trackReference = null;
+ this.trackClass = clazz;
+ this.customizer = (customizer == null) ? this : customizer;
+ // we call clazz.toString to verify clazz is non-null!
+ this.listenerFilter = "(" + Constants.OBJECTCLASS + "="
+ + clazz.toString() + ")";
+ try {
+ this.filter = context.createFilter(listenerFilter);
+ }
+ catch (InvalidSyntaxException e) {
+ /*
+ * we could only get this exception if the clazz argument was
+ * malformed
+ */
+ IllegalArgumentException iae = new IllegalArgumentException(
+ "unexpected InvalidSyntaxException: " + e.getMessage());
+ iae.initCause(e);
+ throw iae;
+ }
+ }
+
+ /**
+ * Create a <code>ServiceTracker</code> on the specified <code>Filter</code>
+ * object.
+ *
+ * <p>
+ * Services which match the specified <code>Filter</code> object will be
+ * tracked by this <code>ServiceTracker</code>.
+ *
+ * @param context The <code>BundleContext</code> against which the tracking
+ * is done.
+ * @param filter The <code>Filter</code> to select the services to be
+ * tracked.
+ * @param customizer The customizer object to call when services are added,
+ * modified, or removed in this <code>ServiceTracker</code>. If
+ * customizer is null, then this <code>ServiceTracker</code> will be
+ * used as the <code>ServiceTrackerCustomizer</code> and this
+ * <code>ServiceTracker</code> will call the
+ * <code>ServiceTrackerCustomizer</code> methods on itself.
+ * @since 1.1
+ */
+ public ServiceTracker(final BundleContext context, final Filter filter,
+ final ServiceTrackerCustomizer customizer) {
+ this.context = context;
+ this.trackReference = null;
+ this.trackClass = null;
+ final Version frameworkVersion = (Version) AccessController
+ .doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ String version = context
+ .getProperty(Constants.FRAMEWORK_VERSION);
+ return (version == null) ? Version.emptyVersion
+ : new Version(version);
+ }
+ });
+ final boolean endMatchSupported = (frameworkVersion
+ .compareTo(endMatchVersion) >= 0);
+ this.listenerFilter = endMatchSupported ? filter.toString() : null;
+ this.filter = filter;
+ this.customizer = (customizer == null) ? this : customizer;
+ if ((context == null) || (filter == null)) {
+ /*
+ * we throw a NPE here to be consistent with the other constructors
+ */
+ throw new NullPointerException();
+ }
+ }
+
+ /**
+ * Open this <code>ServiceTracker</code> and begin tracking services.
+ *
+ * <p>
+ * This implementation calls <code>open(false)</code>.
+ *
+ * @throws java.lang.IllegalStateException If the <code>BundleContext</code>
+ * with which this <code>ServiceTracker</code> was created is no
+ * longer valid.
+ * @see #open(boolean)
+ */
+ public void open() {
+ open(false);
+ }
+
+ /**
+ * Open this <code>ServiceTracker</code> and begin tracking services.
+ *
+ * <p>
+ * Services which match the search criteria specified when this
+ * <code>ServiceTracker</code> was created are now tracked by this
+ * <code>ServiceTracker</code>.
+ *
+ * @param trackAllServices If <code>true</code>, then this
+ * <code>ServiceTracker</code> will track all matching services
+ * regardless of class loader accessibility. If <code>false</code>,
+ * then this <code>ServiceTracker</code> will only track matching
+ * services which are class loader accessible to the bundle whose
+ * <code>BundleContext</code> is used by this
+ * <code>ServiceTracker</code>.
+ * @throws java.lang.IllegalStateException If the <code>BundleContext</code>
+ * with which this <code>ServiceTracker</code> was created is no
+ * longer valid.
+ * @since 1.3
+ */
+ public void open(boolean trackAllServices) {
+ final Tracked t;
+ synchronized (this) {
+ if (tracked != null) {
+ return;
+ }
+ if (DEBUG) {
+ System.out.println("ServiceTracker.open: " + filter);
+ }
+ t = trackAllServices ? new AllTracked() : new Tracked();
+ synchronized (t) {
+ try {
+ context.addServiceListener(t, listenerFilter);
+ ServiceReference[] references = null;
+ if (trackClass != null) {
+ references = getInitialReferences(trackAllServices,
+ trackClass, null);
+ }
+ else {
+ if (trackReference != null) {
+ if (trackReference.getBundle() != null) {
+ references = new ServiceReference[] {trackReference};
+ }
+ }
+ else { /* user supplied filter */
+ references = getInitialReferences(trackAllServices,
+ null,
+ (listenerFilter != null) ? listenerFilter
+ : filter.toString());
+ }
+ }
+ /* set tracked with the initial references */
+ t.setInitial(references);
+ }
+ catch (InvalidSyntaxException e) {
+ throw new RuntimeException(
+ "unexpected InvalidSyntaxException: "
+ + e.getMessage(), e);
+ }
+ }
+ tracked = t;
+ }
+ /* Call tracked outside of synchronized region */
+ t.trackInitial(); /* process the initial references */
+ }
+
+ /**
+ * Returns the list of initial <code>ServiceReference</code>s that will be
+ * tracked by this <code>ServiceTracker</code>.
+ *
+ * @param trackAllServices If <code>true</code>, use
+ * <code>getAllServiceReferences</code>.
+ * @param className The class name with which the service was registered, or
+ * <code>null</code> for all services.
+ * @param filterString The filter criteria or <code>null</code> for all
+ * services.
+ * @return The list of initial <code>ServiceReference</code>s.
+ * @throws InvalidSyntaxException If the specified filterString has an
+ * invalid syntax.
+ */
+ private ServiceReference[] getInitialReferences(boolean trackAllServices,
+ String className, String filterString)
+ throws InvalidSyntaxException {
+ if (trackAllServices) {
+ return context.getAllServiceReferences(className, filterString);
+ }
+ return context.getServiceReferences(className, filterString);
+ }
+
+ /**
+ * Close this <code>ServiceTracker</code>.
+ *
+ * <p>
+ * This method should be called when this <code>ServiceTracker</code> should
+ * end the tracking of services.
+ *
+ * <p>
+ * This implementation calls {@link #getServiceReferences()} to get the list
+ * of tracked services to remove.
+ */
+ public void close() {
+ final Tracked outgoing;
+ final ServiceReference[] references;
+ synchronized (this) {
+ outgoing = tracked;
+ if (outgoing == null) {
+ return;
+ }
+ if (DEBUG) {
+ System.out.println("ServiceTracker.close: " + filter);
+ }
+ outgoing.close();
+ references = getServiceReferences();
+ tracked = null;
+ try {
+ context.removeServiceListener(outgoing);
+ }
+ catch (IllegalStateException e) {
+ /* In case the context was stopped. */
+ }
+ }
+ modified(); /* clear the cache */
+ synchronized (outgoing) {
+ outgoing.notifyAll(); /* wake up any waiters */
+ }
+ if (references != null) {
+ for (int i = 0; i < references.length; i++) {
+ outgoing.untrack(references[i], null);
+ }
+ }
+ if (DEBUG) {
+ if ((cachedReference == null) && (cachedService == null)) {
+ System.out
+ .println("ServiceTracker.close[cached cleared]: "
+ + filter);
+ }
+ }
+ }
+
+ /**
+ * Default implementation of the
+ * <code>ServiceTrackerCustomizer.addingService</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>ServiceTracker</code> has been
+ * constructed with a <code>null ServiceTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation returns the result of calling <code>getService</code>
+ * on the <code>BundleContext</code> with which this
+ * <code>ServiceTracker</code> was created passing the specified
+ * <code>ServiceReference</code>.
+ * <p>
+ * This method can be overridden in a subclass to customize the service
+ * object to be tracked for the service being added. In that case, take care
+ * not to rely on the default implementation of
+ * {@link #removedService(ServiceReference, Object) removedService} to unget
+ * the service.
+ *
+ * @param reference The reference to the service being added to this
+ * <code>ServiceTracker</code>.
+ * @return The service object to be tracked for the service added to this
+ * <code>ServiceTracker</code>.
+ * @see ServiceTrackerCustomizer#addingService(ServiceReference)
+ */
+ public Object addingService(ServiceReference reference) {
+ return context.getService(reference);
+ }
+
+ public void addedService(ServiceReference reference, Object service) {
+ /* do nothing */
+ }
+
+ /**
+ * Default implementation of the
+ * <code>ServiceTrackerCustomizer.modifiedService</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>ServiceTracker</code> has been
+ * constructed with a <code>null ServiceTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation does nothing.
+ *
+ * @param reference The reference to modified service.
+ * @param service The service object for the modified service.
+ * @see ServiceTrackerCustomizer#modifiedService(ServiceReference, Object)
+ */
+ public void modifiedService(ServiceReference reference, Object service) {
+ /* do nothing */
+ }
+
+ /**
+ * Default implementation of the
+ * <code>ServiceTrackerCustomizer.removedService</code> method.
+ *
+ * <p>
+ * This method is only called when this <code>ServiceTracker</code> has been
+ * constructed with a <code>null ServiceTrackerCustomizer</code> argument.
+ *
+ * <p>
+ * This implementation calls <code>ungetService</code>, on the
+ * <code>BundleContext</code> with which this <code>ServiceTracker</code>
+ * was created, passing the specified <code>ServiceReference</code>.
+ * <p>
+ * This method can be overridden in a subclass. If the default
+ * implementation of {@link #addingService(ServiceReference) addingService}
+ * method was used, this method must unget the service.
+ *
+ * @param reference The reference to removed service.
+ * @param service The service object for the removed service.
+ * @see ServiceTrackerCustomizer#removedService(ServiceReference, Object)
+ */
+ public void removedService(ServiceReference reference, Object service) {
+ context.ungetService(reference);
+ }
+
+ /**
+ * Wait for at least one service to be tracked by this
+ * <code>ServiceTracker</code>. This method will also return when this
+ * <code>ServiceTracker</code> is closed.
+ *
+ * <p>
+ * It is strongly recommended that <code>waitForService</code> is not used
+ * during the calling of the <code>BundleActivator</code> methods.
+ * <code>BundleActivator</code> methods are expected to complete in a short
+ * period of time.
+ *
+ * <p>
+ * This implementation calls {@link #getService()} to determine if a service
+ * is being tracked.
+ *
+ * @param timeout The time interval in milliseconds to wait. If zero, the
+ * method will wait indefinitely.
+ * @return Returns the result of {@link #getService()}.
+ * @throws InterruptedException If another thread has interrupted the
+ * current thread.
+ * @throws IllegalArgumentException If the value of timeout is negative.
+ */
+ public Object waitForService(long timeout) throws InterruptedException {
+ if (timeout < 0) {
+ throw new IllegalArgumentException("timeout value is negative");
+ }
+ Object object = getService();
+ while (object == null) {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ if (t.size() == 0) {
+ t.wait(timeout);
+ }
+ }
+ object = getService();
+ if (timeout > 0) {
+ return object;
+ }
+ }
+ return object;
+ }
+
+ /**
+ * Return an array of <code>ServiceReference</code>s for all services being
+ * tracked by this <code>ServiceTracker</code>.
+ *
+ * @return Array of <code>ServiceReference</code>s or <code>null</code> if
+ * no services are being tracked.
+ */
+ public ServiceReference[] getServiceReferences() {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ int length = t.size();
+ if (length == 0) {
+ return null;
+ }
+ return (ServiceReference[]) t
+ .getTracked(new ServiceReference[length]);
+ }
+ }
+
+ /**
+ * Returns a <code>ServiceReference</code> for one of the services being
+ * tracked by this <code>ServiceTracker</code>.
+ *
+ * <p>
+ * If multiple services are being tracked, the service with the highest
+ * ranking (as specified in its <code>service.ranking</code> property) is
+ * returned. If there is a tie in ranking, the service with the lowest
+ * service ID (as specified in its <code>service.id</code> property); that
+ * is, the service that was registered first is returned. This is the same
+ * algorithm used by <code>BundleContext.getServiceReference</code>.
+ *
+ * <p>
+ * This implementation calls {@link #getServiceReferences()} to get the list
+ * of references for the tracked services.
+ *
+ * @return A <code>ServiceReference</code> or <code>null</code> if no
+ * services are being tracked.
+ * @since 1.1
+ */
+ public ServiceReference getServiceReference() {
+ ServiceReference reference = cachedReference;
+ if (reference != null) {
+ if (DEBUG) {
+ System.out
+ .println("ServiceTracker.getServiceReference[cached]: "
+ + filter);
+ }
+ return reference;
+ }
+ if (DEBUG) {
+ System.out.println("ServiceTracker.getServiceReference: " + filter);
+ }
+ ServiceReference[] references = getServiceReferences();
+ int length = (references == null) ? 0 : references.length;
+ if (length == 0) { /* if no service is being tracked */
+ return null;
+ }
+ int index = 0;
+ if (length > 1) { /* if more than one service, select highest ranking */
+ int rankings[] = new int[length];
+ int count = 0;
+ int maxRanking = Integer.MIN_VALUE;
+ for (int i = 0; i < length; i++) {
+ Object property = references[i]
+ .getProperty(Constants.SERVICE_RANKING);
+ int ranking = (property instanceof Integer) ? ((Integer) property)
+ .intValue()
+ : 0;
+ rankings[i] = ranking;
+ if (ranking > maxRanking) {
+ index = i;
+ maxRanking = ranking;
+ count = 1;
+ }
+ else {
+ if (ranking == maxRanking) {
+ count++;
+ }
+ }
+ }
+ if (count > 1) { /* if still more than one service, select lowest id */
+ long minId = Long.MAX_VALUE;
+ for (int i = 0; i < length; i++) {
+ if (rankings[i] == maxRanking) {
+ long id = ((Long) (references[i]
+ .getProperty(Constants.SERVICE_ID)))
+ .longValue();
+ if (id < minId) {
+ index = i;
+ minId = id;
+ }
+ }
+ }
+ }
+ }
+ return cachedReference = references[index];
+ }
+
+ /**
+ * Returns the service object for the specified
+ * <code>ServiceReference</code> if the specified referenced service is
+ * being tracked by this <code>ServiceTracker</code>.
+ *
+ * @param reference The reference to the desired service.
+ * @return A service object or <code>null</code> if the service referenced
+ * by the specified <code>ServiceReference</code> is not being
+ * tracked.
+ */
+ public Object getService(ServiceReference reference) {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ return t.getCustomizedObject(reference);
+ }
+ }
+
+ /**
+ * Return an array of service objects for all services being tracked by this
+ * <code>ServiceTracker</code>.
+ *
+ * <p>
+ * This implementation calls {@link #getServiceReferences()} to get the list
+ * of references for the tracked services and then calls
+ * {@link #getService(ServiceReference)} for each reference to get the
+ * tracked service object.
+ *
+ * @return An array of service objects or <code>null</code> if no services
+ * are being tracked.
+ */
+ public Object[] getServices() {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return null;
+ }
+ synchronized (t) {
+ ServiceReference[] references = getServiceReferences();
+ int length = (references == null) ? 0 : references.length;
+ if (length == 0) {
+ return null;
+ }
+ Object[] objects = new Object[length];
+ for (int i = 0; i < length; i++) {
+ objects[i] = getService(references[i]);
+ }
+ return objects;
+ }
+ }
+
+ /**
+ * Returns a service object for one of the services being tracked by this
+ * <code>ServiceTracker</code>.
+ *
+ * <p>
+ * If any services are being tracked, this implementation returns the result
+ * of calling <code>getService(getServiceReference())</code>.
+ *
+ * @return A service object or <code>null</code> if no services are being
+ * tracked.
+ */
+ public Object getService() {
+ Object service = cachedService;
+ if (service != null) {
+ if (DEBUG) {
+ System.out
+ .println("ServiceTracker.getService[cached]: "
+ + filter);
+ }
+ return service;
+ }
+ if (DEBUG) {
+ System.out.println("ServiceTracker.getService: " + filter);
+ }
+ ServiceReference reference = getServiceReference();
+ if (reference == null) {
+ return null;
+ }
+ return cachedService = getService(reference);
+ }
+
+ /**
+ * Remove a service from this <code>ServiceTracker</code>.
+ *
+ * The specified service will be removed from this
+ * <code>ServiceTracker</code>. If the specified service was being tracked
+ * then the <code>ServiceTrackerCustomizer.removedService</code> method will
+ * be called for that service.
+ *
+ * @param reference The reference to the service to be removed.
+ */
+ public void remove(ServiceReference reference) {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return;
+ }
+ t.untrack(reference, null);
+ }
+
+ /**
+ * Return the number of services being tracked by this
+ * <code>ServiceTracker</code>.
+ *
+ * @return The number of services being tracked.
+ */
+ public int size() {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return 0;
+ }
+ synchronized (t) {
+ return t.size();
+ }
+ }
+
+ /**
+ * Returns the tracking count for this <code>ServiceTracker</code>.
+ *
+ * The tracking count is initialized to 0 when this
+ * <code>ServiceTracker</code> is opened. Every time a service is added,
+ * modified or removed from this <code>ServiceTracker</code>, the tracking
+ * count is incremented.
+ *
+ * <p>
+ * The tracking count can be used to determine if this
+ * <code>ServiceTracker</code> has added, modified or removed a service by
+ * comparing a tracking count value previously collected with the current
+ * tracking count value. If the value has not changed, then no service has
+ * been added, modified or removed from this <code>ServiceTracker</code>
+ * since the previous tracking count was collected.
+ *
+ * @since 1.2
+ * @return The tracking count for this <code>ServiceTracker</code> or -1 if
+ * this <code>ServiceTracker</code> is not open.
+ */
+ public int getTrackingCount() {
+ final Tracked t = tracked();
+ if (t == null) { /* if ServiceTracker is not open */
+ return -1;
+ }
+ synchronized (t) {
+ return t.getTrackingCount();
+ }
+ }
+
+ /**
+ * Called by the Tracked object whenever the set of tracked services is
+ * modified. Clears the cache.
+ */
+ /*
+ * This method must not be synchronized since it is called by Tracked while
+ * Tracked is synchronized. We don't want synchronization interactions
+ * between the listener thread and the user thread.
+ */
+ void modified() {
+ cachedReference = null; /* clear cached value */
+ cachedService = null; /* clear cached value */
+ if (DEBUG) {
+ System.out.println("ServiceTracker.modified: " + filter);
+ }
+ }
+
+ /**
+ * Inner class which subclasses AbstractTracked. This class is the
+ * <code>ServiceListener</code> object for the tracker.
+ *
+ * @ThreadSafe
+ */
+ class Tracked extends AbstractTracked implements ServiceListener {
+ /**
+ * Tracked constructor.
+ */
+ Tracked() {
+ super();
+ }
+
+ /**
+ * <code>ServiceListener</code> method for the
+ * <code>ServiceTracker</code> class. This method must NOT be
+ * synchronized to avoid deadlock potential.
+ *
+ * @param event <code>ServiceEvent</code> object from the framework.
+ */
+ public void serviceChanged(final ServiceEvent event) {
+ /*
+ * Check if we had a delayed call (which could happen when we
+ * close).
+ */
+ if (closed) {
+ return;
+ }
+ final ServiceReference reference = event.getServiceReference();
+ if (DEBUG) {
+ System.out
+ .println("ServiceTracker.Tracked.serviceChanged["
+ + event.getType() + "]: " + reference);
+ }
+
+ switch (event.getType()) {
+ case ServiceEvent.REGISTERED :
+ case ServiceEvent.MODIFIED :
+ if (listenerFilter != null) { // service listener added with
+ // filter
+ track(reference, event);
+ /*
+ * If the customizer throws an unchecked exception, it
+ * is safe to let it propagate
+ */
+ }
+ else { // service listener added without filter
+ if (filter.match(reference)) {
+ track(reference, event);
+ /*
+ * If the customizer throws an unchecked exception,
+ * it is safe to let it propagate
+ */
+ }
+ else {
+ untrack(reference, event);
+ /*
+ * If the customizer throws an unchecked exception,
+ * it is safe to let it propagate
+ */
+ }
+ }
+ break;
+ case ServiceEvent.MODIFIED_ENDMATCH :
+ case ServiceEvent.UNREGISTERING :
+ untrack(reference, event);
+ /*
+ * If the customizer throws an unchecked exception, it is
+ * safe to let it propagate
+ */
+ break;
+ }
+ }
+
+ /**
+ * Increment the tracking count and tell the tracker there was a
+ * modification.
+ *
+ * @GuardedBy this
+ */
+ void modified() {
+ super.modified(); /* increment the modification count */
+ ServiceTracker.this.modified();
+ }
+
+ /**
+ * Call the specific customizer adding method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Item to be tracked.
+ * @param related Action related object.
+ * @return Customized object for the tracked item or <code>null</code>
+ * if the item is not to be tracked.
+ */
+ Object customizerAdding(final Object item,
+ final Object related) {
+ return customizer.addingService((ServiceReference) item);
+ }
+
+ void customizerAdded(final Object item, final Object related, final Object object) {
+ customizer.addedService((ServiceReference) item, object);
+ }
+
+ /**
+ * Call the specific customizer modified method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ void customizerModified(final Object item,
+ final Object related, final Object object) {
+ customizer.modifiedService((ServiceReference) item, object);
+ }
+
+ /**
+ * Call the specific customizer removed method. This method must not be
+ * called while synchronized on this object.
+ *
+ * @param item Tracked item.
+ * @param related Action related object.
+ * @param object Customized object for the tracked item.
+ */
+ void customizerRemoved(final Object item,
+ final Object related, final Object object) {
+ customizer.removedService((ServiceReference) item, object);
+ }
+ }
+
+ /**
+ * Subclass of Tracked which implements the AllServiceListener interface.
+ * This class is used by the ServiceTracker if open is called with true.
+ *
+ * @since 1.3
+ * @ThreadSafe
+ */
+ class AllTracked extends Tracked implements AllServiceListener {
+ /**
+ * AllTracked constructor.
+ */
+ AllTracked() {
+ super();
+ }
+ }
+}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTrackerCustomizer.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTrackerCustomizer.java
new file mode 100644
index 0000000..5369107
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/tracker/ServiceTrackerCustomizer.java
@@ -0,0 +1,97 @@
+package org.apache.felix.dependencymanager4.tracker;
+
+/*
+ * Copyright (c) OSGi Alliance (2000, 2008). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.osgi.framework.ServiceReference;
+
+/**
+ * The <code>ServiceTrackerCustomizer</code> interface allows a
+ * <code>ServiceTracker</code> to customize the service objects that are
+ * tracked. A <code>ServiceTrackerCustomizer</code> is called when a service is
+ * being added to a <code>ServiceTracker</code>. The
+ * <code>ServiceTrackerCustomizer</code> can then return an object for the
+ * tracked service. A <code>ServiceTrackerCustomizer</code> is also called when
+ * a tracked service is modified or has been removed from a
+ * <code>ServiceTracker</code>.
+ *
+ * <p>
+ * The methods in this interface may be called as the result of a
+ * <code>ServiceEvent</code> being received by a <code>ServiceTracker</code>.
+ * Since <code>ServiceEvent</code>s are synchronously delivered by the
+ * Framework, it is highly recommended that implementations of these methods do
+ * not register (<code>BundleContext.registerService</code>), modify (
+ * <code>ServiceRegistration.setProperties</code>) or unregister (
+ * <code>ServiceRegistration.unregister</code>) a service while being
+ * synchronized on any object.
+ *
+ * <p>
+ * The <code>ServiceTracker</code> class is thread-safe. It does not call a
+ * <code>ServiceTrackerCustomizer</code> while holding any locks.
+ * <code>ServiceTrackerCustomizer</code> implementations must also be
+ * thread-safe.
+ *
+ * @ThreadSafe
+ * @version $Revision: 5874 $
+ */
+public interface ServiceTrackerCustomizer {
+ /**
+ * A service is being added to the <code>ServiceTracker</code>.
+ *
+ * <p>
+ * This method is called before a service which matched the search
+ * parameters of the <code>ServiceTracker</code> is added to the
+ * <code>ServiceTracker</code>. This method should return the service object
+ * to be tracked for the specified <code>ServiceReference</code>. The
+ * returned service object is stored in the <code>ServiceTracker</code> and
+ * is available from the <code>getService</code> and
+ * <code>getServices</code> methods.
+ *
+ * @param reference The reference to the service being added to the
+ * <code>ServiceTracker</code>.
+ * @return The service object to be tracked for the specified referenced
+ * service or <code>null</code> if the specified referenced service
+ * should not be tracked.
+ */
+ public Object addingService(ServiceReference reference);
+
+ /** marrs: A service has been added to the ServiceTracker. */
+ public void addedService(ServiceReference reference, Object service);
+
+ /**
+ * A service tracked by the <code>ServiceTracker</code> has been modified.
+ *
+ * <p>
+ * This method is called when a service being tracked by the
+ * <code>ServiceTracker</code> has had it properties modified.
+ *
+ * @param reference The reference to the service that has been modified.
+ * @param service The service object for the specified referenced service.
+ */
+ public void modifiedService(ServiceReference reference, Object service);
+
+ /**
+ * A service tracked by the <code>ServiceTracker</code> has been removed.
+ *
+ * <p>
+ * This method is called after a service is no longer being tracked by the
+ * <code>ServiceTracker</code>.
+ *
+ * @param reference The reference to the service that has been removed.
+ * @param service The service object for the specified referenced service.
+ */
+ public void removedService(ServiceReference reference, Object service);
+}