OSGi 环境下的 Web 开发(一):使用 Equinox 框架

关于 OSGi(Open Service Gateway Initiative) 相关理论知识网上有许多文档,这里就不作介绍了,有兴趣的可以自行查阅(文后附有相关的链接)。
OSGi 容器与 J2EE 容器整合主要有两种方式:1、将 HTTP Server 置于 Equinox 框架中;2、将 Equinox 置于 Servlet 容器中,这里使用第1种方式。

[b]一、环境搭建:[/b]
1、预备环境
JDK(本文使用Sun JDK 5)
Eclipse(本文使用版本为 Eclipse3.5.2,这里的eclipse不是作为开发工具,而仅是使用其 plugin)

2、建立如下目录结构:

DEMO
|- configuration
- config.ini
|- [Bundle.jar]


3、从 Eclipse/Plugins 下拷贝如下 jar 至 DEMO 目录下:

javax.servlet_2.5.0.v200806031605.jar
org.apache.commons.logging_1.0.4.v200904062259.jar
org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar
org.eclipse.equinox.http.jetty_2.0.0.v20090520-1800.jar
org.eclipse.equinox.http.registry_1.0.200.v20090520-1800.jar
org.eclipse.equinox.http.servlet_1.0.200.v20090520-1800.jar
org.eclipse.equinox.registry_3.4.100.v20090520-1800.jar
org.eclipse.osgi.services_3.2.0.v20090520-1800.jar
org.eclipse.osgi_3.5.2.R35x_v20100126.jar
org.mortbay.jetty.server_6.1.15.v200905151201.jar
org.mortbay.jetty.util_6.1.15.v200905182336.jar


4、config.ini 的内容如下:
osgi.bundles=reference\:file\:org.eclipse.equinox.registry_3.4.100.v20090520-1800.jar@start,reference\:file\:org.eclipse.equinox.common_3.5.1.R35x_v20090807-
1100.jar@start,reference\:file\:org.mortbay.jetty.util_6.1.15.v200905182336.jar@start,reference\:file\:org.mortbay.jetty.server_6.1.15.v200905151201.jar@start,reference\:file\:org.eclipse.equinox.http.registry_1.0.200.v20090520-1800.jar@start,reference\:file\:org.eclipse.equinox.http.jetty_2.0.0.v20090520-1800.jar@start,reference\:file\:org.apache.commons.logging_1.0.4.v200904062259.jar@start,reference\:file\:org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,reference\:file\:org.eclipse.equinox.http.servlet_1.0.200.v20090520-1800.jar@start,reference\:file\:javax.servlet_2.5.0.v200806031605.jar@start

osgi.bundles 定义的是OSGi容器启动时,需自启动的 Bundle,其 Bundle 状态为 ACTIVE (如果启动时没有异常)。

5、启动 OSGi
进入命令行,在DEMO目录下键入
java -jar org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console
如能见到 osgi> 则表示容器启动成功。
键入 ss 可查看各 bundle 的状态。
在浏览器中访问 http://localhost/ ,能见到如下页面,则表示 jetty 容器也已成功启动。

[img]http://dl.iteye.com/upload/attachment/253688/1a9b1cc7-6566-3d7b-b866-117941947296.jpg[/img]

如果未成功启动,可键入 start [bundle-id] 重新启动,根据抛出的异常来定位问题。

[b]二、开发 web 应用[/b]
这里的 web 应用与J2EE中的不一样的,J2EE中的web应用主要是以 war 形式提供的,而在OSGi环境中,则是以jar提供的,即 bundle,也即 eclipse 中的一个插件(plug-in)。
1、org.osgidemo.web.jar 的目录结构如下:

org.osgidemo.web.jar
|- META-INF
MANIFEST.MF
|- plugin.xml
|- hello.html


2、MANIFEST.MF 内容如下:
Manifest-Version: 1.0
Require-Bundle: org.eclipse.equinox.http.registry
Bundle-ActivationPolicy: lazy
Bundle-Version: 1.0.0.201005232055
Bundle-Name: org.osgidemo.web
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-SymbolicName: org.osgidemo.web;singleton:=true
Bundle-RequiredExecutionEnvironment: J2SE-1.5


3、plugin.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension id="helloResource" point="org.eclipse.equinox.http.registry.resources">
<resource alias="/hello.html" base-name="/hello.html" />
</extension>
</plugin>

在 plugin.xml 文件中,定义了一个扩展,用来处理对静态资源文件例如图片文件等的请求。

4、hello.html 就是web应用的资源,其内容为:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello OSGi Web</title>
</head>
<body>
<h3>Hello OSGi Web</h3>
</body>
</html>


[b]三、安装 web 应用[/b]
将 org.osgidemo.web.jar 拷贝至 DEMO 目录下,在 osgi 命令提示符下,键入
install reference:file:org.osgidemo.web.jar
命令会返回 Bundle id is [id],这里的 id 是 bundle 唯一编号,这时 bundle 的状态为 INSTALLED,再键入:
start [id]
启动该 bundle 。

[b]四、在浏览器中,访问 http://localhost/hello.html,见到如下画面:[/b]

[img]http://dl.iteye.com/upload/attachment/253690/8ca8f6a3-2b0f-3513-97e1-535c5eb031fc.jpg[/img]

则表明 web 应用已经安装成功,并能成功处理http请求。
如要停止应用
stop [bundle-id]
卸载应用
uninstall [bundle-id]
基于此,web 应用的真正热插拨、热部署成为可能。

[b]五、参考文档[/b]
[url]http://www.iteye.com/wiki/OSGi[/url]
[url]http://www.ibm.com/developerworks/cn/web/0907_osgiweb_liuqing[/url]
[url]http://baike.baidu.com/view/362847.htm?fr=ala0_1_1[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值