这两天看BlueDavy写的一篇OSGi入门的文章《OSGi实战》,在道客巴巴有这篇文章的链接。
在这篇文章里面有这样一个例子:一个网站,有一个验证用户名和密码的页面(UserValidatorWebBundle)。验证的方式有三种:LDAP验证(LDAPValidatorBundle)﹑数据库验证(DBValidatorBundle)和配置文件验证(ConfigValidatorBundle)。在OSGi框架来讲,这分别是四个Bundle,UserValidatorWebBundle提供前端Web访问服务,执行时需要调用到三个验证Bundle中某一个提供的验证服务。三个验证Bundle在实际环境下可以通过启动其中一个﹑停止其它两个的方法实现动态的改变验证方式。
另外,还有一个UserValidatorBundle,这个Bundle实际上只包含了一个验证服务的接口类,在LDAPValidatorBundle﹑DBValidatorBundle和ConfigValidatorBundle三个Bundle之间提供验证服务的公共定义。
特别说明,我的开发环境是Eclipse JaveEE Indigo 3.7.2。
实现UserValidatorBundle
UserValidatorBundle提供验证服务的接口类Validator。由于它不要注册任何的服务和监听器,因此这里我们不需要为该Bundle提供Activator实现。它的作用就是将验证接口Validator通在MANIFEST.MF的Export-Package导出去,然后供其它具体实现验证服务的Bundle使用。
Bundle的详细开发过程可以参考《OSGi系列 - 用Eclipse开发Bundle》,这里只对关键地方进行说明。
UserValidatorBundle的目录结构,注意该Bundle没有Activator类:
Validator.java文件内容:
package org.riawork.demo.service.user; public interface Validator { public boolean validate(String username, String password) throws Exception; }
打开MANIFEST.MF文件,在出现的元数据编辑页面中选择底部的Runtime标签页,然后点击Exported Packages旁边的Add...按钮,添加如下的内容:
这个操作实际上就是在MANIFEST.MF文件里面增加一条Export-Package项目:
Export-Package: org.riawork.demo.service.user
MANIFEST.MF文件完整的内容:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: UserValidatorBundle Bundle-SymbolicName: UserValidatorBundle Bundle-Version: 1.0.0 Import-Package: org.osgi.framework;version="1.3.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Export-Package: org.riawork.demo.service.user
实现LDAPValidatorBundle
LDAPValidatorBundle提供LDAP验证方式。
LDAPValidatorBundle提供了一个LDAPValidator验证类,该类需要实现UserValidatorBundle里面的Valdiator接口,因此需要在MANIFEST.MF文件中通过Import-Package导入Validator接口所在的包org.riawork.demo.service.user。
LDAPValidatorBundle同时也提供了一个LDAPActivator类,用来管理Bundle的生命周期,在start()和stop()方法中实现LDAPValidator验证服务的注册和卸载。
作为演示用途,这里所有的验证方式都使用的是字符串硬编码的简单方式。
LDAPValidatorBundle的目录结构:
打开MANIFEST.MF文件,在出现的元数据编辑页面中选择底部的Dependencies标签页,然后点击Imported Packages旁边的Add...按钮,添加如下的内容:
这个操作实际上就是在MANIFEST.MF文件里面增加一条Import-Package项目:
Import-Package: org.osgi.framework;version="1.3.0",
org.riawork.demo.service.user
MANIFEST.MF文件完整的内容:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: LDAPValidatorBundle Bundle-SymbolicName: LDAPValidatorBundle Bundle-Version: 1.0.0 Bundle-Activator: org.riawork.demo.service.user.impl.LDAPActivator Import-Package: org.osgi.framework;version="1.3.0", org.riawork.demo.service.user Bundle-RequiredExecutionEnvironment: JavaSE-1.6
LDAPValidator.java文件内容:
package org.riawork.demo.service.user.impl; import org.riawork.demo.service.user.Validator; public class LDAPValidator implements Validator { public boolean validate(String username, String password) throws Exception { System.out.println("LDAPValidator.validate()方法被调用"); if ("jerry".equals(username) && "jerry".equals(password)){ return true; } return false; } }
LDAPActivator.java文件内容:
package org.riawork.demo.service.user.impl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.riawork.demo.service.user.Validator; public class LDAPActivator implements BundleActivator { private ServiceRegistration serviceReg = null; public void start(BundleContext bundleContext) throws Exception { System.out.println("LDAPValidator服务被注册"); serviceReg = bundleContext.registerService(Validator.class.getName(), new LDAPValidator(), null); } public void stop(BundleContext bundleContext) throws Exception { if(serviceReg != null) serviceReg.unregister(); System.out.println("LDAPValidator服务被卸载"); } }
实现DBValidatorBundle
DBValidatorBundle提供数据库验证方式,该Bundle的实现方式完全同LDAPValidatorBundle。
DBValidatorBundle的目录结构:
MANIFEST.MF文件内容:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: DBValidatorBundle Bundle-SymbolicName: DBValidatorBundle Bundle-Version: 1.0.0 Bundle-Activator: org.riawork.demo.service.user.impl.DBActivator Import-Package: org.osgi.framework;version="1.3.0", org.riawork.demo.service.user Bundle-RequiredExecutionEnvironment: JavaSE-1.6
DBValidator.java文件内容:
package org.riawork.demo.service.user.impl; import org.riawork.demo.service.user.Validator; public class DBValidator implements Validator { public boolean validate(String username, String password) throws Exception { System.out.println("DBValidator.validate()方法被调用"); if ("jerry".equals(username) && "jerry".equals(password)){ return true; } return false; } }
DBActivator.java文件内容:
package org.riawork.demo.service.user.impl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.riawork.demo.service.user.Validator; public class DBActivator implements BundleActivator { private ServiceRegistration serviceReg = null; public void start(BundleContext bundleContext) throws Exception { System.out.println("DBValidator服务被注册"); serviceReg = bundleContext.registerService(Validator.class.getName(), new DBValidator(), null); } public void stop(BundleContext bundleContext) throws Exception { if(serviceReg != null) serviceReg.unregister(); System.out.println("DBValidator服务被卸载"); } }
实现ConfigValidatorBundle
ConfigValidatorBundle提供配置人家验证方式,该Bundle的实现方式完全同LDAPValidatorBundle。
ConfigValidatorBundle的目录结构:
MANIFEST.MF文件内容:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: ConfigValidatorBundle Bundle-SymbolicName: ConfigValidatorBundle Bundle-Version: 1.0.0 Bundle-Activator: org.riawork.demo.service.user.impl.ConfigActivator Import-Package: org.osgi.framework;version="1.3.0", org.riawork.demo.service.user Bundle-RequiredExecutionEnvironment: JavaSE-1.6
ConfigValidator.java文件内容:
package org.riawork.demo.service.user.impl; import org.riawork.demo.service.user.Validator; public class ConfigValidator implements Validator { public boolean validate(String username, String password) throws Exception { System.out.println("ConfigValidator.validate()方法被调用"); if ("jerry".equals(username) && "jerry".equals(password)){ return true; } return false; } }
ConfigActivator.java文件内容:
package org.riawork.demo.service.user.impl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.riawork.demo.service.user.Validator; public class ConfigActivator implements BundleActivator { private ServiceRegistration serviceReg = null; public void start(BundleContext bundleContext) throws Exception { System.out.println("ConfigValidator服务被注册"); serviceReg = bundleContext.registerService(Validator.class.getName(), new ConfigValidator(), null); } public void stop(BundleContext bundleContext) throws Exception { if(serviceReg != null) serviceReg.unregister(); System.out.println("ConfigValidator服务被卸载"); } }
实现UserValidatorWebBundle
UserValidatorWebBundle提供使用者前端访问。该Bundle提供一个login.html页面,输入用户名和密码后,点击登录按钮,将数据提交到LoginServlet。LoginServlet检查,获取已经注册的Validator验证服务,调用validate()方法检查传过来的用户名和密码是否正确。
该Bundle使用到了Servlet API和HTTP服务,因此必需javax.servlet和org.eclipse.osgi.services两个额外的Bundle。
UserValidatorWebBundle的目录结构:
打开MANIFEST.MF文件,选择底部的Dependencies标签页,添加如下的内容:
这个操作实际上就是在MANIFEST.MF文件里面增加Import-Package项目和Require-Bundle项目:
Import-Package: org.osgi.framework;version="1.3.0", org.riawork.demo.service.user Require-Bundle: javax.servlet;bundle-version="2.5.0", org.eclipse.osgi.services;bundle-version="3.3.0"
MANIFEST.MF文件完整的内容:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: UserValidatorWebBundle Bundle-SymbolicName: UserValidatorWebBundle Bundle-Version: 1.0.0 Bundle-Activator: org.riawork.demo.web.Activator Import-Package: org.osgi.framework;version="1.3.0", org.riawork.demo.service.user Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: javax.servlet;bundle-version="2.5.0", org.eclipse.osgi.services;bundle-version="3.3.0"
在src目录下新建page目录,然后在page目录下新增login.html文件。login.html文件内容:
<HTML> <HEAD> <TITLE>OSGI Opendoc Demo</TITLE> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <script> function login(){ if(document.frmLogin.username.value==""){ alert("必须输入用户名"); document.frmLogin.username.focus(); return; } if(document.frmLogin.userpass.value==""){ alert("必须输入密码"); document.frmLogin.userpass.focus(); return; } document.frmLogin.submit(); } </script> </HEAD> <BODY> <table style="width:45%;border:1px solid;height:100px"> <form name=frmLogin method="post" action="/demo/login"> <tr bgcolor="#C0C0C0"> <td colspan="2" align="center">用户登录模块示例</td> </tr> <tr> <td width="30%">用户名</td> <td><input type="text" name="username" size="30"></td> </tr> <tr> <td>密 码</td> <td><input type="password" name="userpass" size="33"></td> </tr> <tr bgcolor="#C0C0C0"> <td colspan="2" align="center"> <input type=button name=btnSubmit value="登录" onclick="login();"> </td> </tr> </form> </table> </BODY> </HTML>
LoginServlet.java文件内容:
package org.riawork.demo.web; import java.io.IOException; import javax.servlet.ServletOutputStream; 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 org.riawork.demo.service.user.Validator; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private BundleContext context; public LoginServlet(BundleContext context){ this.context=context; } /** * Web Post Method */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request, response); } /** * Web GET Method */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ String username=request.getParameter("username"); String userpass=request.getParameter("userpass"); response.setContentType("text/html"); ServletOutputStream output=response.getOutputStream(); ServiceReference serviceRef=context.getServiceReference(Validator.class.getName()); Validator validator=(Validator) context.getService(serviceRef); if(validator==null){ output.println("No usable validator service"); output.close(); return; } boolean result=false; try { result=validator.validate(username, userpass); if(result) output.println("Login success"); else output.println("Login Fail,please check username and password"); output.close(); return; } catch (Exception e) { output.println("Login error:"); output.println(e.toString()); return; } } }
Activator.java文件内容:
package org.riawork.demo.web; import javax.servlet.Servlet; 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.riawork.demo.web.LoginServlet; public class Activator implements BundleActivator,ServiceListener{ private BundleContext bc; private ServiceReference ref; private Servlet servlet; public void start(BundleContext context) throws Exception { bc=context; servlet=new LoginServlet(bc); registerServlet(); context.addServiceListener(this, "(objectClass=" + HttpService.class.getName() + ")"); } public void stop(BundleContext context) throws Exception { try { unregisterServlet(); } catch (Throwable t) { t.printStackTrace(); } servlet = null; bc = null; ref = null; } public void serviceChanged(ServiceEvent event) { switch (event.getType()){ case ServiceEvent.REGISTERED: registerServlet(); break; case ServiceEvent.UNREGISTERING: unregisterServlet(); break; } } private void registerServlet() { if (ref == null) ref = bc.getServiceReference(HttpService.class.getName()); if (ref != null) { try { HttpService http = (HttpService) bc.getService(ref); http.registerServlet("/demo/login", servlet, null, null); http.registerResources("/demo/page","page",null); System.out.println("已启动用户登录验证web模块,请通过/demo/page/login.htm访问"); } catch (Exception e) { e.printStackTrace(); } } } private void unregisterServlet() { if (ref != null) { try { HttpService http = (HttpService) bc.getService(ref); http.unregister("/demo/login"); http.unregister("/demo/page"); System.out.println("已卸载用户登录验证web模块!"); } catch(Exception e){ e.printStackTrace(); } } } }
创建OSGi Run Configuration
选择菜单Run >> Run Configurations...,在弹出的Run Configurations对话框左边找到OSGi Framework,在该项目上点击鼠标右键选择New菜单项,画面如下:
下表的Bundle在Run Configurations对话框中必须被选中:
- UserValidatorBundle
- LDAPValidatorBundle
- DBValidatorBundle
- ConfigValidatorBundle
- UserValidatorWebBundle
- javax.servlet
- org.eclipse.osgi.services
- org.eclipse.equinox.http.servlet
- org.eclipse.equinox.http.jetty
- org.mortbay.jetty.server
- org.mortbay.jetty.util
配置完毕后,点击右下角的Run按钮,启动OSGi框架。如果一切正常,就会出现osgi>提示符,以及Bundle被启动时输出的提示信息:
从上图我们应该发现一个问题,实际上三个不同的验证方式都被启动了。
动态的切换验证方式
打开浏览器,输入http://localhost/demo/page/login.html,出现输入用户名和密码画面:
输入一个用户名和密码(正确的是jerry和jerry),点击登录按钮,浏览器网址转到了http://localhost/demo/login,这实际上就是我们注册的LoginServlet(参考UserValidatorWebBundle的Activator.java文件)。
请注意,这个时候我们查看OSGi控制台,有看到一条信息“ConfigValidator.validate()方法被调用”,说明当前使用的验证方式是配置文件验证。因为我们提供的三种验证服务都被启动了,换句话说,在OSGi框架中,Validator服务有三个提供者,在服务被调用时具体用的是哪一个提供者会由OSGi框架随机的决定。
停止Config和DB验证,只启动LDAP验证,然后再次提交http://localhost/demo/page/login.html,观察OSGi控制台,发现这次使用的是LDAP验证。
停止LDAP和Config验证,启动DB验证,观察OSGi控制台,是不是就是我们所期望的DB验证?
这篇文章实在是有点太长了,对于没耐心从头看到尾的朋友,下面提供了一个压缩文件下载链接,希望能帮助你省一点点时间。