OSGi 聯絡服務調用之Equinox

來源:互聯網
上載者:User

例子參照infoQ的一個電子書上的 要下載網上很多,不知道是我看到的是閹割版的還是什麼,那本電子書上說的例子不詳細代碼不全。、

經過摸索現在給出一個demo。

 

這個是例子說的是過程是使用者通過搜尋區搜尋一個詞語。這次搜尋分為4個bundle

1 使用者介面demo

2 提供了一個介面bundle ,後面所有的服務都實現他的介面

3 本地服務bundle

4 遠程服務bundle

每個工程都是一個bundle,如

 

 輸入http://localhost/page/index.html  訪問例子

 下載例子網址:

下面只是代碼,具體操作可以參考:

demo工程如下:http://l4.yunpan.cn/lk/QMhEEM83g5Tjk

 

 Activator.java

package demo;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import servlet.tranker.HttpServletTranker;public class Activator implements BundleActivator {public static BundleContext context;HttpServletTranker httpServiceTracker;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;httpServiceTracker = new HttpServletTranker(context);  httpServiceTracker.open();  }/* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */public void stop(BundleContext bundleContext) throws Exception {Activator.context = null;httpServiceTracker.close();}}

index.html

<!DOCTYPE html><!-- saved from url=(0021)http://www.baidu.com/ --><html><script type="text/javascript"src="chrome-extension://kajfghlhfkcocafkcjlajldicbikpgnp/catcher.js"></script><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body huaban_screen_capture_injected="true" youdao="bind"><form name="f" action=/demo><span class="s_ipt_wr"><input type="text" name="word"id="kw" maxlength="100" class="s_ipt" autocomplete="off"></span><input type="submit" value="so 一下就知道" id="su"class="s_btn"></span><div id="sd_1355486068913" style="display: none;"></div></form></body></html>

QueryServlet.java

package servlet;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import localquery.imp.LocalDictQueryImp;import org.osgi.framework.BundleContext;import remotequery.imp.RemoteDictQueryImp;import demo.Activator;import dictquery.interf.DictQueryService;public class QueryServlet extends HttpServlet{private static final long serialVersionUID = -386158027672842993L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=UTF-8");DictQueryService localservice=(DictQueryService) Activator.context.getService(Activator.context.getServiceReference(LocalDictQueryImp.class.getName()));DictQueryService remoteservice=(DictQueryService) Activator.context.getService(Activator.context.getServiceReference(RemoteDictQueryImp.class.getName()));if(req.getParameter("word")!=null && localservice!=null){String local=localservice.query(req.getParameter("word").toString());String remote=remoteservice.query(req.getParameter("word").toString());OutputStream out=resp.getOutputStream();out.write(("word means :<br/>local:"+local).getBytes());out.write(("<br/>remote:"+remote).getBytes());out.close();}else{OutputStream out=resp.getOutputStream();out.write(("sorry no this word!!!").getBytes());out.close();}System.out.println("word:"+req.getParameter("word"));}}

HttpServletTranker.java:

package servlet.tranker;import javax.servlet.ServletException;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import org.osgi.service.http.HttpService;import org.osgi.service.http.NamespaceException;import org.osgi.util.tracker.ServiceTracker;import servlet.QueryServlet;public class HttpServletTranker extends ServiceTracker{public HttpServletTranker(BundleContext context){super(context, HttpService.class.getName(), null);}/** * http://localhost/page/index.html * 使用這個網址就可以查看 * **/@Overridepublic Object addingService(ServiceReference reference){HttpService service = context.getService(reference);try{service.registerResources("/page/index.html", "/page/index.html",null);service.registerServlet("/demo", new QueryServlet(), null, null);} catch (NamespaceException e){e.printStackTrace();} catch (ServletException e){e.printStackTrace();}// TODO Auto-generated method stubreturn super.addingService(reference);}@Overridepublic void removedService(ServiceReference reference, Object service){HttpService httpService = (HttpService) service;httpService.unregister("/page/index.html");httpService.unregister("/demo");super.removedService(reference, service);}}

 

dictquery工程如下:
dictquery.java

package dictquery;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator { private static BundleContext context; 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; } /*  * (non-Javadoc)  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)  */ public void stop(BundleContext bundleContext) throws Exception {  Activator.context = null; }}

DictQueryService.java:

package dictquery.interf;public interface DictQueryService{public String query(String word);}

 

localquery工程:

Activator.java:

package localquery;import localquery.imp.LocalDictQueryImp;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceRegistration;public class Activator implements BundleActivator {private static BundleContext context;private  ServiceRegistration sr=null;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=bundleContext.registerService(LocalDictQueryImp.class.getName(), new LocalDictQueryImp(), 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();}}

LocalDictQueryImp.java:

package localquery.imp;import dictquery.interf.DictQueryService;public class LocalDictQueryImp implements DictQueryService{@Overridepublic String query(String word){// TODO Auto-generated method stubreturn "local你猜猜,"+word+" 是什麼意思!!!";}}

remotedictquery工程如下:

Activator.java

package remotedictquery;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceRegistration;import remotequery.imp.RemoteDictQueryImp;import dictquery.interf.DictQueryService;public class Activator implements BundleActivator {private static BundleContext context;private  ServiceRegistration sr=null;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=bundleContext.registerService(RemoteDictQueryImp.class.getName(), new RemoteDictQueryImp(), 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();}}

RemoteDictQueryImp.java:

package remotequery.imp;import dictquery.interf.DictQueryService;public class RemoteDictQueryImp implements DictQueryService{@Overridepublic String query(String word){// TODO Auto-generated method stubreturn "remote 就不告訴你,"+word+" 是什麼意思!!!";}}

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.