Friday March 14, 2008
Deployment History Window (Part 1)
I'm halfway done with Gareth Uren's suggestion, as recorded in my blog entry yesterday: "a drop down button factory displaying recently run projects that can then be selected and then immediately run again". It isn't implemented in a drop down button factory, but a window. And the entries in the window can't be redeployed yet (so, right now, only a new entry is generated upon deployment, no redeployment can be peformed yet). However, it's pretty cool to see the deployed app's name (and deployment time) added to the window upon deployment:
How is this done? Mainly via an Ant Logger. It's a little bit hacky, but I think it should do the job in 99% of cases. I locate the project.xml file, where the project name is set, and then identify the <name> element, which is the element that defines the name. That's all. The most important method, where everything happens, is the targetStarted method in the org.apache.tools.ant.module.spi.AntLogger class:
@Override
public void targetStarted(AntEvent event) {
BufferedReader br = null;
try {
//Identify the build-impl.xml file:
File buildImplXML = event.getScriptLocation();
//Hacky approach to getting the project.xml file instead:
File projectXML = new File(buildImplXML.getCanonicalPath().replace("build-impl.xml", "project.xml"));
//Read the project.xml file:
br = new BufferedReader(new InputStreamReader(new FileInputStream(projectXML)));
//Get the target's name, so we can test if it is the "run" target or not:
String name = event.getTargetName();
while ((inputLine = br.readLine()) != null) {
//If the target name is "run" and the line starts with <name>:
if (name.equals("run") && inputLine.trim().startsWith("<name>")) {
//Strip the line for the project name:
String projectName = inputLine.trim().replace("<name>", "").replace("</name>", "");
//Get today's date:
java.util.Date today = new Date();
//Create a timestamp:
Timestamp stamp = new java.sql.Timestamp(today.getTime());
//Send everything to the TopComponent, where the "set" method populates the JTextArea:
ListDeployedAppsTopComponent.setProjectNames(projectName + "\n (at" + stamp +")\n");
}
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
try {
br.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}
The next step is to let the user double-click an entry. When this is done, a Run target will be executed, with the project name as the application to be run. Or, at least, that's my plan.
Mar 14 2008, 02:15:40 PM PDT Permalink
File projectXML = new File(buildImplXML.getParentFile(), "project.xml");
Posted by Jesse Glick on March 17, 2008 at 10:02 AM PDT #
Consider also DateFormatter, use of XMLUtil.parse to obtain the parsed XML tree (plus Document.getElementsByTagName or an XPath query), and safer stream handling:
try {
InputStream is = new FileInputStream(projectXML);
try {
// use is...
} finally {
is.close();
}
} catch (IOException x) {...}
Posted by Jesse Glick on March 17, 2008 at 10:05 AM PDT #
Thanks a lot for these really helpful comments!
Posted by Geertjan on March 24, 2008 at 02:16 PM PDT #


