例子參照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+" 是什麼意思!!!";}}