Extwind——支持OSGi應用的Tomcat
最新進度:
實現Tomcat與Equinox的擴展集成,全面兼容原始Tomcat和Equinox。
界面控制臺基本完成,目前可以使用它控制OSGi-App中的bundle,功能(Install, Start, Stop, Uninstall )
在System Bundle(Equinox)中export: javax.servlet,org.extwind.osgi.service
實現HttpService注冊Servlet和Resource
實現HttpService注冊jsp 實現相同HttpContext注冊地Servlet和Resource共享ServletContext
OSGiContext自動安裝OSGi-App根目錄中的jar包
Tomcat-OSGi QuickStart:
1. 準備工作
使用Extwind的Tomcat-OSGi是非常簡單的,首先下載不到2M的rar文件,其中包括以下內容:
extwind-tomcat-osgi-0.1-alpha.jar - Tomcat于Equinox集成jar包,它是必須的。
org.eclipse.osgi-0.1-Alpha.jar - 擴充過的Equinox osgi-framework,它不是必須的,如果不使用它,將需要額外的bundle去支持javax.servlet,javax.servlet.http,org.extwind.osgi.service.http,建議使用它,因為在以后的版本中,它有可能會成為必須得。
osgiManager - Web Application,支持OSGi Application管理的控制臺,通過它可以對每一個OSGi-App中的bundle進行管理
osgiapp - 一個非常簡單的Web Application,里面包含一個簡單的bundle,分別注冊了一個static resource,一個jsp文件,一個Servlet
server.xml - Tomcat 6.x的示例配置文件,可選的
tomcat-users.xml - Tomcat 6.x 控制臺用戶權限配置文件,可選的
[
點擊這里下載 Tomcat-OSGi-QuickStart ]
2. 配置Tomcat 6.x
將Tomcat 6.x 解壓到某目錄,例如 D:\Tomcat6
將extwind-tomcat-osgi-0.1-alpha.jar 和 org.eclipse.osgi-0.1-Alpha.jar 拷貝到 D:\Tomcat6\lib 目錄下
將osgiapp和osgiManager拷貝到 D:\Tomcat6\webapps 目錄下
打開server.xml,在<Host>節點中加入2個屬性 className和hostConfigClass,就像下面一樣:(或者直接使用本例提供的server.xml)
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false" className="org.extwind.tomcat.catalina.core.ExtenseStandardHost" hostConfigClass="org.extwind.tomcat.catalina.startup.ExtenseHostConfig">

將tomcat-users.xml拷貝到D:\Tomcat6\conf 目錄下,覆蓋原配置文件
3. 開始使用
經過以上簡單的準備和配置工作,現在已經可以開始使用了,啟動tomcat,訪問
http://localhost:8080/osgiManager/html/list
認證框中的用戶名和密碼都是 tomcat,之后會進入下面這個頁面

點擊OSGi Console,進入/osgiapp的管理界面

可以看到,org.extwind.test.bundle已經被安裝了,因為OSGi-App啟動時會將根目錄中的jar包作為bundle自動安裝
控制臺提供了4個功能:安裝,啟動,停止,卸載。 在以后的版本中會逐步加強控制臺的管理功能。
在測試bundle中注冊了3個資源

public class TestBundleActivator implements BundleActivator
{
HttpService httpService = null;


public void start(BundleContext context) throws Exception
{
ServiceReference sr = context.getServiceReference(HttpService.class
.getName());
httpService = (HttpService) context.getService(sr);
System.out.println("====================================================");
System.out.println(httpService.getClass().getName());
System.out.println("====================================================");
Dictionary<String, String> initparams = new Hashtable<String, String>();
initparams.put("servlet-name", "petstore");
initparams.put("load-on-startup", "2");
httpService.registerServlet("/test.do", new TestServlet(), initparams, null);
httpService.registerResources("/files", "/resource", null);
httpService.registerResources("*.jsp", "/resource", null);
}


public void stop(BundleContext context) throws Exception
{
httpService.unregister("/test.do");
httpService.unregister("/files");
httpService.unregister("*.jsp");
}

}
3個資源的鏈接分別是
http://localhost:8080/osgiapp/files/test.ini
http://localhost:8080/osgiapp/test.do
http://localhost:8080/osgiapp/resource/date.jsp
這里的jsp并沒有使用Equinox的JspServlet,而是作為一個resource進行注冊
現在,可以通過OSGi Console 對模塊進行熱插拔了。
到這里,已經基本使用了Tomcat-OSGi的簡單功能,更多的功能正在開發中。
獲取最新Tomcat-OSGi的源碼:
http://code.google.com/p/extwind
加入
Extwind Group馬上開始討論
http://groups.google.com/group/extwind
posted on 2009-04-12 02:17
Phrancol Yang 閱讀(2794)
評論(1) 編輯 收藏 所屬分類:
OSGI