Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2007, 2012). 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.
  */
 
 package org.osgi.util.tracker;
 
 import java.util.Map;
The BundleTracker class simplifies tracking bundles much like the ServiceTracker simplifies tracking services.

A BundleTracker is constructed with state criteria and a BundleTrackerCustomizer object. A BundleTracker can use the BundleTrackerCustomizer to select which bundles are tracked and to create a customized object to be tracked with the bundle. The BundleTracker can then be opened to begin tracking all bundles whose state matches the specified state criteria.

The getBundles method can be called to get the Bundle objects of the bundles being tracked. The getObject method can be called to get the customized object for a tracked bundle.

The BundleTracker class is thread-safe. It does not call a BundleTrackerCustomizer while holding any locks. BundleTrackerCustomizer implementations must also be thread-safe.

Parameters:
<T> The type of the tracked object.
Version:
$Id: f21db4fe54284d4810bd9b5fa2528957804e3a21 $
Since:
1.4
ThreadSafe:
 
 public class BundleTracker<T> implements BundleTrackerCustomizer<T> {
 	/* set this to true to compile in debug messages */
 	static final boolean				DEBUGfalse;

The Bundle Context used by this BundleTracker.
 
 	protected final BundleContext		context;

The BundleTrackerCustomizer object for this tracker.
 
Tracked bundles: Bundle object -> customized Object and BundleListener 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.

Returns:
The current Tracked object.
 
 	private Tracked tracked() {
 		return ;
 	}

State mask for bundles being tracked. This field contains the ORed values of the bundle states being tracked.
 
 	final int	mask;

Create a BundleTracker for bundles whose state is present in the specified state mask.

Bundles whose state is present on the specified state mask will be tracked by this BundleTracker.

Parameters:
context The BundleContext against which the tracking is done.
stateMask The bit mask of the ORing of the bundle states to be tracked.
customizer The customizer object to call when bundles are added, modified, or removed in this BundleTracker. If customizer is null, then this BundleTracker will be used as the BundleTrackerCustomizer and this BundleTracker will call the BundleTrackerCustomizer methods on itself.
See also:
org.osgi.framework.Bundle.getState()
	public BundleTracker(BundleContext contextint stateMaskBundleTrackerCustomizer<T> customizer) {
		this. = context;
		this. = stateMask;
		this. = (customizer == null) ? this : customizer;
	}

Open this BundleTracker and begin tracking bundles.

Bundle which match the state criteria specified when this BundleTracker was created are now tracked by this BundleTracker.

Throws:
java.lang.IllegalStateException If the BundleContext with which this BundleTracker was created is no longer valid.
java.lang.SecurityException If the caller and this class do not have the appropriate AdminPermission[context bundle,LISTENER], and the Java Runtime Environment supports permissions.
	public void open() {
		final Tracked t;
		synchronized (this) {
			if ( != null) {
				return;
			}
			if () {
				..println("BundleTracker.open"); //$NON-NLS-1$
			}
			t = new Tracked();
			synchronized (t) {
				Bundle[] bundles = .getBundles();
				if (bundles != null) {
					int length = bundles.length;
					for (int i = 0; i < lengthi++) {
						int state = bundles[i].getState();
						if ((state & ) == 0) {
							/* null out bundles whose states are not interesting */
							bundles[i] = null;
						}
					}
					/* set tracked with the initial bundles */
					t.setInitial(bundles);
				}
			}
			 = t;
		}
		/* Call tracked outside of synchronized region */
		t.trackInitial(); /* process the initial references */
	}

Close this BundleTracker.

This method should be called when this BundleTracker should end the tracking of bundles.

This implementation calls getBundles() to get the list of tracked bundles to remove.

	public void close() {
		final Bundle[] bundles;
		final Tracked outgoing;
		synchronized (this) {
			outgoing = ;
			if (outgoing == null) {
				return;
			}
			if () {
				..println("BundleTracker.close"); //$NON-NLS-1$
			}
			outgoing.close();
			bundles = getBundles();
			 = null;
			try {
catch (IllegalStateException e) {
				/* In case the context was stopped. */
			}
		}
		if (bundles != null) {
			for (int i = 0; i < bundles.lengthi++) {
				outgoing.untrack(bundles[i], null);
			}
		}
	}

Default implementation of the BundleTrackerCustomizer.addingBundle method.

This method is only called when this BundleTracker has been constructed with a null BundleTrackerCustomizer argument.

This implementation simply returns the specified Bundle.

This method can be overridden in a subclass to customize the object to be tracked for the bundle being added.

Parameters:
bundle The Bundle being added to this BundleTracker object.
event The bundle event which caused this customizer method to be called or null if there is no bundle event associated with the call to this method.
Returns:
The specified bundle.
See also:
BundleTrackerCustomizer.addingBundle(org.osgi.framework.Bundle,org.osgi.framework.BundleEvent)
	public T addingBundle(Bundle bundleBundleEvent event) {
result = (T) bundle;
		return result;
	}

Default implementation of the BundleTrackerCustomizer.modifiedBundle method.

This method is only called when this BundleTracker has been constructed with a null BundleTrackerCustomizer argument.

This implementation does nothing.

Parameters:
bundle The Bundle whose state has been modified.
event The bundle event which caused this customizer method to be called or null if there is no bundle event associated with the call to this method.
object The customized object for the specified Bundle.
See also:
BundleTrackerCustomizer.modifiedBundle(org.osgi.framework.Bundle,org.osgi.framework.BundleEvent,java.lang.Object)
	public void modifiedBundle(Bundle bundleBundleEvent event, T object) {
		/* do nothing */
	}

Default implementation of the BundleTrackerCustomizer.removedBundle method.

This method is only called when this BundleTracker has been constructed with a null BundleTrackerCustomizer argument.

This implementation does nothing.

Parameters:
bundle The Bundle being removed.
event The bundle event which caused this customizer method to be called or null if there is no bundle event associated with the call to this method.
object The customized object for the specified bundle.
See also:
BundleTrackerCustomizer.removedBundle(org.osgi.framework.Bundle,org.osgi.framework.BundleEvent,java.lang.Object)
	public void removedBundle(Bundle bundleBundleEvent event, T object) {
		/* do nothing */
	}

Return an array of Bundles for all bundles being tracked by this BundleTracker.

Returns:
An array of Bundles or null 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 t.copyKeys(new Bundle[length]);
		}
	}

