`

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

    博客分类:
  • OSGi
阅读更多
最近一直在研究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
分享到:
评论
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这个代表什么意义?
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当中了,不知道是不是还有什么地方没有处理

3 楼 bottom 2008-07-04  
Apache的JspServlet是不是有同样的package name?

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

如果还不行,最后edit jsf_core.tld,把validator注释掉。看能不能过。
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做些手术,不知道高手们有什么见解
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();
			}
		}
	}
}

相关推荐

    基于OSGi和Spring开发Web应用.doc

    "基于OSGi和Spring开发Web应用" OSGi(Open Service Gateway Initiative)是一种面向服务的框架,能够提供动态模块部署和管理的能力。Spring 是一个轻量级的J2EE开发框架,特点是面向接口编程和非侵入式的依赖注入...

    基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip

    基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip ...

    基于OSGi的Web应用开发系列(全).pdf

    **基于OSGi的Web应用开发**是现代软件开发中的一种技术实践,它允许开发者构建模块化、可扩展和可维护的Web应用。OSGi(Open Service Gateway Initiative)是一种开放的标准,提供了一种服务导向的、模块化的Java...

    基于OSGi和Spring开发Web应用

    在基于OSGi和Spring开发Web应用的过程中,首先需要指定Target Platform到所有需要用到的bundle包目录中,这是为了确保开发环境能够识别并正确加载所有必要的组件。接下来,创建一个服务接口bundle,定义接口类及其...

    基于OSGI的Web开发例子

    在OSGi环境下开发Web应用的好处在于,我们可以独立更新和管理各个组件,避免了传统的Web应用中常见的类加载冲突问题,同时也简化了依赖管理。通过使用Equinox这样的强大框架,我们可以更好地利用OSGi的优势,实现...

    行业分类-设备装置-基于OSGi的Web应用开发平台权限管理自适应设计方法.zip

    在基于OSGi的Web应用开发平台中,权限管理涉及到用户、角色和资源的控制。传统的权限管理通常采用固定的角色-权限模型,但在自适应设计中,系统需要根据用户的行为、环境或者业务需求动态调整权限分配。这通常通过...

    基于OSGi和Spring开发Web应用教程

    在基于OSGi和Spring开发Web应用中,OSGi(Open Services Gateway Initiative)是一个开放标准,用于创建模块化Java应用程序。它允许开发者将应用程序分解为独立的模块,称为bundle,每个bundle都包含自己的类路径、...

    基于VirgoServer进行Spring Osgi Web开发

    在本文中,我们将深入探讨如何基于VirgoServer进行Spring Osgi Web开发,这是一个涉及OSGi容器、Spring框架和Web应用程序的集成技术。首先,我们需要确保拥有正确的开发环境和工具,包括Spring Tool Suite (STS),...

    基于OSGi和Spring开发Web应用的Demo

    本篇将详细探讨如何基于OSGi和Spring来开发Web应用,并通过提供的代码示例进行解析。 首先,OSGi是一种模块化系统,它允许Java应用程序以组件的形式进行组织和管理。这种模块化特性使得开发者可以独立地更新、替换...

    基于osgi整合ssm框架注解开发

    SSM(Spring、SpringMVC、MyBatis)框架是Java Web开发中常见的技术栈,而OSGi(Open Service Gateway Initiative)则是一种模块化系统,主要用于解决Java应用程序的动态性、可扩展性和版本管理问题。将SSM框架与...

    基于 OSGi 和 Spring 开发 Web 应用

    基于 OSGi 和 Spring 开发 Web 应用

    基于OSGi和Spring开发企业级Web应用

    ### 基于OSGi和Spring开发企业级Web应用 #### OSGi与Spring结合的重要性 随着企业级应用复杂度的不断提升,对于软件架构的要求也越来越高。为了更好地满足这一需求,许多开发团队开始关注并采用OSGi(Open Service...

    基于 OSGi 的面向服务的组件编程

    基于 OSGi 的面向服务的组件编程呆呆呆呆呆呆地

    基于java的OSGi 分布式通讯组件 R-OSGi.zip

    【标题】:“基于Java的OSGi分布式通讯组件R-OSGi” 在Java开发领域,OSGi(Open Service Gateway Initiative)是一种模块化系统和Java服务框架,它允许在单个Java虚拟机(JVM)内动态地发现、组合和管理软件模块。...

Global site tag (gtag.js) - Google Analytics