Previous Topic

Next Topic

Book Contents

Book Index

MetaTypeProvider Implementation

Metadata can be defined as a Metatype Provider service in the OSGi framework. Following is an example provider for the "Server" type, which defines a client application.

Listing 1. An example Metatype Provider.

import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;

import org.osgi.service.metatype.MetaTypeProvider;
import org.osgi.service.metatype.ObjectClassDefinition;

public class ServerMetaTypeProvider implements BundleActivator, MetaTypeProvider {
  private OCD ocd = null;
  private AD [] attrDefinitions = null;
  protected static String PID = "clientapp";
  private ServiceRegistration sReg = null;
  
    // Methods inherited from BundleActivator
  public void start(BundleContext bc) throws Exception {

    // Creating an ObjectClassDefinition
    attrDefinitions = new AD[2];
    attrDefinitions[0] = new AD("isServer", AttributeDefinition.BOOLEAN, 0);
    attrDefinitions[0].setDefaultValue(new String[] { "false" });
    attrDefinitions[1] = new AD("port", AttributeDefinition.INTEGER, 0);
    attrDefinitions[1].setDefaultValue(new String[] { "5983" });
    ocd = new OCD(PID, attrDefinitions);  
  
    // Registering the Metatype Provider
    Hashtable regProps = new Hashtable();
    regProps.put(Constants.SERVICE_PID, PID);
    regProps.put("metadata.type", "Server");
    regProps.put("metadata.version", "1.0.0");
    sReg = bc.registerService(MetaTypeProvider.class.getName(), this, regProps);
    ocd.setName("My Example Metatype");
  }

  public void stop(BundleContext bc) throws Exception {
    if (sReg != null) {
      sReg.unregister();
     }
     ocd = null;
  }
  
  // Methods inherited from MetaTypeProvider
  public String[] getLocales() {
    return null;
  }
  
  public ObjectClassDefinition getObjectClassDefinition(String pid, String locale) {
    return ocd;
  }
}

Listing 2. An example ObjectClassDefinition.

import java.io.IOException;
import java.io.InputStream;      

import org.osgi.service.metatype.AttributeDefinition;
import org.osgi.service.metatype.ObjectClassDefinition;

public class OCD implements ObjectClassDefinition {

  private String description;
  private String name;
  // Represents the attributes of this object class
  AttributeDefinition[] requiredADs = new AttributeDefinition[2];
  private String id;

  protected OCD(String id, AttributeDefinition[] requiredADs) {
    this.id = id;
    this.requiredADs = requiredADs;
  }

  public AttributeDefinition[] getAttributeDefinitions(int filter) {
    if (filter == ObjectClassDefinition.OPTIONAL) {
      return null;
    }
    return requiredADs;
  }

  public String getDescription() {
    return description;
  }

  public InputStream getIcon(int arg0) throws IOException {
    return null;
  }

  public String getID() {
    return id;
  }

  public String getName() {
    return name;
  }

  protected void setName(String name) {
    this.name = name;
  }

  protected void setDescription(String description) {
    this.description = description;
  }
}

Listing 3. An example AttributeDefinition

import org.osgi.service.metatype.AttributeDefinition;    

public class AD implements AttributeDefinition {
  private String id;
  private int type = 0;
  private int cardinality = -1;
  private String name;
  private String description;
  private String[] defValue;
  private String[] optionalLabels;
  private String[] optionalValues;
  
  protected AD(String id, int type, int cardinality) {
    this.id = id;
    this.type = type;
    this.cardinality = cardinality;
  }
  
  public int getCardinality() {
    return cardinality;
  }
    
  public String[] getDefaultValue() {
    return defValue;
  }
    
  public String getDescription() {
    return description;
  }
    
  public String getID() {
    return id;
  }

  public String getName() {
    return name;
  }
    
  public String[] getOptionLabels() {
    return optionalLabels;
  }
    
  public String[] getOptionValues() {
    return optionalValues;
  }
  
  public int getType() {
    return type;
  }
    
  public String validate(String arg0) {
    return null;
  }
  
  protected void setOptionLabels(String[] labels) {
    optionalLabels = labels;
  }
  
  protected void setOptionValues(String[] values) {
    optionalValues = values;
  }
    
  protected void setDescription(String description) {
    this.description = description;
  }

  protected void setName(String name) {
    this.name = name;
  }

  protected void setDefaultValue(String[] defValue) {
    this.defValue = defValue;
  }
}