基于OSGi的JSF Web组件开发问题求解
最近一直在研究OSGi Web组建开发,跑了一些小程序中间出现了不少问题,其中很多是由于对OSGi理解不够深入,当然还有些问题,目前也没有解决的办法,因此贴出来想让大伙帮帮忙。
具体情况如下:
1.拿纯Java代码编写的JSP页面进行测试,结果一切正常,说明JspServlet已经成功加载。
2.编写简单的JSF页面进行测试,结果出现错误提示,具体如下:
osgi> Jul 4, 2008 11:58:22 AM org.mortbay.jetty.servlet.ServletHandler handle
WARNING: EXCEPTION
org.apache.jasper.JasperException: Unable to read TLD "META-INF/jsf_core.tld" from JAR file "bundleentry://136/src/main/java/webapp/WEB-INF/lib/jsf-impl-1.2_04-p02.jar": org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: com.sun.faces.taglib.jsf_core.CoreValidator
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.eclipse.equinox.jsp.jasper.JspServlet.service(JspServlet.java:112)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleRequest(ServletRegistration.java:90)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:111)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(HttpServerManager.java:288)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
at org.mortbay.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:677)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:245)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
起初我怀疑可能是不是那个tld文件已经损毁了,但是当我将整个工程转换成普通Web工程在Tomcat下运行一切正常,因此,我判断,首先可以保证tld文件是正常的。
后来在网上查阅了很多这方面的资料(相关资料确实很少,而且问多答少),有些人说需要在servlet-api-2.5.jar和jsp-api.jar之间做出选择,删掉其中一个jar就可以了,而事实上是这两个jar对我进行OSGi组建开发都至关重要,因此无法删除。
注: 关于代码详情,见附件。
下面是相关用到的约束bundle:
idState Bundle
0ACTIVE org.eclipse.osgi_3.3.2.R33x_v20080105
1ACTIVE org.eclipse.equinox.common_3.3.0.v20070426
3ACTIVE javax.servlet_2.4.0.v200706111738
4ACTIVE org.apache.commons.logging_1.0.4.v200706111724
5ACTIVE org.apache.commons.el_1.0.0.v200706111724
6ACTIVE org.apache.commons.logging_1.0.4
7ACTIVE javax.servlet.jsp_2.0.0.v200706191603
10ACTIVE org.eclipse.osgi.services_3.1.200.v20070605
11ACTIVE org.eclipse.equinox.launcher_1.0.1.R33x_v20080118
12ACTIVE org.eclipse.equinox.jsp.jasper.registry_1.0.0.v20070607
15ACTIVE org.eclipse.equinox.http.helper_1.0.0.qualifier
16ACTIVE org.mortbay.jetty_5.1.11.v200706111724
20ACTIVE org.eclipse.equinox.http.registry_1.0.1.R33x_v20071231
21ACTIVE org.eclipse.equinox.http.jetty_1.0.1.R33x_v20070816
22ACTIVE org.eclipse.equinox.registry_3.3.1.R33x_v20070802
23ACTIVE org.eclipse.equinox.http.servlet_1.0.1.R33x_v20070816
24ACTIVE org.eclipse.core.jobs_3.3.1.R33x_v20070709
58ACTIVE org.eclipse.equinox.jsp.jasper_1.0.100.qualifier
76ACTIVE org.apache.jasper_5.5.17.v200806031609
136ACTIVE MyJSFBundle_1.0.0
package org.danlley.osgi.jsf;import javax.servlet.http.HttpServlet;import org.eclipse.equinox.jsp.jasper.JspServlet;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceEvent;import org.osgi.framework.ServiceListener;import org.osgi.framework.ServiceReference;import org.osgi.service.http.HttpService;public class Activator implements BundleActivator, ServiceListener {private BundleContext bc;private ServiceReference ref;private JspServlet _servlet_jsp = null;public void start(BundleContext context) throws Exception {System.out.println("开始启动bundle。。。。");bc = context; registerServlet();context.addServiceListener(this, "(objectname="code">package org.danlley.osgi.jsf.other;import java.io.IOException;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class ServletContextListenerServletAdaptor implements Servlet {private ServletConfig config;private ServletContextListener listener;private Servlet delegate;private ClassLoader jspLoader;public ServletContextListenerServletAdaptor(ServletContextListener listener, Servlet delegate, ClassLoader jspLoader) {this.listener = listener;this.delegate = delegate;this.jspLoader = jspLoader;}public void init(ServletConfig config) throws ServletException {this.config = config;ClassLoader original = Thread.currentThread().getContextClassLoader();try {Thread.currentThread().setContextClassLoader(jspLoader);listener.contextInitialized(new ServletContextEvent(config.getServletContext()));delegate.init(config);} finally {Thread.currentThread().setContextClassLoader(original);}}public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {ClassLoader original = Thread.currentThread().getContextClassLoader();try {Thread.currentThread().setContextClassLoader(jspLoader);delegate.service(req, resp);} finally {Thread.currentThread().setContextClassLoader(original);}}public void destroy() {ClassLoader original = Thread.currentThread().getContextClassLoader();try {Thread.currentThread().setContextClassLoader(jspLoader);delegate.destroy();listener.contextDestroyed(new ServletContextEvent(config.getServletContext()));config = null;} finally {Thread.currentThread().setContextClassLoader(original);}}public ServletConfig getServletConfig() {return config;}public String getServletInfo() {return "";}}
2.手动加载JsfServlet,代码如下:
package org.danlley.osgi.jsf.other;import java.util.Dictionary;import java.util.Hashtable;import javax.faces.webapp.FacesServlet;import javax.servlet.Servlet;import org.eclipse.equinox.http.helper.BundleEntryHttpContext;import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;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;import com.sun.faces.config.ConfigureListener;public class Activator implements BundleActivator {private ServiceTracker httpServiceTracker;String jspContext = "/jsps";String jspFolder = "/page";JspServlet _jspServlet = null;public void start(BundleContext context) throws Exception {System.out.println("开始启动bundle。。。。");httpServiceTracker = new HttpServiceTracker(context);httpServiceTracker.open();}public void stop(BundleContext context) throws Exception {httpServiceTracker.open();}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 {System.out.println("开始注册JspServlet。。。。");HttpContext commonContext = new BundleEntryHttpContext(context.getBundle(), "/page");httpService.registerResources("/page", "/", commonContext);_jspServlet = new JspServlet(context.getBundle(), jspFolder);Servlet adaptedJspServlet = new ContextPathServletAdaptor(_jspServlet, "/page");httpService.registerServlet("/page" + "/*.jsp", adaptedJspServlet, null, commonContext);System.out.println("JspServlet注册结束!");// javax.faces.context.FacesContext _jsf_c=null;// javax.faces.context.FacesContextFactory fac=new FacesContextFactory(_jsf_c);System.out.println("开始注册JsfServlet。。。。");// try {// java.util.Dictionary<String, String> arg=new Hashtable<String, String>();// arg.put("servlet-name", "Faces Servlet");// arg.put("display-name", "Faces Servlet");// arg.put("servlet-class", FacesServlet.class.getName());// arg.put("load-on-startup", "1");// Servlet adaptedJsfServlet = new ContextPathServletAdaptor(new FacesServlet(),// jspContext + "/jsf");// httpService.registerServlet(jspContext + "/jsf/*.jsp", adaptedJsfServlet, arg,// commonContext);// } catch (RuntimeException e) {// e.printStackTrace();// }try {Dictionary<String, String> initparams = new Hashtable<String, String>();initparams.put("servlet-name", "Faces Servlet");Servlet adaptedFacesServlet = new ServletContextListenerServletAdaptor(new ConfigureListener(), new FacesServlet(),_jspServlet.getJspLoader());adaptedFacesServlet = new ContextPathServletAdaptor(adaptedFacesServlet, "/page");httpService.registerServlet("/page/jsf/*.jsp", adaptedFacesServlet, initparams, commonContext);} catch (RuntimeException e) {e.printStackTrace();}System.out.println("JsfServlet注册结束!");} catch (Exception e) {e.printStackTrace();}return httpService;}public void removedService(ServiceReference reference, Object service) {final HttpService httpService = (HttpService) service;httpService.unregister(jspContext);httpService.unregister(jspContext + "/*.jsp");super.removedService(reference, service);}}}