自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

打造一個(gè)Hello World OSGi Web應(yīng)用程序

譯文
開發(fā) 后端
本文將一步一步對(duì)OSGi Web應(yīng)用開發(fā)的技巧進(jìn)行講解,包括程序注冊(cè)方式和聲明方式。在閱讀完本文之后,相信讀者便能夠初步掌握OSGi Web應(yīng)用的開發(fā)過程。

【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 文件

  1. Manifest-Version: 1.0 
  2. Bundle-ManifestVersion: 2 
  3. Bundle-Name: Webapp Plug-in  
  4. Bundle-SymbolicName: com.javaworld.sample.osgi.web.programmatic  
  5. Bundle-Version: 1.0.0 
  6. Bundle-Activator: com.javaworld.sample.osgi.web.webapp.Activator  
  7. Bundle-Vendor: JAVAWORLD  
  8. Bundle-Localization: plugin  
  9. Import-Package: javax.servlet;version="2.4.0",  
  10. javax.servlet.http;version="2.4.0",  
  11. org.osgi.framework;version="1.3.0",  
  12. org.osgi.service.http;version="1.2.0",  
  13. org.osgi.util.tracker;version="1.3.2" 
  14.  

如你所見,Import-Package 清單頭的值定義了你需要導(dǎo)入的包列表。

在插件的根目錄創(chuàng)建一個(gè)簡(jiǎn)單的 helloworld.html 文件,如列表 4 所示。該文件用來顯示消息“Hello From helloworld.html”。

列表 4. helloworld.html

  1. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  2. < html> 
  3. < head> 
  4. < meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
  5. < title>HelloWorld OSGi Web< /title> 
  6. < /head> 
  7. < body> 
  8. < h3>Hello From helloworld.html< /h3> 
  9. < /body> 
  10. < /html> 
  11.  

下一步,創(chuàng)建如列表 5 所示的 HelloWorldServlet。

