Practical OSGi
At Community One North (Oslo) 2009 I held a presentation on practical OSGi. This article contains code from the presentation and a walktrough on how to build and run the code.
The actual presentation slides can be downloaded below in either PDF or via SlideShare. Code is zipped and contains all code that is necessary to build and run the demonstration. You will need to have Java 1.5 or later and Maven 2.x to build and run the code.
Building the code
First unzip the code into a directory of your choice. In my example, I have unzipped it into my home directory. Change directory location to the practical-osgi-demo folder. Build the code using the following command:
>> cd practical-osgi-demo >> mvn clean install
Now, the code packaged and installed into your maven repository. It is vital that this step was successful as when you run the code it looks for the bundles (jar files) inside your local maven repository.
Running the OSGi framework
So, first you need to change your directory to the runner folder. When standing in this directory, you run the following command:
>> cd runner >> mvn pax:run
This will start the OSGi framework and you will see the shell prompt (if not, just press enter). From here you can execute OSGi shell commands. To show available bundles, run ps command. The result will look like this:
-> ps START LEVEL 6 ID State Level Name [ 0] [Active ] [ 0] System Bundle (1.6.0) [ 1] [Active ] [ 5] Apache Felix Configuration Admin Service (1.0.10) [ 2] [Active ] [ 5] Apache Felix Web Management Console (1.2.8) [ 3] [Active ] [ 5] OPS4J Pax ConfMan - Properties Loader (0.2.2) [ 4] [Active ] [ 5] OPS4J Pax Logging - API (1.3.0) [ 5] [Active ] [ 5] OPS4J Pax Logging - Service (1.3.0) [ 6] [Active ] [ 5] OPS4J Pax Web - Service (0.6.0) [ 7] [Active ] [ 5] Example - API (1.0.0) [ 8] [Active ] [ 5] Example - Encoder Pack 1 (1.0.0) [ 9] [Active ] [ 5] Example - Encoder Pack 2 (1.0.0) [ 10] [Active ] [ 5] Example - Sevlet (1.0.0) [ 11] [Active ] [ 1] osgi.compendium (4.1.0.build-200702212030) [ 12] [Active ] [ 1] Apache Felix Shell Service (1.2.0) [ 13] [Active ] [ 1] Apache Felix Shell TUI (1.2.0)
To stop the runner, run shutdown command or just press CRTL + C. Start it again by running the previous pax:run command.
Testing the demo
The demo is running an embedded Jetty server as a bundle. You can test out the demo by launching your browser and go to localhost:8080/index.html url. You can now write any text into the input form and instantly see the encoded text. You can now try to stop encoder2 bundle (number 9) and see what happens when you enter more text.
-> stop 9 -> ps START LEVEL 6 ID State Level Name [ 0] [Active ] [ 0] System Bundle (1.6.0) [ 1] [Active ] [ 5] Apache Felix Configuration Admin Service (1.0.10) [ 2] [Active ] [ 5] Apache Felix Web Management Console (1.2.8) [ 3] [Active ] [ 5] OPS4J Pax ConfMan - Properties Loader (0.2.2) [ 4] [Active ] [ 5] OPS4J Pax Logging - API (1.3.0) [ 5] [Active ] [ 5] OPS4J Pax Logging - Service (1.3.0) [ 6] [Active ] [ 5] OPS4J Pax Web - Service (0.6.0) [ 7] [Active ] [ 5] Example - API (1.0.0) [ 8] [Active ] [ 5] Example - Encoder Pack 1 (1.0.0) [ 9] [Resolved ] [ 5] Example - Encoder Pack 2 (1.0.0) [ 10] [Active ] [ 5] Example - Sevlet (1.0.0) [ 11] [Active ] [ 1] osgi.compendium (4.1.0.build-200702212030) [ 12] [Active ] [ 1] Apache Felix Shell Service (1.2.0) [ 13] [Active ] [ 1] Apache Felix Shell TUI (1.2.0)
To start it again. Run start command.
-> start 9
You can also try the Felix Web Console to do the same commands. Go to localhost:8080/system/console for the console (user: admin, password: admin). You can see a complete list of commands by entering the help command.
-> help bundlelevel <level> <id> ... | <id> - set or get bundle start level. cd [<base-URL>] - change or display base URL. exports <id> ... - list exported packages. headers [<id> ...] - display bundle header properties. help - display impl commands. imports <id> ... - list imported packages. install <URL> [<URL> ...] - install bundle(s). ps [-l | -s | -u] - list installed bundles. refresh [<id> ...] - refresh packages. requirers <id> ... - list requiring bundles. requires <id> ... - list required bundles. resolve [<id> ...] - attempt to resolve the specified bundles. services [-u] [-a] [<id> ...] - list registered or used services. shutdown - shutdown framework. start [-t] <id> [<id> <URL> ...] - start bundle(s). startlevel [<level>] - get or set framework start level. stop [-t] <id> [<id> ...] - stop bundle(s). uninstall <id> [<id> ...] - uninstall bundle(s). update <id> [<URL>] - update bundle. version - display version of framework.




Comments
If you want to comment on this article you need to be logged in.