org.osgi.framework.SynchronousBundleListener Java Examples

The following examples show how to use org.osgi.framework.SynchronousBundleListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * add a bundle listener.
 * 
 * @param listener
 *            a bundle listener.
 * @see org.osgi.framework.BundleContext#addBundleListener(org.osgi.framework.BundleListener)
 */
public void addBundleListener(final BundleListener listener) {
	checkValid();

	final List<BundleListener> registered = bundleListenerMap.get(this);

	if (registered == null || !registered.contains(listener)) {
		if (listener instanceof SynchronousBundleListener) {
			syncBundleListeners
					.add((SynchronousBundleListener) listener);
		} else {
			bundleListeners.add(listener);
		}
		bundleListenerMap.insert(this, listener);
	}
}
 
Example #2
Source File: MainFragment.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * 监听插件安装事件,当有新插件安装或卸载时成功也更新一下
 */
public void ListenerBundleEvent(){
    BundleContextFactory.getInstance().getBundleContext()
            .addBundleListener(
                    new SynchronousBundleListener(){

                        public void bundleChanged(BundleEvent event) {
                            //把插件列表清空
                            bundles.clear();
                            BundleContext context =BundleContextFactory.getInstance().getBundleContext();
                            for(int i=0;i<context.getBundles().length;i++)
                            {
                                bundles.add(context.getBundles()[i]);

                            }
                            adapter.notifyDataSetChanged();
                        }

                    });
}
 
Example #3
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * remove a bundle listener.
 * 
 * @param listener
 *            a bundle listener.
 * @see org.osgi.framework.BundleContext#removeBundleListener(org.osgi.framework.BundleListener)
 * 
 */
public void removeBundleListener(final BundleListener listener) {
	checkValid();

	if (bundle == Concierge.this) {
		return;
	}

	(listener instanceof SynchronousBundleListener ? syncBundleListeners
			: bundleListeners).remove(listener);
	bundleListenerMap.remove(this, listener);
}
 
Example #4
Source File: Listeners.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a bundle listener to the current framework.
 *
 * @param bundle Who wants to add listener.
 * @param listener Object to add.
 */
void addBundleListener(BundleContextImpl bc, BundleListener listener) {
  final ListenerEntry le = new ListenerEntry(bc, listener);
  if (listener instanceof SynchronousBundleListener) {
    secure.checkListenerAdminPerm(bc.bundle);
    synchronized (syncBundleListeners) {
        syncBundleListeners.add(le);
    }
  }
  else {
    synchronized (bundleListeners) {
        bundleListeners.add(le);
    }
  }
}
 
Example #5
Source File: Listeners.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Remove bundle listener from current framework. Silently ignore
 * if listener doesn't exist. If listener is registered more than
 * once remove one instances.
 *
 * @param bundle Who wants to remove listener.
 * @param listener Object to remove.
 */
void removeBundleListener(BundleContextImpl bc, BundleListener listener) {
  final ListenerEntry le = new ListenerEntry(bc, listener);
  if (listener instanceof SynchronousBundleListener) {
    synchronized (syncBundleListeners) {
      secure.checkListenerAdminPerm(bc.bundle);
      syncBundleListeners.remove(le);
    }
  } else {
    synchronized (bundleListeners) {
      bundleListeners.remove(le);
    }
  }
}
 
Example #6
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void addBundleListener(BundleListener bundleListener) {
    checkValid();
    List list = bundleListener instanceof SynchronousBundleListener ? Framework.syncBundleListeners : Framework.bundleListeners;
    if (this.bundle.registeredBundleListeners == null) {
        this.bundle.registeredBundleListeners = new ArrayList();
    }
    if (!this.bundle.registeredBundleListeners.contains(bundleListener)) {
        list.add(bundleListener);
        this.bundle.registeredBundleListeners.add(bundleListener);
    }
}
 
Example #7
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void removeBundleListener(BundleListener bundleListener) {
    checkValid();
    (bundleListener instanceof SynchronousBundleListener ? Framework.syncBundleListeners : Framework.bundleListeners).remove(bundleListener);
    this.bundle.registeredBundleListeners.remove(bundleListener);
    if (this.bundle.registeredBundleListeners.isEmpty()) {
        this.bundle.registeredBundleListeners = null;
    }
}
 
Example #8
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * notify all bundle listeners.
 * 
 * @param state
 *            the new state.
 * @param bundle
 *            the bundle.
 */
void notifyBundleListeners(final int state, final Bundle bundle,
		final Bundle origin) {
	if (syncBundleListeners.isEmpty() && bundleListeners.isEmpty()) {
		return;
	}

	final BundleEvent event = new BundleEvent(state, bundle, origin);

	final SynchronousBundleListener[] syncs;
	final BundleListener[] asyncs;

	// call the hooks, if any
	if (!bundleEventHooks.isEmpty()) {
		final ArrayList<SynchronousBundleListener> syncListeners = new ArrayList<SynchronousBundleListener>(
				syncBundleListeners);
		final ArrayList<BundleListener> asyncListeners = new ArrayList<BundleListener>(
				bundleListeners);

		final ConciergeCollections.DeltaTrackingRemoveOnlyList<BundleContext> contexts = new ConciergeCollections.DeltaTrackingRemoveOnlyList<BundleContext>(
				bundleListenerMap.keySet());

		for (final ServiceReferenceImpl<org.osgi.framework.hooks.bundle.EventHook> sref : bundleEventHooks) {
			final org.osgi.framework.hooks.bundle.EventHook eventHook = sref
					.getService(Concierge.this);
			if (eventHook != null) {
				try {
					eventHook.event(event, contexts);
				} catch (final Throwable t) {
					// TODO: to log?
				}
			}
			sref.ungetService(Concierge.this);
		}

		for (final BundleContext removed : contexts.getRemoved()) {
			if(removed != this.context){ // system bundle contexts listeners always gets events
				for (final BundleListener listener : bundleListenerMap
					.get(removed)) {
					syncListeners.remove(listener);
					asyncListeners.remove(listener);
				}
			}
		}

		syncs = syncListeners.toArray(
				new SynchronousBundleListener[syncListeners.size()]);
		asyncs = asyncListeners
				.toArray(new BundleListener[asyncListeners.size()]);
	} else {
		syncs = syncBundleListeners.toArray(
				new SynchronousBundleListener[syncBundleListeners.size()]);
		asyncs = bundleListeners
				.toArray(new BundleListener[bundleListeners.size()]);
	}

	for (int i = 0; i < syncs.length; i++) {
		syncs[i].bundleChanged(event);
	}

	// asynchronous listeners do not get these events
	final int type = event.getType();
	if (bundleListeners.isEmpty() || (type & (BundleEvent.STARTING
			| BundleEvent.STOPPING | BundleEvent.LAZY_ACTIVATION)) > 0) {
		return;
	}

	for (int i = 0; i < asyncs.length; i++) {
		asyncs[i].bundleChanged(event);
	}
}