concept start() in category osgi

appears as: start()
OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

A bundle activator must implement a simple OSGi interface, which in this case is composed of the two methods start() and stop(). At execution time, the framework constructs an instance of this class and invokes the start() method when the bundle is started and the stop() method when the bundle is stopped. (What we mean by starting and stopping a bundle will become clearer in chapter 3.) Because the framework uses the same activator instance while the bundle is active, you can share member variables between the start() and stop() methods.

You may wonder what the single parameter of type BundleContext in the start() and stop() methods is all about. This is how the bundle gets access to the OSGi framework in which it’s executing. From this context object, the module has access to all the OSGi functionality for modularity, lifecycle, and services. In short, it’s a fairly important object for most bundles, but we’ll defer a detailed introduction of it until later when we discuss the lifecycle layer. The important point to take away from this example is that bundles have a simple way to hook into their lifecycle and gain access to the underlying OSGi framework.

This class implements the OSGi BundleActivator interface. When the bundle is installed and started, the framework constructs an instance of this activator class and invokes the start() method. When the bundle is stopped, the framework invokes the stop() method. The start() method is the starting point for your bundle, sort of like the static main() method in standard Java. After it returns, your bundle is expected to function until the stop() method is invoked at some later point. The stop() method should undo anything done by the start() method.

Listing 14.3. Policy-file reader bundle activator start() method
public void start(BundleContext context) { File policyFile = getPolicyFile(context); List<String> encodedInfos = readPolicyFile(policyFile); encodedInfos.add(0, "ALLOW {" + "[org.osgi.service.condpermadmin.BundleLocationCondition \"" + context.getBundle().getLocation() + "\"]" + "(java.security.AllPermission \"*\" \"*\")" + "} \"Management Agent Policy\""); ConditionalPermissionAdmin cpa = getConditionalPermissionAdmin(context); ConditionalPermissionUpdate u = cpa.newConditionalPermissionUpdate(); List infos = u.getConditionalPermissionInfos(); infos.clear(); for (String encodedInfo : encodedInfos) { infos.add(cpa.newConditionalPermissionInfo(encodedInfo)); } if (!u.commit()) { throw new ConcurrentModificationException( "Permissions changed during update"); } }