您的位置:首页 > Web前端

打造一个Hello World OSGi Web应用程序

2010-06-07 12:59 513 查看
【51CTO精选译文】在《你好,OSGi
》的之前一篇文章
中,我们介绍
了OSGi Web应用开发工具Equinox的配置方法,在这一篇中,我们会进行Hello World OSGi
Web应用程序的开发。该练习中的应用程序是一个包含了两个资源的 OSGi 套件。第一个是 helloworld.html,它是一个静态的
HTML 文件;第二个是 HelloWorldServlet,它是一个 HttpServlet。有一个重点需注意,OSGi 容器提供
HttpService 服务。每个想要处理 HTTP 请求的套件都将调用该服务上的方法来通知 OSGi 容器它能够处理哪些 URL。将 URL
注册为 OSGi 套件可处理,存在两种方式:

51CTO编辑推荐:
OSGi入门与实践全攻略

程序方式:首选检索来自 OSGi 的服务寄存器 HttpService,然后调用其上的方法将请求 URL 注册为套件可处理。

声明方式:在 plugin.xml 文件中定义套件可处理的请求 URL。

我们将一步一步地对这些技巧进行讲解,先从程序注册方式开始。

程序注册方式


按照下面的步骤,可使用程序方式将 URL 注册为插件可处理。

你首先应做的是参加一个新的 OSGi
插件,命名为com.javaworld.sample.osgi.web.programmatic。(有关在 Eclipse 中创建 OSGi
插件的更多信息,请查阅本系列的第一节。)

打开 com.javaworld.sample.osgi.web.programmatic 的 MANIFEST.MF
文件并对其进行修改,导入 javax.servlet, javax.servlet.http, org.osgi.service.http
和org.osgi.util.tracker 包。更改完成之后,你的 MANIFEST.MF 应如列表 3 类似。

列表 3. 程序式插件的 MANIFEST.MF 文件

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-Name: Webapp Plug-in 

Bundle-SymbolicName: com.javaworld.sample.osgi.web.programmatic 

Bundle-Version: 1.0.0

Bundle-Activator: com.javaworld.sample.osgi.web.webapp.Activator 

Bundle-Vendor: JAVAWORLD 

Bundle-Localization: plugin 

Import-Package: javax.servlet;version="2.4.0", 

javax.servlet.http;version="2.4.0", 

org.osgi.framework;version="1.3.0", 

org.osgi.service.http;version="1.2.0", 

org.osgi.util.tracker;version="1.3.2"

如你所见,Import-Package 清单头的值定义了你需要导入的包列表。

在插件的根目录创建一个简单的 helloworld.html 文件,如列表 4 所示。该文件用来显示消息“Hello From
helloworld.html”。

列表 4. helloworld.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>HelloWorld OSGi Web< /title>

</head>

<body>

<h3>Hello From helloworld.html< /h3>

</body>

</html>

下一步,创建如列表 5 所示的 HelloWorldServlet。

列表 5. HelloWorldServlet

package com.javaworld.sample.osgi.web.webapp; 

import java.io.IOException; 

import javax.servlet.ServletException; 

import javax.servlet.http.HttpServlet; 

import javax.servlet.http.HttpServletRequest; 

import javax.servlet.http.HttpServletResponse; 

public class HelloWorldServlet extends HttpServlet{ 

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 

    resp.setContentType("text/html"); 

    resp.getWriter().println("< h3>Hello from HelloWorldServlet< /h3>"); 

  } 

}

HelloWorldServlet 类对 HttpServlet 进行扩展并重写其 doGet() 方法。新的 doGet()
方法唯一的操作就是在输出中写入“Hello from HelloWorldServlet”。

下一步,你需要在 com.javaworld.sample.osgi.web.programmatic
插件启动时执行相同的代码。Activator.java 将作为该插件的套件的激活器(Activator),如列表 6 所示。

列表 6. Activator.java

import org.osgi.framework.BundleActivator; 

import org.osgi.framework.BundleContext; 

import org.osgi.util.tracker.ServiceTracker; 

public class Activator implements BundleActivator { 

  ServiceTracker httpServiceTracker; 

  public void start(BundleContext context) throws Exception { 

    System.out.println("Hello World!!"); 

    httpServiceTracker = new HttpServiceTracker(context); 

    httpServiceTracker.open(); 

  } 

  

