`
fokman
  • 浏览: 238800 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

OSGi原理与最佳实践——字典服务开发

 
阅读更多

第一步、完成字典查询接口Bundle工程
创建一个名为DictQuery的Plugin工程


 在工程的

com.grocal.dictquery.query

package下面创建一个接口

package com.grocal.dictquery.query;

public interface QueryService {
	
	/**
	 * 根据需要的单词返回查询结果
	 * @param word 需要查询的单词
	 * @return 返回的结果
	 */
	String query(String word);

}

 这个bundle是用来提供字典查询服务,所以Activator不需要做任何改动。为了能够让其它的bundle调用这个bundle,需要将这个bundle导出。

打开MANIFEST.MF文件,选择runtime标签页,如下图所示:

然后点击exproted packages中的Add,在弹出来的窗口中选择刚才所创建的package

com.grocal.dictquery.query

点击保存完成导出操作。

此时我们完成了字典查询服务bundle接口开发,接下来我们完成本地字典查询服务的开发

 第二步、实现本地字典查询Bundle

1.按照之前创建工程的步骤,创建一个名字为LocalDictQuery的Plugin工程

2.导入字典查询接口,并实现一个真实的字典查询类

接下来我们编写LocalDictQueryServiceImpl代码,这个是实现了QueryService接口的一个类。

package com.grocal.localdictquery.local.impl;

import java.util.concurrent.ConcurrentHashMap;

import com.grocal.dictquery.query.QueryService;

public class LocalDictQueryServiceImpl implements QueryService {

	private static final ConcurrentHashMap<String, String> dict = new ConcurrentHashMap<String, String>();

	static {
		dict.put("china", "中国");
		dict.put("USA", "美国");
	}

	@Override
	public String query(String word) {
		System.out.println("LocalDictQueryServiceImpl query has called");
		String result = dict.get(word);
		if(null==result){
			result = "U/A";
		}
		return result;
	}

	
}

 编写Activator代码注册Bundle到OSGi框架中。

package com.grocal.localdictquery;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.grocal.dictquery.query.QueryService;
import com.grocal.localdictquery.local.impl.LocalDictQueryServiceImpl;

public class Activator implements BundleActivator {

	private static BundleContext context;
	private ServiceRegistration<?> sr;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.
	 * BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		sr = context.registerService(QueryService.class, new LocalDictQueryServiceImpl(), null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		sr.unregister();
	}

}

 完成字典查询响应Bundle,由于OSGi并没有Web 服务器的bundle,就不能像web应用一样直接将程序部署到web服务器,要通过HttpService将Servlet及资源文件(css,图片)进行注册才可以访问,下面我们建立一个查询Servlet。在src目录下面建立一个page目录,并编写dictquery.html
首先import必要的包

项目结构如图所示:

Activtor 和DictServlet的代码如下:

package com.grocal.dictweb;

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;

import com.grocal.dictweb.servlet.DictServlet;

public class Activator implements BundleActivator, ServiceListener {

	private static BundleContext context;
	private ServiceReference<?> serviceReference;
	private DictServlet servlet;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.
	 * BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		servlet = new DictServlet(context);
		registerServlet();
	}

	private void registerServlet() {
		if (null == serviceReference) {
			serviceReference = context.getServiceReference(HttpService.class);
		}
		if (null != serviceReference) {
			HttpService httpService = (HttpService) context.getService(serviceReference);
			if (null != httpService) {
				servlet.setHttpService(httpService);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		unregisterServlet();
		serviceReference = null;
		servlet = null;
	}

	private void unregisterServlet() {
		if (null != serviceReference) {
			HttpService httpService = (HttpService) context.getService(serviceReference);
			if(null!=httpService){
				servlet.unsetHttpService(httpService);
			}
		}
	}

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

		}
	}

}

 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 BundleContext context;
	private QueryService queryService;
	private HttpService httpService;

	public DictServlet(BundleContext context) {
		this.context = context;
	}

	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();
		ServiceReference<?> serviceRef = context.getServiceReference(QueryService.class.getName());
		if (null != serviceRef) {
			queryService = (QueryService) context.getService(serviceRef);
		}
		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;
		}
	}

}

 运行结果显示:

 
Result is 中国 
 
 
  • 大小: 245.3 KB
  • 大小: 264.5 KB
  • 大小: 133.9 KB
  • 大小: 339.6 KB
  • 大小: 238.3 KB
  • 大小: 185.2 KB
  • 大小: 199.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics