转换一个非OSGi Bundle成为一个标准的OSGi Bundle一般要实现下面几个问题:
1. IMPORTING PACKAGES
The following guidelines is adopted when handle the Import-Package header:
■ Import the packages that the library or framework uses, and don’t import unused
packages, which would tie the bundle to unnecessary dependencies.
■ Specify the version of the packages, so the library or framework won’t use classes
that it isn’t meant to use, which could lead to unexpected behavior.
■ Specify the difference between mandatory and optional dependencies by using the
resolution directive.
示例:
Import-Package: org.hibernate;version="[3.2.6.ga,4.0.0)";resolution:=optional,
org.hibernate.cache;version="[3.2.6.ga,4.0.0)";resolution:=optional,org.hibernate.cfg;
version="[3.2.6.ga,4.0.0)";resolution:=optional,
(...)
com.ibatis.common.util;version="[2.3.0.677, 3.0.0)";resolution:=optional,
com.ibatis.common.xml;version="[2.3.0.677, 3.0.0)";resolution:=optional,
com.ibatis.sqlmap.client;version="[2.3.0.677, 3.0.0)";resolution:=optional
(...)
2. EXPORTING PACKAGES
示例:
Export-Package: org.springframework.orm;version="2.5.6.A",
(...)
3. GIVING AN IDENTITY TO A BUNDLE
Bundle-SymbolicName: org.springframework.core //必须的,只能是字母,下划线,连字符
Bundle-Version: 2.5.6.A //必须的,和上一个Header一起唯一标示一个Bundle
Bundle-Name: Spring Core //可选的,方便开发人员阅读
4. JAR打包规范
■ The META-INF/MANIFEST.MF file must be the first entry in the JAR, and the jar command
enforces this rule (so you shouldn’t try to package your OSGi bundles manually).
■ The manifest format has strict requirements. For instance, lines can’t be longer than 72
characters and the file should end with an empty line.
参考:http://download.oracle.com/javase/1.5.0/docs/guide/jar/jar.html
5. Bun Tool
(1) 下载地址:http://www.aqute.biz/repo/biz/aQute/bnd/
(2) Create OSGi Bundle: http://blog.springsource.com/2008/02/18/creating-osgi-bundles/
6. 案例演示:The Bud Tool
以commons-logging-1.1.1.jar为例,bnd-0.0.401.jar重命名为bnd.jar方便使用,见附件。
(1)检查一个JAR文件内部结构
java -jar bnd.jar commons-logging-1.1.1.jar
(2) 打开MAINFEST.MF文件及检查Package依赖
java -jar bnd.jar print commons-logging-1.1.1.jar
(3) 生成OSGi格式的Bundle
java -jar bnd.jar wrap commons-logging-1.1.1.jar
将生成commons-logging-1.1.1.bar, 查看该Bar文件的MAINFEST.MF文件:
java -jar bnd.jar print commons-logging-1.1.1.bar
默认情况下,wrap命令使用的property文件为:
Export-Package: *
Import-Package: AllReferencedPackages
若需明确指定需要Import或Export的Package,可参考(5).
(4)指定新生成的Bundle
java -jar bnd.jar wrap -output commons-logging-osgi-1.1.1.jar commons-logging-1.1.1.jar
将生成OSGi格式的Bundle:commons-logging-osgi-1.1.1.jar
(5)精确指定生成的MAINFEST.MF中的内容:增加Properties文件
commons-logging-1.1.1.bnd:
# Assign version
version=1.1.1
# Assign name
bundle_name=commons.logging
Bundle-Name: OSGi'fied Common Logging- ${bundle_name} ${version}
Bundle-SymbolicName: ${bundle_name}
Bundle-Version: ${version}
Export-Package: org.apache.commons.logging;version=${version}
Private-Package: org.apache.commons.logging.impl
命令:
java -jar bnd.jar wrap -output commons-logging-osgi-1.1.1.jar -properties commons-logging-1.1.1.bnd commons-logging-1.1.1.jar