1 37 import java.util.Properties ; 38 import java.io.FileNotFoundException ; 39 40 import org.apache.log4j.PropertyConfigurator; 41 import org.webharvest.definition.ScraperConfiguration; 42 import org.webharvest.runtime.Scraper; 43 44 public class CommandLine { 45 46 private static String getArgValue(String [] args, String name) { 47 for (int i = 0; i < args.length; i++) { 48 String curr = args[i]; 49 int eqIndex = curr.indexOf('='); 50 if (eqIndex >= 0) { 51 String argName = curr.substring(0, eqIndex).trim(); 52 String argValue = curr.substring(eqIndex+1).trim(); 53 54 if (argName.toLowerCase().startsWith(name.toLowerCase())) { 55 return argValue; 56 } 57 } 58 } 59 60 return ""; 61 } 62 63 public static void main(String [] args) throws FileNotFoundException { 64 String configFilePath = getArgValue(args, "config"); 65 if ("".equals(configFilePath)) { 66 System.err.println("You must specify configuration file path using config=<path> argument!"); 67 System.exit(1); 68 } 69 70 String workingDir = getArgValue(args, "workdir"); 71 if ("".equals(workingDir)) { 72 System.err.println("You must specify working directory path using workdir=<path> argument!"); 73 System.exit(1); 74 } 75 76 String isDebug = getArgValue(args, "debug"); 77 78 Properties props = new Properties (); 79 props.setProperty("log4j.rootLogger", "INFO, stdout"); 80 props.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender"); 81 props.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout"); 82 props.setProperty("log4j.appender.stdout.layout.ConversionPattern", "%-5p (%20F:%-3L) - %m\n"); 83 84 props.setProperty("log4j.appender.file", "org.apache.log4j.DailyRollingFileAppender"); 85 props.setProperty("log4j.appender.file.File", workingDir + "/out.log"); 86 props.setProperty("log4j.appender.file.DatePattern", "yyyy-MM-dd"); 87 props.setProperty("log4j.appender.file.layout", "org.apache.log4j.PatternLayout"); 88 props.setProperty("log4j.appender.file.layout.ConversionPattern", "%-5p (%20F:%-3L) - %m\n"); 89 90 PropertyConfigurator.configure(props); 91 92 ScraperConfiguration config = new ScraperConfiguration(configFilePath); 93 Scraper scraper = new Scraper(config, workingDir); 94 95 if ("yes".equalsIgnoreCase(isDebug)) { 96 scraper.setDebug(true); 97 } 98 99 String proxyHost = getArgValue(args, "proxyHost"); 100 if (!"".equals(proxyHost)) { 101 String proxyPort = getArgValue(args, "proxyPort"); 102 if (!"".equals(proxyPort)) { 103 int port = Integer.parseInt(proxyPort); 104 scraper.getHttpClientManager().setHttpProxy(proxyHost, port); 105 } else { 106 scraper.getHttpClientManager().setHttpProxy(proxyHost); 107 } 108 } 109 110 String proxyUser = getArgValue(args, "proxyUser"); 111 if (!"".equals(proxyUser)) { 112 String proxyPassword = getArgValue(args, "proxyPassword"); 113 scraper.getHttpClientManager().setHttpProxyCredentials(proxyUser, proxyPassword); 114 } 115 116 scraper.execute(); 117 } 118 119 } | Popular Tags |