Returns the customized object for the specified Bundle if the specified bundle is being tracked by this BundleTracker.

Parameters:
bundle The Bundle being tracked.
Returns:
The customized object for the specified Bundle or null if the specified Bundle is not being tracked.
	public T 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 BundleTracker. The specified bundle will be removed from this BundleTracker . If the specified bundle was being tracked then the BundleTrackerCustomizer.removedBundle method will be called for that bundle.

Parameters:
bundle The Bundle to be removed.
	public void remove(Bundle bundle) {
		final Tracked t = tracked();
		if (t == null) { /* if BundleTracker is not open */
			return;
		}
		t.untrack(bundlenull);
	}

Return the number of bundles being tracked by this BundleTracker.

Returns:
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 BundleTracker. The tracking count is initialized to 0 when this BundleTracker is opened. Every time a bundle is added, modified or removed from this BundleTracker the tracking count is incremented.

The tracking count can be used to determine if this BundleTracker 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 BundleTracker since the previous tracking count was collected.

Returns:
The tracking count for this BundleTracker or -1 if this BundleTracker 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();
		}
	}

Return a Map with the Bundles and customized objects for all bundles being tracked by this BundleTracker.

Returns:
A Map with the Bundles and customized objects for all services being tracked by this BundleTracker. If no bundles are being tracked, then the returned map is empty.
Since:
1.5
	public Map<Bundle, T> getTracked() {
		Map<Bundle, T> map = new HashMap<Bundle, T>();
		final Tracked t = tracked();
		if (t == null) { /* if BundleTracker is not open */
			return map;
		}
		synchronized (t) {
			return t.copyEntries(map);
		}
	}

Return if this BundleTracker is empty.

Returns:
true if this BundleTracker is not tracking any bundles.
Since:
1.5
	public boolean isEmpty() {
		final Tracked t = tracked();
		if (t == null) { /* if BundleTracker is not open */
			return true;
		}
		synchronized (t) {
			return t.isEmpty();
		}
	}

Inner class which subclasses AbstractTracked. This class is the SynchronousBundleListener object for the tracker.

Since:
1.4
ThreadSafe:
	private final class Tracked extends AbstractTracked<Bundle, T, BundleEventimplements SynchronousBundleListener {
Tracked constructor.
			super();
		}

BundleListener method for the BundleTracker class. This method must NOT be synchronized to avoid deadlock potential.

Parameters:
event BundleEvent object from the framework.
		public void bundleChanged(final BundleEvent event) {
			/*
			 * Check if we had a delayed call (which could happen when we
			 * close).
			 */
			if () {
				return;
			}
			final Bundle bundle = event.getBundle();
			final int state = bundle.getState();
			if () {
				..println("BundleTracker.Tracked.bundleChanged[" + state + "]: " + bundle); //$NON-NLS-1$ //$NON-NLS-2$
			}
			if ((state & ) != 0) {
				track(bundleevent);
				/*
				 * If the customizer throws an unchecked exception, it is safe
				 * to let it propagate
				 */
else {
				untrack(bundleevent);
				/*
				 * 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.

Parameters:
item Item to be tracked.
related Action related object.
Returns:
Customized object for the tracked item or null if the item is not to be tracked.
customizerAdding(final Bundle itemfinal BundleEvent related) {
			return .addingBundle(itemrelated);
		}

Call the specific customizer modified method. This method must not be called while synchronized on this object.

Parameters:
item Tracked item.
related Action related object.
object Customized object for the tracked item.
		void customizerModified(final Bundle itemfinal BundleEvent relatedfinal T object) {
			.modifiedBundle(itemrelatedobject);
		}

Call the specific customizer removed method. This method must not be called while synchronized on this object.

Parameters:
item Tracked item.
related Action related object.
object Customized object for the tracked item.
		void customizerRemoved(final Bundle itemfinal BundleEvent relatedfinal T object) {
			.removedBundle(itemrelatedobject);
		}
	}
New to GrepCode? Check out our FAQ X