Automatic Version Numbering in Web Applications with Hudson (Part 3)

In this third and last part of the tutorial (part 1, part 2) we add the version reading feature to a ZK application, which is actually not a big difference to the regular jsp page that we created earlier.
It is also a good exercise how to add the ZK library to an existing web project.

Requirements:

  • Netbeans 6.8
  • Glassfish V3 Application Server
  • Installed ZK 5.0 Plugin for Netbeans (link)

(Older Versions and non-EE6 work as well)

Tutorial Part 2

  • Open the web application project from part 1 and 2
  • Add the ZK Library

    Add Library

    Import LIbrary

    Add Library

  • Create a web.xml
    Java EE6 applications out-of-the-box do not carry deployment descriptor files (web.xml) anymore, it’s optional now, but we need it for ZK.
    (in case you use EE5 you dont need to create this file)

    Add web.xml

    Add web.xml

  • Modify web.xml
    We need to add the relevant entries for ZK, which the ZK plugin adds for us when creating a new ZK application, but adding the library manually requires us to make these changes. I just copy and paste the default entries from a new ZK application project.
    The complete web.xml file. Do not copy from here, future version of ZK might require different settings. Not all settings are necessary to run a ZK project also.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
    
     <listener>
     <description>ZK listener for session cleanup</description>
     <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
     </listener>
     <servlet>
     <description>ZK loader for ZUML pages</description>
     <servlet-name>zkLoader</servlet-name>
     <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
     <!-- Must. Specifies URI of the update engine (DHtmlUpdateServlet).
     It must be the same as <url-pattern> for the update engine.
     -->
     <init-param>
     <param-name>update-uri</param-name>
     <param-value>/zkau</param-value>
     </init-param>
     <!-- [Optional] Specifies whether to compress the output
     of the ZK loader. It speeds up the transmission over slow Internet.
     However, if you configure a filter to post-processing the
     output or your server will do the compression, you might have to disable it.
    
     Default: true
     <init-param>
     <param-name>compress</param-name>
     <param-value>true</param-value>
     </init-param>
     -->
     <!-- [Optional] Specifies the default log level: OFF, ERROR, WARNING,
     INFO, DEBUG and FINER. If not specified, the system default is used.
     <init-param>
     <param-name>log-level</param-name>
     <param-value>OFF</param-value>
     </init-param>
     -->
     <load-on-startup>1</load-on-startup><!-- Must -->
     </servlet>
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>*.zul</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>*.zhtml</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>*.svg</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>*.xml2html</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>*.mul</url-pattern>
     </servlet-mapping>
    
     <!-- Optional. Uncomment it if you want to use richlets.
     -->
     <servlet-mapping>
     <servlet-name>zkLoader</servlet-name>
     <url-pattern>/zk/*</url-pattern>
     </servlet-mapping>
    
     <servlet>
     <description>The asynchronous update engine for ZK</description>
     <servlet-name>auEngine</servlet-name>
     <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
    
     <!-- [Optional] Specifies whether to compress the output
     of the ZK loader. It speeds up the transmission over slow Internet.
     However, if your server will do the compression, you might have to disable it.
    
     Default: true
     <init-param>
     <param-name>compress</param-name>
     <param-value>true</param-value>
     </init-param>
     -->
     <!-- [Optional] Specifies the AU processor for particular prefix.
     <init-param>
     <param-name>processor0</param-name>
     <param-value>/upload=com.my.MyUploader</param-value>
     </init-param>
     -->
     </servlet>
     <servlet-mapping>
     <servlet-name>auEngine</servlet-name>
     <url-pattern>/zkau/*</url-pattern>
     </servlet-mapping>
    
     <!-- Uncomment if you want to use the ZK filter to post process the HTML output
     generated by other technology, such as JSP and velocity.
     <filter>
     <filter-name>zkFilter</filter-name>
     <filter-class>org.zkoss.zk.ui.http.DHtmlLayoutFilter</filter-class>
     <init-param>
     <param-name>extension</param-name>
     <param-value>html</param-value>
     </init-param>
     <init-param>
     <param-name>compress</param-name>
     <param-value>true</param-value>
     </init-param>
     </filter>
     <filter-mapping>
     <filter-name>zkFilter</filter-name>
     <url-pattern>/test/filter.dsp</url-pattern>
     </filter-mapping>
     <filter-mapping>
     <filter-name>zkFilter</filter-name>
     <url-pattern>/test/filter2.dsp</url-pattern>
     </filter-mapping>
     -->
    
     <!-- ///////////// -->
     <!-- DSP (optional) -->
     <!-- Uncomment it if you want to use DSP
     However, it is turned on since zkdemo/userguide uses DSP to generate CSS.
     -->
     <servlet>
     <servlet-name>dspLoader</servlet-name>
     <servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class>
     <init-param>
     <param-name>class-resource</param-name>
     <param-value>true</param-value>
     </init-param>
     </servlet>
     <servlet-mapping>
     <servlet-name>dspLoader</servlet-name>
     <url-pattern>*.dsp</url-pattern>
     </servlet-mapping>
    
     <!-- /////////// -->
     <!-- Miscellaneous -->
     <session-config>
     <session-timeout>60</session-timeout>
     </session-config>
    
     <!-- MIME mapping -->
     <mime-mapping>
     <extension>doc</extension>
     <mime-type>application/vnd.ms-word</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>gif</extension>
     <mime-type>image/gif</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>htm</extension>
     <mime-type>text/html</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>html</extension>
     <mime-type>text/html</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>jnlp</extension>
     <mime-type>application/x-java-jnlp-file</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>jpeg</extension>
     <mime-type>image/jpeg</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>jpg</extension>
     <mime-type>image/jpeg</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>js</extension>
     <mime-type>text/javascript</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>pdf</extension>
     <mime-type>application/pdf</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>png</extension>
     <mime-type>image/png</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>rar</extension>
     <mime-type>application/x-rar-compressed</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>txt</extension>
     <mime-type>text/plain</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>xls</extension>
     <mime-type>application/vnd.ms-excel</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>xml</extension>
     <mime-type>text/xml</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>xul</extension>
     <mime-type>application/vnd.mozilla.xul-xml</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>zhtml</extension>
     <mime-type>text/html</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>zip</extension>
     <mime-type>application/x-zip</mime-type>
     </mime-mapping>
     <mime-mapping>
     <extension>zul</extension>
     <mime-type>text/html</mime-type>
     </mime-mapping>
    
     <welcome-file-list>
     <welcome-file>index.zul</welcome-file>
     <welcome-file>index.zhtml</welcome-file>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     </welcome-file-list>
    
    </web-app>
    
    
  • Add a ZK page zkversionpage.zul
    Only a label is placed on the the zk page and we apply a controller class

    Add ZK page

    <?xml version="1.0" encoding="UTF-8"?>
    
    <zk xmlns="http://www.zkoss.org/2005/zul">
    
    <window id="win" apply="controller.zkversionpageController">
     <label id="lblVersion"/>
    </window>
    
    </zk>
    
  • Add the controller class zkversionpageController.java
    Here the magic happens. It is pretty much the same as the JSP variant, just we need to get the hands on the ServletContext with the 3 extra lines

     Session sess = Sessions.getCurrent();
     HttpSession hses = (HttpSession) sess.getNativeSession();
     ServletContext sCon = hses.getServletContext();
    

    The complete controllerclass

    package controller;
    
    import java.io.IOException;
    import java.util.Properties;
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSession;
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    import org.zkoss.zul.Label;
    import org.zkoss.zk.ui.Session;
    import org.zkoss.zk.ui.Sessions;
    
    public class zkversionpageController extends GenericForwardComposer {
    
     private Label lblVersion;
    
     @Override
     public void doAfterCompose(Component comp) throws Exception {
     super.doAfterCompose(comp);
    
     lblVersion.setValue(getVersionNo());
     }
    
     private String getVersionNo() {
     String versionReturn ="";
    
     try {
     Session sess = Sessions.getCurrent();
     HttpSession hses = (HttpSession) sess.getNativeSession();
     ServletContext sCon = hses.getServletContext();
     Properties prop = new Properties();
     prop.load(sCon.getResourceAsStream("/META-INF/MANIFEST.MF"));
    
     String appVersion = prop.getProperty("version");
     String appDate = prop.getProperty("id");
     versionReturn = "Version: " + appVersion + " (" + appDate +")";
    
     } catch (IOException ex) {
     versionReturn = "No version available.";
     return versionReturn;
    
     }
     return versionReturn;
     }
    }
    
  • Run the application
    (after Hudson build it)

    Running ZK application


About these ads

One thought on “Automatic Version Numbering in Web Applications with Hudson (Part 3)

  1. Pingback: Automatic Version Numbering in Web Applications with Hudson (Add-On) « The JavaDude Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s