Step 3: Create the Popcorn Vendor Bundle

In this step the Popcorn Vendor bundle is created that imports the package containing the VendorService interface and registers an implementation of the service with the OSGi framework. This step is very similar to the previous step where we created the Hotdog Vendor bundle.

  1. Create a new plug-in project called org.eclipse.soda.sat.tutorial.vendor.popcorn that is targeted for the standard OSGi framework.
  2. Set the Plug-in ID field to org.eclipse.soda.sat.tutorial.vendor.popcorn, making it match the name of the plug-in project.
  3. Set the Plug-in Name field to Popcorn Vendor.
  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.vendor.popcorn.
  9. Add to the package a class called PopcornVendor that implements the VendorService interface as follows:
    package org.eclipse.soda.sat.tutorial.vendor.popcorn;
    
    import org.eclipse.soda.sat.tutorial.vendor.service.VendorService;
    
    public class PopcornVendor extends Object implements VendorService {
      private String name;
    
      public PopcornVendor(String name) {
        super();
        setName(name);
      }
    
      public String getName() {
        return name;
      }
    	
      public String sell() {
        return "popcorn";  //$NON-NLS-1$
      }
    	
      private void setName(String name) {
        this.name = name;
      }
    }
    
  10. Imported Packages
  11. 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.
  12. In the Package Explorer select the package org.eclipse.soda.sat.tutorial.vendor.popcorn and then choose File > New > Other... and then expand Service Activator Toolkit, select Bundle Activator and click the Next button.
  13. 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.vendor.popcorn.bundle and the Name field should have been set to Activator. Click the Next button.
  14. On the second page of the Bundle Activator Wizard click the Add... to the right of the Exported Service list. Enter the service name VendorService. The interface org.eclipse.soda.sat.tutorial.vendor.service.VendorService should now appear in the Exported Service list.
  15. Exporting the VendorService

  16. Click the Finish button to have the Bundle Activator Wizard generate the bundle activator.
  17. Update bundle activator as follows:
     
    package org.eclipse.soda.sat.tutorial.vendor.popcorn.bundle;
    
    import org.eclipse.soda.sat.core.framework.BaseBundleActivator;
    import org.eclipse.soda.sat.core.util.LogUtility;
    import org.eclipse.soda.sat.tutorial.vendor.popcorn.PopcornVendor;
    import org.eclipse.soda.sat.tutorial.vendor.service.VendorService;
    
    public class Activator extends BaseBundleActivator {
      protected void activate() {
        LogUtility.logInfo("The Popcorn Vendor bundle has been activated");  //$NON-NLS-1$
        addExportedVendorService();  //$NON-NLS-1$
      }
    
      private void addExportedVendorService() {
        VendorService service = new PopcornVendor("Orville Redenbacher");  //$NON-NLS-1$
        addExportedService(VendorService.SERVICE_NAME, service, null);
      }
    
      protected void deactivate() {
        LogUtility.logInfo("The Popcorn Vendor bundle has been deactivated");  //$NON-NLS-1$
      }
    }
    
  18. 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.
  19. 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.
  20. Imported Packages
  21. 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.
  22. The plug-in's META-INF/MANIFEST.MF file should be as follows.
    Manifest-Version: 1.0
    Bundle-Activator: org.eclipse.soda.sat.tutorial.vendor.popcorn.bundle.Activator
    Bundle-ManifestVersion: 2
    Bundle-Name: Popcorn Vendor
    Bundle-SymbolicName: org.eclipse.soda.sat.tutorial.vendor.popcorn
    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