KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > vwms > ImagePusher


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders.vwms;
11
12 import java.util.*;
13
14 import org.mmbase.util.Queue;
15 import org.mmbase.util.logging.*;
16
17 /**
18  * A class used to schedule images for transfer.
19  * ImagePusher periodically (approx. every 4 seconds) checks it's parent's ({@link ImageMaster}) filelist,
20  * and pushes the fields in that list on a {@link Queue}, which is, in turn, handled by a {@link FileCopier}
21  * instance.<br />
22  * The reason that this is done by ImagePusher, and not ImageMaster, is that this way double requests
23  * for the same file can be filtered out. This seems useful, so perhaps {@link PageMaster} might need to use this
24  * scheduling system too (or otherwise this should be build into FileCopier).
25  *
26  * @author Rico Jansen
27  * @author Pierre van Rooden (javadocs)
28  * @version $Id: ImagePusher.java,v 1.10 2004/02/11 20:43:23 keesj Exp $
29  */

30 public class ImagePusher implements Runnable JavaDoc {
31
32     private static final Logger log = Logging.getLoggerInstance(ImagePusher.class);
33
34     /**
35      * The thread reference.
36      * Setting this object to null stops the thread.
37      */

38     Thread JavaDoc kicker = null;
39     /**
40      * Sleeptime for this thread. unused as sleeping is arranged by the Queue object.
41      */

42     int sleepTime=4444;
43     /**
44      * The class that started the pusher ({@link ImageMaster}).
45      * Used to retrieve the filelist that needs to be handled.
46      */

47     ImageMaster parent;
48
49     /**
50     * Queue containing the file-copy tasks that need to be performed by {@link FileCopier}
51     */

52     Queue files2copy=new Queue(128);
53
54     /**
55      * Thread that handles the actual file transfers.
56      */

57     FileCopier filecopier=new FileCopier(files2copy);
58
59     /**
60      * Constructor for the ImagePusher
61      * @param parent The parent that contains and adds the files to handle.
62      */

63     public ImagePusher(ImageMaster parent) {
64         this.parent=parent;
65         init();
66     }
67
68     /**
69      * Initializes the insatnce by starting a thread.
70      */

71     public void init() {
72         this.start();
73     }
74
75
76     /**
77      * Starts the main Thread.
78      */

79     public void start() {
80         /* Start up the main thread */
81         if (kicker == null) {
82             kicker = new Thread JavaDoc(this,"Image");
83             kicker.setDaemon(true);
84             kicker.start();
85         }
86     }
87
88     /**
89      * Stops the main Thread.
90      */

91     public void stop() {
92         /* Stop thread */
93         kicker = null;
94     }
95
96     /**
97      * Main loop, exception protected
98      */

99     public void run () {
100         if (kicker!=null) {
101             try {
102                 doWork();
103             } catch(Exception JavaDoc e) {
104                 e.printStackTrace();
105             }
106         }
107     }
108
109     /**
110      * Main work loop.
111      * Periodically checks the files field of its parent. If new files need to be transferred,
112      * the files in the parent are cleared. The original list is then purged of duplicates
113      * before they are stored in the Queue. A seperate thread ({@link FileCopier}) then takes care
114      * of the actual transfer.
115      */

116       public void doWork() {
117         Vector procfiles;
118         aFile2Copy file;
119         Hashtable files;
120
121         log.info("ImagePusher Active");
122         while (kicker!=null) {
123             try { Thread.sleep(sleepTime); } catch (InterruptedException JavaDoc e) {}
124             if (parent.files.size()>0) {
125                 synchronized(parent.files) {
126                     procfiles=parent.files;
127                     parent.files=new Vector();
128                 }
129                 log.service("ImagePusher processing "+procfiles.size()+" files");
130                 files=killdups(procfiles);
131                 for (Enumeration e =files.keys();e.hasMoreElements();) {
132                     file=(aFile2Copy)e.nextElement();
133                     files2copy.append(file);
134                 }
135             }
136         }
137     }
138
139     /**
140      * Removes duplicates from a list of files.
141      * @param Vector files that need to be checked for duplicates
142      * @return a <code>Hashtable</code> whose keys contain the files (without duplicates).
143      * Would probably be a bit more clear if the keys are returned instead.
144      */

145     private Hashtable killdups(Vector files) {
146         Hashtable hfiles=new Hashtable(files.size());
147         aFile2Copy file;
148         for (Enumeration e=files.elements();e.hasMoreElements();) {
149             file=(aFile2Copy)e.nextElement();
150             hfiles.put(file,"feep");
151         }
152         log.info("ImagePusher -> "+files.size()+" - "+hfiles.size()+" dups "+(files.size()-hfiles.size()));
153         return hfiles;
154     }
155 }
156
Popular Tags