Posts Tagged ‘felix’

Logging with OSGi and knopflerfish

In the previous post Getting started We had prepared our Target platform to run on Equinox and tested it, also we noticed that the logging is not working so today we will configure it on both Equinox and Knopflerfish which is my favourite as it has a very easy to use GUI, which enables developers to easily work with it :)

Now log4j.properties is a resources file like any other resources, the thing is in an OSGi environment you can not inject resources unless you used a fragment to do so. a Fragment is an empty project that has it is own META-INF directory that contains a MANIFEST.MF file to configure the fragment. now we have two options to make a fragment either we do it without a src folder just resources directly or we do make an src folder and put resources in it, now some would say that we don’t need the src folder as we are not going to write any code in the fragment, while this is true you need to know the fact that adding a fragment without src folder will work as expected on Equinox but it wont on knopflerfish, on the other hand making the fragment with src folder that contains the resources will be picked in the expected way by both Equinox and knopflerfish(please note that am not working on felix not yet, but I’ll update this info when I test it).

to sum it up we are going to make a fragment that will deliver resources data for a bundle(in our case it is a log4j.properties to org.apache.log4j),  and we are going to make sure that this bundle will be working correctly on both Equinox and Knopflerfish. So lets get started!

  • We will make a Fragment project, go to:

File –> New –> Other –> Plug-in Development –> Fragment Project

fragment

I will call it Log4jConfig , keep the create a java project checked as deiscussed erlier and change the Target Platform to an OSGi framework and choose Equinox!

log4jconfig

now we will configure our fragment as shown in the following pic! and note that we can only add one host for each fragment, I have chosen “org.springframework.osgi.log4j.osgi” now click finish.

screenshot-new-fragment-project

Now in the src folder make a new file and call it log4j.properties, I have configured one log4j. your workspace should looks like this:

log4jproject

If you take a look at our target folder you will see that we have two bundles provides log4j “org.springframework.osgi.log4j.osgi” and “com.springsource.org.apache.log4j” and both of them exports the “org.apache.log4j” which will make a conflict when trying to run the project! to solve this we need to deactivate the second one as our fragment is already hosted by “org.springframework.osgi.log4j.osgi”, to do that go to run configurations and un select the the “com.springsource.org.apache.log4j” from the target platform.

run

click run and you should see logging on your console!

logging

now we need to test on knopflerfish:

  • To deploy the log4jConfig fragment right click on it and click Export:

plug-in Development –> Deployable plug-ins and fragments

and click next and in the directory choose the OSGi-Target project root. and click finish.

screenshot-export-1

Refresh you OSGi-Target and you will find a new folder called plugins containing a JAR file called log4jConfig_1.0.0.jar

exported

now open Knopflerfish and follow the instructions:

  • add the log4jConfig_1.0.0.jar to it by

File –> Open bundle from file

  • make sure that it is status is installed not resolved. (if it is resolved click on the refresh button).
  • as in first step add the following bundles from our target folder:
  1. aopalliance-1.0.jar
  2. com.springsource.org.aopalliance-1.0.0.jar
  3. com.springsource.org.apache.commons.logging-1.1.1.jar
  4. commons-logging-1.1.1.jar
  5. log4j.osgi-1.2.15-SNAPSHOT.jar
  6. spring-aop-2.5.5.jar
  7. spring-beans-2.5.5.jar
  8. spring-context-2.5.5.jar
  9. spring-context-support-2.5.5.jar
  10. spring-core-2.5.5.jar
  11. spring-osgi-annotation-1.1.0.jar
  12. spring-osgi-core-1.1.0.jar
  13. spring-osgi-extender-1.1.0.jar
  14. spring-osgi-io-1.1.0.jar
  15. spring-osgi-mock-1.1.0.jar
  • start them all and make sure to leave the “spring-osgi-extender-1.1.0.jar” to the end (last one to start). you should see the logging on the Knopflerfish console:
  • If you want to turn off Knopflerfish make sure to stop “spring-osgi-extender-1.1.0.jar” as if you don’t when you start Knopflerfish next time logging wont work! because the status of the log4jConfig_1.0.0.jar will be resolved and not installed.

screenshot-knopflerfish-osgi-desktop-knopflerfish

Spring DM-OSGi Getting started

Am using STS as an IDE which is basically eclipse but preloaded with plugins to fit this kinda job! So I’ll be talking about it (every thing should work just fine on any other eclipse). To day I’ll talk on how to prepare the environment to start an Spring DM-OSGi project. we will start with creating a target-platform for the project, which means the set of jars that will be the backbone to run my project.

  • Open your IDE and make a new project by going to

File –> New –> Project…

new-project

we will use a plain project for the target-platform name it OSGi-Target and click finish.

osgi-target

  • Now we will extract the OSGi-Target JARs using Maven (a quick maven tutorial can be found here) I have prepared my pom file and you have to make sure to change the following tag with your own value:

<taget-platform.root>
/home/fadi/Programs/STS/workSpaces/OSGi-Series/OSGi-Target/target
</taget-platform.root>

create a new folder on the project and call it target

target-hir

Am using m2eclipse plugin for maven integration. to run the file Right click on the file:

Run as –> maven build…

in the goals field make sure to right package

screenshot-external-tools-configurations

Click run and maven will download and prepare your target folder you should see something like the following if every thing goes fine!

maven-build

Now refresh your target folder and you should see the newly installed JARs!

Now we need to make sure that STS/eclipse will run this platform when we make our first bundle!

  • Go to :

Window –> Preferences –> Plug-in Development –> Target Platform

change the location to match your target folder that you made earlier…

screenshot-preferences

click reload and you will see the 22 JARs in your target folder being displayed.

  • We can test our platform now by going to Run-configuration and creating new OSGi Framework, lets call it Target!

screenshot-run-configurations

click run and you will see that you have a warning on the console says that you don’t have a logging appenders declared(this is what we are going to fix in the next post), we can check our work by typing on the console “ss” which will give us the status of our Target:

screenshot-java-osgi-target-pomxml-springsource-tool-suite-1

That’s it for now and next post we will configure log4j using a fragment.

This is an on going Series and I hope to find some interested people who can help me in it :)

OSGi and Spring DM Series

Soon I’ll be starting a series of posts about OSGi and Spring DM, this is from my experience about it (which is some kind new), And I will try to shed the light on the problems I faced but did not found enough resources about so I had to figure it out by trail and error! I hope it will be useful and I don’t think t is going to be on a regular bases!

shI will start with the basic definition from the wikipedia:

The OSGi Alliance (formerly known as the Open Services Gateway initiative, now an obsolete name) is an open standards organization founded in March 1999. The Alliance and its members have specified a Java-based service platform that can be remotely managed. The core part of the specifications is a framework that defines an application life cycle management model, a service registry, an Execution environment and Modules. Based on this framework, a large number of OSGi Layers, APIs, and Services have been defined.

Tag Cloud
Categories
Archives