Using XML and Jar Utility API to Build a Rule-Based Java EE Auto-Deployer
Pages: 1, 2, 3, 4
And the generated XML document of the deployment rule (rule.xml) looks like this:
<?xml version="1.0"?>
<rule>
<modules>
<datasource>
<targetUrl>t3://localhost:7001</targetUrl>
<xaType>NONXA</xaType>
<jndiName>jdbc/testDS</jndiName>
<user>colin</user>
<passWord>ARdHTJSytteIAE</passWord>
<dbUrl>jdbc:oracle:thin:@localhost:1521:localdb</dbUrl>
<driver>oracle.jdbc.driver.OracleDriver</driver>
</datasource>
<sql>
<targetUrl>t3://localhost:7001</targetUrl>
<jndiName>jdbc/testDS</jndiName>
<sqlStatement>sql/testDS.sql</sqlStatement>
</sql>
<jms>
<targetUrl>t3://localhost:7001</targetUrl>
<destType>PTP</destType>
<serverName>testJmsServer</serverName>
<resName>testJmsRes</resName>
<connFactory>jmsTestConnFactory</connFactory>
<facJndiName>jms/conn</facJndiName>
<destName>testQueue</destName>
<destJndiName>jms/testQueue</destJndiName>
</jms>
<ldap>
<host>ldapread-server.com</host>
<port>489</port>
<basedn>cn=one_button_deployment,o=system_configuration</basedn>
<ldif>ldif/test.ldif</ldif>
</ldap>
<application>
<targetUrl>t3://localhost:7001</targetUrl>
<appName>SOManager</appName>
<version>Release v1.0</version>
</application>
</modules>
</rule>
Design of the Deployment Tool
For this sample tool, a Factory design pattern is applied to build deployment module, POJOs, from a deployment rule, after that these POJOs are serialized to XML documents by a RuleGenerator using XStream utility library. The class design is shown in Figure 4.

Figure 4. Class diagram of deployment tool
As shown in Figure 5, the RuleGenerator reads a DeployPlan as a file or input stream and then parses the plan to generate a DeployRule Java object. The DeployRule Java object will be serialized to XML document using XStream utility library.

Figure 5. Sequence diagram of deployment rule generation
The sample code of RuleGenerator is shown below:
package onebuttondeploy.tool;
import java.io.FileWriter;
import java.util.ArrayList;
import onebuttondeploy.Constants;
import onebuttondeploy.rule.ApplicationModuleVO;
import onebuttondeploy.rule.DatasourceModuleVO;
import onebuttondeploy.rule.DeployModule;
import onebuttondeploy.rule.DeployRule;
import onebuttondeploy.rule.DeploymentModuleFactory;
import onebuttondeploy.rule.JmsModuleVO;
import onebuttondeploy.rule.LdapModuleVO;
import onebuttondeploy.rule.SqlModuleVO;
import com.thoughtworks.xstream.XStream;
/**
* @author Colin (Chun) Lu
* @Email colinlucs@gmail.com
*/
public class RuleGenerator {
/*
* Convert deploy plan to XML document
*/
public static String getXmlRule(String plan) throws Exception{
return generateDeployRuleXml(plan2Rule(plan));
}
/*
* Instantiate DeployRule object from deploy plan
*/
private static DeployRule plan2Rule(String plan) throws Exception{
if (plan == null)
return null;
DeployRule rule = new DeployRule();
String[] deployModules = plan.split("\n");
for (int i = 0; i < deployModules.length; i++) {
DeployModule module =
DeploymentModuleFactory.getDeploymenModule(deployModules[i]);
if (module != null)
rule.addModules(module);
}
return rule;
}
/*
* Serialize DeployRule object to XML document usign XStream
*/
private static String generateDeployRuleXml(DeployRule rule) throws Exception{
if (rule == null)
return null;
String xml = "<?xml version=\"1.0\"?> \n";
XStream xstream = new XStream();
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);
xml += xstream.toXML(rule);
return xml;
}
}