KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > Main


1 package org.oddjob;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.Array JavaDoc;
5 import java.util.Enumeration JavaDoc;
6
7 import org.apache.log4j.ConsoleAppender;
8 import org.apache.log4j.Level;
9 import org.apache.log4j.Logger;
10 import org.apache.log4j.PatternLayout;
11 import org.apache.log4j.PropertyConfigurator;
12 import org.oddjob.state.JobState;
13 import org.oddjob.util.OddjobLockedException;
14
15
16 /**
17  * This is a very simple wrapper around Oddjob with a main method.
18  *
19  * @author Rob Gordon
20  */

21 public class Main {
22     private static final Logger logger = Logger.getLogger(Main.class);
23     
24     /**
25      * Parse the command args and configure Oddjob.
26      *
27      * @param args The args.
28      * @return A configured and ready to run Oddjob.
29      */

30     public Oddjob init(String JavaDoc args[]) {
31         
32         String JavaDoc oddjobFile = null;
33         String JavaDoc name = null;
34         String JavaDoc logConfig = null;
35         
36         int startArg = 0;
37         
38         // cycle through given args
39
for (int i = 0; i < args.length; i++) {
40             String JavaDoc arg = args[i];
41
42             if (arg.equals("-help") || arg.equals("-h")) {
43                 usage();
44                 return null;
45             } else if (arg.equals("-n") || arg.equals("-name")) {
46                 name = args[++i];
47                 startArg += 2;
48             } else if (arg.equals("-l") || arg.equals("-log")) {
49                 logConfig = args[++i];
50                 startArg += 2;
51             } else if (arg.equals("-f") || arg.equals("-file")) {
52                 oddjobFile = args[++i];
53                 startArg += 2;
54             }
55         }
56
57         if (logConfig != null) {
58             conifgureLog(logConfig);
59         }
60         // crude attempt to see if logging is initialise either statically or via
61
// a file. If it hasn't set a default so we don't get warnings.
62
Enumeration JavaDoc enumeration = Logger.getRootLogger().getAllAppenders();
63         boolean hasAppenders = enumeration.hasMoreElements();
64         if (!hasAppenders) {
65             Logger.getRoot().addAppender(new ConsoleAppender(
66                     new PatternLayout("%-5p %m%n")));
67             Logger.getRoot().setLevel(Level.ERROR);
68         }
69         
70         Oddjob oddjob = new Oddjob();
71         
72         if (oddjobFile == null) {
73             oddjobFile = "oddjob.xml";
74         }
75         oddjob.setFile(new File JavaDoc(oddjobFile));
76         if (name == null) {
77             name = "Oddjob";
78         }
79         oddjob.setName(name);
80         
81         // pass remaining args into Oddjob.
82
Object JavaDoc newArray = Array.newInstance(String JavaDoc.class, args.length - startArg);
83         System.arraycopy(args, startArg, newArray, 0, args.length - startArg);
84         oddjob.setArgs((String JavaDoc[]) newArray);
85         
86         return oddjob;
87     }
88
89     /**
90      * Configure logging from a log file.
91      *
92      * @param logConfigFileName The log file name.
93      */

94     public void conifgureLog(String JavaDoc logConfigFileName) {
95         System.setProperty("log4j.defaultInitOverride", "true");
96         PropertyConfigurator.configure(logConfigFileName);
97     }
98     
99     /**
100      * Display usage info.
101      *
102      */

103     public void usage() {
104         System.out.println("usage: oddjob [options]");
105         System.out.println("-f -file job file.");
106         System.out.println("-n -name Oddjob name.");
107         System.out.println("-l -log log4j properties file.");
108     }
109     
110     public static class ShutdownHook extends Thread JavaDoc {
111         private final Oddjob oddjob;
112         public ShutdownHook(Oddjob oddjob) {
113             this.oddjob = oddjob;
114         }
115         
116         public void run() {
117             Thread JavaDoc killer = new Thread JavaDoc(new Runnable JavaDoc() {
118                 public void run() {
119                     try {
120                         Thread.sleep(5000);
121                     }
122                     catch (InterruptedException JavaDoc e) {
123                         return;
124                     }
125                     Runtime.getRuntime().halt(-1);
126                 }
127             });
128             killer.start();
129             oddjob.stop();
130             while (oddjob.lastJobStateEvent().getJobState() == JobState.EXECUTING) {
131                 logger.debug("Oddjob not stoped, yeilding.");
132                 Thread.yield();
133             }
134             JobState state = oddjob.lastJobStateEvent().getJobState();
135             boolean destroyed = false;
136             while (!destroyed) {
137                 try {
138                     oddjob.destroy();
139                     destroyed = true;
140                 } catch (OddjobLockedException e) {
141                     logger.debug("Oddjob locked during destroy, yeilding.");
142                     Thread.yield();
143                 }
144             }
145             killer.interrupt();
146             if (state == JobState.EXCEPTION) {
147                 logger.error("Oddjob complete. State [" + state + "].",
148                     oddjob.lastJobStateEvent().getException());
149             } else {
150                 logger.info("Oddjob complete. State [" + state + "].");
151             }
152             if (state != JobState.COMPLETE) {
153                 // really really bad but how else to get the state out.
154
Runtime.getRuntime().halt(1);
155             }
156         }
157     }
158     
159     /**
160      * The main.
161      *
162      * @param args The command line args.
163      */

164     public static void main(String JavaDoc[] args) {
165
166         Main ojm = new Main();
167         Oddjob oddjob = ojm.init(args);
168         if (oddjob == null) {
169             return;
170         }
171         Runtime.getRuntime().addShutdownHook(new ShutdownHook(oddjob));
172         try {
173             oddjob.run();
174         } catch (Throwable JavaDoc t) {
175             logger.fatal("Exception running Oddjob.", t);
176             // uses halt, not exit as we don't want to invoke the
177
// shutdown hook
178
Runtime.getRuntime().halt(1);
179         }
180     }
181 }
182
Popular Tags