iPOJO in 10 minutes -- Additional Exercises
- Based on the English Dictionary service implementation, develop and deploy a French Dictionary service implementation
- Copy the spell.english directory into a spell.french directory
- Perform the changes below in the new directory (spell.french)
- Modify the spell.english.bnd file:
- rename the file to spell.french.bnd
- open the file and replace Private-Package: spell.english with Private-Package: spell.french
- Modify the build.xml file:
- open the file and replace project name="spell.english" default="package" basedir="" with project name="spell.french" default="package" basedir=""
- Develop the class spell.french.FrenchDictionary and place it in the src directory
- Compile the class using the ant command (as before)
- Deploy and start your new service (in felix): start file:../spell.french/output/spell.french.jar
- Stop the English Dictionary service by using the stop command (as before) -- e.g. stop 10.
- Is the Spell Checker service still valid?
- Can you spell-check the same words as before stopping the service? explain why
- What happens if you re-start the English Dictionary service? explain why
- What happens if you stop the French Dictionary service? explain why
- Modify your code so that only French dictionaries can be used:
- please refer to the iPOJO documentation on:
- add a 'language' service property to your FrenchDictionary and EnglishDictionary components
e.g.:
@ServiceProperty(name = "language", value = "fr")
String language = "fr";
- add a filter attribute to the Requires annotation of the SpellCheck component
e.g.
@Requires(filter="(language=fr)")
private DictionaryService dictionary;
- make sure to recompile and rebundle your updated code (e.g. check the dates of the .jar archives)
- from the felix console, reload the updated bundles:
- update <bundle_id>: updates the bundle with the given id (reloads its .jar archive from the location indicated when the bundle was installed initially
- also from the felix console you may use the following commands to check the state of your application:
- instances: lists all instances and their state (valid or invalid)
- instance <instance_id>: details about the instance with the given id
(documentation here)
- help: a list of all available commands
- Update your code so that the French Dictionary service is the preferred service -- this means that if both the English and the French Dictionary services are available, then the Spell Checker service connects to the French one; also, if the Spell Checker is already connected to the English service and the French service becomes available, then the Spell Checker re-connects to the French service.
To achieve this you can use the dynamic priority policy option in the @Requires annotation of the Spell Checker, and use the comparator option with your own Comparator class to rank the priorities of dictionary services based on their language property.
Please see the documentation referenced in the exercise above concerning the use of the @Requires annotation
- in the spell.checker package create a new comparator class: MyComparator.java
- this class must implement java.util.Comparator<ServiceReference> and hence must implement this interface's compare method:
public int compare(ServiceReference o1, ServiceReference o2) -- please see Java API Comparator
- to get the value of a service property you can use the method getProperty(String property_name) defined in the ServiceReference class -- please see OSGi API ServiceReference
E.g. (String)o1.getProperty("language");
- you may check your comparator class here
- update the @Requires annotation in the spell.checker.SpellCheck class to use a dynamic priority binding policy with your custom comparator:
@Requires(policy = BindingPolicy.DYNAMIC_PRIORITY, comparator = MyComparator.class)
private DictionaryService dictionary;
- update the build.xml file of the spell.check bundle so as to also include felix.jar in the classpath (since this .jar archive contains the ServiceReference.class that is imported in the MyComparator.class)
- in the spell.checker/build.xml file, under the target <target name="buildclasspath"> add:
<copy file="../ipojo-distribution-1.12.1/bin/felix.jar " todir="${lib.dir}"/>
- you may check your updated build.xml file here
- Transform your previous OSGi project into an iPOJO project
- Analyse the code of the OSGi project and for each class determine which methods could be replaced with which iPOJO annotations;
- Optional: update the code, the bnd and the ant files (build.xml); then deploy and test the bundles on the felix platform