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.
org.eclipse.soda.sat.tutorial.customer that is targeted for
the standard OSGi framework.
org.eclipse.soda.sat.tutorial.customer, making it match the
name of the plug-in project.
Customer.
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.
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.
Import-Package manifest header rather than the
Require-Bundle manifest header.
org.eclipse.soda.sat.tutorial.customer.
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:
Customer is composed of a
VendorService from which it periodically buys.
start(VendorService) method is called by the
Activator class' activate() method to
start the Customer when a VendorService
has been acquired from the OSGi framework.
stop() method is called by the
Activator class' deactivate() method to
stop the Customer when its VendorService
has been released back to the OSGi framework.
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;
}
}
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.
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.
org.eclipse.soda.sat.tutorial.customer.bundle and the
Name field should have been set to Activator. Click
the Next button.
VendorService. The interface
org.eclipse.soda.sat.tutorial.vendor.service.VendorService
should now appear in the Imported Service list.
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$
}
}
org.eclipse.soda.sat.core.framework will now
appear in the Imported Packages list.
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
Copyright © 2001, 2007 IBM Corporation and others. All Rights Reserved.