Nerd Notes

/dev/brain: no space left on device

Archive for November 2006

Struttin’ from the CLI

leave a comment »

Suppose you want to write a shell script that calls every action in your struts-config.xml in order to, say, stress test every callable url in your application.

Assume you have the following struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

The point here is that you want to extract the path attribute value for each action and complete it with a full blown URI. You could do that with a lot of grep and/or awk or you could just use xmlstarlet. So, go now, download a copy and install it, or fire up your favourite package manager (be it apt-get, synaptic, portage or whatever) and let it do the job for you.

Once you have xmlstarlet installed, just do:

$ xmlstarlet sel -t -m "/struts-config/action-mappings/action" \
-o http://localhost/ctx -v "@path" -o .do -n struts-config.xml

and you should get the following output:


This basically means that you can, for instance, use wget to grab all those URIs:

$ xmlstarlet sel -t \
-m "/struts-config/action-mappings/action" \
-o "wget http://localhost/ctx" \
-v "@path" \
-o .do -n \
struts-config.xml | sh

This will fire up wget for each action path in the struts-config.xml. Neat, uh?

Under the hood, xmlstarlet is simply using some xslt transformation on the xpath expressions given in the above template to do its work. So, in case you want to sharpen your xml tools, you’ll need to get a better understanding of xpath at least, since it has to be used in the select templates.


Written by Mirko Caserta

November 15, 2006 at 3:34 pm

Posted in CLI, struts, XML

UNIX time to human readable

with one comment

The current issue of the Gentoo Weekly Newsletter has a good clue on how to easily convert a date from the standard UNIX time format (which basically is a number which represents the number of seconds elapsed since January 1, 1970 at midnight, UTC) to a human readable form. The best solution is using date -d on Linux systems such as in:

$ date -d @1161911504
Fri Oct 27 03:11:44 CEST 2006

However, on BSD derived systems, such as Mac OS X (and maybe Solaris), date -d won’t work so you have to used instead:

$ date -r 1161911504
Fri Oct 27 03:11:44 CEST 2006

Written by Mirko Caserta

November 1, 2006 at 8:10 pm

Posted in CLI, Console, Linux, UNIX