Tutorial OSGi avec Apache Felix - Partie 4

Revenir au sommaire

=Requirements=


 * Felix Framework Distribution (voir Tutorial OSGi avec Apache Felix - Partie 1)
 * with the iPOJO Core bundle (see next section)
 * Eclipse.
 * To support OSGi development, Eclipse needs the PDE (Plug-in Development Environment) plug-in, bundled in Eclipse IDE for Java EE Developers
 * iPOJO Nature plug-in for Eclipse, available as an archive distribution
 * Install the iPOJO Project Nature and iPOJO Nature Dependencies plug-ins

Set up your Felix installation

 * 1) Run Felix using the following command in the Felix installation directory
 * 2) Install iPOJO, using the following Felix Gogo shell commands:
 * 1) Install iPOJO, using the following Felix Gogo shell commands:

Useful Gogo commands
Note: To ease shell lines edition on Linux, you might use the rlwrap tool:


 * Print the help of the given command
 * Lists the bundles installed in the framework
 * Lists the services provided by the given bundle
 * can be written
 * Lists the services consumed by the given bundle
 * can be written
 * Prints the publicly available iPOJO factories
 * Prints the details of the given iPOJO factory
 * Prints the names and states of iPOJO instances
 * Prints the details of the given iPOJO component instance
 * Prints the publicly available iPOJO factories
 * Prints the details of the given iPOJO factory
 * Prints the names and states of iPOJO instances
 * Prints the details of the given iPOJO component instance
 * Prints the names and states of iPOJO instances
 * Prints the details of the given iPOJO component instance
 * Prints the details of the given iPOJO component instance
 * Prints the details of the given iPOJO component instance

Eclipse Configuration
By default, Eclipse uses its installation directory as the Target Platform, i.e. the group of bundles accessible by your bundle project. You should declare the installation folder of the Felix Framework as the target platform to avoid compilation and access rights errors.


 * 1) Open Eclipse preferences
 * 2) Plug-in Development > Target Platform
 * 3) Add...
 * 4) Create an empty target platform (check nothing...)
 * 5) Add the bin and bundle folders of your Felix installation to the Target Platform
 * 6) Set the new target platform as the current one

=I. Activator=


 * 1) Create an Eclipse Plug-in project fr.erods.tp.bundle1
 * 2) * New > Plug-in project
 * 3) * Select the option "Target Platform" > "an OSGi framework" > standard
 * 4) * Next
 * 5) * Check "Generate an activator"
 * 6) * Finish
 * 7) Open the activator class and print some text when the bundle starts and stops
 * 8) Export the bundle JAR file with iPOJO Nature
 * 9) * Right click on the bundle project > Export > iPOJO Bundle
 * 10) * Choose an output folder (the Felix installation folder)
 * 11) * Finish
 * 12) Install and test the bundle using the Felix shell (start/update/stop)

Questions:
 * Indicate when start and stop methods are called.
 * Why the BundleContext can be kept as a  class member ?

=II. Pure-OSGi service=


 * 1) Create an new bundle project fr.erods.tp.api.
 * 2) * Activator is not needed: uncheck "Generate an activator" in step 2
 * 3) Create the   interface with two methods:
 * 4) Export the API package
 * 5) * Open the Manifest.mf file of fr.erods.tp.api
 * 6) * In tab "Runtime" > "Exported Packages" > Add
 * 7) ** Select the  package
 * 8) Import the API package in bundle 1
 * 9) * Open the Manifest.mf file of fr.erods.tp.bundle1
 * 10) * In tab "Dependencies" > "Imported Packages" > Add
 * 11) * Select the  package
 * 12) Implement the service in bundle 1
 * 13) * Create the class, implementing   in the bundle 1.
 * 14) Register the service with the activator of bundle 1
 * 15) * see registerService
 * 16) * see unregister
 * 17) Implement a service consumer in a new bundle
 * 18) * Create project fr.erods.tp.bundle2
 * 19) * Import the API package
 * 20) ** In the Manifest.mf file of bundle 2
 * 21) ** In tab "Dependencies" > "Imported Packages" > Add
 * 22) ** Select the  package
 * 23) * Implement a  class, implementing the OSGi   interface
 * 24) * Print a message when the service is bound/unbound
 * 25) Test the bundles in Felix
 * 1) * Print a message when the service is bound/unbound
 * 2) Test the bundles in Felix

Questions:
 * What happens when the consumer is started after the service bundle and why ?
 * Correct this behavior, using getServiceReference and getService

=II. iPOJO: provide and consume with iPOJO=


 * 1) Add the iPOJO Nature to projects bundle 1 and bundle 2
 * 2) * Select the projects
 * 3) * Right click > Add iPOJO Nature
 * 4) * Check Use annotations

