• 在BulletinListModule中仿照右侧左上角的扩展点增加一个左侧扩展点,用于显示“查询”字样
  • 根据上述扩展点,在新的bundle中实现扩展
  • 通过DS描述component,在本bundle中,有两个component,一个为进入查询页面进入的action,另一个是查询功能本身。


package com.company.jncz.query;

import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.equinox.http.helper.BundleEntryHttpContext;
import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.osgi.framework.Bundle;
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.service.http.NamespaceException;
import org.osgi.util.tracker.ServiceTracker;

import cn.org.osgi.bulletin.po.Bulletin;
import cn.org.osgi.module.hibernate.service.CommonDaoService;
import cn.org.osgi.opendoc.bulletin.mvc.SimpleMVCFrameworkActivator;
import cn.org.osgi.opendoc.bulletin.service.WebCommand;

public class QueryBulletinCommand implements WebCommand{
	private CommonDaoService service;
	
	private HttpService hs;
	
	private HttpServiceTracker httpServiceTracker;

	// ----------------------------------------------Public Method
	
	public void setService(CommonDaoService service){
		this.service=service;
	}
	
	public void unsetService(CommonDaoService service){
		if(this.service!=service)
			return;
		this.service=null;
	}
	
	
	public void unsetHs() {
		if(this.hs!=hs){
			return;
		}
		this.hs = null;
		httpServiceTracker.close();
	}

	public void setHs(HttpService hs) {
		this.hs = hs;
		httpServiceTracker = new HttpServiceTracker(SimpleMVCFrameworkActivator.getContext());
		httpServiceTracker.open();
	}
	
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		DetachedCriteria dc = DetachedCriteria.forClass(Bulletin.class);
		dc.add(Restrictions.eq("author", name));
		dc.add(Restrictions.like("content", content, MatchMode.ANYWHERE));
		System.out.println("before query");
		List list = service.queryDynamic(dc);
		System.out.println("查询之后:"+list.size());
		request.setAttribute("list", list);
		request.getRequestDispatcher("/pagess/queryresult.jsp").forward(request, response);
		return "";
	}

	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 {
				Bundle[] bs = this.context.getBundles();
				Bundle b = null;
				for (int i = 0; i < bs.length; i++) {
					System.out.println(bs[i].getBundleContext());
					if(bs[i].getSymbolicName().equals("QueryBundleModule")){
						b = bs[i];
//						System.out.println("QueryBundleModule");
						break;
					}
				}
				HttpContext commonContext = new BundleEntryHttpContext(b, "/page"); //$NON-NLS-1$
				hs.registerResources("/pagess", "/", commonContext); //$NON-NLS-1$ //$NON-NLS-2$

				Servlet adaptedJspServlet = new ContextPathServletAdaptor(new JspServlet(b, "/page"), "/pagess");  //$NON-NLS-1$//$NON-NLS-2$
				hs.registerServlet("/pagess/*.jsp", adaptedJspServlet, null, commonContext); //$NON-NLS-1$
				System.out.println("映射完毕");
			} catch (NamespaceException e) {
				e.printStackTrace();
			} catch (ServletException e) {
				e.printStackTrace();
			}
			return httpService;
		}

		public void removedService(ServiceReference reference, Object service) {
			final HttpService httpService = (HttpService) service;
			httpService.unregister("/pagess"); //$NON-NLS-1$
			httpService.unregister("/pagess/*.jsp"); //$NON-NLS-1$
			super.removedService(reference, service);
		}			
	}
}


在这个过程中碰到了一个比较郁闷的问题,
BundleEntryHttpContext和JspServlet都需要bundle,而且必须是当前页面所在的bundle,所以,通过
Bundle[] bs = this.context.getBundles();
				Bundle b = null;
				for (int i = 0; i < bs.length; i++) {
					System.out.println(bs[i].getBundleContext());
					if(bs[i].getSymbolicName().equals("QueryBundleModule")){
						b = bs[i];
						break;
					}
				}

来获取bundle,而QueryBundleModule就是当前的页面所在bundle的name。
这样才可以,达到映射的正确性。
另外一点,需要注意的是,build.properties需要经常查看,如果自己新加了什么东西,那么一定要多看看,看是否勾选中了,否则导出的bundle不会包含变动,造成很多的诡异的问题。
由于不想使用Activator,所以获取bundle的时候,感觉很怪异,不知道如何能简化。
评论
发表评论

您还没有登录,请登录后发表评论

jncz
搜索本博客
最近加入圈子
存档
最新评论