osgi-扩展bluedavy提供的“简单方式下的留言板”遇到的问题

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

[/list]

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的时候,感觉很怪异,不知道如何能简化。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值