Apache Felix Tutorial Example 2b :: Apache Felix Home Subprojects Dependency Manager Event Admin File Install Framework Gogo Shell Health Checks Inventory Log Logback Maven bundle plugin Maven SCR plugin Metatype Service Preferences Service Remote Shell Script console plugin Lightweight shell Shell TUI Web Console Downloads Documentation Documentation Downloads Getting Started News Community Apache Felix Project Info Contributing Projects Using Felix Development Coding Standards DEPENDENCIES file template Provisional OSGi API Policy Release Management Site How To Using the OSGi Compliance Tests FAQS Apache Felix Bundle Plugin Frequently Asked Questions Apache Felix SCR Plugin Frequently Asked Questions Subprojects Apache Felix Dependency Manager Guides Apache Felix Dependency Manager - Migrating from earlier versions Dependency Manager - Annotations Dependency Manager - Background Dependency Manager - Bundles and Dependencies Dependency Manager - Design Patterns Dependency Manager - Development Dependency Manager - History Dependency Manager - JavaDocs Dependency Manager - Migrating from other solutions. Dependency Manager - Performance Tuning Dependency Manager - Resource adapters Dependency Manager - What’s new in version 4? Dependency Manager Lambda What’s New in R15 Reference Dependency Manager - Adapter Dependency Manager - Aspect Dependency Manager - Bundle Adapter Dependency Manager - Bundle Dependency Dependency Manager - Components Dependency Manager - Configuration Dependency Dependency Manager - Dependencies Dependency Manager - External Links Dependency Manager - Factory Configuration Adapter Service Dependency Manager - Resource Adapter Dependency Manager - Resource Dependency Dependency Manager - Service Dependency Dependency Manager - Service Scopes Dependency Manager - Singleton Component Dependency Manager - Thread Model Dependency Manager Annotations Tutorials Dependency Manager - Annotations Dependency Manager - Getting Started Dependency Manager - Leveraging the shell Dependency Manager sample projects Apache Felix Event Admin Apache Felix File Install Apache Felix Framework Apache Felix Framework Bundle Cache Apache Felix Framework Configuration Properties Apache Felix Framework Frequently Asked Questions Apache Felix Framework Launching and Embedding Apache Felix Framework Usage Documentation Apache Felix Framework Security Apache Felix Gogo RFC 147 Overview Apache Felix Health Checks Apache Felix Inventory Printer Apache Felix Log Apache Felix Logback Apache Felix Maven Bundle Plugin (BND) Apache Felix Metatype Service Apache Felix OSGi Bundle Repository (OBR) Apache Felix Preferences Service Apache Felix Remote Shell Apache Felix Shell Apache Felix Shell TUI Apache Felix Web Console Extending the Apache Felix Web Console Web Console RESTful API Web Console Security Provider Extensions Branding the Web Console Providing Resources Providing Web Console Plugins Web Console Logging Web Console Output Templating Tutorials Apache Felix Application Demonstration Apache Felix OSGi Tutorial OSGi Frequently Asked Questions OSGI Tutorial Apache Felix Tutorial Example 1 - Service Event Listener Bundle Apache Felix Tutorial Example 2 Apache Felix Tutorial Example 2b Apache Felix Tutorial Example 3 Apache Felix Tutorial Example 4 Apache Felix Tutorial Example 5 Example 6 - Spell Checker Service Bundle Example 7 - Spell Checker Client Bundle Example 8 - Spell Checker Service using Service Binder Example 9 - Spell Checker Service using Declarative Services Apache License 2.0 Site map Documentation master Documentation master Documentation Tutorials OSGI Tutorial Apache Felix Tutorial Example 2b Edit this Page Apache Felix Tutorial Example 2b Example 2b - Alternate Dictionary Service Bundle This example creates an alternative implementation of the dictionary service defined in Example 2. The source code for the bundle is identical except that instead of using English words, French words are used. The only other difference is that in this bundle we do not need to define the dictionary service interface again, since we can just import the definition from the bundle in Example 2. The main point of this example is to illustrate that multiple implementations of the same service may exist; this example will also be of use to us in Example 5. In the following source code, the bundle uses its bundle context to register the dictionary service. We implement the dictionary service as an inner class of the bundle activator class, but we could have also put it in a separate file. The source code for our bundle is as follows in a file called Activator.java: /* * Apache Felix OSGi tutorial. **/ package tutorial.example2b; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceEvent; import tutorial.example2.service.DictionaryService; /** * This class implements a simple bundle that uses the bundle * context to register an French language dictionary service * with the OSGi framework. The dictionary service interface is * defined in a separate class file and is implemented by an * inner class. This class is identical to the class in * Example 2, except that the dictionary contains French words. **/ public class Activator implements BundleActivator { /** * Implements BundleActivator.start(). Registers an * instance of a dictionary service using the bundle context; * attaches properties to the service that can be queried * when performing a service look-up. * @param context the framework context for the bundle. **/ public void start(BundleContext context) { Hashtable<String, String> props = new Hashtable<String, String>(); props.put("Language", "French"); context.registerService( DictionaryService.class.getName(), new DictionaryImpl(), props); } /** * Implements BundleActivator.stop(). Does nothing since * the framework will automatically unregister any registered services. * @param context the framework context for the bundle. **/ public void stop(BundleContext context) { // NOTE: The service is automatically unregistered. } /** * A private inner class that implements a dictionary service; * see DictionaryService for details of the service. **/ private static class DictionaryImpl implements DictionaryService { // The set of words contained in the dictionary. String[] m_dictionary = { "bienvenue", "au", "tutoriel", "osgi" }; /** * Implements DictionaryService.checkWord(). Determines * if the passed in word is contained in the dictionary. * @param word the word to be checked. * @return true if the word is in the dictionary, * false otherwise. **/ public boolean checkWord(String word) { word = word.toLowerCase(); // This is very inefficient for (int i = 0; i < m_dictionary.length; i++) { if (m_dictionary[i].equals(word)) { return true; } } return false; } } } We must create a manifest.mf file that contains the metadata for our bundle; the manifest file contains the following: Bundle-Name: French dictionary Bundle-Description: A bundle that registers a French dictionary service Bundle-Vendor: Apache Felix Bundle-Version: 1.0.0 Bundle-Activator: tutorial.example2b.Activator Import-Package: org.osgi.framework, tutorial.example2.service We specify which class implements the Bundle-Activator interface and also specify that our bundle imports the OSGi core package and the dictionary service interface package using the Import-Package attribute. Th dictionary service package will be fulfilled by the bundle in Example 2, which exports this package. (Note: Make sure your manifest file ends in a trailing carriage return or else the last line will be ignored.) To compile our source code, we need the felix.jar file (found in Felix' bin directory) and the example2.jar file in our class path. We compile the source file using a command like: javac -d c:\classes *.java This command compiles all source files and outputs the generated classes into a subdirectory of the c:\classes directory; this subdirectory is tutorial\example2b, named after the package we specified in the source file. For the above command to work, the c:\classes directory must exist. After compiling, we need to create a JAR file containing the generated package directories. We will also add our manifest file that contains the bundle’s meta-data to the JAR file. To create the JAR file, we issue the command: jar cfm example2b.jar manifest.mf -C c:\classes tutorial\example2b This command creates a JAR file using the manifest file we created and includes all of the classes in the tutorial\example2b directory inside of the c:\classes directory. Once the JAR file is created, we are ready to install and start the bundle. To run Felix, we follow the instructions described in usage.html. When we start Felix, it asks for a profile name, we will put all of our bundles in a profile named tutorial. After running Felix, we should make sure that the bundle from Example 1 is active. We can use the Felix lb shell command to get a list of all bundles, their state, and their bundle identifier number. If the Example 1 bundle is not active, we should start the bundle using the start command and the bundle’s identifier number that is displayed by the lb command. Now we can install and start our dictionary service bundle. Assuming that we created our bundle in the directory c:\tutorial, we can install and start it in Felix' shell using the following command: start file:/c:/tutorial/example2b.jar The above command installs and starts the bundle in a single step; it is also possible to install and start the bundle in two steps by using the Felix install and start shell commands. To stop the bundle, use the Felix stop shell command. If the bundle from Example 1 is still active, then we should see it print out the details of the service event it receives when our new bundle registers its dictionary service. Using the Felix shell lb command to get the bundle identifier number for our dictionary service bundle and we can stop and restart it at will using the stop and start commands, respectively. Each time we start and stop our dictionary service bundle, we should see the details of the associated service event printed from the bundle from Example 1. In Example 3, we will create a client for our dictionary service. To exit Felix, we use the shutdown command. Content licensed under AL2. UI licensed under MPL-2.0 with extensions licensed under AL2