KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > mirroring > Job


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.mirroring;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import net.sf.drftpd.master.RemoteSlave;
27 import net.sf.drftpd.master.usermanager.User;
28 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
29
30 /**
31  * @author zubov
32  * @author mog
33  * @version $Id: Job.java,v 1.19.2.2 2004/06/24 20:40:30 zubov Exp $
34  */

35 public class Job {
36     protected int _transferNum;
37     protected Set JavaDoc _destSlaves;
38     protected LinkedRemoteFileInterface _file;
39     protected User _owner;
40     protected int _priority;
41     protected Object JavaDoc _source;
42     protected long _timeCreated;
43     protected long _timeSpent;
44
45     public Job(
46         LinkedRemoteFileInterface file,
47         Set JavaDoc destSlaves,
48         Object JavaDoc source,
49         User owner,
50         int priority,
51         int transferNum) {
52         super();
53         _destSlaves = new HashSet JavaDoc(destSlaves);
54         _file = file;
55         _source = source;
56         _owner = owner;
57         _priority = priority;
58         _timeCreated = System.currentTimeMillis();
59         _timeSpent = 0;
60         _transferNum = transferNum;
61         if (_transferNum > destSlaves.size())
62             throw new IllegalArgumentException JavaDoc("transferNum cannot be greater than destSlaves.size()");
63     }
64
65     public void addTimeSpent(long time) {
66         _timeSpent += time;
67     }
68
69     /**
70      * Returns a List of slaves that can be used with {@see net.sf.drftpd.master.SlaveManagerImpl#getASlave(Collection, char, FtpConfig)}
71      */

72     public Set JavaDoc getDestinationSlaves() {
73         return Collections.unmodifiableSet(_destSlaves);
74     }
75
76     /**
77      * Returns the file (or directory, if directories can be submitted as jobs,) for this job.
78      * This file is used to tell the slaves what file to transfer & receive.
79      */

80     public LinkedRemoteFileInterface getFile() {
81         return _file;
82     }
83
84     /**
85      * Returns user that is the owner of this file/job or null (or exception) if not applicable.
86      */

87     public User getOwner() {
88         return _owner;
89     }
90
91     /**
92      * Returns the priority of this job.
93      */

94     public int getPriority() {
95         return _priority;
96     }
97
98     /**
99      * Instance that submitted this object.
100      *
101      * For example so that Archive instance can see if this job was submitted by itself
102      *
103      * @see java.util.EventObject#getSource()
104      */

105     public Object JavaDoc getSource() {
106         return _source;
107     }
108
109     /**
110      * This is the time that the job was created
111      */

112     public long getTimeCreated() {
113         return _timeCreated;
114     }
115     /**
116      * This is the amount of time spent processing this job
117      */

118     public long getTimeSpent() {
119         return _timeSpent;
120     }
121
122     /**
123      * returns true if this job has nothing more to send
124      */

125     public boolean isDone() {
126         return _transferNum < 1;
127     }
128
129     public synchronized void sentToSlave(RemoteSlave slave) {
130         if (_destSlaves.remove(slave)) {
131             _transferNum--;
132         } else {
133             throw new IllegalArgumentException JavaDoc("Slave " + slave.getName() + " does not exist as a destination slave for job " + this);
134         }
135         if (_destSlaves.isEmpty() && _transferNum > 0) {
136             throw new IllegalStateException JavaDoc("Job cannot have a destSlaveSet of size 0 with transferNum > 0");
137         }
138     }
139
140     private String JavaDoc outputDestinationSlaves() {
141         String JavaDoc toReturn = new String JavaDoc();
142         for (Iterator JavaDoc iter = getDestinationSlaves().iterator();iter.hasNext();) {
143             RemoteSlave rslave = (RemoteSlave) iter.next();
144             if (!iter.hasNext())
145                 return toReturn + rslave.getName();
146             toReturn = toReturn + rslave.getName() + ",";
147         }
148         return toReturn;
149     }
150     
151     public String JavaDoc toString() {
152         String JavaDoc toReturn =
153             "Job[file="
154                 + getFile().getName()
155                 + "],dest=["
156                 + outputDestinationSlaves()
157                 + "],owner=";
158         if (getOwner() != null) {
159             toReturn += getOwner().getUsername();
160         } else {
161             toReturn += "null";
162         }
163         toReturn += "]";
164         return toReturn;
165     }
166
167     public void setDone() {
168         _transferNum = 0;
169     }
170 }
Popular Tags