package com.zyg.osgi.translateclient.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.framework.BundleContext; public class TranslateServiceServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public TranslateServiceServlet(BundleContext bundleContext) { // TODO Auto-generated constructor stub } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }我们先不写其中的逻辑,我们先写一个最后跳转到的静态页面(html)。
<HTML> <HEAD> <TITLE>OSGi Dictionary Query Demo</TITLE> <meta http-equiv="Content-Type" content="text/html;charset=GBK" /> <script> function queryWord(){ if(document.frmCalc.query_word.value==""){ alert("必须输入单词"); document.frmCalc.query_word.focus(); return; } document.frmCalc.submit(); } </script> </HEAD> <BODY> <table style="width:100px;border:1px solid;height:100px"> <form name="frmCalc" method="post" action="/servlet/translateServlet"> <tr bgcolor="#C0C0C0"> <td colspan="4" align="center">翻译助手</td> </tr> <tr> <td><input type="text" name="query_word" size="20"></td> <td><input type=button name=btnSubmit value="查询" onclick="queryWord();"></td> </tr> </form> </table> </BODY> </HTML>
<servlet> <servlet-name>Add</servlet-name> <servlet-class>cn.edu.hpu.test.add</servlet-class> </servlet> <servlet-mapping> <servlet-name>Add</servlet-name> <url-pattern>/add</url-pattern> </servlet-mapping>但是我们的plug-in项目是没有这种配置文件的,所以我们要来解决这个问题。
package com.zyg.osgi.translateclient; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceEvent; import org.osgi.framework.ServiceListener; public class Activator implements BundleActivator,ServiceListener { private static BundleContext context; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; } public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } @Override public void serviceChanged(ServiceEvent arg0) { // TODO Auto-generated method stub } }
package com.zyg.osgi.translateclient; import javax.servlet.Servlet; import javax.servlet.ServletException; 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 org.osgi.service.http.NamespaceException; import com.zyg.osgi.translateclient.servlet.TranslateServiceServlet; public class Activator implements BundleActivator,ServiceListener { private static BundleContext context; private ServiceReference serviceReference; private Servlet servlet; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; //在TranslateServiceServlet中创建一个构造方法,将bundleContext传进去 servlet=new TranslateServiceServlet(bundleContext); //注册Servlet registerServlet(); } public void stop(BundleContext bundleContext) throws Exception { //注销Servlet等资源 unRegisterServlet(); serviceReference=null; Activator.context = null; } private void registerServlet() { if(null==serviceReference){ //通过上下文获取服务对象的“引用”(需要通过MANIFEST.MF引入org.osgi.service.http包) serviceReference=context.getServiceReference(HttpService.class); } if(null!=serviceReference){ //得到http服务对象 HttpService httpService=context.getService(serviceReference); if(httpService!=null){ try { //注册Servlet //四个参数分别是:(映射地址,Servlet类本身,Dictionary,上下文) httpService.registerServlet("/servlet/translateServlet", servlet, null, null); //注册静态资源(html等文件)位置 //四个参数分别是(访问地址,静态文件所在路径,上下文) httpService.registerResources("/page", "pages", null); System.out.println("翻译助手服务已启动成功,请通过/page/translate.html访问!"); } catch (ServletException e) { e.printStackTrace(); } catch (NamespaceException e) { e.printStackTrace(); } } } } private void unRegisterServlet() { if(null!=serviceReference){ //得到http服务对象 HttpService httpService=context.getService(serviceReference); if(httpService!=null){ try { //注销Servlet //四个参数分别是:映射地址 httpService.unregister("/servlet/translateServlet"); //注销静态资源(html等文件)位置 //参数是访问地址 httpService.unregister("/page"); System.out.println("翻译助手服务已停用成功,谢谢使用!"); } catch (Exception e) { e.printStackTrace(); } } } } @Override public void serviceChanged(ServiceEvent event) { // TODO Auto-generated method stub switch (event.getType()) { case ServiceEvent.REGISTERED: registerServlet(); break; case ServiceEvent.UNREGISTERING: unRegisterServlet(); break; default: break; } } }
package com.zyg.osgi.translateclient.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; 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 com.zyg.osgi.translateasslt.service.TranslateService; public class TranslateServiceServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private BundleContext context; public TranslateServiceServlet(BundleContext bundleContext) { this.context=bundleContext; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1、取得用户传来的英文单词 String queryWord=req.getParameter("query_word"); //2、调用方法查找中文含义 //获取服务 TranslateService translateService=null; ServiceReference serviceRef=context.getServiceReference(TranslateService.class.getName()); if(null!=serviceRef){ translateService=(TranslateService)context.getService(serviceRef); } //3、返回结果给用户 resp.setContentType("text/html;charset=GBK"); PrintWriter writer=resp.getWriter(); if(translateService==null){ writer.println("没有开放翻译服务!"); writer.close(); return; } String result=translateService.translate(queryWord); writer.println("结果"+result); writer.close(); return; } }其中需要通过MANIFEST.MF引入com.zyg.osgi.translateasslt.service包。
package com.zyg.osgi.localtranslateasslt; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import com.zyg.osgi.localtranslateasslt.impls.TranslateServiceLocalImpl; import com.zyg.osgi.translateasslt.service.TranslateService; public class Activator implements BundleActivator { private static BundleContext context; private ServiceRegistration<TranslateService> sr; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; //注册Service服务 sr=bundleContext.registerService(TranslateService.class, new TranslateServiceLocalImpl(), null); System.out.println("本地查询服务已启动!"); } public void stop(BundleContext bundleContext) throws Exception { ServiceReference serviceRef=context.getServiceReference(TranslateService.class.getName()); bundleContext.ungetService(serviceRef); System.out.println("本地查询服务已停止!"); Activator.context = null; } }
package com.zyg.osgi.remotetranslateasslt; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import com.zyg.osgi.remotetranslateasslt.impls.TranslateServiceRemoteImpl; import com.zyg.osgi.translateasslt.service.TranslateService; public class Activator implements BundleActivator { private static BundleContext context; private ServiceRegistration<TranslateService> sr; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; //注册Service服务 sr=bundleContext.registerService(TranslateService.class, new TranslateServiceRemoteImpl(), null); System.out.println("远程查询服务已启动!"); } public void stop(BundleContext bundleContext) throws Exception { ServiceReference serviceRef=context.getServiceReference(TranslateService.class.getName()); bundleContext.ungetService(serviceRef); System.out.println("远程查询服务已停止!"); Activator.context = null; } }
上面是最原生的方式来注册服务等,我们下次会介绍依赖注入的方式来对我们的这个Web项目进行改造。
转载请注明出处:http://blog.csdn.net/acmman/article/details/50916011