1 24 25 27 package weblech.ui; 28 29 import weblech.spider.SpiderConfig; 30 import weblech.spider.Spider; 31 import weblech.spider.Constants; 32 import weblech.util.Logger; 33 34 import java.util.Properties ; 35 import java.io.FileInputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.IOException ; 38 39 import org.apache.log4j.Category; 40 41 public class TextSpider implements Constants 42 { 43 44 private static Category _logClass = Category.getInstance(TextSpider.class); 45 46 public static void main(String [] args) 47 { 48 _logClass.debug("main()"); 49 50 if(args.length < 1 || args.length > 2) 51 { 52 usage(); 53 System.exit(0); 54 } 55 56 String propsFile = null; 57 boolean resume = false; 58 if(args.length == 1) 59 { 60 propsFile = args[0]; 61 } 62 else if(!args[0].equals("-resume")) 63 { 64 usage(); 65 System.exit(0); 66 } 67 else 68 { 69 resume = true; 70 propsFile = args[1]; 71 } 72 73 Properties props = null; 74 try 75 { 76 FileInputStream propsIn = new FileInputStream (propsFile); 77 props = new Properties (); 78 props.load(propsIn); 79 propsIn.close(); 80 } 81 catch(FileNotFoundException fnfe) 82 { 83 _logClass.error("File not found: " + args[0], fnfe); 84 System.exit(1); 85 } 86 catch(IOException ioe) 87 { 88 _logClass.error("IO Exception caught reading config file: " + ioe.getMessage(), ioe); 89 System.exit(1); 90 } 91 92 _logClass.debug("Configuring Spider from properties"); 93 SpiderConfig config = new SpiderConfig(props); 94 _logClass.debug(config); 95 Spider spider = new Spider(config); 96 97 if(resume) 98 { 99 _logClass.info("Reading checkpoint..."); 100 spider.readCheckpoint(); 101 } 102 103 _logClass.info("Starting Spider..."); 104 spider.start(); 105 106 System.out.println("\nHit any key to stop Spider\n"); 107 try 108 { 109 while(spider.isRunning()) 110 { 111 if(System.in.available() != 0) 112 { 113 System.out.println("\nStopping Spider...\n"); 114 spider.stop(); 115 break; 116 } 117 pause(SPIDER_STOP_PAUSE); 118 } 119 } 120 catch(IOException ioe) 121 { 122 _logClass.error("Unexpected exception caught: " + ioe.getMessage(), ioe); 123 System.exit(1); 124 } 125 } 126 127 private static void pause(long howLong) 128 { 129 try 130 { 131 Thread.sleep(howLong); 132 } 133 catch(InterruptedException ignored) 134 { 135 } 136 } 137 138 private static void usage() 139 { 140 System.out.println("Usage: weblech.ui.TextSpider [-resume] [config file]"); 141 } 142 143 } | Popular Tags |