Difference between revisions of "Tutorial OSGi avec Apache Felix - Partie 4"
Jump to navigation
Jump to search
Line 148: | Line 148: | ||
=VI. Gogo Command with iPOJO= |
=VI. Gogo Command with iPOJO= |
||
+ | |||
− | + | # Create a new bundle, with iPOJO Nature and annotations |
|
+ | # Create a Gogo command provider to create and delete jobs, and to list current jobs status |
||
+ | |||
+ | ==Minimal Gogo command component== |
||
+ | |||
+ | <pre> |
||
+ | 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!"); |
||
+ | } |
||
+ | } |
||
+ | </pre> |
||
=VII. Event Admin with iPOJO= |
=VII. Event Admin with iPOJO= |
Revision as of 18:52, 19 December 2012
Requirements
- Felix Framework Distribution (voir Tutorial OSGi avec Apache Felix - Partie 1)
- with the iPOJO Core bundle[1] (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
- Run Felix using the following command in the Felix installation directory
java -jar bin/felix.jar
- Install iPOJO, using the following Felix Gogo shell commands:
obr:list
obr:deploy -s "Apache Felix Log Service"
obr:deploy -s "Apache Felix iPOJO"
obr:deploy -s "Apache Felix iPOJO Gogo Command"
Useful Gogo commands
Note: To ease shell lines edition on Linux, you might use the rlwrap tool: rlwrap java -jar bin/felix.jar
help {command}
- Print the help of the given command
lb
- Lists the bundles installed in the framework
inspect capability service {bundle ID}
- Lists the services provided by the given bundle
capability
can be writtenc
inspect requirement service {bundle ID}
- Lists the services consumed by the given bundle
requirement
can be writtenr
ipojo:factories
- Prints the publicly available iPOJO factories
ipojo:factory {factory name}
- Prints the details of the given iPOJO factory
ipojo:instances
- Prints the names and states of iPOJO instances
ipojo:instance {instance name}
- 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.
- Open Eclipse preferences
- Plug-in Development > Target Platform
- Add...
- Create an empty target platform (check nothing...)
- Add the bin and bundle folders of your Felix installation to the Target Platform
- Set the new target platform as the current one
I. Activator
- Create an Eclipse Plug-in project fr.erods.tp.bundle1
- New > Plug-in project
- Select the option "Target Platform" > "an OSGi framework" > standard
- Next
- Check "Generate an activator"
- Finish
- Open the activator class and print some text when the bundle starts and stops
- Export the bundle JAR file with iPOJO Nature
- Right click on the bundle project > Export > iPOJO Bundle
- Choose an output folder (the Felix installation folder)
- Finish
- Install and test the bundle using the Felix shell (start/update/stop)
II. Pure-OSGi service
- Create an new bundle project fr.erods.tp.api.
- Activator is not needed: uncheck "Generate an activator" in step 2
- Create the
fr.erods.tp.api.IScheduleService
interface with two methods:int createJob(Runnable aRunnable, int aDelay)
void deleteJob(int aId)
- Export the API package
- Open the Manifest.mf file of fr.erods.tp.api
- In tab "Runtime" > "Exported Packages" > Add
- Select the
fr.erods.tp.api
package
- Select the
- Implement the service in bundle 1
- Create the class
fr.erods.tp.bundle1.ScheduleImpl
, implementingIScheduleService
in the bundle 1.
- Create the class
- Register the service with the activator of bundle 1
- see registerService()
- see unregister()
- Implement a service consumer in a new bundle
- Create project fr.erods.tp.bundle2
- Import the API package
- In the Manifest.mf file of bundle 2
- In tab "Dependencies" > "Imported Packages" > Add
- Select the
fr.erods.tp.api
package
- Implement a
Consumer
class, implementing the OSGiServiceListener
interface - Print a message when the service is bound/unbound
- Test the bundles in Felix
- What happens when the consumer is started after the service bundle ?
- Correct this behavior, using getServiceReference() and getService()
II. iPOJO: provide and consume with iPOJO
- Add the iPOJO Nature to projects bundle 1 and bundle 2
- Select the projects
- Right click > Add iPOJO Nature
- Check Use annotations'
Consumer
- Implement a
ConsumerIpojo
class in bundle 2- See How to use iPOJO Annotations
- The component must be instantiated automatically
- Print a message when the component is (in)validated and when the service is (un)bound
- Test
Provider
- Implement a
ServiceProviderIpojo
class in bundle 1- See How to use iPOJO Annotations
- The component must be instantiated automatically
- The component must provide its service
- Print a message when the component is (in)validated
- Test
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
- Create interface
IJobListener
in the API package, with two methods:
void onJobCreation(int aId)
void onJobDeletion(int aId)
- Modify the schedule component (in bundle 1)
- Make it require listeners, as an optional requirement
- Notify them on job creation/deletion
- Create a new bundle project fr.erods.tp.bundle3
- Add the iPOJO Nature and annotations
- Let it import the API package
- Create a component providing an
IJobListener
service in bundle 3 - Add job creation / deletion operations in a consumer
- Test
IV. Config Admin with iPOJO
V. JMX MBean with iPOJO
1. Create a new bundle, with iPOJO annotations to list current jobs status
VI. Gogo Command with iPOJO
- Create a new bundle, with iPOJO Nature and annotations
- 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!");
}
}
VII. Event Admin with iPOJO
1. Create a new bundle, with iPOJO annotations to send events on jobs status changes
IX. Extender with iPOJO
??
X. 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
public static
class members
private static
are allowed
- Use interfaces to declare constants