How to create a Spring-DM bundle using PAX scripts
From JQuantLib
Richard Gomes founded JQuantLib in Sep/07. He is Brazilian and settled in London since Aug/06. Richard is available for immediate assignment as consultant specialized on Java technologies and open source tools. |
This is a work in progress. Use at your own risk. :(
Overview
[http:/www.jquantlib.org/ JQuantLib] team is currently evaluating how OSGi must be used in order to make JQuantLib a library which applies the best practices on high availability on top of an OSGi container. This article started to explore how PAX scripts can be used with (or without) Spring DM can be used but we started other explorations as well.
Features
- Uses Maven for automation
- You can choose any OSGi framework from Equinox, Felix, Knopflerfish or Concierge
- You take advantage of SpringFramework for additional features on top of OSGi
- You can use Log4J immediately in your bundles, without hassle
Note
- Before going ahead, you need to install Pax Construct
Procedure
Below you can see a script I'm working on which uses Pax scripts.
#!/bin/bash -x
##################################################################
### These parameters need to be obtained from the command line ###
### Edit by hand from the time being ###
##################################################################
framework=equinox
groupId=org.jquantlib
artifactId=contrib.osgi
service=filetransfer
##################################################################
## Create project
pax-create-project -g ${groupId} -a ${artifactId}
cd ${artifactId}
## Set maven repositories
# to be tested: pax-add-repository -i com.springsource.repository.bundles.external -u
http://repository.springsource.com/maven/bundles/external
pax-add-repository -i spring-maven-milestone -u http://maven.springframework.org/milestone
pax-add-repository -i ops4j.releasees -u http://repository.ops4j.org/maven2/
## Import Spring stuff
# to be tested: pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender -v 1.1.2 -- -DimportTransitive -DwidenScope
pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender -v 1.1.0-m2 -- -DimportTransitive -DwidenScope
## Import Log4J
pax-import-bundle -g org.ops4j.pax.logging -a pax-logging-api -v 1.1.0
pax-import-bundle -g org.ops4j.pax.logging -a pax-logging-service -v 1.1.0
## Create wrappers
pax-create-module -a wrappers
cd wrappers
pax-wrap-jar -a asm -v 2.2.3
pax-wrap-jar -a aopalliance -v 1.0
cd ..
## Create our bundle
pax-create-bundle -n ${service}-api -p ${groupId}.${artifactId} -- -Dinternals=false -Dspring
pax-create-bundle -n ${service}-impl -p ${groupId}.${artifactId} -- -Dinterface=false -Dspring -Djunit
cd ${service}-impl
pax-import-bundle -a ${service}-api
## Start OSGi framework
cd ..
mvn clean install pax:provision -Dframework=${framework}
When you run the above script, it does all the magic for you, starts Equinox and deploys all needed bundles. When Equinox starts, you will see a bunch of log messages, which means Log4J is tied properly! After that, you can verify your installed bundles:
osgi> ss Framework is launched. id State Bundle 0 ACTIVE org.eclipse.osgi_3.4.2.R34x_v20080826-1230 1 ACTIVE org.eclipse.osgi.util_3.1.300.v20080303 2 ACTIVE org.eclipse.osgi.services_3.1.200.v20070605 3 ACTIVE org.springframework.bundle.osgi.extender_1.1.0.m2 4 ACTIVE org.springframework.bundle.osgi.core_1.1.0.m2 5 ACTIVE jcl104.over.slf4j_1.4.3 6 ACTIVE slf4j.api_1.4.3 7 ACTIVE slf4j.log4j12_1.4.3 8 ACTIVE org.springframework.bundle.osgi.io_1.1.0.m2 9 ACTIVE org.springframework.bundle.spring.aop_2.5.4 10 ACTIVE org.springframework.bundle.spring.beans_2.5.4 11 ACTIVE org.springframework.bundle.spring.context_2.5.4 12 ACTIVE org.springframework.bundle.spring.core_2.5.4 13 ACTIVE org.springframework.bundle.spring.test_2.5.4 14 ACTIVE org.springframework.osgi.aopalliance.osgi_1.0.0.SNAPSHOT 15 ACTIVE slf4j.simple_1.4.3 16 ACTIVE org.ops4j.pax.logging.pax-logging-api_1.1.0 17 ACTIVE org.ops4j.pax.logging.pax-logging-service_1.1.0 18 ACTIVE asm_2.2.3 19 ACTIVE aopalliance_1.0.0 20 ACTIVE org.jquantlib.contrib.osgi.filetransfer-api_1.0.0.SNAPSHOT 21 ACTIVE org.jquantlib.contrib.osgi.filetransfer-impl_1.0.0.SNAPSHOT osgi> close
Import projects into Eclipse
You can import all created stuff into Eclipse by simply File > Import... > General > Maven Projects. Press Next>, select the folder contrib.osgi and press OK. Then Select All and press Finish.
Pending
- Make the script a little smarter, it will be able to either create a project from scratch with a module in it or add a new module to an existing project.
- Configure Log4J so that you will be able to choose the appenders you need, etc. This article explains the concept and this tutorial page shows it in detail.
Links
- Pax-Construct
- How to enable Log4J
- Using Log4J in Eclipse Equinox/OSGi
- Pax plugin for Eclipse
- Tutorial for Spring Dynamic Modules (DM) for OSGi™ Service Platforms
- More sample shell scripts: http://d.hatena.ne.jp/keiono/20080701 and http://www.ops4j.org/projects/pax/construct/examples/SPRING_OSGI_EXAMPLE
- Eclipse extensions versus services
See also
- How to create OSGi bundles using Felix iPOJO
- How to create a Spring-DM bundle project using Maven (and Eclipse...)
- How to create a Spring-DM bundle using PAX scripts
RichardGomes 10:10, 27 October 2008 (UTC)


