KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > RemoteSlave


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.master;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.*;
24
25 import net.sf.drftpd.SlaveUnavailableException;
26 import net.sf.drftpd.event.SlaveEvent;
27 import net.sf.drftpd.master.config.FtpConfig;
28 import net.sf.drftpd.remotefile.LinkedRemoteFile;
29 import net.sf.drftpd.slave.Slave;
30 import net.sf.drftpd.slave.SlaveStatus;
31 import net.sf.drftpd.slave.Transfer;
32
33 import org.apache.log4j.Logger;
34 import org.jdom.Element;
35
36 /**
37  * @author mog
38  * @version $Id: RemoteSlave.java,v 1.41.2.1 2004/06/11 00:59:57 mog Exp $
39  */

40 public class RemoteSlave implements Comparable JavaDoc {
41     /**
42      * Used by JUnit tests
43      * @deprecated
44      */

45     public RemoteSlave(String JavaDoc name, Collection list) {
46         Properties p = new Properties();
47         p.setProperty("name", name);
48         _name = name;
49         updateConfig(p);
50     }
51
52     private int _maxPath;
53
54     private static final Logger logger = Logger.getLogger(RemoteSlave.class);
55
56     private InetAddress JavaDoc _inetAddress;
57     private long _lastDownloadSending = 0;
58     private long _lastPing;
59     private long _lastUploadReceiving = 0;
60     private SlaveManagerImpl _manager;
61     private Collection _masks;
62     private String JavaDoc _name;
63     private Slave _slave;
64     private SlaveStatus _status;
65     private Properties _config;
66
67     public void oldRemoteSlave(String JavaDoc name, Collection masks) {
68         if (name.equalsIgnoreCase("all"))
69             throw new IllegalArgumentException JavaDoc(
70                 name
71                     + " is a reserved keyword, it can't be used as a slave name");
72         _name = name;
73         _masks = masks;
74     }
75
76     public void oldRemoteSlave(String JavaDoc name, Collection masks, Element config) {
77         //this(name, masks);
78
for (Iterator i = config.getChildren().iterator(); i.hasNext();) {
79             Element e = (Element) i.next();
80             try {
81                 _config.put(e.getName(), e.getText());
82             } catch (Exception JavaDoc e1) {
83             }
84         }
85     }
86
87     public RemoteSlave(Properties config) {
88         String JavaDoc name = FtpConfig.getProperty(config, "name");
89         _name = name;
90         updateConfig(config);
91     }
92
93     public void updateConfig(Properties config) {
94         String JavaDoc name = FtpConfig.getProperty(config, "name");
95         if (name.equalsIgnoreCase("all")) {
96             throw new IllegalArgumentException JavaDoc(
97                 name
98                     + " is a reserved keyword, it can't be used as a slave name");
99         }
100         if (!name.equalsIgnoreCase(_name)) {
101             throw new IllegalArgumentException JavaDoc(
102                 name
103                     + " is not the same name as currently set for this RemoteSlave");
104         }
105         List masks = new ArrayList();
106         String JavaDoc[] maskitems = config.getProperty("masks", "").split(",");
107         for (int i = 0; i < maskitems.length; i++) {
108             masks.add(maskitems[i]);
109         }
110         _masks = masks;
111         _config = config;
112     }
113
114     public Element getConfigXML() {
115         Element root = new org.jdom.Element("slave");
116         Enumeration e = _config.keys();
117         while (e.hasMoreElements()) {
118             String JavaDoc key = (String JavaDoc) e.nextElement();
119             Element tmp = new Element(key);
120             tmp.setText((String JavaDoc) _config.get(key));
121             root.addContent(tmp);
122         }
123         Iterator i = _masks.iterator();
124         while (i.hasNext()) {
125             String JavaDoc mask = (String JavaDoc) i.next();
126             Element tmp = new Element("mask");
127             tmp.setText(mask);
128             root.addContent(tmp);
129         }
130         return root;
131     }
132
133     public int compareTo(Object JavaDoc o) {
134         if (!(o instanceof RemoteSlave))
135             throw new IllegalArgumentException JavaDoc();
136         return getName().compareTo(((RemoteSlave) o).getName());
137     }
138
139     public boolean equals(Object JavaDoc obj) {
140         if (obj instanceof RemoteSlave) {
141             RemoteSlave rslave = (RemoteSlave) obj;
142             if (rslave.getName().equals(getName())) {
143                 return true;
144             }
145         }
146         return false;
147     }
148
149     public InetAddress JavaDoc getInetAddress() {
150         return _inetAddress;
151     }
152
153     public long getLastDownloadSending() {
154         return _lastDownloadSending;
155     }
156
157     public long getLastTransfer() {
158         return Math.max(getLastDownloadSending(), getLastUploadReceiving());
159     }
160
161     public long getLastUploadReceiving() {
162         return _lastUploadReceiving;
163     }
164
165     public SlaveManagerImpl getManager() {
166         return _manager;
167     }
168
169     public Collection getMasks() {
170         return _masks;
171     }
172
173     public Hashtable getConfig() {
174         return _config;
175     }
176
177     /**
178      * Returns the name.
179      */

180     public String JavaDoc getName() {
181         return _name;
182     }
183
184     /**
185      * Throws NoAvailableSlaveException only if slave is offline
186      */

187     public Slave getSlave() throws SlaveUnavailableException {
188         if (_slave == null)
189             throw new SlaveUnavailableException("slave is offline");
190         return _slave;
191     }
192
193     /**
194      * Returns the RemoteSlave's stored SlaveStatus
195      */

196     public SlaveStatus getStatus() throws SlaveUnavailableException {
197         if (_status == null) {
198             throw new SlaveUnavailableException();
199         }
200         return _status;
201     }
202
203     public synchronized void updateStatus() throws SlaveUnavailableException {
204         try {
205             _status = getSlave().getSlaveStatus();
206         } catch (RemoteException JavaDoc e) {
207             _status = null;
208         }
209     }
210
211     /**
212      * @param ex RemoteException
213      * @return true If exception was fatal and the slave was removed
214      */

215     public void handleRemoteException(RemoteException JavaDoc ex) {
216         // if (!isFatalRemoteException(ex)) {
217
// logger.log(
218
// Level.WARN,
219
// "Caught non-fatal exception from "
220
// + getName()
221
// + ", not removing",
222
// ex);
223
// return false;
224
// }
225
logger.warn("Exception from " + getName() + ", removing", ex);
226         setOffline(ex.getCause().getMessage());
227     }
228
229     public int hashCode() {
230         return getName().hashCode();
231     }
232
233     public boolean isAvailable() {
234         return _slave != null;
235     }
236
237     public boolean isAvailablePing() {
238         try {
239             getSlave().ping();
240         } catch (RemoteException JavaDoc e) {
241             handleRemoteException(e);
242             return false;
243         } catch (SlaveUnavailableException e) {
244             return false;
245         }
246         return isAvailable();
247     }
248
249     /**
250      * @deprecated use isAvailablePing() instead
251      * @throws RemoteException
252      * @throws SlaveUnavailableException
253      */

254     public void ping() throws RemoteException JavaDoc, SlaveUnavailableException {
255         if (_slave == null)
256             throw new SlaveUnavailableException(getName() + " is offline");
257         if (System.currentTimeMillis() > _lastPing + 1000) {
258             getSlave().ping();
259         }
260     }
261     public void setLastDownloadSending(long lastDownloadSending) {
262         _lastDownloadSending = lastDownloadSending;
263     }
264     public void setLastUploadReceiving(long lastUploadReceiving) {
265         _lastUploadReceiving = lastUploadReceiving;
266     }
267
268     public void setManager(SlaveManagerImpl manager) {
269         if (_manager != null)
270             throw new IllegalStateException JavaDoc("Can't overwrite manager");
271         _manager = manager;
272     }
273
274     public void setMasks(Collection masks) {
275         _masks = masks;
276     }
277
278     public void setOffline(String JavaDoc reason) {
279         if (_manager == null) {
280             throw new RuntimeException JavaDoc("_manager == null");
281         }
282         if (_slave != null) {
283             _manager.getConnectionManager().dispatchFtpEvent(
284                 new SlaveEvent("DELSLAVE", reason, this));
285         }
286         _slave = null;
287         _status = null;
288         _inetAddress = null;
289         _maxPath = 0;
290     }
291
292     public void setSlave(
293         Slave slave,
294         InetAddress JavaDoc inetAddress,
295         SlaveStatus status,
296         int maxPath) {
297         if (slave == null)
298             throw new IllegalArgumentException JavaDoc();
299         _slave = slave;
300         _inetAddress = inetAddress;
301         _status = status;
302         _maxPath = maxPath;
303     }
304
305     public String JavaDoc toString() {
306         try {
307             return getName() + "[slave=" + getSlave().toString() + "]";
308         } catch (SlaveUnavailableException e) {
309             return getName() + "[slave=offline]";
310         }
311     }
312
313     public static Hashtable rslavesToHashtable(Collection rslaves) {
314         Hashtable map = new Hashtable(rslaves.size());
315         for (Iterator iter = rslaves.iterator(); iter.hasNext();) {
316             RemoteSlave rslave = (RemoteSlave) iter.next();
317             map.put(rslave.getName(), rslave);
318         }
319         return map;
320     }
321
322     public long getLastTransferForDirection(char dir) {
323         if (dir == Transfer.TRANSFER_RECEIVING_UPLOAD) {
324             return getLastUploadReceiving();
325         } else if (dir == Transfer.TRANSFER_SENDING_DOWNLOAD) {
326             return getLastDownloadSending();
327         } else if (dir == Transfer.TRANSFER_THROUGHPUT) {
328             return getLastTransfer();
329         } else {
330             throw new IllegalArgumentException JavaDoc();
331         }
332     }
333
334     /**
335      * Returns an updated slaveRoot
336      */

337     public LinkedRemoteFile getSlaveRoot()
338         throws IOException JavaDoc, SlaveUnavailableException {
339         return getSlave().getSlaveRoot();
340     }
341
342     public void setLastDirection(char direction, long l) {
343         switch (direction) {
344             case Transfer.TRANSFER_RECEIVING_UPLOAD :
345                 setLastUploadReceiving(l);
346                 return;
347             case Transfer.TRANSFER_SENDING_DOWNLOAD :
348                 setLastDownloadSending(l);
349                 return;
350             default :
351                 throw new IllegalArgumentException JavaDoc();
352         }
353     }
354 }
355
Popular Tags