Java Software Automation with Jakarta Ant
Pages: 1, 2, 3
Practical Example
Imagine that you need to do some automation in your Web application, say, in a servlet. You can find the complete source code in the references section.
Servlets are multithreaded applications, so we need to create a separate temporary directory for each customer. We're going to use session id for that.
...
protected void service( HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession();
String baseDir = getServletContext().getRealPath( "/WEB-INF/ant/");
String dir = baseDir+session.getId().hashCode();
File ff = new File( dir);
if( !ff.exists()) {
boolean rc = ff.mkdir();
if( !rc) throw new Exception( "Unable to create directory: "+dir);
}
...
Project project = new Project();
File buildFile = new File( baseDir, "build.xml");
project.setUserProperty( "session.id", ""+session.getId().hashCode());
project.setUserProperty( "ant.file", buildFile.getAbsolutePath());
project.setUserProperty( "ant.version", Main.getAntVersion());
project.setBaseDir( new File( baseDir));
project.init();
ProjectHelper.configureProject( project, buildFile);
...
Because of the servlet specifics, we will need to implement our own BuildListener and InputHandler.
The first class is the HtmlReportLogger, which will use a PrintWriter from the HttpServletRequest to prepare an HTML page with the build results to return to the Web browser.
project.addBuildListener( new HtmlReportLogger( res.getWriter()));
The second class is the ServletLogger, which utilizes the ServletContext logging facilities to write the report to the application server log.
project.addBuildListener( new ServletLogger( this));
The InputHandler implementation is using the properties from the Ant Project to retrieve requested values.
import org.apache.tools.ant.*;
import org.apache.tools.ant.input.*;
public class ProjectInputHandler implements InputHandler {
private Project project;
public ProjectInputHandler( Project project) {
this.project = project;
}
public void handleInput( InputRequest request) throws BuildException {
Object o = project.getProperty( request.getPrompt());
if( o==null)
throw new BuildException( "Unable to find input for \'"+
request.getPrompt()+"\'");
request.setInput( o.toString());
if( !request.isInputValid())
throw new BuildException( "Found invalid input "+o+
" for \'"+request.getPrompt()+"\'");
}
}
To populate said project properties, we will use the parameters supplied in the Web application environment.
...
// servlet Init parameters
for( Enumeration en = getInitParameterNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
project.setUserProperty( key, getInitParameter( key));
}
// ServletContext
for( Enumeration en = context.getInitParameterNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
project.setUserProperty( key, context.getInitParameter( key));
}
for( Enumeration en = context.getAttributeNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
Object atr = context.getAttribute( key);
if( atr!=null) project.setUserProperty( key, atr.toString());
}
// HttpSession
for( Enumeration en = session.getAttributeNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
Object atr = session.getAttribute( key);
if( atr!=null) project.setUserProperty( key, atr.toString());
}
// Servlet Request
for( Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
Object atr = request.getAttribute( key);
if( atr!=null) project.setUserProperty( key, atr.toString());
}
for( Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
project.setProperty( key, request.getParameter( key));
}
// HttpServlet Request
for( Enumeration en = request.getHeaderNames(); en.hasMoreElements(); ) {
String key = ( String) en.nextElement();
project.setProperty( key, request.getHeader( key));
}
...