1 25 26 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 ; 35 import java.io.Writer ; 36 import java.net.HttpURLConnection ; 37 import java.net.URL ; 38 import java.util.StringTokenizer ; 39 40 public abstract class ComicsSystem implements Runnable 41 { 42 private boolean initialized = false; 43 44 protected ComicManager cmgr; 45 protected Comic comic; 46 protected Writer out; 47 protected long verbosityLevel = 0; 48 protected boolean debug = false; 49 50 53 public final void initialize(ComicManager cmgr, Comic comic, Writer 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 ("initialize() not called"); 68 69 update(getArguments(comic.system)); 70 } 71 72 75 public static final String [] getArguments(String argumentString) 76 { 77 return getArguments(argumentString, "\n\r"); 78 } 79 public static final String [] getArguments(String argumentString, String delim) 80 { 81 StringTokenizer strtok = new StringTokenizer (argumentString, delim); 82 83 int tokens = strtok.countTokens(); 84 String r[] = new String [tokens]; 85 for (int i = 0; i < tokens; i++) 86 r[i] = strtok.nextToken(); 87 88 return r; 89 } 90 91 94 public boolean URLExists(URL url) 95 { 96 try 97 { 98 HttpURLConnection con = (HttpURLConnection )url.openConnection(); 99 if (con.getResponseCode() == HttpURLConnection.HTTP_OK) 100 { 101 java.io.InputStream in = con.getInputStream(); 103 if (in.available() == 0) 104 return false; 105 else 106 return true; 107 } 108 } 109 catch (Exception e) 110 { 111 if (verbosityLevel > 10) 112 { 113 PrintWriter w = new PrintWriter (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 w = new PrintWriter (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 s) 132 { 133 if (out != null) 134 { 135 try 136 { 137 out.write(s); 138 out.flush(); 139 } 140 catch (java.io.IOException e){} 141 } 142 } 143 144 public void logln(String 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 e){} 155 } 156 } 157 158 public abstract void update(String arguments[]); 159 } | Popular Tags |