Consumer

 * 1) Implement a   class in bundle 2
 * 2) * See How to use iPOJO Annotations
 * 3) * The component must be instantiated automatically
 * 4) * Print a message when the component is (in)validated and when the service is (un)bound
 * 5) Test
 * 6) Set the service requirement as optional
 * 7) Test

Questions:
 * Why are the callbacks called in that order ?
 * What happens if you use a missing optional requirement ?

Provider

 * 1) Implement a   class in bundle 1
 * 2) * See How to use iPOJO Annotations
 * 3) * The component must be instantiated automatically
 * 4) * The component must provide its service
 * 5) * Print a message when the component is (in)validated
 * 6) Test
 * 7) Add a service controller and alternate the service presence
 * 8) Test

Question:
 * How does the consumers react to the service presence cycle ?

Bonus

 * 1) Add a "service.ranking" property to the pure-OSGi and the iPOJO service, and an "implementation" property to identify them
 * 2) Print the name of the implementation the consumers are bound to
 * 3) Modify the ranking value

Question:
 * How does a service the service selection works ?

Note: The consumer should call methods of the service and traces should be printed in order to show which service the consumer is bound to.

=III. Whiteboard pattern=


 * 1) Create interface   in the API package, with two methods:
 * 2) Create a new bundle project fr.erods.tp.bundle3
 * 3) * Add the iPOJO Nature and annotations
 * 4) * Let it import the API package
 * 5) Create a component providing an   service in bundle 3
 * 6) Add job creation / deletion operations in a consumer
 * 1) Create a component providing an   service in bundle 3
 * 2) Add job creation / deletion operations in a consumer

Subscription pattern

 * 1) Modify the schedule component to allow listeners registration:
 * 2) Register component of bundle 3 as a job listener
 * 3) Test
 * 1) Register component of bundle 3 as a job listener
 * 2) Test

Whiteboard pattern

 * 1) Modify the schedule component (in bundle 1)
 * 2) * Remove the registration methods created above
 * 3) * Add a requirement to an array of s, as an optional requirement
 * 4) * Notify them on job creation/deletion
 * 5) Test

Questions:
 * What are the pros/cons of the whiteboard pattern vs. the subscription pattern ?
 * Bonus: compare this whiteboard pattern implementation with the iPOJO Whiteboard pattern handler

=IV. JMX MBean with iPOJO=


 * 1) Create a new bundle, with iPOJO Nature and annotations
 * 2) Create a component, which doesn't provide any service, but contains public methods to create/delete/list   jobs
 * 3) Use iPOJO JMX annotations to export those methods
 * 4) You will need the iPOJO JMX handler to test this component: install it using the following command in the Felix shell:
 * 5) Install the bundle in the Felix framework
 * 6) Run   (in the Oracle Java distribution) or   (in the Oracle Java distribution) or
 * 7) * You might need to install the JMX plug-in in VisualVM:
 * 8) ** Tools > Plugins > Available Plugi-ns
 * 9) ** Install the VisualVM-MBeans plug-in
 * 10) Connect VisualVM to your Felix JVM and play with the JMX beans
 * 1) Connect VisualVM to your Felix JVM and play with the JMX beans

=V. Gogo Command with iPOJO=


 * 1) Create a new bundle, with iPOJO Nature and annotations
 * 2) Create a Gogo command provider to create and delete jobs, and to list current jobs status

Minimal Gogo command component
import org.apache.felix.ipojo.annotations.Component; import org.apache.felix.ipojo.annotations.Instantiate; import org.apache.felix.ipojo.annotations.Provides; import org.apache.felix.ipojo.annotations.ServiceProperty; import org.apache.felix.service.command.Descriptor;

@Component(immediate = true) // Provide the component class as a service @Provides(specifications = ListComponentsCommand.class) // Instantiate the component automatically @Instantiate public class ListComponentsCommand {

// Gogo scope/namespace @ServiceProperty(name = "osgi.command.scope", value = "test") String scope;

// Gogo commands provided by this class (must be methods names) @ServiceProperty(name = "osgi.command.function", value = "{}") String[] function = new String[] { "test" };

// Method description @Descriptor("test") public void test { System.out.println("test!"); } }

=VI. Config Admin with iPOJO=

See Combining iPOJO and Configuration Admin

=VII. Event Admin with iPOJO= 1. Create a new bundle, with iPOJO annotations to send events on jobs status changes

=VIII. Extender with iPOJO= ??

=Guidelines & Good Practices ("OSGi Zen")=


 * Always separate API and implementations
 * Always release services and resources once you're done or when their bundle is stopping
 * Always indicate requested/provided packages versions
 * Avoid  class members
 * are allowed
 * Use interfaces to declare constants