av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

打造一個(gè)HelloWorldOSGiWeb應(yīng)用程序

【精選譯文】在《你好,OSGi》的之前一篇文章中,我們介紹了OSGi Web應(yīng)用開發(fā)工具Equinox的配置方法,在這一篇中,我們會進(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 請求的套件都將調(diào)用該服務(wù)上的方法來通知 OSGi 容器它能夠處理哪些 URL。將 URL 注冊為 OSGi 套件可處理,存在兩種方式:

目前創(chuàng)新互聯(lián)公司已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、烏蘭網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

編輯推薦:OSGi入門與實(shí)踐全攻略

程序方式:***檢索來自 OSGi 的服務(wù)寄存器 HttpService,然后調(diào)用其上的方法將請求 URL 注冊為套件可處理。

聲明方式:在 plugin.xml 文件中定義套件可處理的請求 URL。

我們將一步一步地對這些技巧進(jìn)行講解,先從程序注冊方式開始。

程序注冊方式

按照下面的步驟,可使用程序方式將 URL 注冊為插件可處理。

你首先應(yīng)做的是參加一個(gè)新的 OSGi 插件,命名為com.javaworld.sample.osgi.web.programmatic。(有關(guān)在 Eclipse 中創(chuàng)建 OSGi 插件的更多信息,請查閱本系列的***節(jié)。)

打開 com.javaworld.sample.osgi.web.programmatic 的 MANIFEST.MF 文件并對其進(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è)簡單的 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 類對 HttpServlet 進(jìn)行擴(kuò)展并重寫其 doGet() 方法。新的 doGet() 方法唯一的操作就是在輸出中寫入“Hello from HelloWorldServlet”。

下一步,你需要在 com.javaworld.sample.osgi.web.programmatic 插件啟動時(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 類對 BundleActivator 進(jìn)行擴(kuò)展并實(shí)現(xiàn)了兩個(gè)方法:

start():當(dāng) OSGi 容器啟動該插件時(shí)調(diào)用 start() 方法。在start()HttpServiceTracker 類 的一個(gè)對象;這是你用來跟蹤 HttpService 的 ServiceTracker 類。一旦你擁有了 HttpService 類的一個(gè)對象,可以調(diào)用它的 open() 方法來開始跟蹤 HttpService。

stop():當(dāng)關(guān)閉插件時(shí),OSGi 容器調(diào)用 stop() 方法。在 stop() 方法內(nèi),你調(diào)用 HttpServiceTracker 對象的 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)境中的套件動態(tài)的注冊以及取消注冊 HttpService 的 URI 名稱空間中的資源和 servlet —— 換句話說,即將請求 URI 映射到一個(gè)靜態(tài) HTML 文件或一個(gè) HttpServlet。HttpServiceTracker 類是類型 ServiceTracker 的一個(gè)對象,后者簡化了對 HttpService 的跟蹤。(有關(guān) OSGi 的 ServiceTracker 的更多信息,請查閱本系列文章的***節(jié)中的“跟蹤服務(wù)”。)

列表 7 中 HttpServiceTracker 類重寫了兩個(gè)方法:addingService() 和 removedService()。有必要對這兩個(gè)方法進(jìn)行解釋一下:

addingService()

一個(gè)回調(diào)方法,一旦 HttpService 可用時(shí)將對其調(diào)用。在這個(gè)方法中,首先調(diào)用 HttpService.registerResources("/helloworld.html", "/helloworld.html", null),將 helloworld.html 文件映射到 /helloworld.html。之后,每當(dāng)你請求 http://localhost/helloworld.html 時(shí), HttpService 將為用戶提供 helloworld.html。請注意,你無需將 helloworld.html 映射到 /helloworld.html URL;文件名無需匹配該地址,并且你可以將其映射到類似 /test.html 的文件上。

如果想要在你的插件中提供(serve)多個(gè) HTML 文件,你需要創(chuàng)建多個(gè)目錄。如果想要一個(gè) /html 目錄,可以通過調(diào)用 HttpService.registerResources("/html", "/html", null) 來注冊它。然后,如果你還想要訪問 html 文件夾中的 test.htm,相應(yīng)的地址是 http://localhost/html/test.html。registerServlet() 方法用于將 URL 映射到 HttpServlet 類。在這個(gè)簡單的代碼中,利用對 registerServlet("/helloworld", new HelloWorldServlet(), null, null) 的調(diào)用將 /helloworld URL 映射到 HelloWorldServlet 類。如需將初始化參數(shù)傳遞到你的 HttpServlet,你可以創(chuàng)建一個(gè) java.util.Dictionary  對象并將其作為第三方自變量傳遞到 registerServlet()。

removedService()

每當(dāng)重寫你的 ServiceTracker 中的 addingService() 方法來獲得一個(gè)服務(wù)時(shí),還是重寫 removedService() 來取消該服務(wù)。在 removedService() 方法內(nèi),你調(diào)用 unregister() 方法來取消注冊  /helloworld.html 和  /helloworld URI。這將通知 HttpService :com.javaworld.sample.osgi.web.programmatic 不再想要為指定 URL 提供請求服務(wù)。如果你調(diào)用 unregister() 方法來取消對 servlet 的注冊, 該 servlet 的 destroy() 方法將被調(diào)用以便對其自身進(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。

聲明注冊方式

你可能已經(jīng)注意到,通過程序方式將請求 URL 注冊為 OSGi 創(chuàng)建可處理,相應(yīng)的工作流并不小。而且,如果想要更改 helloworld.html 的 URL(比如從 /helloworld.html 更改到 /hello.html),你將不得不更新 HttpServiceTracker.java,重新編譯代碼,然后在 OSGi 容器中對其進(jìn)行部署。下面,我們來看看聲明方式,它稍微簡單點(diǎn)。

1. 創(chuàng)建一個(gè)新的插件項(xiàng)目,com.javaworld.sample.osgi.web.declarative。選擇 OSGi Equinox 框架作為目標(biāo)平臺。

2. 編輯 com.javaworld.sample.osgi.web.declarative 套件的 MANFIEST.MF 文件,導(dǎo)入 javax.servlet 和 javax.servlet.http 包并將 org.eclipse.equinox.http.registry  設(shè)置為該套件的被請求套件。完成這項(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è)套件符號名的列表,在對導(dǎo)入搜索之后并且在套件路徑搜索之前,需對其進(jìn)行搜索。不過,對其請求套件,只有那些標(biāo)記為通過被請求套件導(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 文件,將所有請求注冊為它能夠處理,如列表 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.  

請注意,plugin.xml 具有兩個(gè) < extension> 元素。***個(gè),具有 id 屬性,其值為 helloServlet,表示 HelloWorldServlet類將被用于處理 /decl/helloworld 請求。通過將 point 屬性的值設(shè)置為 org.eclipse.equinox.http.registry.servlets,你可以標(biāo)示這是 servlet 類。第二個(gè) < extension> 元素,具有指定值為 helloResource 的 id 屬性,表示用戶請求 /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 容器,敬請關(guān)注!


本文題目:打造一個(gè)HelloWorldOSGiWeb應(yīng)用程序
路徑分享:http://uogjgqi.cn/article/dpocjic.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流