Showing posts with label osgi. Show all posts
Showing posts with label osgi. Show all posts

Monday, December 8, 2008

OSGi example = Spring Dynamic Modules + Equinox Declarative Services

Well, it took me a while to came up with that example.
As I am working on design of some fancy spring osgi equipped application some research was needed to check how and if it works as described ;).
But why not to make this more interesting and add some integration to this deliberations.

Summary


In this post I am going to show how to integrate Spring DM and OSGi DS (Equinox implementation). The only purpose for the code is to show that it is possible. It is only OSGi powered version of helloWorld software :D


What can be useful ?





Main idea



As it was late in the night usability of the example is not catchy :D "hello" here "hello there" but the main idea is to show that it works :D.
Ok so we have 3 bundles. Two of them are using declarative services and the last one is using spring to provide it services :D



Programmer notes


First thing to mention. To build spring bean I am using maven. As you all know there is something called maven project structure. The best place to store META-INF is resources directory in src catalog. Don't do it this way. To make bundle visible for PDE tooling we need to store META-INF in the root of the project. Of course this requires changes in pom.xml project description.




As we have it visible as a bundle let me add some content.
First our service:

package nugae.spring.example;
public interface IHelloService {
  void sayHello();
}

It has its implementation in nugae.spring.example.impl package.
Next we need to define our service definition. It is recommended to split osgi and beans definition.


I assume that you may know standard bean definition so I will only focus on the osgi definition (component-osgi.xml):

Basically, saying that we deliver some service can be done with this declaration:

<osgi:service id="springService" ref="helloService" interface="nugae.spring.example.IHelloService" />

At the moment (in theory) we have our service (and bundle) ready to deployment.



Lets move then to our declarative services. As I've mention we'll add consumer and another provider bundles.
It is worth to mention that in eclipse 3.5M3 we have DS tooling available in PDE.

Declaring service using DS is nothing more complicated than using spring approach.
Neither specifying services required.

So what we have here ?
We just specified that our bundle requires two services (one delivered by other DS bundle and second provided by Spring bundle.




So let bring it to life :D



To bring the DS functionality to our target platform we need some bundles (can be checked out from :pserver:anonymous@dev.eclipse.org:/cvsroot/rt):


The simplest way to run spring osgi support is to use target provided by spring framework.

It contains all required bundles.



Conclusions


It works. Well I am surprised cause sometimes not everything work as described ;).
I think that OSGi brought new quality to developing java application. The level of freedom we've using different approaches make it even more attractive.
If you want to run this example (hopefully you will be able following this post)
source codes can be downloaded from here.

Thursday, January 31, 2008

OSGi happy family: UserAdmin

Bundles, wiring, life-cycle and services architecture are not the only part of OSGi specification. Although they're "Core", standard covers also a number of services that we most likely gonna need - like HTTP, position (GPS!), preferences or user management.

User Admin Service comes into action whenever our application is run by multiple users with different privileges. It brings notion of Roles, Users and Groups, who have their properties and credentials. User can perform actions on behalf of selected role if he's one of that role's basic members and has all memberships required by that role. Authorization code looks like this:

User user = (User) UserAdmin.getUser("jacek");
Authorization auth = userAdmin.getAuthorization(user);
if (auth.hasRole("CVS_USERS")) {
// ...
}



Naturally users can come and go at any time, thus we're given actually two services: one for querying users (UserAdmin) and another to track any changes (UserAdminListener).
Generic specification allows for wide range of implementations behind UserAdmin. Equinox's one lays on Preferences (yet another OSGi service).
However UserAdmin is functionally very similar to JAAS, JAAS depends on JDK1.3 which is still to high barier for OSGi.

Friday, November 16, 2007

Eclipse-LazyStart and testing plugins

Once again I've made a stupid mistake. I was supposed to add some functionality, and I've started from testing what I did. To my surprise the plugin's start method, i was depended on (lets call it org.hsia), wasn't called. It was called during the normal launch.
My first move was changing org.hsia Eclipse-LazyStart to false.
"The Eclipse-LazyStart header is used to specify if a bundle should be started before the first class or resource is accessed from that bundle" so, I thought if lazy start is false plugin will be started during the platform start - wrong (thanks Chris).
The problem in my case was that I didn't call any of the org.hsia classes. Solution in this case is simple, set org.hsia Eclipse-LazyStart to true and call:

@BeforeClass
public static void setUpEditor() {
//force Editor activator to be called
MetricsEditorPlugin.getPlugin();
}

in the test.
Why I've posted that. Maybe you have the same problem, and somentimes is better to lern on sbd else mistakes :)