http://blog.sina.com.cn/s/blog_492dbb6b01014jvo.html
本文面向具有osgi基础的初级读者。内容主要包括
- 如何用maven创建osgi项目
- 如何启动osgi框架
- 如何在osgi外部与osgi框架通信
- 如何应用jndi配置资源引用
- 如何发布osgi服务
- 如何创建基于osgi的web应用项目
1.用maven创建osgi项目
用maven的目的是使开发相率更高,而且不需要自己修改manifest.mf文件,用maven插件即可自动完成创建manifest并打包。创建这种项目的要求如下:
项目包类型为:<packaging>bundle</packaging>
要使用maven自动创建manifest.mf文件,需要插件maven-bundle-plugin,配置如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${pom.name}</Bundle-Name>
<Bundle-Version>${pom.version}</Bundle-Version>
<Bundle-Activator>com.javaworld.sample.tomcat.webActivator</Bundle-Activator>
<Export-Package>com.javaworld.sample.tomcat</Export-Package>
<Import-Package>
org.osgi.framework
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
使用maven-bundle-plugin请参考:http://wso2.org/library/tutorials/develop-osgi-bundles-using-maven-bundle-plugin ,配置好后在项目目录下执行mvn install即可
同时还要引用osgi框架支持依赖包:
<dependencies>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>framework</artifactId>
<version>3.4.2.R34x_v20080826</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
注意这里的type和scope,这种用法不仅表名bundel项目可以引用第三方bundle项目,普通项目也可以。下面将会看到。
2.启动osgi框架,并在外部与osgi框架通信
这里用Equinox OSGi框架,让EquinoxContainer 实现接口OsgiServices 并实现OsgiContainable 的start和stop方法。对于框架的启动和停止,我们放在tomcat启动和停止时触发。
首先用maven创建普通的项目,在pom中添加依赖
<dependencies>
<dependency>
<groupId>apache-tomcat</groupId>
<artifactId>catalina</artifactId>
<version>5.5.12</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>services</artifactId>
<version>3.1.200.v20071203</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>framework</artifactId>
<version>3.4.2.R34x_v20080826</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
public interface OsgiServices {
public Object getOSGiService(String serviceName);
public Class<?> getBundleClass(String bundleName, String className)
throws ClassNotFoundException;
}
//OsgiContainable 扩展OsgiServices 并提供start和stop接口