Previous Topic

Next Topic

Book Contents

Book Index

Creating a WireAdmin Listener

The WireAdminListener interface allows you to receive WireAdminEvents.

The WireAdminListener interface allows you to receive WireAdminEvent-s notifying of changes in the state of the wire. The WireAdminEvent class provides the following types of events and their correspondent class fields:

WireAdminListener objects must be registered as a service by using the WireConstants.WIREADMIN_EVENTS service property. The value of this property represents a bitwise OR of all the event types the listener is interested in. If a WireAdminListener object is registered without a service property WireConstants.WIREADMIN_EVENTS, then the WireAdminListener will receive no events.

The following example registers a Wire Admin Listener for exceptions generated by producers and consumers.

Listing 1. Creating a Wire Admin Listener

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.wireadmin.*;

import java.util.Hashtable;

public class ExceptionsListener implements BundleActivator, WireAdminListener {

  private ServiceRegistration reg;

  public void start(BundleContext bc) {
    //bitwise OR of the event types
    Integer bitmask = new Integer(WireAdminEvent.PRODUCER_EXCEPTION |
                                  WireAdminEvent.CONSUMER_EXCEPTION);
    Hashtable props = new Hashtable();
    props.put(WireConstants.WIREADMIN_EVENTS, bitmask);
    reg = bc.registerService(WireAdminListener.class.getName(), this, props);
  }

  public void stop(BundleContext bc) {
    reg.unregister();
  }

  // WireAdminListener implementation
  public void wireAdminEvent(WireAdminEvent e) {
    switch (e.getType()) {
      case WireAdminEvent.PRODUCER_EXCEPTION:
      // do some work
      break;
      case WireAdminEvent.CONSUMER_EXCEPTION:
      // do some work
      break;
    }
  }
}