This is a good showcase for SAT since it demonstrates the simplicity of building a bundle that imports multiple dynamic services as a prerequisite to exporting a service.
public class Activator extends BaseBundleActivator {
private HotdogVendor vendorService;
public Activator() {
super();
vendorService= new HotdogVendor();
}
protected String[] getImportedServiceNames() {
return new String[] {
BunService.SERVICE_NAME,
WienerService.SERVICE_NAME
};
}
protected void activate() {
System.out.println("Hotdog Vendor has been activated");
BunService bunService = getBunService();
WienerService wienerService = getWienerService();
vendorService.bind(bunService, wienerService);
addExportedService(VendorService.SERVICE_NAME, vendorService, null);
}
protected void deactivate() {
vendorService.unbind();
System.out.println("Hotdog Vendor has been deactivated");
}
private BunService getBunService() {
return (BunService) getImportedService(BunService.SERVICE_NAME);
}
private WienerService getWienerService() {
return (WienerService) getImportedService(WienerService.SERVICE_NAME);
}
}
- The method
getImportedServiceNames()
returns the names of the imported services. TheSERVICE_NAME
field is the fully qualified name of the service interface and is an SAT convention. - The methods
getBunService()
andgetWienerService()
are helper methods; another SAT convention. - The
activate()
method is a hook method that only gets called when all the bundle's imported services have been acquired. When called from theactivate()
method, thegetBunService()
andgetWienerService()
method are guaranteed to not returnnull
. - The
deactivate()
method is a hook method that only gets called when the bundle loses one of its imported services. - The use of a bind method in
activate()
and an unbind method indeactivate()
is another SAT convention that simplifies the binding and unbinding of imported services. It would be equally valid to call setter methods instead. - The methods
activate()
anddeactivate()
are a pair and will get called multiple times during the lifetime of the bundle as its imported services change.
Historically this has always been the first example used to teach SAT since everyone understand that you're not selling hotdogs without both buns and wieners!
Credit for this example goes squarely to Paul Vanderlei, my colleague and buddy at IBM. Paul has been involved with SAT from the very start while developing it at OTI and later at IBM. Paul is the first person to tell me when "it's too hard for the caveman". Much of SAT's simplicity is thanks to Paul and his alter-ego, the caveman, who has existed long before the Geico ad campaign.
To learn more about SAT, check out the introductory tutorial.