<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Extreme Pattern: hellojsp bundle</title>
    <link>http://blog.extremepattern.com/articles/2006/10/26/hellojsp-bundle</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>enjoy</description>
    <item>
      <title>hellojsp bundle</title>
      <description>&lt;h3&gt;源起&lt;/h3&gt;


	&lt;p&gt;試著完成一個簡易的 &lt;span class="caps"&gt;JSP&lt;/span&gt; bundle，然後跑在 OSGi 的平台上。&lt;/p&gt;


	&lt;p&gt;&lt;img src="/files/my_osgi.png" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;要測前先看看之前的環境設定，先確定可以跑起來。&lt;/p&gt;


	&lt;p&gt;&lt;a href="/articles/2006/10/25/hello-osgi-jsp-example"&gt;Hello OSGi &lt;span class="caps"&gt;JSP&lt;/span&gt; examples&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href="/articles/2006/10/19/hello-osgi-servlet"&gt;Hello OSGi Servlet&lt;/a&gt;&lt;/p&gt;


	&lt;h3&gt;hellojsp project&lt;/h3&gt;


	&lt;p&gt;到 eclipse 建立一個純 OSGi 的 Plugin project，最後 export 成為 jar 即可。&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;span class="caps"&gt;META&lt;/span&gt;-INF/MANIFEST.MF&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;OSGI&lt;/span&gt;-INF/component.xml&lt;/li&gt;
		&lt;li&gt;src/hello/Component.java&lt;/li&gt;
		&lt;li&gt;build.properties&lt;/li&gt;
		&lt;li&gt;web/index.html &lt;/li&gt;
		&lt;li&gt;web/hello.jsp&lt;/li&gt;
	&lt;/ol&gt;


	&lt;h3&gt;&lt;span class="caps"&gt;MANIFEST&lt;/span&gt;.MF&lt;/h3&gt;


&lt;pre&gt;
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
&lt;/pre&gt;

	&lt;h3&gt;component.xml&lt;/h3&gt;


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

	&lt;h3&gt;src/hello/Component.java&lt;/h3&gt;


&lt;pre&gt;
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;
    }

}
&lt;/pre&gt;

	&lt;h3&gt;build.properties&lt;/h3&gt;


&lt;pre&gt;
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               OSGI-INF/,\
               web/
&lt;/pre&gt;

	&lt;h3&gt;OSGi console&lt;/h3&gt;


&lt;pre&gt;
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
&lt;/pre&gt;

	&lt;h3&gt;測試&lt;/h3&gt;


	&lt;p&gt;因為資源映射到 web 目錄下，所以直接用下面連結可以測試。&lt;/p&gt;


	&lt;p&gt;http://localhost/index.html&lt;/p&gt;


	&lt;p&gt;http://localhost/hello.jsp&lt;/p&gt;


	&lt;h3&gt;觀察&lt;/h3&gt;


	&lt;ol&gt;
	&lt;li&gt;log service 改用 setXX/unsetXX 方式比較直覺，不需要再去找，OSGi 可以確保 active 到 deactive 之間這些服務運作正常。&lt;/li&gt;
	&lt;/ol&gt;</description>
      <pubDate>Thu, 26 Oct 2006 19:54:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:ef021f1d-3858-4ff6-9851-211da9abea34</guid>
      <author>LIN</author>
      <link>http://blog.extremepattern.com/articles/2006/10/26/hellojsp-bundle</link>
      <category>eclipse</category>
      <category>java</category>
    </item>
  </channel>
</rss>

