關于作者
The author of this article is co-author of the book "Struts Best Practices" published in German (2004) and French (Feb 2005). He has been working with Java n-tier web since 1998, including SilverStream appserver, before the advent of OS and J2EE. His company, Infonoia SA is editor of multi-platform, multi-repository software and solutions around document retrieval and publishing. Infonoia also provides consulting services, mentoring and training (English, French, German) on Best Practices in the web technologies and frameworks they use, such as Struts, Eclipse and RSP. Wolfgang is also available for speaking engagements on RSP. Wolfgang can be reached at wgehner at infonoia dot com.
一、關于這篇文章
這篇文章將會分成兩個部分通過分析一個實例去分析如果使用OSGI構建一個WEB應用。
第一個部分只是簡單的介紹了如何在應用中創建擴展點(extension points),第二部分將通過映射你自己的Servlet和依賴關系使其能夠作為一個新的Web應用運行
二、準備工作
1. jdk, 最好將1.5和1.6都準備好,因為下一篇文章將會用到jdk1.6
2. Eclipse 3.3
3.下載本文中需要用到的示例應用包,[下載地址]
三、運行這個應用
1. 將Rspdemo-0.2.zip解壓到C盤,形成目錄 C:\rsp
2. 雙擊 C:\rsp\t.bat ,啟動 Tomcat ,你也可以去 C:\rsp\apache-tomcat-5.5.15\bin里面啟動,效果都一樣,它里面的Tomcat幾乎是原裝的
3.打開瀏覽器,訪問 http://localhost/rspwebapp ,你應該會看到這個效果

OK,去 http://localhost/rspwebapp/struts.jsp 這個頁面看看,這個頁面是由Struts模塊生成的

這個頁面的功能自己嘗試一下就知道了
四、嘗試修改應用代碼
1.將Eclipse3.3解壓縮(覆蓋)到 C:\rsp\eclipse (由于原文件夾內包含Workspace和Project相關信息,所以不建議刪除原eclipse文件夾),接著運行Eclipse,Workspace默認就行了,在[Package Explorer]面板里就會有3個Working Set,里面都是Plug-in Project,也就是傳說中的Bundle了。
注:你可能會發現所有的[MANIFEST.MF]幾乎都有錯誤提示,原因可能是這個Web應用是在Linux/Unix操作系統寫的導致Eclipse的Editer無法識別該格式(JAVA文件也是如此),所以只需要用Text Editor打開一次該文件,讓Eclipse自動改變文件格式就行了。
2. 展開所有Project,打開org.rsp.sample.usage.servlet.MyServlet,修改一下doGet()方法,加入一些能在頁面上顯示的東西

3. 用 Plug-in Manifest Editor 打開 MANIFEST.MF, 使用Overview面板中的Export Wizard,將這個插件輸出到獨立Jar包(默認設置)

4. 訪問 http://localhost/rspwebapp/platform/sp_redeploy 頁面重新部署插件,然后再訪問 http://localhost/rspwebapp,頁面中多了
--------- Hello, Phrancol ! ---------
這3個Working set包含的內容:
Equinox - 簡單的說,就是OSGI的運行環境。
Rsp samples - Web application的內容,包括配置文件,資源文件等。
Simplejars - Rsp samples里面所需要的Jar包,每個jar包都被作為一個獨立的Plug-in.
五、Web application的目錄結構
org.rsp.sample.webapp實際上就是一個web應用,可以在C:\rsp\apache-tomcat-5.5.15\conf\Catalina\localhost里面找到Context的配置文件。
在WebContent/WEB-INF/web.xml里面配置Equinox的servletbridge啟動OSGI運行環境。
進入C:\rsp\workspace\org.rsp.sample.webapp\WebContent\WEB-INF\platform目錄,發現里面的目錄結構幾乎與Eclipse的目錄結構一樣,
這幾個目錄的用處:
configuration - 里面默認的 config.ini 列出所有可用的bundle,和他們的啟動級別,Equinox將自動讀取這個配置文件啟動bundles
plugins - bundle都放在這里面
links - 與Eclipse里面的links目錄作用一樣,讀取外部bundle
Eclipse的網站上有Equinox更詳細的介紹和簡單的示例 [點擊這里]
注意:這個示例中使用的Equinox應該是較老的版本,最新的版本對目錄結構和bridge實現都有更好的改進,可以在Eclipse的CVS上獲取最新的Equinox代碼
六、訪問其他bundle
打開org.rsp.sample.webapp/WebContent/struts.jsp,能看到<c:import url="/platform/do/myAct" />,也就是http://localhost/rspwebapp/platform/do/myAct ,這個頁面是由 org.rsp.framework.struts 提供的,打開它里面的 plugin.xml,可以發現
這樣一段配置
<extension id="myservletID" name="myservletName" point="org.eclipse.equinox.servlet.ext.servlets">
<alias>/do</alias>
<servlet-class>org.rsp.framework.struts.base.BundleActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>*/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>chainConfig</param-name>
<param-value>*/WEB-INF/chain-config.xml</param-value>
</init-param>
<httpcontext-name>cross-context</httpcontext-name>
</extension>
里面就是關于 Extension 和 Extension points 的相關配置,在Equinox上有更詳細的介紹,于是大概能理解這個頁面是如何來的了,也大概能理解如何使用其他bundle的資源。
結束語
偶爾從Google上搜索到了這篇文章,感覺對像我這樣的OSGI初學者很有用,于是將其意思整理了一下,帖在BLOG上,希望能對路過的OSGI愛好者有所幫助。
如果想更好的理解作者的意圖,建議看原文。
歡迎大家通過郵箱osgi.phrancol@gmail.com與我討論有關OSGI的話題。
Developing Eclipse/OSGi Web Applications Part 1
Developing Eclipse/OSGi Web Applications Part 2
Equinox in a Servlet Container
posted on 2007-08-29 22:40
Phrancol Yang 閱讀(3681)
評論(1) 編輯 收藏 所屬分類:
OSGI