OSGi原理与最佳实践——字典服务declarative Service实现

一、重构DictQueryProject 删除Activator类

二、删除LocalDictQuery里面的Activator类,新建文件夹OSGI-INF,在此文件夹里面创建一个component.xml文件。

 

 修改component.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<component name="DictQueryService">
	<implementation
		class="com.grocal.localdictquery.local.impl.LocalDictQueryServiceImpl" />
	<service>
		<provide interface="com.grocal.dictquery.query.QueryService" />
	</service>
</component>
 implementation标记:指定接口的实现类
service标记:接口的定义类
同理修改DictWeb里面的DictServlet,删除BoundContext相关的应用,修改后的DictServlet如下:
package com.grocal.dictweb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

import com.grocal.dictquery.query.QueryService;

public class DictServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private QueryService queryService;
	private HttpService httpService;


	public void setHttpService(HttpService httpService) {
		this.httpService = httpService;
		try {
			httpService.registerServlet("/dict/queryServlet", this, null, null);
			httpService.registerResources("/dict/page", "page", null);
			System.out.println("注册service /dict/page/dictquery.html");
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (NamespaceException e) {
			e.printStackTrace();
		}
	}

	public void unsetHttpService(HttpService httpService) {
		if (httpService != this.httpService) {
			return;
		}
		this.httpService.unregister("/dict/queryServlet");
		this.httpService.unregister("/dict/page");
		System.out.println("取消service注册");
		this.httpService = null;
	}

	public void setQueryService(QueryService queryService) {
		this.queryService = queryService;
	}

	public void unsetQueryService(QueryService queryService) {
		if (queryService != this.queryService) {
			return;
		}
		this.queryService = null;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
		doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
		String queryWord = req.getParameter("query_word");
		resp.setContentType("text/html;charset=utf-8");
		ServletOutputStream output = resp.getOutputStream();
		if (queryService == null) {
			output.println("No available dictquery service");
			output.close();
			return;
		}

		try {
			output.println("Result is " + queryService.query(queryWord));
			output.close();
			return;
		} catch (Exception e) {
			output.println("Error occurs");
			output.println(e.toString());
			output.close();
			return;
		}
	}

}
 component的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<component name="DictQueryServlet">
	<implementation class="com.grocal.dictweb.servlet.DictServlet" />
	<reference name="QueryService" interface="com.grocal.dictquery.query.QueryService" bind="setQueryService"
	 unbind="unsetQueryService" policy="dynamic" cardinality="0..1"/>
	<reference name="HttpService" interface="org.osgi.service.http.HttpService" bind="setHttpService" 
	 unbind="unsetHttpService" policy="dynamic"/>
</component>
 运行工程,在这里值得注意的一点是需要加入另外两个bundle,以及设置bundle的start level 为2
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值