ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using XML and Jar Utility API to Build a Rule-Based Java EE Auto-Deployer
Pages: 1, 2, 3, 4

The following is the code sample of the Deployer object:



package onebuttondeploy.deployer;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;

import onebuttondeploy.rule.ApplicationModuleVO;
import onebuttondeploy.rule.DatasourceModuleVO;
import onebuttondeploy.rule.DeployRule;
import onebuttondeploy.rule.JmsModuleVO;
import onebuttondeploy.rule.LdapModuleVO;
import onebuttondeploy.rule.SqlModuleVO;

/**
* @author Colin (Chun) Lu
* @Email colinlucs@gmail.com
*/
public class Deployer {
    private DeploymentProvider provider;
    public static Logger logger = Logger.getLogger(Deployer.class);
    
    
    public Deployer(DeploymentProvider provider) {
        this.provider = provider;
    }
    
    /*
     * Factory method: instantiate the provider object, 
     * and wires this object to deployer instance.
     */
    public static final Deployer getInstance(String providerClass) throws Exception{
        Class c = Class.forName(providerClass);
        DeploymentProvider provider = (DeploymentProvider)c.newInstance();
        return new Deployer(provider);
    }
    
    /*
     * Main deploy method: invokes provider's deploy method, 
     * passing the EAR as byte array
     */
    public String deploy(byte[] appArchive) throws Exception{
        DeployRule rule = parseXmlRule(appArchive);
        String[] moduleResults = provider.deploy(rule, appArchive);
        String result="";
        for (int i = 0; moduleResults != null 
            && i<moduleResults.length; i++) {
            if (i == 0)
                result = moduleResults[i];
            else
                result += "\n" + moduleResults[i];
        }
        return result;
    }    
      /*
     * EAR analyzer method: 
     * 1. extract the xml rule document using JarEntry 
     * 2. De-serialize XML to DeployRule using XStream
     */
    private DeployRule parseXmlRule(byte[] appArchive) throws Exception{
        JarInputStream appEar = new JarInputStream(new ByteArrayInputStream(appArchive));
        JarEntry entry = null;
        StringBuffer strOut = new StringBuffer();
        String xml = null;
        while ((entry = appEar.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            logger.debug("entry name="+entryName);
            if(entryName.equals("EXT-INF/rule.xml")){
                String aux;
                BufferedReader br = new BufferedReader(new InputStreamReader(appEar));
                while ((aux=br.readLine())!=null)
                    strOut.append(aux);
                xml = strOut.toString();
                appEar.close();
                br.close();
                break;
            }
        }
        logger.debug("xml="+xml);
        DeployRule rule = null;
        if (xml != null) {
            XStream xstream = new XStream();    // With XPP3 lib
            xstream.alias("rule", DeployRule.class);
            xstream.alias("modules", ArrayList.class);
            xstream.alias("datasource", DatasourceModuleVO.class);
            xstream.alias("jms", JmsModuleVO.class);
            xstream.alias("sql", SqlModuleVO.class);
            xstream.alias("ldap", LdapModuleVO.class);
            xstream.alias("application", ApplicationModuleVO.class);
            rule = (DeployRule)xstream.fromXML(xml);
        }
        return rule;
    }
}

Benefits

Utilizing this rule-based auto-deployer in your Java EE application deployment task has the following benefits:

  • Significantly simplified deployment task: as you can see, this auto-deployer application can really achieve the goal of deployment to be a pushing one-button task.

  • Well-defined deployment flow: the auto-deployer application helps to clearly define the responsibilities of developer, assembler, and deployer. It releases the heavy load of an application deployer by letting an assembler make a well-defined deployment rule, and the deployer doesn't need to know the technical dependencies and requirements of each application. This makes the deployment much more efficient.

  • Centralized and transacted deployment management: In a very large enterprise environment, applications are usually deployed on to hundreds of different systems. This deployer application provides a good mean of a centralized management. In addition, it can manage deployment as a transacted action by implementing un-deploy/re-deploy methods. Therefore, the deployer can rollback the deployment or switch to different version very easily.

  • Application server independent: it is happening that a few different vendors' application servers are running together in a very large enterprise environment. Because the core of this auto-deployer application is vendor independent. It is very easy to support heterogeneous application servers by developing different deploy providers and plug in to the application.

Conclusions

This article discussed the challenges in deploying large Java EE applications. And then an intelligent auto-deployer is proposed to make the deployment task easier. This auto-deployer can significantly simplify the deployment task in a large enterprise environment. It also helps the enterprise to maintain a well-defined application deployment flow.

Resources

Colin (Chun) Lu is the systems analyst at Telus Mobility. He has been working in J2EE architecture design and development with WebLogic, and WebSphere since 1999. Currently he is working in the area of SOA for Java EE application design and integration. You can reach Colin at colinlucs@gmail.com


Return to ONJava.