1 package org.oddjob; 2 3 import java.io.File ; 4 import java.lang.reflect.Array ; 5 import java.util.Enumeration ; 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 21 public class Main { 22 private static final Logger logger = Logger.getLogger(Main.class); 23 24 30 public Oddjob init(String args[]) { 31 32 String oddjobFile = null; 33 String name = null; 34 String logConfig = null; 35 36 int startArg = 0; 37 38 for (int i = 0; i < args.length; i++) { 40 String 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 Enumeration 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 (oddjobFile)); 76 if (name == null) { 77 name = "Oddjob"; 78 } 79 oddjob.setName(name); 80 81 Object newArray = Array.newInstance(String .class, args.length - startArg); 83 System.arraycopy(args, startArg, newArray, 0, args.length - startArg); 84 oddjob.setArgs((String []) newArray); 85 86 return oddjob; 87 } 88 89 94 public void conifgureLog(String logConfigFileName) { 95 System.setProperty("log4j.defaultInitOverride", "true"); 96 PropertyConfigurator.configure(logConfigFileName); 97 } 98 99 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 { 111 private final Oddjob oddjob; 112 public ShutdownHook(Oddjob oddjob) { 113 this.oddjob = oddjob; 114 } 115 116 public void run() { 117 Thread killer = new Thread (new Runnable () { 118 public void run() { 119 try { 120 Thread.sleep(5000); 121 } 122 catch (InterruptedException 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 Runtime.getRuntime().halt(1); 155 } 156 } 157 } 158 159 164 public static void main(String [] 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 t) { 175 logger.fatal("Exception running Oddjob.", t); 176 Runtime.getRuntime().halt(1); 179 } 180 } 181 } 182 | Popular Tags |