hellojsp bundle

0

源起

試著完成一個簡易的 JSP bundle,然後跑在 OSGi 的平台上。

要測前先看看之前的環境設定,先確定可以跑起來。

Hello OSGi JSP examples

Hello OSGi Servlet

hellojsp project

到 eclipse 建立一個純 OSGi 的 Plugin project,最後 export 成為 jar 即可。

  1. META-INF/MANIFEST.MF
  2. OSGI-INF/component.xml
  3. src/hello/Component.java
  4. build.properties
  5. web/index.html
  6. web/hello.jsp

MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloJsp Plug-in
Bundle-SymbolicName: HelloJsp
Bundle-Version: 1.0.0
Bundle-Localization: plugin
Import-Package: javax.servlet;version="2.4.0",
 org.eclipse.equinox.jsp.jasper,
 org.osgi.framework;version="1.4.0",
 org.osgi.service.component;version="1.0.0",
 org.osgi.service.http;version="1.2.0",
 org.osgi.service.log;version="1.3.0" 
Service-Component: OSGI-INF/component.xml

component.xml

<?xml version="1.0" encoding="UTF-8"?>
<component name="hello.component">
  <implementation class="hello.Component"/>
  <reference name="LOG" 
    interface="org.osgi.service.log.LogService" 
    cardinality="1..1" 
    bind="setLog" unbind="unsetLog" 
    policy="static"/>
  <reference name="HTTP" 
    interface="org.osgi.service.http.HttpService" 
    cardinality="1..1" 
    policy="dynamic" 
    bind="setHttp" 
    unbind="unsetHttp"/>
</component>

src/hello/Component.java

package hello;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.eclipse.equinox.jsp.jasper.ContextPathServletAdaptor;
import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.log.LogService;

public class Component {

    LogService log;
    HttpService httpService;
    static final String ROOT = "/web";

    protected void activate(ComponentContext context) {

        log.log(LogService.LOG_INFO, "active hello.Component");
        HttpContext commonContext = this.httpService.createDefaultHttpContext();

        JspServlet jspServlet = new JspServlet(context.getBundleContext()
                .getBundle(), ROOT);

        Servlet adaptedJspServlet = new ContextPathServletAdaptor(jspServlet,
                "/", ROOT);
        try {
            httpService.registerResources("/", ROOT, commonContext);
            httpService.registerServlet("/*.jsp", 
                            adaptedJspServlet, null, commonContext);            
        } catch (NamespaceException ne) {
            log.log(LogService.LOG_ERROR, ne.getMessage());
        } catch (ServletException se) {
            log.log(LogService.LOG_ERROR, se.getMessage());
        }
    }

    protected void deactivate(ComponentContext context) {
        log.log(LogService.LOG_INFO, "deactive hello.Component");

    }

    protected void setLog(LogService logService) {
        this.log = logService;

    }

    protected void unsetLog(LogService logService) {
        this.log = null;
    }

    protected void setHttp(HttpService httpService) {
        this.httpService = httpService;
    }

    protected void unsetHttp(HttpService httpService) {
        this.httpService.unregister("/");
        this.httpService.unregister("/*.jsp");
        this.httpService = null;
    }

}

build.properties

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               OSGI-INF/,\
               web/

OSGi console

Framework is launched.

id    State       Bundle
0    ACTIVE      system.bundle_3.3.0.v20061023
27    ACTIVE      javax.servlet.jsp_2.0.0.200610251427
28    ACTIVE      org.apache.commons.el_1.0.0
29    ACTIVE      org.apache.jasper_5.5.17.200610251427
37    ACTIVE      org.eclipse.equinox.jsp.jasper_1.0.0.200610251427
38    ACTIVE      org.eclipse.equinox.jsp.jstl_1.0.0
40    ACTIVE      javax.servlet_2.4.0.200609281713
41    ACTIVE      org.apache.commons.logging_1.0.4
42    ACTIVE      org.eclipse.equinox.ds_1.0.0.v20060828
43    ACTIVE      org.eclipse.equinox.http.jetty_1.0.0.v20061012
44    ACTIVE      org.eclipse.equinox.http.servlet_1.0.0.v20061023
47    ACTIVE      org.eclipse.equinox.log_1.0.100.v20060717
48    ACTIVE      org.eclipse.osgi.services_3.1.100.v20060918
49    ACTIVE      org.mortbay.jetty_5.1.11.200609281713
51    ACTIVE      HelloJsp_1.0.0

測試

因為資源映射到 web 目錄下,所以直接用下面連結可以測試。

http://localhost/index.html

http://localhost/hello.jsp

觀察

  1. log service 改用 setXX/unsetXX 方式比較直覺,不需要再去找,OSGi 可以確保 active 到 deactive 之間這些服務運作正常。

Comments

(leave url/email »)

   Preview comment