  public void stop(BundleContext context) throws Exception { 

    System.out.println("Goodbye World!!"); 

    httpServiceTracker.close(); 

    httpServiceTracker = null; 

  } 

}

Activator 类对 BundleActivator 进行扩展并实现了两个方法:

start():当 OSGi 容器启动该插件时调用 start() 方法。在start()HttpServiceTracker 类
的一个对象;这是你用来跟踪 HttpService 的 ServiceTracker 类。一旦你拥有了 HttpService
类的一个对象,可以调用它的 open() 方法来开始跟踪 HttpService。

stop():当关闭插件时,OSGi 容器调用 stop() 方法。在 stop() 方法内,你调用 HttpServiceTracker
对象的 close() 方法来终止跟踪 HttpService。

最后一步是创建 HttpServiceTracker 类,如列表 7 所示。

列表 7. HttpServiceTracker

import org.osgi.framework.BundleContext; 

import org.osgi.framework.ServiceReference; 

import org.osgi.service.http.HttpService; 

import org.osgi.util.tracker.ServiceTracker; 

public class HttpServiceTracker extends ServiceTracker{ 

  public HttpServiceTracker(BundleContext context) { 

    super(context, HttpService.class.getName(), null); 

  } 

  public Object addingService(ServiceReference reference) { 

    HttpService httpService = (HttpService) context.getService(reference); 

    try { 

      httpService.registerResources("/helloworld.html", "/helloworld.html", null); 

      httpService.registerServlet("/helloworld", new HelloWorldServlet(), null, null); 

    } catch (Exception e) { 

      e.printStackTrace(); 

    } 

    return httpService; 

  } 

  public void removedService(ServiceReference reference, Object service) { 

    HttpService httpService = (HttpService) service; 

    httpService.unregister("/helloworld.html"); 

    httpService.unregister("/helloworld"); 

    super.removedService(reference, service); 

  } 

}

HttpServiceTracker 介绍

HttpService 是一项 OSGi 服务,允许 OSGi 环境中的套件动态的注册以及取消注册 HttpService 的 URI
名称空间中的资源和 servlet —— 换句话说,即将请求 URI 映射到一个静态 HTML 文件或一个
HttpServlet。HttpServiceTracker 类是类型 ServiceTracker 的一个对象,后者简化了对
HttpService 的跟踪。(有关 OSGi 的 ServiceTracker 的更多信息,请查阅本系列文章的第一节中的“跟踪服务”。)

列表 7 中 HttpServiceTracker 类重写了两个方法:addingService() 和
removedService()。有必要对这两个方法进行解释一下:

addingService()

一个回调方法,一旦 HttpService 可用时将对其调用。在这个方法中,首先调用
HttpService.registerResources("/helloworld.html", "/helloworld.html",
null),将 helloworld.html 文件映射到 /helloworld.html。之后,每当你请求
http://localhost/helloworld.html

时, HttpService 将为用户提供 helloworld.html。请注意,你无需将 helloworld.html 映射到
/helloworld.html URL;文件名无需匹配该地址,并且你可以将其映射到类似 /test.html 的文件上。

如果想要在你的插件中提供(serve)多个 HTML 文件,你需要创建多个目录。如果想要一个 /html 目录,可以通过调用
HttpService.registerResources("/html", "/html", null) 来注册它。然后,如果你还想要访问
html 文件夹中的 test.htm,相应的地址是
http://localhost/html/test.html

。registerServlet()
方法用于将 URL 映射到 HttpServlet 类。在这个简单的代码中,利用对
registerServlet("/helloworld", new HelloWorldServlet(), null, null) 的调用将
/helloworld URL 映射到 HelloWorldServlet 类。如需将初始化参数传递到你的
HttpServlet,你可以创建一个 java.util.Dictionary  对象并将其作为第三方自变量传递到
registerServlet()。

removedService()

每当重写你的 ServiceTracker 中的 addingService() 方法来获得一个服务时,还是重写
removedService() 来取消该服务。在 removedService() 方法内,你调用 unregister() 方法来取消注册 
/helloworld.html 和  /helloworld URI。这将通知 HttpService
:com.javaworld.sample.osgi.web.programmatic 不再想要为指定 URL 提供请求服务。如果你调用
unregister() 方法来取消对 servlet 的注册, 该 servlet 的 destroy() 方法将被调用以便对其自身进行清除。

