KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Abstracts automatic updating of the comics.
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.Writer JavaDoc;
35 import java.util.*;
36
37 public class ComicsUpdater
38 {
39     private static final Map locks = Collections.synchronizedMap(new HashMap());
40
41     /**
42      * Check all comics for new strips. This function does not return until all the threads have finished.
43      */

44     public static void updateAll(ComicManager cmgr, Writer JavaDoc out, long verbosityLevel, boolean debug) throws Exception JavaDoc
45     {
46         Comic comics[] = cmgr.getComics();
47         List threads = new ArrayList();
48         for (int i = 0; i < comics.length; i++)
49         {
50             Thread JavaDoc 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 JavaDoc)threads.get(i)).join();
57
58         if (out != null)out.flush();
59     }
60     public static void updateAll(ComicManager cmgr) throws Exception JavaDoc
61     {
62         updateAll(cmgr, null, 0, false);
63     }
64
65     /**
66      * Update a specific strip.
67      */

68     public static Thread JavaDoc update(ComicManager cmgr, long comicID, Writer JavaDoc out, long verbosityLevel, boolean debug) throws Exception JavaDoc
69     {
70         Long JavaDoc id = new Long JavaDoc(comicID);
71         if (!locks.containsKey(id))
72             locks.put(id, new Object JavaDoc());
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 JavaDoc 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 JavaDoc thread = new Thread JavaDoc(system);
92                         thread.start();
93                         return thread;
94                     }
95                     catch (ClassNotFoundException JavaDoc 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