Previous Topic

Next Topic

Book Contents

Book Index

Filtering the Data Output

A document on filtering produced data.

The produced output can be filtered in two ways

As mentioned earlier in this document, the data filtering can be time-based or value-based. Value-based filtering can be applied for Number data only.

For constructing value-based filters, the following wire constants can be used:

For constructing time-based filters, the WireConstants.WIREVALUE_ELAPSED ("wirevalue.elapsed") constant can be used. It represents the elapsed time in milliseconds between the current value and the previous value sent by the producer.

The following listing illustrates producer-made output filtering. It provides value-based filtering in the producer's polled method. It sends information about the current temperature only if it is higher than 25 degrees.

                    import org.osgi.framework.*;
import org.osgi.service.wireadmin.*;

import java.util.Hashtable;
  
public class ProducerThatFilters implements Producer {
    
    BundleContext bc;    
    private double currentTemperature;    

    public ProducerThatFilters(BundleContext bc) {
      this.bc = bc;
      Hashtable props = new Hashtable(3);
      Class[] flavors = new Class[] {Double.class};
      //the producer registers for Double data output
      props.put(WireConstants.WIREADMIN_PRODUCER_FLAVORS, flavors);
      props.put(WireConstants.SERVICE_PID, "producer.that.filters");
      // set this registration property with some value
      //to indicate that filtering is performed by the producer
      props.put(WireConstants.WIREADMIN_PRODUCER_FILTERS, "some.value");
      bc.registerService(Producer.class.getName(), this, props);
    }

    public void consumersConnected(Wire[] wires) {
        ...
    }
    
    //the data is sent only if it is no smaller than 25
    public Object polled(Wire wire) {
      return (currentTemperature >= 25.d) ? new Double(currentTemperature) : null;
    }
}
              

Filtering data through the Wire itself is illustrated in Listing from the Getting the WireAdmin Service part.