KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > comics > ComicsSystem


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Abstract base class for comic systems. These are the classes that locates new strips and adds them to the database.
28  */

29 package net.killingar.forum.comics;
30
31 import net.killingar.forum.internal.Comic;
32 import net.killingar.forum.internal.managers.ComicManager;
33
34 import java.io.PrintWriter JavaDoc;
35 import java.io.Writer JavaDoc;
36 import java.net.HttpURLConnection JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39
40 public abstract class ComicsSystem implements Runnable JavaDoc
41 {
42     private boolean initialized = false;
43
44     protected ComicManager cmgr;
45     protected Comic comic;
46     protected Writer JavaDoc out;
47     protected long verbosityLevel = 0;
48     protected boolean debug = false;
49
50     /**
51      * Set the parameters needed to run the update.
52      */

53     public final void initialize(ComicManager cmgr, Comic comic, Writer JavaDoc out, long verbosityLevel, boolean debug)
54     {
55         this.cmgr = cmgr;
56         this.comic = comic;
57         this.out = out;
58         this.verbosityLevel = verbosityLevel;
59         this.debug = debug;
60
61         initialized = true;
62     }
63
64     public final void run()
65     {
66         if (!initialized)
67             throw new RuntimeException JavaDoc("initialize() not called");
68
69         update(getArguments(comic.system));
70     }
71
72     /**
73      * Get the arguments from a string (tokenize it with "\n\r")
74      */

75     public static final String JavaDoc[] getArguments(String JavaDoc argumentString)
76     {
77         return getArguments(argumentString, "\n\r");
78     }
79     public static final String JavaDoc[] getArguments(String JavaDoc argumentString, String JavaDoc delim)
80     {
81         StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(argumentString, delim);
82
83         int tokens = strtok.countTokens();
84         String JavaDoc r[] = new String JavaDoc[tokens];
85         for (int i = 0; i < tokens; i++)
86             r[i] = strtok.nextToken();
87
88         return r;
89     }
90
91     /**
92      * Check if an URL exists. Returns false if the document found is 0 bytes.
93      */

94     public boolean URLExists(URL JavaDoc url)
95     {
96         try
97         {
98             HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)url.openConnection();
99             if (con.getResponseCode() == HttpURLConnection.HTTP_OK)
100             {
101                 // if the document is 0 bytes, we count it as a failure.
102
java.io.InputStream JavaDoc in = con.getInputStream();
103                 if (in.available() == 0)
104                     return false;
105                 else
106                     return true;
107             }
108         }
109         catch (Exception JavaDoc e)
110         {
111             if (verbosityLevel > 10)
112             {
113                 PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
114                 w.println("exception while trying to find url (");
115                 w.println(url);
116                 w.println("):");
117                 e.printStackTrace(w);
118             }
119         }
120
121         if (verbosityLevel > 10)
122         {
123             PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
124             w.println("could not find URL (");
125             w.println(url);
126             w.println(")");
127         }
128         return false;
129     }
130
131     public void log(String JavaDoc s)
132     {
133         if (out != null)
134         {
135             try
136             {
137                 out.write(s);
138                 out.flush();
139             }
140             catch (java.io.IOException JavaDoc e){}
141         }
142     }
143
144     public void logln(String JavaDoc s)
145     {
146         if (out != null)
147         {
148             try
149             {
150                 out.write(s);
151                 out.write("\n");
152                 out.flush();
153             }
154             catch (java.io.IOException JavaDoc e){}
155         }
156     }
157
158     public abstract void update(String JavaDoc arguments[]);
159 }
Popular Tags