wiki:a4ds

Annotations for OSGi Declarative Services (A4DS)

I started to think about Java 6 annotations for OSGi Declarative Services (DS) and implemented a little prototype as a proof of concept. Any feedback would be highly appreciated.

Kai Tödter (kai.toedter@…)

Idea

OSGi DS is a very nice and convenient way to deal with services in dynamic systems. With A4DS, you would get some additional benefits:

  • Even easier to use: Most Java programmers are familiar using annotations but probably don’t want to deal with XML
  • Better refactoring support: The annotation processor takes care of it, like the renaming of bind/unbind methods
  • Annotations know about the attributes of a class/method etc. and can make assumptions about the intention of the developer. E.g. the annotation processor could assume that ALL implemented interfaces are supposed to be declared as services if not stated otherwise
  • Compile time consistency checks. E.g. raise an error if an explicitly stated service is NOT a Java interface the class implements.

Please read my  blog post for additional information.

Example

This annotated Java source:PM/trunk/a4ds/com.siemens.ct.osgi.a4ds.test/src/com/siemens/ct/osgi/a4ds/test/data/TestComponent1.java leads to the automatic generation of the following XML file for DS: source:PM/trunk/a4ds/com.siemens.ct.osgi.a4ds.test/OSGI-INF/TestComponent1.xml

Disclaimer

The current implementation is only a proof of concept prototype. It is neither complete nor error free. The only purpose is to just check and test a few ideas I have. Don't use it in a real project, just test it using test projects!

Browse the Code

To get a quick idea about the currently implemented annotations and the annotation processor, take a look at all the files in directory source:PM/trunk/a4ds/com.siemens.ct.osgi.a4ds/src/com/siemens/ct/osgi/a4ds

Subversion Access

Anonymous svn access available at  https://max-server.myftp.org:444/svn/pm/PM/trunk/a4ds, just get all projects. In the a4ds project you find the annotations and the annotation processor, in the a4ds.test project there are a few rudimentary unit tests.

Running the Tests

  • Requirement is a Java 6 environment and JUnit 4
  • Check out all projects from  https://max-server.myftp.org:444/svn/pm/PM/trunk/a4ds
  • Browse project com.siemens.ct.osgi.a4ds.test
  • Change the string testFileDirectory in the class com.siemens.ct.osgi.a4ds.test.CompilerUtils to fit your installation path
  • Run all tests in package com.siemens.ct.osgi.a4ds.test
  • The file !TestComponent3Test should not compile using the annotation processor, since a non implemented interface "XXX" in used in the annotation. But a test checks that and is green if the compilation fails.

Testing the Annotation Processor in Eclipse

  • This description is for Eclipse >= 3.4
  • Export the plug-in com.siemens.ct.osgi.a4ds
  • Copy the exported plug-in into the dropins folder of your Eclipse IDE
  • Restart Eclipse
  • Check out project com.siemens.ct.osgi.a4ds.test
  • Open the project's properties and take a look at the Java Compiler/Annotation? Processing options. Here is where to tell Eclipse that a project wants to use available annotation processors
  • Delete all files in the OSGI-INF directory
  • Open the class com.siemens.ct.osgi.a4ds.test.data.!TestComponent1
  • Make a change and save
  • Now, in the OSGI-INF directory there should be the generated !TestComponent1.xml
  • Open the class com.siemens.ct.osgi.a4ds.test.data.!TestComponent3
  • It will not compile, take a look at the error tooltip

OSGi Dependencies

Since all annotations have the retention policy SOURCE, ther is no runtime dependency to a4ds. The best practice is to declare an optional import of com.siemens.ct.osgo.a4ds in the projects that use the annotations.

Knows Issues

  • Dependency to Java 6

Use of A4DS in Person Manager

I created a tree view that uses A4DS. The project is com.siemens.ct.pm.ui.views.treeview.a4ds, see Person Manager