在equinox环境开发web使用的"利器" - registerResources()方法 详解

在equinox环境开发web应用的利器 -- registerResources()方法 详解registerResources()方法详解1、简介re

在equinox环境开发web应用的"利器" -- registerResources()方法 详解

registerResources()方法详解

1、简介
registerResources(...)是org.osgi.service.http.HttpService类中提供的方法,可以直接向jetty服务器中注册静态的资源。


2、使用说明
registerResources(String alias, String name, HttpContext context)

A、参数 alias

作用:
??????????

B、参数name
作用:设置alias中目录以及文件在bundle中的位置。
?????????? name实际上是一个目录名称,目录中存在alias中设定的访问内容
????????? ?当alias设定为具体的文件名称时,name也需要设定为相应路径下文件的全名
约束:
1)name不能以 / 结尾
2)name可以为 /

C、参数context
作用:context当前bundle的上下文内容。
????? 如果为空,equinox自动创建一个默认的context。

3、使用举例

举例1:


假设 网站为?http://www.teamlet.org? alias="/test"?
???? registerResources("/test", "/", null)
???? 则通过?http://www.teamlet.org/test?可以访问 test下的所有静态文件内容

举例2:

假设 网站为?http://www.teamlet.org? alias="/test/*.jsp"?
???? registerResources("/test/*.jsp", "/", null)
???? 则通过?http://www.teamlet.org/test?可以访问 test下的所有jsp内容,而所有html等其他静态内容不可以访问


4、ResourceServlet与registerResources的比较

tAdaptor适配器。
ResourceServlet可以利用ServletAdaptor设置filter来保护资源,设置字符集等预处理。

5、实用代码:

package?org.teamlet.osgi.test.servlet.filter;

import?java.io.IOException;
import?javax.servlet.*;
import?javax.servlet.Filter;
import?javax.servlet.FilterConfig;
import?javax.servlet.Servlet;
import?javax.servlet.ServletException;
import?javax.servlet.ServletRequest;

import?org.eclipse.equinox.http.helper.BundleEntryHttpContext;
import?org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import?org.eclipse.equinox.http.helper.FilterServletAdaptor;
import?org.eclipse.equinox.http.helper.ResourceServlet;
import?org.eclipse.equinox.jsp.jasper.JspServlet;
import?org.osgi.framework.BundleActivator;
import?org.osgi.framework.BundleContext;
import?org.osgi.framework.ServiceReference;
import?org.osgi.service.http.HttpContext;
import?org.osgi.service.http.HttpService;
import?org.osgi.util.tracker.ServiceTracker;

public?class?Activator?implements?BundleActivator?{

?private?ServiceTracker?httpServiceTracker;

?public?void?start(BundleContext?context)?throws?Exception?{
??httpServiceTracker?=?new?HttpServiceTracker(context);
??httpServiceTracker.open();
?}

?public?void?stop(BundleContext?context)?throws?Exception?{
??httpServiceTracker.close();
?}

?private?class?HttpServiceTracker?extends?ServiceTracker?{

??public?HttpServiceTracker(BundleContext?context)?{
???super(context,?HttpService.class.getName(),?null);
??}

??public?Object?addingService(ServiceReference?reference)?{
???final?HttpService?httpService?=?(HttpService)?context.getService(reference);
???try?{
????HttpContext?commonContext?=?new?BundleEntryHttpContext(context.getBundle(),?"/webroot");?
????httpService.registerResources("/jsp/*.jsp",?"/",?commonContext);?
????httpService.registerResources("/jsp/*.html",?"/test",?commonContext);
???}?catch?(Exception?e)?{
????e.printStackTrace();
???}
???return?httpService;
??}

??public?void?removedService(ServiceReference?reference,?Object?service)?{
???final?HttpService?httpService?=?(HttpService)?service;
???httpService.unregister("/jsp");?
???httpService.unregister("/jsp/*.jsp");?
???super.removedService(reference,?service);
??}???
?}
}