⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listeners.java

📁 OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2003-2004, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright *   notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above *   copyright notice, this list of conditions and the following *   disclaimer in the documentation and/or other materials *   provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its *   contributors may be used to endorse or promote products derived *   from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */package org.knopflerfish.framework;import java.security.*;import java.util.Collection;import java.util.Set;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashSet;import java.util.HashMap;import java.util.Iterator;import java.util.Dictionary;import java.util.Enumeration;import java.util.Vector;import java.util.Hashtable;import java.util.EventListener;import org.osgi.framework.*;/** * Here we handle all listeners that bundles have registered. * * @author Jan Stein */public class Listeners  implements BundleListener, FrameworkListener, ServiceListener {  /**   * All bundle event listeners.   */  private HashSet bundleListeners = new HashSet();  private HashSet syncBundleListeners = new HashSet();  /**   * All framework event listeners.   */  private HashSet frameworkListeners = new HashSet();  /**   * All service event listeners.   */  private ServiceListenerState serviceListeners = new ServiceListenerState();  /**   * Add a bundle listener to current framework.   *   * @param bundle Who wants to add listener.   * @param listener Object to add.   */  void addBundleListener(Bundle bundle, BundleListener listener) {    ListenerEntry le = new ListenerEntry(bundle, listener);    if (listener instanceof SynchronousBundleListener) {      synchronized (syncBundleListeners) {	syncBundleListeners.add(le);      }    } else {      synchronized (bundleListeners) {	bundleListeners.add(le);      }    }  }  /**   * 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(Bundle bundle, BundleListener listener) {    ListenerEntry le = new ListenerEntry(bundle, listener);    if (listener instanceof SynchronousBundleListener) {      synchronized (syncBundleListeners) {	syncBundleListeners.remove(le);      }    } else {      synchronized (bundleListeners) {	bundleListeners.remove(le);      }    }  }  /**   * Add a bundle listener to current framework.   *   * @param bundle Who wants to add listener.   * @param listener Object to add.   */  void addFrameworkListener(Bundle bundle, FrameworkListener listener) {    ListenerEntry le = new ListenerEntry(bundle, listener);    synchronized (frameworkListeners) {      frameworkListeners.add(le);    }  }  /**   * Remove framework listener from current framework. Silently ignore   * if listener doesn't exist. If listener is registered more than   * once remove all instances.   *   * @param bundle Who wants to remove listener.   * @param listener Object to remove.   */  void removeFrameworkListener(Bundle bundle, FrameworkListener listener) {    synchronized (frameworkListeners) {      frameworkListeners.remove(new ListenerEntry(bundle, listener));    }  }  /**   * Add a service listener with filter to current framework.   * If no filter is wanted, call with filter param set to null.   *   * @param bundle Who wants to add listener.   * @param listener Object to add.   * @param filter LDAP String used for filtering event before calling listener.   */  void addServiceListener(Bundle bundle, ServiceListener listener, String filter)    throws InvalidSyntaxException {    serviceListeners.add(bundle, listener, filter);  }  /**   * Remove service listener from current framework. Silently ignore   * if listener doesn't exist. If listener is registered more than   * once remove all instances.   *   * @param bundle Who wants to remove listener.   * @param listener Object to remove.   */  void removeServiceListener(Bundle bundle, ServiceListener listener) {    serviceListeners.remove(bundle, listener);  }  /**   * Remove all listener registered by a bundle in the current framework.   *   * @param b Bundle which listeners we want to remove.   */  void removeAllListeners(Bundle b) {    removeAllListeners(syncBundleListeners, b);    removeAllListeners(bundleListeners, b);    removeAllListeners(frameworkListeners, b);    serviceListeners.removeAll(b);  }  /**   * Convenience method for throwing framework error event.   *   * @param b Bundle which caused the error.   * @param t Throwable generated.   */  void frameworkError(Bundle b, Throwable t) {    frameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, b, t));  }  //  // BundleListener interface  //  /**   * Receive notification that a bundle has had a change occur in it's lifecycle.   *   * @see org.osgi.framework.BundleListener#bundleChanged   */  public void bundleChanged(final BundleEvent evt) {    ListenerEntry [] bl, tmp;    synchronized (bundleListeners) {      tmp = new ListenerEntry[bundleListeners.size()];      bundleListeners.toArray(tmp);    }    synchronized (syncBundleListeners) {      bl = new ListenerEntry[tmp.length + syncBundleListeners.size()];      syncBundleListeners.toArray(bl);    }    System.arraycopy(tmp, 0, bl, bl.length - tmp.length, tmp.length);    for (int i = 0; i < bl.length; i++) {      final ListenerEntry l = bl[i];      try {	AccessController.doPrivileged(new PrivilegedAction() {	    public Object run() {	      ((BundleListener)l.listener).bundleChanged(evt);	      return null;	    }	  });      } catch (Throwable pe) {	frameworkError(l.bundle, pe);      }    }  }  //  // FrameworkListener interface  //  /**   * Receive notification of a general framework event.   *   * @see org.osgi.framework.FrameworkListener#frameworkEvent   */  public void frameworkEvent(final FrameworkEvent evt) {    if (Debug.errors) {      if (evt.getType() == FrameworkEvent.ERROR) {	Debug.println("errors - FrameworkErrorEvent bundle #" + evt.getBundle().getBundleId());	Debug.printStackTrace("errors - FrameworkErrorEvent throwable: ", evt.getThrowable());      }    }    ListenerEntry [] fl;    synchronized (frameworkListeners) {      fl = new ListenerEntry[frameworkListeners.size()];      frameworkListeners.toArray(fl);    }    for (int i = 0; i < fl.length; i++) {      final ListenerEntry l = fl[i];      try {	AccessController.doPrivileged(new PrivilegedAction() {	    public Object run() {	      ((FrameworkListener)l.listener).frameworkEvent(evt);	      return null;	    }	  });      } catch (Throwable pe) {	// We can't report Error events again, since we probably would	// go into a infinite loop.	if (evt.getType() != FrameworkEvent.ERROR) {	  frameworkError(l.bundle, pe);	}      }    }  }  //  // ServiceListener interface  //  /**   * Receive notification that a service has had a change occur in it's lifecycle.   *   * @see org.osgi.framework.ServiceListener#serviceChanged   */  public void serviceChanged(final ServiceEvent evt) {    ServiceReferenceImpl sr = (ServiceReferenceImpl)evt.getServiceReference();    String[] c = (String[])sr.getProperty(Constants.OBJECTCLASS);    Permission[] perms = new Permission[c.length];    for (int i = 0; i < c.length; i++) {      perms[i] = new ServicePermission(c[i], ServicePermission.GET);    }    Set sl = serviceListeners.getMatchingListeners(evt);    int n = 0;    for (Iterator it = sl.iterator(); it.hasNext(); n++) {      final ServiceListenerEntry l = (ServiceListenerEntry)it.next();      try {	for (int j = 0; j < c.length; j++) {	  if (l.bundle.hasPermission(perms[j])) {	    try {	      AccessController.doPrivileged(new PrivilegedAction() {		  public Object run() {		    ((ServiceListener)l.listener).serviceChanged(evt);		    return null;		  }		});	    } catch (Throwable pe) {	      frameworkError(l.bundle, pe);	    }	    break;	  }	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -