Eclipse RCP and Spring-DM Part II

UPDATE 12/03/07 – I have branched quizbang to continue working on OSGi related functionality separate from the trunk. All work to-date using pax is here:
branch
– I will merge this back into the main branch once the startup timing issue described below is resolved. The first point release of quizbang will continue to use eclipse registry extensions instead of Spring-DM for module extensions.

I have recently used pax-construct to spring-ify an RCP application. The application uses two plugins. One is a slightly modified version of the “Hello World with a View” eclipse example template plugin. The other is a simple non-visual plugin that exposes a service. The Activator classes of each plugin have been modified to include an instance variable of type org.springframework.context.ApplicationContext. Static getter and setter methods allow for an AppContextSetter bean which implements org.springframework.context.ApplicationContextAware to be referenced from the bean configuration and set a spring application context.

A few other things to keep in mind are that the xml configuration files for spring will be stored under META-INF/spring. When launching the eclipse application it is important to use a config.ini to control the sequence of bundle activation so that the spring osgi extender is started before org.eclipse.core.runtime. Also using pax-construct allows all of the dependencies to be specified on the pom.xml. This includes eclipse plugin dependencies like org.eclipse.core.runtime. YMMV, but I have only succeeded with this under windows but that is because the swt fragment needs to be treated as a separate dependency in the pom, and I could not find a gtk fragment in the repository yet.

The following gets us started with the shell of the project:
(please note that the double dash is not rendering as two dashes. I need to tweak my css to get a more appropriate cut-and-paste stlye...)

$ pax-create-project -g net.sourceforge -a quizbang
$ cd quizbang
$ pax-add-repository -i spring-milestones \
-u http://s3.amazonaws.com/maven.springframework.org/milestone
$ pax-add-repository -i spring-snapshots \
-u http://static.springframework.org/maven2-snapshots -- -Dsnapshots "-Dreleases=false"
$ pax-add-repository -i eclipse-repos \
-u http://repo1.maven.org/eclipse

Take some time to look through the newly created artifacts. Specifically quizbang\poms\compiled\pom.xml will be the parent pom for our bundles.

The following will add dependencies to the pom. You can run from the main project folder, but may want to run individually from the bundles that we will create in the next step. For instance on an eclipse plugin that makes no UI contribution, you do not need to have a dependency on org.eclipse.ui.


pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender -v 1.0-rc1-SNAPSHOT -- -DwidenScope -DimportTransitive
pax-import-bundle -g org.eclipse.core -a runtime -v 3.3.100-v20070530 -- -DwidenScope -DimportTransitive
pax-import-bundle -g org.eclipse -a ui -v 3.3.0-I20070614-0800 -- -DwidenScope -DimportTransitive
pax-import-bundle -g org.eclipse.swt.win32.win32 -a x86 -v 3.3.0-v3346 -- -DwidenScope -DimportTransitive
pax-import-bundle -g org.slf4j -a slf4j-simple -v 1.4.3

Now we will create the two bundles that will be for our code:

pax-create-bundle -p net.sourceforge.quizbang.example -n net.sourceforge.quizbang.example -- -Dspring -Djunit
pax-create-bundle -p net.sourceforge.quizbang.sampleplugin-n net.sourceforge.quizbang.sampleplugin -- -Dspring -Djunit

The bundles will be generated with sample code, but this needs to be replaced with the code from the quizbang sourceforge project. Refer to the svn repository for the code to add.

Now the last step is mvn clean install pax:eclipse -DdownloadSources and to import these as projects in eclipse. I am testing with eclipse-rcp-europa-fall-win32. You may want to skip importing the eclipse projects from the provision folder. Also double check your launch configuration against the one stored in svn because it can get overriden silently in eclipse. And the config.ini should provide capability to order the activation of bundles, but the astute code reviewer will notice a workaround on line 83 of View.java in the example plugin. I am forcing a delay to allow for the context to be loaded. Also on both of the generated META-INF/MANIFEST.MF files I had to manually add “org.springframework.core.io.support” to the list of packages under “Import-Package”.

