OSGi 环境下的 Web 开发(一):使用 Equinox 框架
关于 OSGi(Open Service Gateway Initiative) 相关理论知识网上有许多文档,这里就不作介绍了,有兴趣的可以自行查阅(文后附有相关的链接)。
OSGi 容器与 J2EE 容器整合主要有两种方式:1、将 HTTP Server 置于 Equinox 框架中;2、将 Equinox 置于 Servlet 容器中,这里使用第1种方式。
一、环境搭建:
1、预备环境
JDK(本文使用Sun JDK 5)
Eclipse(本文使用版本为 Eclipse3.5.2,这里的eclipse不是作为开发工具,而仅是使用其 plugin)
2、建立如下目录结构:
DEMO |- configuration - config.ini |- [Bundle.jar]
javax.servlet_2.5.0.v200806031605.jarorg.apache.commons.logging_1.0.4.v200904062259.jarorg.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jarorg.eclipse.equinox.http.jetty_2.0.0.v20090520-1800.jarorg.eclipse.equinox.http.registry_1.0.200.v20090520-1800.jarorg.eclipse.equinox.http.servlet_1.0.200.v20090520-1800.jarorg.eclipse.equinox.registry_3.4.100.v20090520-1800.jarorg.eclipse.osgi.services_3.2.0.v20090520-1800.jarorg.eclipse.osgi_3.5.2.R35x_v20100126.jarorg.mortbay.jetty.server_6.1.15.v200905151201.jarorg.mortbay.jetty.util_6.1.15.v200905182336.jar
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
org.osgidemo.web.jar |- META-INF MANIFEST.MF |- plugin.xml |- hello.html
Manifest-Version: 1.0Require-Bundle: org.eclipse.equinox.http.registryBundle-ActivationPolicy: lazyBundle-Version: 1.0.0.201005232055Bundle-Name: org.osgidemo.webBundle-ManifestVersion: 2Import-Package: org.osgi.framework;version="1.3.0"Bundle-SymbolicName: org.osgidemo.web;singleton:=trueBundle-RequiredExecutionEnvironment: J2SE-1.5
<?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>
<!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>