Component 的作用 :
1. 对外提供 SERVICE
2. 使用其他 Component 提供的SERVICE
3. 交由OSGI框架管理生命周期
****组件的生命周期和BUNDLE关联,当BUNDLE停止时BUNDLE中所有组件也会停止,组件的生命周期有ACTIVATE 和DEACTIVATE 控制,组件激活时被调用ACTIVATE方法, 组件停止时被调用DEACTIVATE.(可不实现)同时这两个方法可以获取ComponentContext
Component 生命周期:
1. BUNDLE 启动 加载Component并解析 ,此时Component状态为 enabled
2. 之后根据Component 的配置找到了服务,如果满足了,那么此时Component的状态为Statisfied ,未满足状态为unStatisfied.
3. 当Component被凋用时,状态为Statisfied时 激活Component 如果有ACTIVATE方法存在调用这个方法.
4. 当BUNDLE停止时OSGI将通知了Component服务,有DEACTIVATE方法掉用着个方法,之后把该Component设置为unStatisfied,在完成这些工作后Component设置为Disabled.
OSGI 搭建系统时常碰到的ClassLoader问题;
ClassNotFoundException 和 NoClassDefError 两个异常;
着是ClassLoader加载类出现问题时常碰到的两个异常.
区别ClassNotFoundException 是指通过ClassLoader加载不到所需要的类,
NoClassDefError是指通过ClassLoader已经找到了所需要的类,但找不到该类所依赖的其他的类.
MANIFEST.MF 说明文件.
IMPORT –PACKAGE:指需要依赖的包
EXPORT –PACKAGE:指对外暴露的接口包
SERVICE –COMPONENT:指定提供服务的组件说明文件信息.
5. 开发OSGI 的JSP应用
1. 首先建一个Project –in project
2. 在MANIFEST.MF---选择Dependencies---Required Plug-ins中—add:
javax.servlet,
javax.servlet.jsp,
org.apache.commons.el,
org.apache.commons.logging,
org.apache.jasper,
org.eclipse.equinox.jsp.jasper,
org.eclipse.equinox.http.servlet,
org.eclipse.equinox.http.registry,
org.eclipse.osgi.services,
org.eclipse.equinox.http.jetty,
org.eclipse.equinox.jsp.jasper.registry
***jetty WED服务器很有可能和你的机器中的其他东西端口冲突
这时候用-Dorg.osgi.service.http.port=8080, ,在arguments—VM arguments:输入.
3. 在新建WEB资源,在工程中的COM.....SERVICE包中已经生成了一个Activator类,为其添加两条控制台输出并注册资源,需要注意的是
是在名为org.eclipse.equinox.http.helper的bundle
****在OSGI框架中的通过这样的方法来获取服务:
ServiceReference serviceRef = context.getServiceReference(服务标识名);
Object service = context.getService(serviceRef);
例:
ServiceReference sr = context.getServiceReference(HttpService.class.getName());
HttpService hs = (HttpService) context.getService(sr);
HttpService通过下面的方法注册Servlet
HttpService.registerServlet(Servlet Mapping Url, Servlet实例,实例的配置信息);
例:
Servlet jspServlet = new ContextPathServletAdaptor( new JspServlet(context.getBundle(), "/web/"), "/jsp");
hs.registerServlet("/jsp/*.jsp", jspServlet, null, hc);
HttpContext通过下面的方法注册配置文件信息
HttpService.registerResources(资源Mapping url资源路径 , httpcontext 实例);
例:
HttpContext hc = new BundleEntryHttpContext(context.getBundle(), "/web");
hs.registerResources("/jsp", "/", hc);
要想使用过滤器就,实例 Filer 类,然后通过 HttpService.registerServlet注册.
例:
Filter filter = new Filter() {
public void init(FilterConfig arg0) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)
throws IOException, ServletException {
System.out.println("in filter.doFilter()");
fc.doFilter(request, response);
}
};
hs.registerServlet("/jsp/hello.jsp",new FilterServletAdaptor(filter, null, jspServlet), null, null);
4. 建一个LoginServlet
在dopost里面用request.getParameter(“user”);取页面值.
进行逻辑处理,下面为跳转页面.
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/hello.jsp");
dispatcher.forward(request, response);
5. 在工程下建一个Plugin.xml文件<配置扩展点>
<plugin>
<extension point="org.eclipse.equinox.http.registry.resources">
<resource
alias="/web"
base-name="/web"/>
</extension>
<extension point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/login"
class="com.example.http.service.LoginServlet"/>
</extension>
</plugin>
6. 使用IDE把要用的BUNDLE选上,点Validate Bundle检验直到提示NO Problems
Were detected 然后点运行.