第一步、完成字典查询接口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; } } }
运行结果显示:
相关推荐
资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...
OSGI原理与最佳实践的完整版,共12章 第1 章OSGi 简介 第2 章OSGi 框架简介 第3 章基于Spring-DM 实现Petstore 第4 章基于Apache CXF 实现分布式Petstore 第5 章构建OSGI Bundle Repositor'y 第6 章OSGi 规范解读 ...
OSGi原理与最佳实践完整版,OSGI简介 OSGI框架简介 基于Spring-DM实现Petstore
OSGi原理与最佳实践.pdf .
OSGi原理与最佳实践(完整版下载),找了好久,绝对是完整版
OSGi原理与最佳实践例子 pdf书籍
InfoQ - OSGi原理与最佳实践精选版.zip
OSGi原理与最佳实践完整版_源码
OSGi原理与最佳实践(两章那种)与源代码
OSGi原理与最佳实践(完整版).PDF
本书基于作者多年使用OSGi的经验而编写,涵盖了...最后对OSGi知识进行深入讲解,通过对OSGi规范和实现框架(Equinox、Felix、Spring-DM和Apache CXF)的分析,以及最佳实践的介绍,帮助读者更好地掌握如何使用OSGi。
包括OSGi原理与最佳实践完整版、精简版以及书中的源代码
OSGi原理与最佳实践基于作者多年使用0SGi的经验而编写,涵盖了0SGi从/kfqN深入的知识体系,从OSGi的简介开始,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种...
本书基于作者多年使用0SGi的经验而编写,涵盖了0SGi从入门到深入的知识体系,从OSGi的简介开始,介绍0SGi的作用及基本概念;其后进入OSGi实战。
网上收集的OSGI资料. 包括: OSGi原理与最佳实践(精选版).pdf OSGI实战和源码.rar osgi进阶.pdf Introduce.OSGi.ppt OSGi.in.action.ppt r4.cmpn.pdf r4.core.pdf r4.enterprise.pdf