基于OSGi的JSF Web组件开发有关问题求解

基于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:
id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.3.2.R33x_v20080105
1 ACTIVE      org.eclipse.equinox.common_3.3.0.v20070426
3 ACTIVE      javax.servlet_2.4.0.v200706111738
4 ACTIVE      org.apache.commons.logging_1.0.4.v200706111724
5 ACTIVE      org.apache.commons.el_1.0.0.v200706111724
6 ACTIVE      org.apache.commons.logging_1.0.4
7 ACTIVE      javax.servlet.jsp_2.0.0.v200706191603
10 ACTIVE      org.eclipse.osgi.services_3.1.200.v20070605
11 ACTIVE      org.eclipse.equinox.launcher_1.0.1.R33x_v20080118
12 ACTIVE      org.eclipse.equinox.jsp.jasper.registry_1.0.0.v20070607
15 ACTIVE      org.eclipse.equinox.http.helper_1.0.0.qualifier
16 ACTIVE      org.mortbay.jetty_5.1.11.v200706111724
20 ACTIVE      org.eclipse.equinox.http.registry_1.0.1.R33x_v20071231
21 ACTIVE      org.eclipse.equinox.http.jetty_1.0.1.R33x_v20070816
22 ACTIVE      org.eclipse.equinox.registry_3.3.1.R33x_v20070802
23 ACTIVE      org.eclipse.equinox.http.servlet_1.0.1.R33x_v20070816
24 ACTIVE      org.eclipse.core.jobs_3.3.1.R33x_v20070709
58 ACTIVE      org.eclipse.equinox.jsp.jasper_1.0.100.qualifier
76 ACTIVE      org.apache.jasper_5.5.17.v200806031609
136 ACTIVE      MyJSFBundle_1.0.0
1 楼 danlley 2008-07-04  
不知道为什么附件上传不了,因此我在这里将Activator贴出来

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, "(objectClass=" + HttpService.class.getName() + ")");
	}

	public void stop(BundleContext arg0) throws Exception {
		try {
			unregisterServlet();
		} catch (Throwable t) {
			t.printStackTrace();
		}

		bc = null;
		ref = null;
	}

	public void serviceChanged(ServiceEvent event) {
		switch (event.getType()) {
		case ServiceEvent.REGISTERED:
			registerServlet();
			break;

		case ServiceEvent.UNREGISTERING:
			unregisterServlet();
			break;
		}
	}

	private void registerServlet() {
		if (ref == null) {
			ref = bc.getServiceReference(HttpService.class.getName());
		}

		if (ref != null) {
			final HttpService httpService = (HttpService) bc.getService(ref);
			try {
				HttpService http = (HttpService) bc.getService(ref);
				System.out.println("开始注册JspServlet。。。。");
				_servlet_jsp = new JspServlet(bc.getBundle(), "webapp");
				http.registerServlet("/webapp/*.jsp", _servlet_jsp, null, null);
				http.registerResources("/webapp", "webapp", null);
				System.out.println("JspServlet注册结束!");
				HttpServlet _servlet=null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void unregisterServlet() {
		if (ref != null) {
			try {
				final HttpService httpService = (HttpService) bc.getService(ref);
				httpService.unregister("/jsp-examples");
				httpService.unregister("/jsp-examples/*.jsp");
				System.out.println("已卸载用户登录验证web模块!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
2 楼 danlley 2008-07-04  
由于我在注册JspServlet的时候使用的是org.eclipse.equinox.jsp.jasper中的org.eclipse.equinox.jsp.jasper.JspServlet,而在jsp-api.jar中也有Apache的JspServlet,这里会不会是因为这两个包之间有冲突,我是不是需要对jsp-api.jar做些手术,不知道高手们有什么见解
3 楼 bottom 2008-07-04  
Apache的JspServlet是不是有同样的package name?

检查一下classpath中(包括系统classpath)有无同名(同package)class。

如果还不行,最后edit jsf_core.tld,把validator注释掉。看能不能过。
4 楼 danlley 2008-07-07  
我当时估计可能是由于JsfServlet在加载的过程中出问题了,因此采用了手动加载JsfServlet的方式, 具体操作法如下:
1.新增一个ServletContextListenerServletAdaptor类,具体内容如下:
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);
		}
	}

}



这样一来,我又遇到了新的问题:

Nested Exception:
java.lang.NoClassDefFoundError: Could not initialize class org.apache.jasper.compiler.JspRuntimeContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.sun.faces.config.ConfigureListener.isJspTwoOne(ConfigureListener.java:1498)
at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:1542)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:403)
at org.danlley.osgi.jsf.other.ServletContextListenerServletAdaptor.init(ServletContextListenerServletAdaptor.java:30)
at org.eclipse.equinox.http.helper.ContextPathServletAdaptor.init(ContextPathServletAdaptor.java:32)
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.init(ServletRegistration.java:64)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.registerServlet(ProxyServlet.java:142)
at org.eclipse.equinox.http.servlet.internal.HttpServiceImpl.registerServlet(HttpServiceImpl.java:50)
at org.danlley.osgi.jsf.other.Activator$HttpServiceTracker.addingService(Activator.java:78)
at org.osgi.util.tracker.ServiceTracker$Tracked.trackAdding(ServiceTracker.java:1064)
at org.osgi.util.tracker.ServiceTracker$Tracked.trackInitialServices(ServiceTracker.java:926)
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:330)
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:274)
at org.danlley.osgi.jsf.other.Activator.start(Activator.java:32)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:999)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:993)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:974)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:346)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:260)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:252)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:260)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:150)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:300)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:285)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:221)
at java.lang.Thread.run(Thread.java:619)



但事实上,这个类我已经加载到工程的classpath当中了,不知道是不是还有什么地方没有处理

5 楼 ghy200692162 2012-02-28  
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注册结束!"); 

想请问 httpService.registerServlet("/page" + "/*.jsp", adaptedJspServlet, null, commonContext);  这句话是做什么?/page" + "/*.jsp这个代表什么意义?