Identity Manager: adding versioning information to your deployment

Identity Manager projects using the NetBeans IDE plugin usually face the problem that when deploying the application e.g. idm.war and loading the custom objects in the database repository (e.g. via lh import custom-init-full.xml). If you use Subversion or CVS, there is a quick and easy way to include a revision number with every build. There a two components which need a revision number:
  • the web application (here we include a page called 'about.html' which dislays the svn revision number)
  • the repository (here we add a dummy IDM object e.g. an E-Mail Template which contains the revision number of the working copy when building idm.war). If you import your custom-init-full.xml file, the E-Mail will be imported into the repository. We will use a JSP to retrieve the object from within the BUI
Step 1: Adding an ant target

First thing is to modify build.xml. Add a target called getSVNinfo. In my setting I have:

<target name="getSVNinfo" depends="-set-properties">
  <exec executable="${svnbin}" resultproperty="svninfo" output="${idmProjectDir}/${svnhtml}">
    <arg line="info"/>
  </exec>
  <writeSvnInfoToXMLObject svnbin="${svnbin}" cbedir="${idmProjectDir}" outputfile="${idmProjectDir}/${svnxml}"/>
</target>
with
svnbin = /usr/bin/svn
idmProjectDir = /home/steffo/projects/idm/customerA
svnhtml = custom/debug/about.html
svnxml = custom/WEB-INF/config/EMailTemplates/about.xml

You might want to push "about.xml" to an svn-ignored directory.

Step 2: Writing your ant task

If you have never written an ant-task: it's easy (if you don't sophisticated features). Here is my code

/\*
 \* Main.java
 \*
 \* Created on 5. Januar 2009, 16:12
 \*
 \* This code is only for demo puroses.
 \* 
 \*/

package com.sun.idmide.anttools;

import java.lang.Runtime;
import java.lang.Process;
import java.io.BufferedWriter;
import java.lang.StringBuffer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.File;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;

/\*\*
 \*
 \* @author Steffo
 \*/
public class WriteSVNStringToXMLObj extends Task{
    private String cbeDirname;
    private String outputFile;
    private String svnbin;

    public WriteSVNStringToXMLObj() {
    }
    
    public void setCbedir(String s){
        cbeDirname = s;
    }
    public void setOutputfile(String s){
        outputFile = s;
    }
    public void setSvnbin(String s){
        svnbin = s;
    }
    
    private String runSVNcommand(String subcommand, String workingCopy) {
             File cbeDir = new File(workingCopy); 
             StringBuffer result = new StringBuffer(2000);
             StringBuffer stdout = new StringBuffer(1000);
             StringBuffer stderr = new StringBuffer(1000);
             try {  
                    Runtime rt = Runtime.getRuntime();             
                    Process p = rt.exec(svnbin + " " + subcommand, new String[0], cbeDir);
                    BufferedReader is = new  BufferedReader(new InputStreamReader(p.getInputStream()));
                    BufferedReader error = new  BufferedReader(new InputStreamReader(p.getErrorStream()));
                    OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream(outputFile), "UTF-8");
                    String line;
                    while ( (line = is.readLine()) != null) {
                           stdout.append(line + "<BR>\\n");
                    }
                    while ( (line = error.readLine()) != null) {
                        stderr.append(line + "<BR>\\n");  
                    }
                } catch (Throwable t) {
                     t.printStackTrace();
                } 
               result.append(stdout);
               result.append(stderr);
               
               return result.toString();
    }
                        
    @Override
    public void execute() throws BuildException{
                   
        try {  
           
            OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream(outputFile), "UTF-8");
            BufferedWriter out = new BufferedWriter(osw);
            
            StringBuffer xml = new StringBuffer(2000);
            xml.append("<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE EmailTemplate PUBLIC 'waveset.dtd' 'waveset.dtd'>\\n");
            xml.append("<EmailTemplate id='#ID#IDRepoVersion' name='IDRepoVersion' displayName='IDRepoVersion'>\\n");
            xml.append("<subject>SVN Version</subject>\\n");
            xml.append("<body>\\n");
            xml.append(runSVNcommand("info", cbeDirname) + "\\n");
            xml.append("##############################");
            xml.append(runSVNcommand("status", cbeDirname) + "\\n");
            xml.append("</body>\\n");
            xml.append("<MemberObjectGroups>\\n");
            xml.append("<ObjectRef type='ObjectGroup' id='#ID#All' name='All'/>\\n");
            xml.append("</MemberObjectGroups>\\n");
            xml.append("</EmailTemplate>\\n");
            out.write(xml.toString());
            System.out.println(xml.toString());  
            out.close();
        } catch (Throwable t) {
            t.printStackTrace();
}}}

Compile it and put the JAR into tools directory of you NB IDM project folder.

Step 3. Declaring the taskdef in ant

In order to use the taskdef in ant, include the following in your build.xml file:

<taskdef name='writeSvnInfoToXMLObject' classname="com.sun.idmide.anttools.WriteSVNStringToXMLObject">
        <classpath>
            <fileset dir="${tools.dir}">
                  <include name="anttools.jar" />
            </fileset>
        </classpath>
    </taskdef>
Step 4. Writing the JSP which retrieves the XML object This very simple JSP retrieves the object from the repository. You might want add pretty printer code.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
   
   <%@ page import="com.waveset.ui.FormUtil" %>
   <%@ page import="com.waveset.session.SessionFactory" %>
   <%@ page import="com.waveset.session.InternalSession" %>
   <%@ page import="com.waveset.object.LighthouseContext" %>
   <%@ page import="java.lang.StringBuffer" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h2>Version in IDM Respository</h2>
        <% SessionFactory sf = new SessionFactory();
           LighthouseContext is = sf.getServerInternalContext();
           FormUtil fu = new FormUtil();
           StringBuffer sb = new StringBuffer(2000);
           fu.getObject(is, "EmailTemplate", "#ID#IDRepoVersion").toXml(sb);        
        %>
        
<%= sb.toString() %>
    </body>
</html>

        
    
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

steffo

Search

Top Tags
Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today