例子参照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;
@Override
protected 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
* 使用这个网址就可以查看
* **/
@Override
public 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 stub
return super.addingService(reference);
}
@Override
public 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
{
@Override
public String query(String word)
{
// TODO Auto-generated method stub
return "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
{
@Override
public String query(String word)
{
// TODO Auto-generated method stub
return "remote 就不告诉你,"+word+" 是什么意思!!!";
}
}