Thursday, April 8, 2010

Where is DS Annotation?

I used Declarative Service in my application, JAM Circle.
Declarative Service is cool stuff.
It makes to solve bundles startup order.
But I confused a thing.
How do I inject the service at startup time?
I think services needs to initialize at startup.
So I choosed the way to solve by singleton class like below.

scr.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.kompiro.jamcircle.kanban.ui.KanbanUIContext">
<implementation class="org.kompiro.jamcircle.kanban.ui.KanbanUIContext"/>
<reference bind="setKanbanService" cardinality="1..1" interface="org.kompiro.jamcircle.kanban.service.KanbanService" name="KanbanService" policy="static"/>
<reference bind="setScriptingService" cardinality="1..1" interface="org.kompiro.jamcircle.scripting.ScriptingService" name="ScriptingService" policy="static"/>
</scr:component>

and implementation of Context class(The name inspired from "ApplicationContext")

package org.kompiro.jamcircle.kanban.ui;

import org.kompiro.jamcircle.kanban.service.KanbanService;
import org.kompiro.jamcircle.scripting.ScriptingService;

public class KanbanUIContext {

private static KanbanUIContext context;
private KanbanService kanbanService;
private ScriptingService scriptingService;

public KanbanUIContext() {
KanbanUIContext.context = this;
}

public static KanbanUIContext getDefault(){
return context;
}

public KanbanService getKanbanService() {
return kanbanService;
}

public void setKanbanService(KanbanService kanbanService) {
this.kanbanService = kanbanService;
}

public ScriptingService getScriptingService() {
return scriptingService;
}

public void setScriptingService(ScriptingService scriptService) {
this.scriptingService = scriptService;
}

}


It's not good way(T_T)... KanbanUIContext is a singleton class and be used to call ServiceLocator Pattern. It is not easy to test by code...
I haven't know there is no way to inject to use annotation for field injection.
Is there any way to use annotation and field injection?

9 comments:

Antoine said...

How would the other bundles digest your annotation on your code ? The framework cannot parse your classes to look for your code. Also, it would introduce more dependencies, more API, etc.

Neil Bartlett said...

Yes, annotations exist for DS in Bnd. They are build-time annotations, so the runtime is the same and uses an XML file, but you never need to write the XML yourself.

Some example code with Bnd DS annotations is in my old blog post: http://njbartlett.name/blog/2009/11/30/osgi-its-time-to-ban-bundle-activators/

Lars Vogel said...

You may want to look at Eclipse e4 which allows to use annotations to inject services into model(UI) components.

http://www.vogella.de/articles/EclipseE4/article.html

Clement said...

iPOJO also provides annotations and you will have a pretty simple POJO (http://felix.apache.org/site/how-to-use-ipojo-annotations.html). iPOJO supports field injection.

Moreover, you can then rely on all iPOJO features such as synchronization support, composition and event-based interactions.

Hiroki Kondo(kompiro) said...

> Antoine
Thanks your comment.
Umm, there are many frameworks, I've known the other comments...
I'd like to use simple framework because I don't want to pay initialize cost.

Hiroki Kondo(kompiro) said...

> Neil
Oh! I forgot it Bnd DS.
I think that is the best framework for my purpose. Thanks!

Hiroki Kondo(kompiro) said...

>Lars
Thank you for your comment!
I'm very interested in e4!
In near future, I'll migrate base platform to e4!

Hiroki Kondo(kompiro) said...

> Clement
Thank you for your comment!
How much is iPOJO's initialize cost?
Is it not too high to use desktop app?
I think desktop app needs to start faster and faster, don't you?

Clement said...

> Hiroko
The iPOJO initialization cost is the same as DS. Indeed, the manipulation is done offline, so does not involve any extra-cost at runtime. We use iPOJO is several desktop application successfully without any startup issue (due to iPOJO).