开发步骤:
1、创建基于Equinox的plugin工程,工程名为DictQuery,用于定义一个字典查询的接口。
package org.sunny.dictQuery; public interface QueryService { String queryWord(String word); }
2、创建基于Equinox的plugin工程,工程名为RemoteDictQuery,实现了字典查询接口,在Activator类中注册服务。在Manifest.mf配置中导入org.sunny.dictQuery包。
package org.sunny.remote; import java.util.concurrent.ConcurrentHashMap; import org.sunny.dictQuery.QueryService; public class RemoteDictQueryServiceImpl implements QueryService { private static final ConcurrentHashMapdict = new ConcurrentHashMap (); static { dict.put("sky", "天空"); dict.put("computer", "计算机"); } @Override public String queryWord(String word) { System.out.println("RemoteDictQueryServiceImpl.queryWord called!"); String result = dict.get(word); if (result == null) result = "N/A"; return result; } }
package org.sunny.remote; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.sunny.dictQuery.QueryService; 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 = context.registerService(QueryService.class.getName(), new RemoteDictQueryServiceImpl(), 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(); } }
3、创建基于Equinox的plugin工程,工程名为LocalDictQuery,实现了字典查询接口,采用Declarative Service方法注册服务,此工程不需要Activator类。在Manifest.mf配置中导入org.sunny.dictQuery包。
package org.sunny.ldq; import java.util.concurrent.ConcurrentHashMap; import org.sunny.dictQuery.QueryService; public class LocalDictQueryServiceImpl implements QueryService { private static final ConcurrentHashMapdict = new ConcurrentHashMap (); static { dict.put("test", "测试"); dict.put("china", "中国"); } @Override public String queryWord(String word) { System.out.println("LocalDictQueryServiceImpl.queryWord called!"); String result = dict.get(word); if (result == null) result = "N/A"; return result; } }
工程下建目录OSGI-INF,Component.xm配置如下。
4、创建基于Equinox的plugin工程,工程名为QueryServlet,用于提供http服务。
在src目录下创建page目录,置放dictQuery.html,内容如下。
demo
QueryServlet类注册servlet和resource,响应http请求。
package org.sunny.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.service.http.HttpService; import org.sunny.dictQuery.QueryService; public class QueryServlet extends HttpServlet { private static final long serialVersionUID = 1L; private QueryService query = null; private HttpService http = null; public QueryServlet() { } public void setQueryService(QueryService query) { this.query = query; } public void unsetQueryService(QueryService query) { if (this.query != query) return; this.query = null; } public void setHttpService(HttpService http) { this.http = http; try { http.registerServlet("/query", this, null, null); http.registerResources("/static", "page", null); } catch (Exception e) { e.printStackTrace(); } } public void unsetHttpService(HttpService http) { this.http.unregister("/query"); this.http.unregister("/static"); this.http = null; } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String word = req.getParameter("query_word"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); if (query != null) { out.println(query.queryWord(word)); } else { out.println("no impl for query servcie..."); out.close(); return; } } }
在工程下建立OSGI-INF目录,配置component.xml,以declarative service方式引入QueryService和HttpService。
在Manifest.mf中要导入的包有java.servlet,java.servlet.http,org.sunny.dictQuery,org.osgi.service.http。
5、配置运行时环境。
在osgi控制台输入ss命令,可以看到以下内容。
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.2.R36x_v20110210
1 ACTIVE org.apache.commons.logging_1.0.4.v201005080501
2 ACTIVE DictQuery_1.0.0.qualifier
3 ACTIVE org.eclipse.equinox.http.jetty_2.0.0.v20100503
4 ACTIVE LocalDictQuery_1.0.0.qualifier
5 ACTIVE org.eclipse.osgi.services_3.2.100.v20100503
6 ACTIVE javax.servlet_2.5.0.v200910301333
7 ACTIVE RemoteDictQuery_1.0.0.qualifier
8 ACTIVE QueryServlet_1.0.0.qualifier
9 ACTIVE org.eclipse.equinox.http.servlet_1.1.0.v20100503
10 ACTIVE org.eclipse.equinox.ds_1.2.1.R36x_v20100803
11 ACTIVE org.mortbay.jetty.server_6.1.23.v201004211559
12 ACTIVE org.mortbay.jetty.util_6.1.23.v201004211559
13 ACTIVE org.eclipse.equinox.util_1.0.200.v20100503
在浏览器输入http://localhost/static/dictQuery.htm访问,填入sky查询,输出“天空”,后台打印“RemoteDictQueryServiceImpl.queryWord called!”。
在osgi控制台输入stop 7命令,停止运行RemoteDictQuery bundle。填入test查询,输出“测试”,后台打印“LocalDictQueryServiceImpl.queryWord called!”。
附件中是这四个工程的project文件,可download并运行,good luck!