Once you have everything setup you should see the following from console:
console output

And the following from the example RCP app which will substitute a String value provided from the OSGi exposed service into the content provider of the view:

Example RCP

You may experience a few hiccups along the way but this tech is maturing quickly. I would imagine that this method outlined here would be superseded by new tooling that is in the works for eclipse iam and q4e. Also special thanks to Stuart M. who has been instrumental in helping me figure this out and helping me to realize pax is the easier way.

If you have followed along this far and are still struggling I’ve also stored my project root folder and poms in SVN as well. Checkout project-shell as the quizbang main project folder. Inside that folder checkout net.sourceforge.quizbang.example and net.sourceforge.quizbang.sampleplugin. That should give you a heads up on a working Eclipse-RCP app with Spring-DM.

Next installment will be addressing the real functionality of the quizbang project.

4 Comments »

  1. for those who like to copy+paste instructions, here they are without the extra formatting (ie. plain quotes and double-dash instead of long-dash) – hopefully this will render ok!

    pax-create-project -g net.sourceforge -a quizbang

    cd quizbang

    pax-add-repository -i spring-milestones -u http://s3.amazonaws.com/maven.springframework.org/milestone
    pax-add-repository -i spring-snapshots -u http://static.springframework.org/maven2-snapshots — -Dsnapshots “-Dreleases=false”
    pax-add-repository -i eclipse-repos -u http://repo1.maven.org/eclipse

    pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender -v 1.0-rc1-SNAPSHOT — -DwidenScope -DimportTransitive
    pax-import-bundle -g org.eclipse.core -a runtime -v 3.3.100-v20070530 — -DwidenScope -DimportTransitive
    pax-import-bundle -g org.eclipse -a ui -v 3.3.0-I20070614-0800 — -DwidenScope -DimportTransitive
    pax-import-bundle -g org.eclipse.swt.win32.win32 -a x86 -v 3.3.0-v3346 — -DwidenScope -DimportTransitive
    pax-import-bundle -g org.slf4j -a slf4j-simple -v 1.4.3

    pax-create-bundle -p net.sourceforge.quizbang.example — -Dspring -Djunit
    pax-create-bundle -p net.sourceforge.quizbang.sampleplugin — -Dspring -Djunit

    REM replace the sample code with Jeff’s source from subversion…

    mvn clean install pax:eclipse -DdownloadSources

  2. Rather than battle with WordPress formatting, I decided to post the unformatted instructions as a comment on the OPS4J wiki over at:

    http://wiki.ops4j.org/confluence/display/ops4j/2007/11/27/Eclipse+RCP+and+Spring-DM+tutorial+by+Jeff+Caldwell

    HTH

  3. Nauman said

    I was stuck up, in how to delay view creation, i seen you mentioned in your paragraph above on line 83, but i haven’t found “QuizBangServiceWrapper” file in your repository. Just curious how you are delaying the creation of View.

    Thanks

  4. jeffcaldwell said

    Nauman,

    That is a good question. I do apologize if I am not able to answer sufficiently because this was my last foray into osgi-spring. Looking at the code in the osgi-working branc http://quizbang.svn.sourceforge.net/viewvc/quizbang/branches/osgi-working/net.sourceforge.quizbang.example/src/main/java/net/sourceforge/quizbang/example/View.java?view=markup
    it appears that the code I was referencing was later commented out. As far as net.sourceforge.quizbang.example.internal.QuizBangServiceWrapper goes that was added after I started trying to factor out the osgi code. There wasn’ much there. In fact the quizbang rcp project as a whole has been languishing for a while as I have been focusing on GWT based projects lately.

    The delay I was referring to was just putting the thread to sleep (a real hack I know) – but you can see it in all its glory in an older version that was current when I originally posted this article: http://quizbang.svn.sourceforge.net/viewvc/quizbang/branches/osgi-working/net.sourceforge.quizbang.example/src/main/java/net/sourceforge/quizbang/example/View.java?revision=7&view=markup

RSS feed for comments on this post · TrackBack URI

Leave a Comment