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.Writer ; 35 import java.util.*; 36 37 public class ComicsUpdater 38 { 39 private static final Map locks = Collections.synchronizedMap(new HashMap()); 40 41 44 public static void updateAll(ComicManager cmgr, Writer out, long verbosityLevel, boolean debug) throws Exception 45 { 46 Comic comics[] = cmgr.getComics(); 47 List threads = new ArrayList(); 48 for (int i = 0; i < comics.length; i++) 49 { 50 Thread t = update(cmgr, comics[i].ID, out, verbosityLevel, debug); 51 if (t != null) 52 threads.add(t); 53 } 54 55 for (int i = 0; i < threads.size(); i++) 56 ((Thread )threads.get(i)).join(); 57 58 if (out != null)out.flush(); 59 } 60 public static void updateAll(ComicManager cmgr) throws Exception 61 { 62 updateAll(cmgr, null, 0, false); 63 } 64 65 68 public static Thread update(ComicManager cmgr, long comicID, Writer out, long verbosityLevel, boolean debug) throws Exception 69 { 70 Long id = new Long (comicID); 71 if (!locks.containsKey(id)) 72 locks.put(id, new Object ()); 73 74 75 synchronized(locks.get(id)) 76 { 77 Comic comic = cmgr.getComic(comicID); 78 79 if (comic != null) 80 { 81 if (comic.system != null) 82 { 83 StringTokenizer strtok = new StringTokenizer(comic.system, "\n\r"); 84 85 String systemName = strtok.nextToken(); 86 try 87 { 88 ComicsSystem system = (ComicsSystem)Class.forName(systemName).newInstance(); 89 system.initialize(cmgr, comic, out, verbosityLevel, debug); 90 if (out != null){out.write("Updating "+comic.name+"\n"); out.flush();} 91 Thread thread = new Thread (system); 92 thread.start(); 93 return thread; 94 } 95 catch (ClassNotFoundException e) 96 { 97 if (out != null){out.write("Class not found: "+systemName+"\n");out.flush();} 98 } 99 } 100 } 101 else 102 { 103 if (out != null){out.write("Error getting comic data\n");out.flush();} 104 } 105 } 106 return null; 107 } 108 } | Popular Tags |