列表 5. HelloWorldServlet

  1. package com.javaworld.sample.osgi.web.webapp;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. public class HelloWorldServlet extends HttpServlet{  
  8. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
  9. resp.setContentType("text/html");  
  10. resp.getWriter().println("< h3>Hello from HelloWorldServlet< /h3>");  
  11. }  
  12. }  
  13.  

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

  1. import org.osgi.framework.BundleActivator;  
  2. import org.osgi.framework.BundleContext;  
  3. import org.osgi.util.tracker.ServiceTracker;  
  4. public class Activator implements BundleActivator {  
  5. ServiceTracker httpServiceTracker;  
  6. public void start(BundleContext context) throws Exception {  
  7. System.out.println("Hello World!!");  
  8. httpServiceTracker = new HttpServiceTracker(context);  
  9. httpServiceTracker.open();  
  10. }  
  11.  
  12. public void stop(BundleContext context) throws Exception {  
  13. System.out.println("Goodbye World!!");  
  14. httpServiceTracker.close();  
  15. httpServiceTracker = null;  
  16. }  
  17. }  
  18.  

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

  1. import org.osgi.framework.BundleContext;  
  2. import org.osgi.framework.ServiceReference;  
  3. import org.osgi.service.http.HttpService;  
  4. import org.osgi.util.tracker.ServiceTracker;  
  5. public class HttpServiceTracker extends ServiceTracker{  
  6. public HttpServiceTracker(BundleContext context) {  
  7. super(context, HttpService.class.getName(), null);  
  8. }  
  9. public Object addingService(ServiceReference reference) {  
  10. HttpService httpService = (HttpService) context.getService(reference);  
  11. try {  
  12. httpService.registerResources("/helloworld.html""/helloworld.html"null);  
  13. httpService.registerServlet("/helloworld"new HelloWorldServlet(), nullnull);  
  14. catch (Exception e) {  
  15. e.printStackTrace();  
  16. }  
  17. return httpService;  
  18. }  
  19. public void removedService(ServiceReference reference, Object service) {  
  20. HttpService httpService = (HttpService) service;  
  21. httpService.unregister("/helloworld.html");  
  22. httpService.unregister("/helloworld");  
  23. super.removedService(reference, service);  
  24. }  
  25. }  
  26.  

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 文件

  1. Manifest-Version: 1.0 
  2. Bundle-ManifestVersion: 2 
  3. Bundle-Name: Declarative Plug-in  
  4. Bundle-SymbolicName: com.javaworld.sample.osgi.web.declarative;singleton:=true  
  5. Bundle-Version: 1.0.0 
  6. Bundle-Vendor: JAVAWORLD  
  7. Bundle-Localization: plugin  
  8. Import-Package: javax.servlet;version="2.4.0",  
  9. javax.servlet.http;version="2.4.0" 
  10. Require-Bundle: org.eclipse.equinox.http.registry  
  11.  

這個(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

  1. < ?xml version="1.0" encoding="UTF-8"?> 
  2. < ?eclipse version="3.0"?> 
  3. < plugin> 
  4. < extension-point id="servlets" name="HttpService servlets" schema="schema/servlets.exsd"/> 
  5. < extension-point id="resources" name="HttpService resources" schema="schema/resources.exsd"/> 
  6. < extension-point id="httpcontexts" name="HttpService httpcontexts" schema="schema/httpcontexts.exsd"/> 
  7. < extension 
  8. id="helloServlet" 
  9. point="org.eclipse.equinox.http.registry.servlets"> 
  10. < servlet 
  11. alias="/decl/helloworld" 
  12. class="com.javaworld.sample.osgi.web.webapp.HelloWorldServlet"> 
  13. < /servlet> 
  14. < /extension> 
  15. < extension 
  16. id="helloResource" 
  17. point="org.eclipse.equinox.http.registry.resources"> 
  18. < resource 
  19. alias="/decl/helloworld.html" 
  20. base-name="/helloworld.html" 
  21. /> 
  22. < /extension> 
  23. < /plugin> 
  24.  

請(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)注!

【編輯推薦】

  1. 你好,OSGi!OSGi入門必讀系列
  2. 初探OSGi Web應(yīng)用開發(fā)利器Equinox:環(huán)境設(shè)置
  3. OSGi與Spring:設(shè)置Spring DM開發(fā)環(huán)境
  4. OSGi和Spring入門:什么是Spring動(dòng)態(tài)模型(Spring DM)?
  5. OSGi是什么:Java語言的動(dòng)態(tài)模塊系統(tǒng)
責(zé)任編輯:yangsai 來源: 51CTO.com
相關(guān)推薦

2011-03-10 10:45:47

Azure“Hello Worl

2009-09-16 17:15:19

OSGi Bundle

2013-10-30 22:10:28

Clouda程序

2009-10-22 11:03:20

OSGi Web應(yīng)用程

2023-09-21 08:00:00

ChatGPT編程工具

2024-03-13 13:53:10

C++程序開發(fā)

2022-04-27 10:51:00

PythonMLCubePodman

2013-05-13 09:31:29

Web App開發(fā)WebApp

2009-09-28 14:49:08

Spring DMOSGi服務(wù)

2011-05-11 10:58:39

iOS

2010-07-12 10:11:27

ibmdwWeb

2011-01-20 07:16:48

Web程序員

2013-12-26 15:25:15

Android開發(fā)安裝開發(fā)環(huán)境Hello World

2012-08-17 10:07:58

IBMdW

2021-02-01 17:29:19

FlutterHello World開發(fā)

2012-05-03 11:21:58

ApacheCXFJava

2010-08-13 13:05:30

Flex應(yīng)用程序

2011-06-09 09:31:40

Qt 實(shí)例

2011-11-29 16:30:42

jQTouch移動(dòng)web應(yīng)用

2023-05-19 08:49:58

SQLAlchemy數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)