日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
打造一個HelloWorldOSGiWeb應用程序

【精選譯文】在《你好,OSGi》的之前一篇文章中,我們介紹了OSGi Web應用開發(fā)工具Equinox的配置方法,在這一篇中,我們會進行Hello World OSGi Web應用程序的開發(fā)。該練習中的應用程序是一個包含了兩個資源的 OSGi 套件。***個是 helloworld.html,它是一個靜態(tài)的 HTML 文件;第二個是 HelloWorldServlet,它是一個 HttpServlet。有一個重點需注意,OSGi 容器提供 HttpService 服務。每個想要處理 HTTP 請求的套件都將調用該服務上的方法來通知 OSGi 容器它能夠處理哪些 URL。將 URL 注冊為 OSGi 套件可處理,存在兩種方式:

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

編輯推薦:OSGi入門與實踐全攻略

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

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

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

程序注冊方式

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

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

打開 com.javaworld.sample.osgi.web.programmatic 的 MANIFEST.MF 文件并對其進行修改,導入 javax.servlet, javax.servlet.http, org.osgi.service.http 和org.osgi.util.tracker 包。更改完成之后,你的 MANIFEST.MF 應如列表 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 清單頭的值定義了你需要導入的包列表。

在插件的根目錄創(chuàng)建一個簡單的 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 進行擴展并重寫其 doGet() 方法。新的 doGet() 方法唯一的操作就是在輸出中寫入“Hello from HelloWorldServlet”。

下一步,你需要在 com.javaworld.sample.osgi.web.programmatic 插件啟動時執(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 進行擴展并實現(xiàn)了兩個方法:

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

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

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

addingService()

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

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

removedService()

每當重寫你的 ServiceTracker 中的 addingService() 方法來獲得一個服務時,還是重寫 removedService() 來取消該服務。在 removedService() 方法內,你調用 unregister() 方法來取消注冊  /helloworld.html 和  /helloworld URI。這將通知 HttpService :com.javaworld.sample.osgi.web.programmatic 不再想要為指定 URL 提供請求服務。如果你調用 unregister() 方法來取消對 servlet 的注冊, 該 servlet 的 destroy() 方法將被調用以便對其自身進行清除。

現(xiàn)在,HelloWorld OSGi Web應用程序已經(jīng)準備就緒,并且你可以在 Equinox OSGi 框架中執(zhí)行你全部的套件。你應該能夠通過 http://localhost/helloworld.html 訪問 helloworld.html,以及通過 http://localhost/helloworld 訪問 HelloWorld 的servlet。

聲明注冊方式

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

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

2. 編輯 com.javaworld.sample.osgi.web.declarative 套件的 MANFIEST.MF 文件,導入 javax.servlet 和 javax.servlet.http 包并將 org.eclipse.equinox.http.registry  設置為該套件的被請求套件。完成這項修改之后,你的 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.  

這個 Require-Bundle 清單頭包含一個套件符號名的列表,在對導入搜索之后并且在套件路徑搜索之前,需對其進行搜索。不過,對其請求套件,只有那些標記為通過被請求套件導出的包才是可見的。

3. 從 com.javaworld.sample.osgi.web.programmatic 套件將 helloworld.html 和 HelloWorldServlet.java 復制到 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 具有兩個 < extension> 元素。***個,具有 id 屬性,其值為 helloServlet,表示 HelloWorldServlet類將被用于處理 /decl/helloworld 請求。通過將 point 屬性的值設置為 org.eclipse.equinox.http.registry.servlets,你可以標示這是 servlet 類。第二個 < extension> 元素,具有指定值為 helloResource 的 id 屬性,表示用戶請求 /decl/helloworld.html 應返回 helloworld.html 給用戶。

現(xiàn)在,使用聲明方式重新創(chuàng)建的 HelloWorld OSGi Web應用已經(jīng)準備好了,并且你可以在 Equinox OSGi框架中執(zhí)行你全部的套件。你可以通過 http://localhost/decl/helloworld.html 訪問 helloworld.html 以及通過 http://localhost/decl/helloworld 訪問 HelloWorld servlet。在下一篇,也是本系列的***一篇文章中,將介紹如何在Eclipse外部執(zhí)行OSGi 容器,敬請關注!


本文標題:打造一個HelloWorldOSGiWeb應用程序
轉載注明:http://www.5511xx.com/article/dpocjic.html