KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.mmbase.module.core.MMBaseContext;
13 import org.mmbase.util.*;
14 import org.mmbase.util.logging.*;
15
16 /**
17  * A background process that starts file copy commands.
18  * The process uses a {@link Queue} that holds {@link aFile2Copy} objects.
19  * The copier is started by file transfer VWMs such as PageMaster, who maintain
20  * the queue by filling it with information on files to transfer.
21  * FileCopier does not copy files itself, instead, it starts a {@link SCPcopy} class for each
22  * file to be copied.
23  *
24  * @author Rico Jansen
25  * @author Pierer van Rooden (javadocs)
26  */

27 public class FileCopier implements Runnable JavaDoc {
28
29     // logger
30
private static Logger log = Logging.getLoggerInstance(FileCopier.class.getName());
31
32     /**
33      * The thread reference.
34      * Setting this object to null stops the thread.
35      */

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

40     int sleepTime=8888;
41     /**
42      * Queue of {@link aFile2Copy} objects that need to be handled.
43      */

44     Queue files;
45
46     /**
47      * Constructor for the Filecopier
48      * @param files the {@link Queue} that holds (or will hold) the files to copy.
49      * The queue is maintained by the calling class.
50      */

51     public FileCopier(Queue files) {
52         this.files=files;
53         init();
54     }
55
56     /**
57      * Intializes the class.
58      * Starts the thread for this class.
59      */

60     public void init() {
61         this.start();
62     }
63
64
65     /**
66      * Starts the main Thread.
67      */

68     public void start() {
69         /* Start up the main thread */
70         if (kicker == null) {
71             kicker = MMBaseContext.startThread(this,"FileCopier");
72         }
73     }
74
75     /**
76      * Stops the main Thread.
77      */

78     public void stop() {
79         /* Stop thread */
80         kicker = null;
81     }
82
83     /**
84      * Main loop, exception-protected.
85      * @see #doWork
86      */

87     public void run () {
88         if (kicker!=null) {
89             try {
90                 doWork();
91             } catch(Exception JavaDoc e) {
92                 log.error(Logging.stackTrace(e));
93             }
94         }
95     }
96
97     /**
98      * Main work loop.
99      * Checks the Queue, and if a new object is available, creates a new {@link SCPcopy} object to
100      * handle the transfer.
101      */

102     public void doWork() {
103         //String sshpath="/sr/local/bin";
104
aFile2Copy afile;
105
106         log.debug("Active");
107         while (kicker!=null) {
108             try {
109                 afile=(aFile2Copy)files.get();
110                 if (afile!=null) {
111                     log.info("Copying "+afile.srcpath+"/"+afile.filename);
112                     SCPcopy scpcopy=new SCPcopy(afile.sshpath,afile.dstuser,afile.dsthost,afile.dstpath);
113                     scpcopy.copy(afile.srcpath,afile.filename);
114                 } else {
115                     log.error("afile is null ?");
116                 }
117             } catch (InterruptedException JavaDoc e) {
118                 log.debug(Thread.currentThread().getName() +" was interruped.");
119                 break;
120             }
121         }
122     }
123 }
124
Popular Tags