Step 4: Create the Customer Bundle

In this step the Customer bundle is created that imports the package containing the VendorService interface and acquires an implementation of the service from the OSGi framework. The Customer bundle is at the top of the bundle food chain, meaning that it does not provide any services, but rather uses services provided by others.

  1. Create a new plug-in project called org.eclipse.soda.sat.tutorial.customer that is targeted for the standard OSGi framework.
  2. Set the Plug-in ID field to org.eclipse.soda.sat.tutorial.customer, making it match the name of the plug-in project.
  3. Set the Plug-in Name field to Customer.
  4. Take care on the second page of the wizard to uncheck the checkbox titled Generate an activator, a Java class that controls the plug-in's life cycle since we do not want the PDE to generate a bundle activator. We shall be using the SAT's Bundle Activator Wizard to generate a bundle activator.
  5. Automated Management of Dependencies
  6. Open the plug-in's META-INF/MANIFEST.MF file using the manifest editor. Turn to the Dependencies page and add the bundle org.eclipse.soda.sat.tutorial.vendor.service to the Automated Management of Dependencies list. Doing this will result in the bundle appearing in the plug-in project's Plug-in Dependencies.
  7. Also under the Automated Management of Dependencies section, select the Import-Package radio button. This tells the editor that you wish the bundle's dependencies to be expressed using the Import-Package manifest header rather than the Require-Bundle manifest header.
  8. Go to the Package Explorer and add a package to the project called org.eclipse.soda.sat.tutorial.customer.
  9. Add to the package a class called Customer. This model class is fairly complicated, but it is not really necessary to understand every detail. The most important things to understand are as follows: The Customer class is as follows:
    package org.eclipse.soda.sat.tutorial.customer;
    
    import org.eclipse.soda.sat.core.util.LogUtility;
    import org.eclipse.soda.sat.tutorial.vendor.service.VendorService;
    
    public class Customer extends Object {
      private VendorService vendor;
    
      private VendorService getVendor() {
        return vendor;
      }
    
      public void eat() {
        VendorService vendor = getVendor();
        if (vendor == null) return;  // Early return.		
        String name = vendor.getName();
        String product = vendor.sell();
        LogUtility.logInfo("The Customer bought " + product + " from " + name);  //$NON-NLS-1$  //$NON-NLS-2$
      }
    		
      public void setVendor(VendorService vendor) {
        this.vendor = vendor;
      }		
    }
    
  10. Return to the manifest editor. Turn to the Dependencies page and in the Automated Management of Dependencies section click the add dependencies link. This will cause the Imported Packages list to be updated with the dependencies as computed by the PDE. The package org.eclipse.soda.sat.tutorial.vendor.service will now appear in the Imported Packages list. This is the recommended way to manage a bundle's dependencies.
  11. In the Package Explorer select the package org.eclipse.soda.sat.tutorial.customer and then choose File > New > Other... and then expand Service Activator Toolkit, select Bundle Activator and click the Next button.
  12. On the first page of the Bundle Activator Wizard accept the default values. The Package field should have been set to org.eclipse.soda.sat.tutorial.customer.bundle and the Name field should have been set to Activator. Click the Next button.
  13. On the second page of the Bundle Activator Wizard click the Add... to the right of the Imported Service list. Enter the service name VendorService. The interface org.eclipse.soda.sat.tutorial.vendor.service.VendorService should now appear in the Imported Service list.
  14. Importing the VendorService

  15. Click the Finish button to have the Bundle Activator Wizard generate the bundle activator.
  16. Update bundle activator as follows:
     
    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;
      }
    
      protected void start() throws Exception {
        LogUtility.logInfo("The Customer bundle has been started");  //$NON-NLS-1$		
        Customer customer = new Customer();
        setCustomer(customer);
      }
    	
      protected void stop() throws Exception {
        setCustomer(null);
        LogUtility.logInfo("The Customer bundle has been stopped");  //$NON-NLS-1$
      }
    }
    
  17. Return to the manifest editor. Turn to the Overview page and notice that in the General Information section the wizard has set the Activator field to the name of the generated bundle activator.
  18. Turn to the manifest editor's Dependencies page and notice that in the Automated Management of Dependencies section the wizard has updated the list of bundles by adding the necessary Equinox and SAT runtime bundles.
  19. Imported Packages
  20. Click the add dependencies link. This will cause the Imported Packages list to be updated with the dependencies as computed by the PDE. The package org.eclipse.soda.sat.core.framework will now appear in the Imported Packages list.
  21. The plug-in's META-INF/MANIFEST.MF file should be as follows.
    Manifest-Version: 1.0
    Bundle-Activator: org.eclipse.soda.sat.tutorial.customer.bundle.Activator
    Bundle-ManifestVersion: 2
    Bundle-Name: Customer
    Bundle-SymbolicName: org.eclipse.soda.sat.tutorial.customer
    Bundle-Version: 1.0.0
    Import-Package: org.eclipse.soda.sat.core.framework,
     org.eclipse.soda.sat.core.util,
     org.eclipse.soda.sat.tutorial.vendor.service