1 package jp.co.areaweb.tools.command;
\r
4 import javax.xml.parsers.*;
\r
5 import org.w3c.dom.*;
\r
9 public static final boolean debug = false;
\r
12 * Jobファイルに記述されたコマンドを順次実行する。
\r
13 * @param jobFile Jobコマンドが記述されたファイル
\r
14 * @throws Exception JOBファイルの読み込み障害
\r
16 public static void execJob(File jobFile) throws Exception {
\r
17 execJob(jobFile, null);
\r
21 * Jobファイルに記述されたコマンドを順次実行する。
\r
22 * @param jobFile Jobコマンドが記述されたファイル
\r
23 * @param workDir コマンドを実行するさいのカレントディレクトリ
\r
24 * @throws Exception JOBファイルの読み込み障害
\r
26 public static void execJob(File jobFile, File workDir) throws Exception {
\r
27 if (jobFile == null) {
\r
28 throw new IOException("実行するジョブファイルが指定されていません。");
\r
32 System.out.println("[s JobFile] " + jobFile.getName());
\r
35 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
\r
36 DocumentBuilder builder = factory.newDocumentBuilder();
\r
37 Node rootNode = builder.parse(new FileInputStream(jobFile));
\r
38 NodeList nl = rootNode.getChildNodes();
\r
39 for (int i=0; i < nl.getLength(); i++) {
\r
40 Node jobgroupTag = nl.item(i);
\r
41 if (jobgroupTag.getNodeType() == Node.ELEMENT_NODE) {
\r
42 if ((jobgroupTag.getNodeName()).equals("jobgroup")) {
\r
43 execJobElement(jobFile, workDir, jobgroupTag.getChildNodes());
\r
45 else if ((jobgroupTag.getNodeName()).equals("xalan")) {
\r
46 Xalan.execJobElement(jobgroupTag.getChildNodes(), workDir);
\r
52 System.out.println("[e JobFile] " + jobFile.getName());
\r
57 * Jobファイルに記述されたコマンドを順次実行する。
\r
58 * @param jobFile Jobコマンドが記述されたファイル
\r
59 * @param workDir コマンドを実行するさいのカレントディレクトリ
\r
60 * @param jobgroupNodes NodeList
\r
61 * @throws Exception JOBファイルの読み込み障害
\r
63 protected static void execJobElement(File jobFile, File workDir, NodeList jobgroupNodes)
\r
66 System.out.println("XmlJob.execJobElement()");
\r
69 String stdout = null;
\r
70 String errout = null;
\r
71 Vector<Command> commands = new Vector<Command>();
\r
73 for (int i=0; i < jobgroupNodes.getLength(); i++) {
\r
74 Node node = jobgroupNodes.item(i);
\r
75 if (node.getNodeType() == Node.ELEMENT_NODE) {
\r
76 String tagName = node.getNodeName();
\r
77 if (tagName.equals("job")) {
\r
78 NodeList jobNodes = node.getChildNodes();
\r
79 Command command = new Command();
\r
80 for (int j=0; j < jobNodes.getLength(); j++) {
\r
81 Node jobNode = jobNodes.item(j);
\r
82 if (jobNode.getNodeType() == Node.ELEMENT_NODE) {
\r
83 String tagName2 = jobNode.getNodeName();
\r
84 if (tagName2.equals("cmd")) {
\r
85 command.setCmd(jobNode.getFirstChild().getNodeValue());
\r
87 else if (tagName2.equals("workDir")) {
\r
88 command.setWorkDir(new File(jobNode.getFirstChild().getNodeValue()));
\r
92 commands.addElement(command);
\r
94 else if (tagName.equals("stdout")) {
\r
95 NamedNodeMap attrs = node.getAttributes();
\r
96 stdout = attrs.getNamedItem("file").getNodeValue();
\r
98 else if (tagName.equals("errout")) {
\r
99 NamedNodeMap attrs = node.getAttributes();
\r
100 errout = attrs.getNamedItem("file").getNodeValue();
\r
106 execJobs(stdout, errout, commands);
\r
110 * Vector commandsに格納されたコマンドを順次実行する。
\r
111 * @param stdout string
\r
112 * @param errout string
\r
113 * @param commands Vector
\r
114 * @throws IOException JOBファイルの読み込み障害
\r
116 protected static void execJobs(String stdout, String errout, Vector<Command> commands) throws IOException {
\r
118 PrintWriter stdoutPW;
\r
119 if (stdout == null) {
\r
120 stdoutPW = new PrintWriter(System.out);
\r
123 stdoutPW = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(stdout))));
\r
127 for (int i=0; i < commands.size(); i++) {
\r
128 Command command = commands.get(i);
\r
129 command.execCommand();
\r
130 stdoutPW.println(command.getOutput());
\r
140 * @param args strings
\r
141 * @throws Exception error
\r
143 static public void main(String[] args) throws Exception {
\r
144 if (args.length < 1) {
\r
145 System.out.println("exp: java tool.job.XmlJob [jobFileName]");
\r
149 XmlJob.execJob(new File(args[0]));
\r