Step 4: Updating the Customer Bundle to Import a Vendor Service using a Filter

In this step the Customer bundle is updated to import an implementation of the VendorService using an LDAP filter.

  1. In the Package Explorer find the org.eclipse.soda.sat.tutorial.customer project.
  2. Find the bundle activator and update it as follows. The updates are highlighted in bold.
    package org.eclipse.soda.sat.tutorial.customer.bundle;
    
    import org.eclipse.soda.sat.core.framework.BaseBundleActivator;
    import org.eclipse.soda.sat.core.util.LogUtility;
    import org.eclipse.soda.sat.tutorial.customer.Customer;
    import org.eclipse.soda.sat.tutorial.vendor.service.VendorService;
    
    public class Activator extends BaseBundleActivator {
      private Customer customer;
    
      protected void activate() {
        LogUtility.logInfo("The Customer bundle has been activated");  //$NON-NLS-1$
        VendorService vendor = getVendorService();
        Customer customer = getCustomer();
        customer.setVendor(vendor);
        customer.eat();
      }
    
      protected void deactivate() {
        Customer customer = getCustomer();
        customer.setVendor(null);
        LogUtility.logInfo("The Customer bundle has been deactivated");  //$NON-NLS-1$
      }
    
      private Customer getCustomer() {
        return customer;
      }
    
      protected String[] getImportedServiceNames() {
        return new String[] {
          VendorService.SERVICE_NAME
        };
      }
    
      private VendorService getVendorService() {
        return (VendorService) getImportedService(VendorService.SERVICE_NAME);
      }
    
      private void setCustomer(Customer customer) {
        this.customer = customer;
      }
    
      private String getVendorFilter() {
        StringBuffer buffer = new StringBuffer(50);
        buffer.append('(');
        buffer.append(VendorService.SPICINESS_PROPERTY);
        buffer.append("<=");  //$NON-NLS-1$
        buffer.append(8);
        buffer.append(')');
        String filter = buffer.toString();
        return filter;
      }
    
      protected void start() throws Exception {
        LogUtility.logInfo("The Customer bundle has been started");  //$NON-NLS-1$
        Customer customer = new Customer();
        setCustomer(customer);
        String filter = getVendorFilter();
        addImportedServiceFilter(VendorService.SERVICE_NAME, filter);
      }
    
      protected void stop() throws Exception {
        setCustomer(null);
        LogUtility.logInfo("The Customer bundle has been stopped");  //$NON-NLS-1$
      }
    }
  3. As you can see, the start() methods has been updated to create an LDAP filter that is passed to the addImportedServiceFilter(String, String) method. This causes the imported VendorService to have the LDAP filter "(spiciness<=8)", which is created by the getVendorFilter() method.
  4. At runtime the activate() method will only be called when the Customer bundle has acquired an imported VendorService that has a registered VendorService.SPICINESS_PROPERTY that is less than or equal to 8.
  5. Once acquired, if the service's registered properties change such that VendorService.SPICINESS_PROPERTY is greater than 8 then the deactivate() method will be called.
  6. Also, the Customer bundle can change its imported service filters at any time by simply calling the addImportedServiceFilter(String, String) method, or the removeImportedServiceFilter(String) method. Changes to the imported service filters will be immediately reflected in the bundles' acquired imported services.