现在,HelloWorld OSGi Web应用程序已经准备就绪,并且你可以在 Equinox OSGi
框架中执行你全部的套件。你应该能够通过
http://localhost/helloworld.html

访问
helloworld.html,以及通过
http://localhost/helloworld

访问 HelloWorld
的servlet。

声明注册方式


你可能已经注意到,通过程序方式将请求 URL 注册为 OSGi 创建可处理,相应的工作流并不小。而且,如果想要更改
helloworld.html 的 URL(比如从 /helloworld.html 更改到 /hello.html),你将不得不更新
HttpServiceTracker.java,重新编译代码,然后在 OSGi 容器中对其进行部署。下面,我们来看看声明方式,它稍微简单点。

1. 创建一个新的插件项目,com.javaworld.sample.osgi.web.declarative。选择 OSGi
Equinox 框架作为目标平台。

2. 编辑 com.javaworld.sample.osgi.web.declarative 套件的 MANFIEST.MF 文件,导入
javax.servlet 和 javax.servlet.http 包并将
org.eclipse.equinox.http.registry  设置为该套件的被请求套件。完成这项修改之后,你的 MANIFEST.MF
文件将与列表 8 类似。

列表 8. 声明方式插件的 MANIFEST.MF 文件

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-Name: Declarative Plug-in 

Bundle-SymbolicName: com.javaworld.sample.osgi.web.declarative;singleton:=true 

Bundle-Version: 1.0.0

Bundle-Vendor: JAVAWORLD 

Bundle-Localization: plugin 

Import-Package: javax.servlet;version="2.4.0", 

javax.servlet.http;version="2.4.0"

Require-Bundle: org.eclipse.equinox.http.registry

这个 Require-Bundle
清单头包含一个套件符号名的列表,在对导入搜索之后并且在套件路径搜索之前,需对其进行搜索。不过,对其请求套件,只有那些标记为通过被请求套件导出的包
才是可见的。

3. 从 com.javaworld.sample.osgi.web.programmatic 套件将 helloworld.html 和
HelloWorldServlet.java 复制到 com.javaworld.sample.osgi.web.declarative
套件。

4. 最后,更改 com.javaworld.sample.osgi.web.declarative 套件的 plugin.xml
文件,将所有请求注册为它能够处理,如列表 9 所示。

Listing 9. plugin.xml

<?xml version="1.0" encoding="UTF-8"?>

<?eclipse version="3.0"?>

<plugin>

<extension-point id="servlets" name="HttpService servlets" schema="schema/servlets.exsd"/>

<extension-point id="resources" name="HttpService resources" schema="schema/resources.exsd"/>

<extension-point id="httpcontexts" name="HttpService httpcontexts" schema="schema/httpcontexts.exsd"/>

<extension

id="helloServlet"

point="org.eclipse.equinox.http.registry.servlets">

<servlet alias="/decl/helloworld"

class="com.javaworld.sample.osgi.web.webapp.HelloWorldServlet">

</servlet>

</extension>

<extension id="helloResource"

point="org.eclipse.equinox.http.registry.resources">

<resource alias="/decl/helloworld.html"

base-name="/helloworld.html" />

</extension>

</plugin>

请注意,plugin.xml 具有两个 < extension> 元素。第一个,具有 id 属性,其值为
helloServlet,表示 HelloWorldServlet类将被用于处理 /decl/helloworld 请求。通过将 point
属性的值设置为 org.eclipse.equinox.http.registry.servlets,你可以标示这是 servlet 类。第二个
< extension> 元素,具有指定值为 helloResource 的 id 属性,表示用户请求
/decl/helloworld.html 应返回 helloworld.html 给用户。

现在,使用声明方式重新创建的 HelloWorld OSGi Web应用已经准备好了,并且你可以在 Equinox
OSGi框架中执行你全部的套件。你可以通过
http://localhost/decl/helloworld.html

访问 helloworld.html 以及通过
http://localhost/decl/helloworld

访问
HelloWorld servlet。在下一篇,也是本系列的最后一篇文章中,将介绍如何在Eclipse外部执行OSGi 容器,敬请关注!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