KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > weblech > ui > TextSpider


1 /*
2  * This is the MIT license, see also http://www.opensource.org/licenses/mit-license.html
3  *
4  * Copyright (c) 2001 Brian Pitcher
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */

24
25 // $Header: /cvsroot/weblech/weblech/src/weblech/ui/TextSpider.java,v 1.1 2002/06/09 11:34:38 weblech Exp $
26

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 JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 import org.apache.log4j.Category;
40
41 public class TextSpider implements Constants
42 {
43     /** For class-related messages */
44     private static Category _logClass = Category.getInstance(TextSpider.class);
45
46     public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc props = null;
74         try
75         {
76             FileInputStream JavaDoc propsIn = new FileInputStream JavaDoc(propsFile);
77             props = new Properties JavaDoc();
78             props.load(propsIn);
79             propsIn.close();
80         }
81         catch(FileNotFoundException JavaDoc fnfe)
82         {
83             _logClass.error("File not found: " + args[0], fnfe);
84             System.exit(1);
85         }
86         catch(IOException JavaDoc 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 JavaDoc 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 JavaDoc 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 } // End class TextSpider
144
Popular Tags