打造一個(gè)Hello World OSGi Web應(yīng)用程序
譯文【51CTO精選譯文】在《你好,OSGi》的之前一篇文章中,我們介紹了OSGi Web應(yīng)用開發(fā)工具Equinox的配置方法,在這一篇中,我們會(huì)進(jìn)行Hello World OSGi Web應(yīng)用程序的開發(fā)。該練習(xí)中的應(yīng)用程序是一個(gè)包含了兩個(gè)資源的 OSGi 套件。***個(gè)是 helloworld.html,它是一個(gè)靜態(tài)的 HTML 文件;第二個(gè)是 HelloWorldServlet,它是一個(gè) HttpServlet。有一個(gè)重點(diǎn)需注意,OSGi 容器提供 HttpService 服務(wù)。每個(gè)想要處理 HTTP 請(qǐng)求的套件都將調(diào)用該服務(wù)上的方法來通知 OSGi 容器它能夠處理哪些 URL。將 URL 注冊(cè)為 OSGi 套件可處理,存在兩種方式:
51CTO編輯推薦:OSGi入門與實(shí)踐全攻略
程序方式:***檢索來自 OSGi 的服務(wù)寄存器 HttpService,然后調(diào)用其上的方法將請(qǐng)求 URL 注冊(cè)為套件可處理。
聲明方式:在 plugin.xml 文件中定義套件可處理的請(qǐng)求 URL。
我們將一步一步地對(duì)這些技巧進(jìn)行講解,先從程序注冊(cè)方式開始。
程序注冊(cè)方式
按照下面的步驟,可使用程序方式將 URL 注冊(cè)為插件可處理。
你首先應(yīng)做的是參加一個(gè)新的 OSGi 插件,命名為com.javaworld.sample.osgi.web.programmatic。(有關(guān)在 Eclipse 中創(chuàng)建 OSGi 插件的更多信息,請(qǐng)查閱本系列的***節(jié)。)
打開 com.javaworld.sample.osgi.web.programmatic 的 MANIFEST.MF 文件并對(duì)其進(jìn)行修改,導(dǎo)入 javax.servlet, javax.servlet.http, org.osgi.service.http 和org.osgi.util.tracker 包。更改完成之后,你的 MANIFEST.MF 應(yīng)如列表 3 類似。
列表 3. 程序式插件的 MANIFEST.MF 文件
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: Webapp Plug-in
- Bundle-SymbolicName: com.javaworld.sample.osgi.web.programmatic
- Bundle-Version: 1.0.0
- Bundle-Activator: com.javaworld.sample.osgi.web.webapp.Activator
- Bundle-Vendor: JAVAWORLD
- Bundle-Localization: plugin
- Import-Package: javax.servlet;version="2.4.0",
- javax.servlet.http;version="2.4.0",
- org.osgi.framework;version="1.3.0",
- org.osgi.service.http;version="1.2.0",
- org.osgi.util.tracker;version="1.3.2"
如你所見,Import-Package 清單頭的值定義了你需要導(dǎo)入的包列表。
在插件的根目錄創(chuàng)建一個(gè)簡(jiǎn)單的 helloworld.html 文件,如列表 4 所示。該文件用來顯示消息“Hello From helloworld.html”。
列表 4. helloworld.html
- < !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>HelloWorld OSGi Web< /title>
- < /head>
- < body>
- < h3>Hello From helloworld.html< /h3>
- < /body>
- < /html>
下一步,創(chuàng)建如列表 5 所示的 HelloWorldServlet。
列表 5. HelloWorldServlet
- package com.javaworld.sample.osgi.web.webapp;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class HelloWorldServlet extends HttpServlet{
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.setContentType("text/html");
- resp.getWriter().println("< h3>Hello from HelloWorldServlet< /h3>");
- }
- }
HelloWorldServlet 類對(duì) HttpServlet 進(jìn)行擴(kuò)展并重寫其 doGet() 方法。新的 doGet() 方法唯一的操作就是在輸出中寫入“Hello from HelloWorldServlet”。
下一步,你需要在 com.javaworld.sample.osgi.web.programmatic 插件啟動(dòng)時(shí)執(zhí)行相同的代碼。Activator.java 將作為該插件的套件的激活器(Activator),如列表 6 所示。
列表 6. Activator.java
- import org.osgi.framework.BundleActivator;
- import org.osgi.framework.BundleContext;
- import org.osgi.util.tracker.ServiceTracker;
- public class Activator implements BundleActivator {
- ServiceTracker httpServiceTracker;
- public void start(BundleContext context) throws Exception {
- System.out.println("Hello World!!");
- httpServiceTracker = new HttpServiceTracker(context);
- httpServiceTracker.open();
- }
- public void stop(BundleContext context) throws Exception {
- System.out.println("Goodbye World!!");
- httpServiceTracker.close();
- httpServiceTracker = null;
- }
- }
Activator 類對(duì) BundleActivator 進(jìn)行擴(kuò)展并實(shí)現(xiàn)了兩個(gè)方法:
start():當(dāng) OSGi 容器啟動(dòng)該插件時(shí)調(diào)用 start() 方法。在start()HttpServiceTracker 類 的一個(gè)對(duì)象;這是你用來跟蹤 HttpService 的 ServiceTracker 類。一旦你擁有了 HttpService 類的一個(gè)對(duì)象,可以調(diào)用它的 open() 方法來開始跟蹤 HttpService。
stop():當(dāng)關(guān)閉插件時(shí),OSGi 容器調(diào)用 stop() 方法。在 stop() 方法內(nèi),你調(diào)用 HttpServiceTracker 對(duì)象的 close() 方法來終止跟蹤 HttpService。
***一步是創(chuàng)建 HttpServiceTracker 類,如列表 7 所示。
列表 7. HttpServiceTracker
- import org.osgi.framework.BundleContext;
- import org.osgi.framework.ServiceReference;
- import org.osgi.service.http.HttpService;
- import org.osgi.util.tracker.ServiceTracker;
- public class HttpServiceTracker extends ServiceTracker{
- public HttpServiceTracker(BundleContext context) {
- super(context, HttpService.class.getName(), null);
- }
- public Object addingService(ServiceReference reference) {
- HttpService httpService = (HttpService) context.getService(reference);
- try {
- httpService.registerResources("/helloworld.html", "/helloworld.html", null);
- httpService.registerServlet("/helloworld", new HelloWorldServlet(), null, null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return httpService;
- }
- public void removedService(ServiceReference reference, Object service) {
- HttpService httpService = (HttpService) service;
- httpService.unregister("/helloworld.html");
- httpService.unregister("/helloworld");
- super.removedService(reference, service);
- }
- }
HttpServiceTracker 介紹
HttpService 是一項(xiàng) OSGi 服務(wù),允許 OSGi 環(huán)境中的套件動(dòng)態(tài)的注冊(cè)以及取消注冊(cè) HttpService 的 URI 名稱空間中的資源和 servlet —— 換句話說,即將請(qǐng)求 URI 映射到一個(gè)靜態(tài) HTML 文件或一個(gè) HttpServlet。HttpServiceTracker 類是類型 ServiceTracker 的一個(gè)對(duì)象,后者簡(jiǎn)化了對(duì) HttpService 的跟蹤。(有關(guān) OSGi 的 ServiceTracker 的更多信息,請(qǐng)查閱本系列文章的***節(jié)中的“跟蹤服務(wù)”。)
列表 7 中 HttpServiceTracker 類重寫了兩個(gè)方法:addingService() 和 removedService()。有必要對(duì)這兩個(gè)方法進(jìn)行解釋一下:
addingService()
一個(gè)回調(diào)方法,一旦 HttpService 可用時(shí)將對(duì)其調(diào)用。在這個(gè)方法中,首先調(diào)用 HttpService.registerResources("/helloworld.html", "/helloworld.html", null),將 helloworld.html 文件映射到 /helloworld.html。之后,每當(dāng)你請(qǐng)求 http://localhost/helloworld.html
時(shí), HttpService 將為用戶提供 helloworld.html。請(qǐng)注意,你無需將 helloworld.html 映射到 /helloworld.html URL;文件名無需匹配該地址,并且你可以將其映射到類似 /test.html 的文件上。
如果想要在你的插件中提供(serve)多個(gè) HTML 文件,你需要?jiǎng)?chuàng)建多個(gè)目錄。如果想要一個(gè) /html 目錄,可以通過調(diào)用 HttpService.registerResources("/html", "/html", null) 來注冊(cè)它。然后,如果你還想要訪問 html 文件夾中的 test.htm,相應(yīng)的地址是 http://localhost/html/test.html
。registerServlet() 方法用于將 URL 映射到 HttpServlet 類。在這個(gè)簡(jiǎn)單的代碼中,利用對(duì) registerServlet("/helloworld", new HelloWorldServlet(), null, null) 的調(diào)用將 /helloworld URL 映射到 HelloWorldServlet 類。如需將初始化參數(shù)傳遞到你的 HttpServlet,你可以創(chuàng)建一個(gè) java.util.Dictionary 對(duì)象并將其作為第三方自變量傳遞到 registerServlet()。
removedService()
每當(dāng)重寫你的 ServiceTracker 中的 addingService() 方法來獲得一個(gè)服務(wù)時(shí),還是重寫 removedService() 來取消該服務(wù)。在 removedService() 方法內(nèi),你調(diào)用 unregister() 方法來取消注冊(cè) /helloworld.html 和 /helloworld URI。這將通知 HttpService :com.javaworld.sample.osgi.web.programmatic 不再想要為指定 URL 提供請(qǐng)求服務(wù)。如果你調(diào)用 unregister() 方法來取消對(duì) servlet 的注冊(cè), 該 servlet 的 destroy() 方法將被調(diào)用以便對(duì)其自身進(jìn)行清除。
現(xiàn)在,HelloWorld OSGi Web應(yīng)用程序已經(jīng)準(zhǔn)備就緒,并且你可以在 Equinox OSGi 框架中執(zhí)行你全部的套件。你應(yīng)該能夠通過 http://localhost/helloworld.html
訪問 helloworld.html,以及通過 http://localhost/helloworld
訪問 HelloWorld 的servlet。
聲明注冊(cè)方式
你可能已經(jīng)注意到,通過程序方式將請(qǐng)求 URL 注冊(cè)為 OSGi 創(chuàng)建可處理,相應(yīng)的工作流并不小。而且,如果想要更改 helloworld.html 的 URL(比如從 /helloworld.html 更改到 /hello.html),你將不得不更新 HttpServiceTracker.java,重新編譯代碼,然后在 OSGi 容器中對(duì)其進(jìn)行部署。下面,我們來看看聲明方式,它稍微簡(jiǎn)單點(diǎn)。
1. 創(chuàng)建一個(gè)新的插件項(xiàng)目,com.javaworld.sample.osgi.web.declarative。選擇 OSGi Equinox 框架作為目標(biāo)平臺(tái)。
2. 編輯 com.javaworld.sample.osgi.web.declarative 套件的 MANFIEST.MF 文件,導(dǎo)入 javax.servlet 和 javax.servlet.http 包并將 org.eclipse.equinox.http.registry 設(shè)置為該套件的被請(qǐng)求套件。完成這項(xiàng)修改之后,你的 MANIFEST.MF 文件將與列表 8 類似。
列表 8. 聲明方式插件的 MANIFEST.MF 文件
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: Declarative Plug-in
- Bundle-SymbolicName: com.javaworld.sample.osgi.web.declarative;singleton:=true
- Bundle-Version: 1.0.0
- Bundle-Vendor: JAVAWORLD
- Bundle-Localization: plugin
- Import-Package: javax.servlet;version="2.4.0",
- javax.servlet.http;version="2.4.0"
- Require-Bundle: org.eclipse.equinox.http.registry
這個(gè) Require-Bundle 清單頭包含一個(gè)套件符號(hào)名的列表,在對(duì)導(dǎo)入搜索之后并且在套件路徑搜索之前,需對(duì)其進(jìn)行搜索。不過,對(duì)其請(qǐng)求套件,只有那些標(biāo)記為通過被請(qǐng)求套件導(dǎo)出的包才是可見的。
3. 從 com.javaworld.sample.osgi.web.programmatic 套件將 helloworld.html 和 HelloWorldServlet.java 復(fù)制到 com.javaworld.sample.osgi.web.declarative 套件。
4. ***,更改 com.javaworld.sample.osgi.web.declarative 套件的 plugin.xml 文件,將所有請(qǐng)求注冊(cè)為它能夠處理,如列表 9 所示。
Listing 9. plugin.xml
- < ?xml version="1.0" encoding="UTF-8"?>
- < ?eclipse version="3.0"?>
- < plugin>
- < extension-point id="servlets" name="HttpService servlets" schema="schema/servlets.exsd"/>
- < extension-point id="resources" name="HttpService resources" schema="schema/resources.exsd"/>
- < extension-point id="httpcontexts" name="HttpService httpcontexts" schema="schema/httpcontexts.exsd"/>
- < extension
- id="helloServlet"
- point="org.eclipse.equinox.http.registry.servlets">
- < servlet
- alias="/decl/helloworld"
- class="com.javaworld.sample.osgi.web.webapp.HelloWorldServlet">
- < /servlet>
- < /extension>
- < extension
- id="helloResource"
- point="org.eclipse.equinox.http.registry.resources">
- < resource
- alias="/decl/helloworld.html"
- base-name="/helloworld.html"
- />
- < /extension>
- < /plugin>
請(qǐng)注意,plugin.xml 具有兩個(gè) < extension> 元素。***個(gè),具有 id 屬性,其值為 helloServlet,表示 HelloWorldServlet類將被用于處理 /decl/helloworld 請(qǐng)求。通過將 point 屬性的值設(shè)置為 org.eclipse.equinox.http.registry.servlets,你可以標(biāo)示這是 servlet 類。第二個(gè) < extension> 元素,具有指定值為 helloResource 的 id 屬性,表示用戶請(qǐng)求 /decl/helloworld.html 應(yīng)返回 helloworld.html 給用戶。
現(xiàn)在,使用聲明方式重新創(chuàng)建的 HelloWorld OSGi Web應(yīng)用已經(jīng)準(zhǔn)備好了,并且你可以在 Equinox OSGi框架中執(zhí)行你全部的套件。你可以通過 http://localhost/decl/helloworld.html
訪問 helloworld.html 以及通過 http://localhost/decl/helloworld
訪問 HelloWorld servlet。在下一篇,也是本系列的***一篇文章中,將介紹如何在Eclipse外部執(zhí)行OSGi 容器,敬請(qǐng)關(guān)注!
【編輯推薦】