Brent Bain wrote:

I've tried the task that Peter Reilly attached to one of the emails about a
month ago for an XPath iterator but I can't seem to get it to work with the
current ant-contrib tasks.

I tested it with the latest ant-contrib - 1.0b1, you may have an older version in your path.
Also, it uses the java 1.5 xpath api.


If you just want to run the scripts, and not extract the names, ips or jvm names, you can do:
<macrodef name="startengine">
<attribute name="script"/>
<sequential>
<echo>Start a machine using script @{script}</echo>
</sequential>
</macrodef>
<typedef name="xpathiter" classname="XPathIterator"
classpath="classes"/>
<ac:for param="script">
<xpathiter expression="/servers/server/jvm/scripts/jvmStart/text()"
file="machines.xml" trim="yes"/>
<sequential>
<startengine script="@{script}"/>
</sequential>
</ac:for>



If you want to access the names and jvms, things are a little more complicated.
You will need to do two loops, one over the servers, and an inner one over
the jvms.


You may be able to use xmltask for this or you could use a script to do it.
For example with beanshell and java1.5 one can do this:

   <macrodef name="startengine">
     <attribute name="name"/>
     <attribute name="ip"/>
     <attribute name="jvm"/>
     <attribute name="script"/>
     <sequential>
       <echo>Start on @{name} (@{ip}) jvm @{jvm} script @{script}</echo>
     </sequential>
   </macrodef>

   <macrodef name="stopengine">
     <attribute name="name"/>
     <attribute name="ip"/>
     <attribute name="jvm"/>
     <attribute name="script"/>
     <sequential>
       <echo>stop on @{name} (@{ip}) jvm @{jvm} script @{script}</echo>
     </sequential>
   </macrodef>

   <scriptdef name="runscript" language="beanshell">
     <attribute name="macro"/>
     <attribute name="file"/>
     <attribute name="script"/>
     <![CDATA[
     import javax.xml.xpath.*;
     import org.xml.sax.*;
     import org.w3c.dom.*;
     import java.util.*;
     xpath = XPathFactory.newInstance().newXPath();

getString(obj, exp) {
return xpath.evaluate(exp, obj, XPathConstants.STRING);
}
getNodes(obj, exp) {
// Convert to a list
ret = new ArrayList();
nodes = xpath.evaluate(exp, obj, XPathConstants.NODESET);
for (i = 0; i < nodes.getLength(); ++i) {
ret.add(nodes.item(i));
}
return ret;
}
try {
source = new InputSource(attributes.get("file"));
for (server : getNodes(source, "/servers/server")) {
name = getString(server, "name/text()");
ip = getString(server, "ip/text()");
for (jvm : getNodes(server, "jvm")) {
jvmName = getString(jvm, "@name");
startScript = getString(jvm, "scripts/" + attributes.get("script") + "/text()");
macro = project.createTask(attributes.get("macro"));


                macro.setDynamicAttribute("name", name);
                macro.setDynamicAttribute("ip", ip);
                macro.setDynamicAttribute("jvm", jvmName);
                macro.setDynamicAttribute("script", startScript);

                macro.execute();
            }
        }
     } catch (Exception ex) {
        ex.printStackTrace();
     }
   ]]></scriptdef>

   <runscript file="machines.xml" script="jvmStart" macro="startengine"/>
   <runscript file="machines.xml" script="jvmStop" macro="stopengine"/>

resulting in:
xpath:
[echo] Start on Machine1 (172.16.1.24) jvm JVM1 script jvm1_startscript.sh
[echo] Start on Machine1 (172.16.1.24) jvm JVM3 script jvm3_startscript.sh
[echo] Start on Machine2 (172.16.1.25) jvm JVM2 script jvm2_startscript.sh
[echo] stop on Machine1 (172.16.1.24) jvm JVM1 script jvm1_stopscript.sh
[echo] stop on Machine1 (172.16.1.24) jvm JVM3 script jvm3_stopscript.sh
[echo] stop on Machine2 (172.16.1.25) jvm JVM2 script jvm2_stopscript.sh



Peter


 I've also thought about doing this with the xslt
task but that also seems like a headache (I can get a comma delimited
property for the different jvm names on a given server and then another xslt
step to actually pull out the script names that I will then execute -- but
that just seems clunky).

Has anyone else run into this type of problem or have some other solution
that I'm missing?  Any recommendations on the xml document (I can get IT to
change it if needed).  The number of servers/jvms will change depending on
any number of factors so it's not always just going to be 2 machines and 3
jvms...

In advance, my hair thanks you (I've been pulling it out all morning trying
to grasp how to handle this!).
Thanks,
Brent





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to