Saturday, October 30, 2004

ANT logging PART II

If you are using ANT for daily EOD build, then you may wish to get a pager or e-mail notification when an error occurs during the build process. Integrating log4j with ANT is fairly straight-forward, as the previous part has explored. But there might be scenarios where using the default log4j configuration may not be desirable.

1. If you have more than one build routines running under the same project, you may not wish to maintain multiple log4j.properties files.

2. If you are deploying an enterprise application, and your EOD build script requires the loading of databases, you may have additional interested parties such as DBAs and network administrators who will monitor your process and potentially make changes. Maintaining a separate properties file for logging may not be acceptable to them. In that case, you may find yourself having to consolidate the log4j.properties file into a properties file configuring the major aspects of your ANT program.

The following code demonstrates how to write a custom ANT task to hook a particular ANT script to log4j, and to read the main ANT properties file for that program for the log4j settings.


Properties props = new Properties();
try {
//props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(
// "ant_config.properties"));
props.load(new FileInputStream(configFile));
Enumeration enum =
props.propertyNames();
while(enum.hasMoreElements()){
String name =
(String) enum.nextElement();
if(!name.startsWith("log4j.")){
props.remove(name);
}
else{
log(name + "=" + props.getProperty(name));
}
}
PropertyConfigurator.configure(props);
} catch (IOException e) {
e.printStackTrace();
}
project.addBuildListener(new Log4jListener());

After this is done, then there is no mystery in getting log4j to send out e-mail or pager notifications when something goes wrong with your build process. Below is an example log4j configuration that enables the SMTPAppender for e-mails.

# log4j configuration
log4j.rootLogger=INFO, R, mail
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=build.log
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.threshold=ERROR
log4j.appender.mail.SMTPHost=youmailhost.com
log4j.appender.mail.BufferSize=1
log4j.appender.mail.subject=Build failure
log4j.appender.mail.from=yourbuild@build.com
log4j.appender.mail.to=xyz@zyx.com,zzz@att.mobile.com
log4j.appender.mail.layout=org.apache.log4j.HTMLLayout
#log4j.appender.mail.layout.ConversionPattern=%d{ISO8601} %5p [%t] (%F:%L) - %m%n

Friday, October 08, 2004

Dynamic resize of iframe in IE 5.5 with HTML documents formatted with
tags

I did a few google searches, and there doesn't seem to have any ready-made javascript for dynamically resizing an iframe in Internet Explorer 5.5 where the document layout is done using
tags. So here is the script for that:


var dhtml_frame = document.frames(frame_id);
var dhtml_frame_doc = dhtml_frame.document;
while(dhtml_frame_doc.readyState != "complete"){};
var height = 0;
var width = 0;
for(var i = 0; i < tagname ="="> height)
{
height = dhtml_frame_doc.all[i].offsetHeight;
}
if(dhtml_frame_doc.all[i].offsetWidth > width)
{
width = dhtml_frame_doc.all[i].offsetWidth;
}
}
}
iframe.style.height = height + 20;
iframe.style.width = width + 20;

Tuesday, October 05, 2004

Interim addition on ANT

Before I finish my second part on error-handling/reporting in ANT, I will explore how to deploy ANT in a production environment.

The default distribution of ANT provides platform independent scripts that enable ANT to be deployed to different platforms. However, the default installation instructions pollute the host environment with the ANT_HOME variable. In a production environment, having multiple applications depend on a single ANT instance may create undesired dependencies.

One solution to this problem is to specify only the ANT_HOME and ANT_HOME/lib variables at execution time, so that each application can have a different ANT location.

In Windows, you can wrap around your ANT execution by creating a .bat file with the following commands:

set ANT_HOME=${ACTUAL_ANT_HOME_DIR}
set PATH=%PATH%;%ANT_HOME%/bin
ant ${TargetName}

In Unix, you can create a shell script that duplicates the functionality above:

ANT_HOME=${ACTUAL_ANT_HOME_DIR}
PATH=%PATH%;%ANT_HOME%/bin
ant ${TargetName}

One disadvantage of this approach is that the actual command is not platform independent. As of version 1.6.2, ANT allows you to specify the above information in a single line of code which is compatible with both Windows and Unix. You can simply launch the ANT environment by issuing the command:

java -classpath "${ANT_LIB_DIR}/ant-launcher.jar" -Dant.home="${ANT_HOME_DIR} -Dant.Dant.library.dir="${ANT_LIB_DIR}" org.apache.tools.ant.launch.Launcher ${TargetName}