Time for action - implementing a bundle activator

The activator for this bundle will only be responsible for registering commands. It's not of greater complexity than the ones we've seen so far.

Create the class BookshelfTuiActivator and make it register an instance of the BookshelfServiceProxy at start.

The values for the command-related properties were defined in the proxy as constants earlier. Defining them in the service class makes it easier to update them later (for example, when we include the add command).

public class BookshelfTuiActivator implements BundleActivator
{
public void start(BundleContext bc)
{
Hashtable props = new Hashtable();
props.put("osgi.command.scope", BookshelfServiceProxy.SCOPE);
props.put("osgi.command.function",
BookshelfServiceProxy.FUNCTIONS);
bc.registerService(
BookshelfServiceProxy.class.getName(),
new BookshelfServiceProxy(bc),
props);
}
public void stop(BundleContext bc)
{
}
}

The service is registered as we did before. The difference is that we also provide the service properties along with the register request.

You still need to configure the bundle plugin in the POM, and you're done. Nothing really special for this configuration, compared to the previous ones. For reference, here's what that section would look like:

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Category>sample</Bundle-Category>
<Bundle-SymbolicName>
${artifactId}
</Bundle-SymbolicName>
<Export-Package>
com.packtpub.felix.bookshelf.service.tui
</Export-Package>
<Bundle-Activator>
com.packtpub.felix.bookshelf.service.tui.activator.-
BookshelfTuiActivator
</Bundle-Activator>
<Private-Package>
com.packtpub.felix.bookshelf.service.tui.activator
</Private-Package>
</instructions>
<remoteOBR>repo-rel</remoteOBR>
<prefixUrl>
file:///C:/projects/felixbook/releases</prefixUrl>
<ignoreLock>true</ignoreLock>
</configuration>
</plugin>

With this last update, we can build and deploy this bundle for a test run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.93.169