001: /*
002: * This is the MIT license, see also http://www.opensource.org/licenses/mit-license.html
003: *
004: * Copyright (c) 2001 Brian Pitcher
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in
014: * all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022: * SOFTWARE.
023: */
024:
025: // $Header: /cvsroot/weblech/weblech/src/weblech/ui/TextSpider.java,v 1.1 2002/06/09 11:34:38 weblech Exp $
026: package weblech.ui;
027:
028: import weblech.spider.SpiderConfig;
029: import weblech.spider.Spider;
030: import weblech.spider.Constants;
031: import weblech.util.Logger;
032:
033: import java.util.Properties;
034: import java.io.FileInputStream;
035: import java.io.FileNotFoundException;
036: import java.io.IOException;
037:
038: import org.apache.log4j.Category;
039:
040: public class TextSpider implements Constants {
041: /** For class-related messages */
042: private static Category _logClass = Category
043: .getInstance(TextSpider.class);
044:
045: public static void main(String[] args) {
046: _logClass.debug("main()");
047:
048: if (args.length < 1 || args.length > 2) {
049: usage();
050: System.exit(0);
051: }
052:
053: String propsFile = null;
054: boolean resume = false;
055: if (args.length == 1) {
056: propsFile = args[0];
057: } else if (!args[0].equals("-resume")) {
058: usage();
059: System.exit(0);
060: } else {
061: resume = true;
062: propsFile = args[1];
063: }
064:
065: Properties props = null;
066: try {
067: FileInputStream propsIn = new FileInputStream(propsFile);
068: props = new Properties();
069: props.load(propsIn);
070: propsIn.close();
071: } catch (FileNotFoundException fnfe) {
072: _logClass.error("File not found: " + args[0], fnfe);
073: System.exit(1);
074: } catch (IOException ioe) {
075: _logClass.error("IO Exception caught reading config file: "
076: + ioe.getMessage(), ioe);
077: System.exit(1);
078: }
079:
080: _logClass.debug("Configuring Spider from properties");
081: SpiderConfig config = new SpiderConfig(props);
082: _logClass.debug(config);
083: Spider spider = new Spider(config);
084:
085: if (resume) {
086: _logClass.info("Reading checkpoint...");
087: spider.readCheckpoint();
088: }
089:
090: _logClass.info("Starting Spider...");
091: spider.start();
092:
093: System.out.println("\nHit any key to stop Spider\n");
094: try {
095: while (spider.isRunning()) {
096: if (System.in.available() != 0) {
097: System.out.println("\nStopping Spider...\n");
098: spider.stop();
099: break;
100: }
101: pause(SPIDER_STOP_PAUSE);
102: }
103: } catch (IOException ioe) {
104: _logClass.error("Unexpected exception caught: "
105: + ioe.getMessage(), ioe);
106: System.exit(1);
107: }
108: }
109:
110: private static void pause(long howLong) {
111: try {
112: Thread.sleep(howLong);
113: } catch (InterruptedException ignored) {
114: }
115: }
116:
117: private static void usage() {
118: System.out
119: .println("Usage: weblech.ui.TextSpider [-resume] [config file]");
120: }
121:
122: } // End class TextSpider
|