KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.rmi.registry.LocateRegistry JavaDoc;
28 import java.rmi.registry.Registry JavaDoc;
29 import java.rmi.server.RMIClientSocketFactory JavaDoc;
30 import java.rmi.server.RMIServerSocketFactory JavaDoc;
31 import java.rmi.server.RMISocketFactory JavaDoc;
32 import java.rmi.server.RemoteServer JavaDoc;
33 import java.rmi.server.UnicastRemoteObject JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.ListIterator JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.Set JavaDoc;
45
46 import net.sf.drftpd.FatalException;
47 import net.sf.drftpd.NoAvailableSlaveException;
48 import net.sf.drftpd.ObjectNotFoundException;
49 import net.sf.drftpd.SlaveUnavailableException;
50 import net.sf.drftpd.event.SlaveEvent;
51 import net.sf.drftpd.master.usermanager.UserFileException;
52 import net.sf.drftpd.remotefile.LinkedRemoteFile;
53 import net.sf.drftpd.remotefile.MLSTSerialize;
54 import net.sf.drftpd.slave.Slave;
55 import net.sf.drftpd.slave.SlaveStatus;
56 import net.sf.drftpd.util.SafeFileWriter;
57
58 import org.apache.log4j.Level;
59 import org.apache.log4j.Logger;
60 import org.drftpd.slave.socket.SocketSlaveImpl;
61 import org.drftpd.slaveselection.SlaveSelectionManagerInterface;
62 import org.jdom.Document;
63 import org.jdom.Element;
64 import org.jdom.JDOMException;
65 import org.jdom.input.SAXBuilder;
66 import org.jdom.output.XMLOutputter;
67
68 /**
69  * @author mog
70  * @version $Id: SlaveManagerImpl.java,v 1.94.2.1 2004/06/25 20:31:36 mog Exp $
71  */

72 public class SlaveManagerImpl
73     extends UnicastRemoteObject JavaDoc
74     implements SlaveManager {
75     private static final Logger logger =
76         Logger.getLogger(SlaveManagerImpl.class.getName());
77
78     /**
79      * Checksums call us with null BaseFtpConnection.
80      */

81 // public static RemoteSlave getASlave(
82
// Collection slaves,
83
// char direction,
84
// FtpConfig config,
85
// BaseFtpConnection conn,
86
// LinkedRemoteFileInterface file)
87
// throws NoAvailableSlaveException {
88
// return config.getSlaveManager().getSlaveSelectionManager().getASlave(
89
// slaves,
90
// direction,
91
// conn,
92
// file);
93
// }
94

95 // public static Collection getAvailableSlaves()
96
// throws NoAvailableSlaveException {
97
// ArrayList availableSlaves = new ArrayList();
98
// for (Iterator iter = _rslaves.iterator(); iter.hasNext();) {
99
// RemoteSlave rslave = (RemoteSlave) iter.next();
100
// if (!rslave.isAvailable())
101
// continue;
102
// availableSlaves.add(rslave);
103
// }
104
// if (availableSlaves.isEmpty()) {
105
// throw new NoAvailableSlaveException("No slaves online");
106
// }
107
// return availableSlaves;
108
// }
109

110 // public static RemoteSlave loadRSlave(Element slaveElement) {
111
// List masks = new ArrayList();
112
// List maskElements = slaveElement.getChildren("mask");
113
// for (Iterator i2 = maskElements.iterator(); i2.hasNext();) {
114
// masks.add(((Element) i2.next()).getText());
115
// }
116
// return new RemoteSlave(slaveElement);
117
// slaveElement.getChildText("name").toString(),
118
// masks,
119
// slaveElement);
120
// }
121

122         public static List JavaDoc loadRSlaves()
123         {
124             if (_self == null) return null;
125             return _self.loadSlaves();
126         }
127         
128         private Properties JavaDoc elementToProps(Element config)
129         {
130             Properties JavaDoc props = new Properties JavaDoc();
131             String JavaDoc masks = "";
132             List JavaDoc maskElements = config.getChildren("mask");
133             for (Iterator JavaDoc i2 = maskElements.iterator(); i2.hasNext();) {
134                 if (!masks.equals("")) masks += ",";
135                 masks += ((Element) i2.next()).getText();
136             }
137             props.put("masks", masks);
138             for (Iterator JavaDoc i = config.getChildren().iterator(); i.hasNext();) {
139                 Element e = (Element) i.next();
140                 if (e.getName().equalsIgnoreCase("mask")) continue;
141                 try {
142                     props.setProperty(e.getName(), e.getText());
143                 } catch (Exception JavaDoc e1) {
144                 }
145             }
146             return props;
147         }
148         
149     public List JavaDoc loadSlaves() {
150         ArrayList JavaDoc rslaves;
151         try {
152             Document doc =
153                 new SAXBuilder().build(new FileReader JavaDoc("conf/slaves.xml"));
154             List JavaDoc children = doc.getRootElement().getChildren("slave");
155             rslaves = new ArrayList JavaDoc(children.size());
156             for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
157                 //slavemanager is set in the slavemanager constructor
158
Properties JavaDoc props = elementToProps((Element) i.next());
159                 rslaves.add(new RemoteSlave(props));
160             }
161             rslaves.trimToSize();
162         } catch (Exception JavaDoc ex) {
163             //logger.log(Level.INFO, "Error reading masks from slaves.xml", ex);
164
throw new FatalException(ex);
165         }
166         Collections.sort(rslaves);
167         return rslaves;
168     }
169
170     public static Collection JavaDoc rslavesToMasks(Collection JavaDoc rslaves) {
171             if (_self != null) return _self.getMasks(rslaves);
172             return null;
173         }
174         
175     public Collection JavaDoc getMasks(Collection JavaDoc rslaves) {
176         ArrayList JavaDoc masks = new ArrayList JavaDoc();
177         for (Iterator JavaDoc iter = rslaves.iterator(); iter.hasNext();) {
178             RemoteSlave rslave2 = (RemoteSlave) iter.next();
179             masks.addAll(rslave2.getMasks());
180         }
181         return masks;
182     }
183
184     /**
185      * @deprecated
186      */

187 // public static void saveFilesXML(Element root) {
188
// File filesDotXml = new File("files.xml");
189
// File filesxmlbak = new File("files.xml.bak");
190
// filesxmlbak.delete();
191
// filesDotXml.renameTo(filesxmlbak);
192
// try {
193
// FileWriter out = new FileWriter(filesDotXml);
194
// new XMLOutputter(" ", true).output(root, out);
195
// out.flush();
196
// } catch (IOException ex) {
197
// logger.log(
198
// Level.WARN,
199
// "Error saving to " + filesDotXml.getPath(),
200
// ex);
201
// }
202
// }
203

204     public void setRSlavesManager() {
205         for (Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
206             RemoteSlave rslave = (RemoteSlave) iter.next();
207             rslave.setManager(this);
208         }
209     }
210
211     private ConnectionManager _cm;
212
213     protected List JavaDoc _rslaves;
214         protected RMIServerSocketFactory JavaDoc _ssf;
215         protected RMIClientSocketFactory JavaDoc _csf;
216         
217     private SlaveSelectionManagerInterface _slaveSelectionManager;
218         
219         private static SlaveManagerImpl _self = null;
220         
221     protected SlaveManagerImpl() throws RemoteException JavaDoc {
222             _self = this;
223     }
224         
225     public void init(
226         Properties JavaDoc cfg,
227         List JavaDoc rslaves,
228         RMIServerSocketFactory JavaDoc ssf,
229         ConnectionManager cm)
230         throws RemoteException JavaDoc
231         {
232         _csf = RMISocketFactory.getSocketFactory();
233                 _ssf = ssf;
234         _cm = cm;
235         _rslaves = rslaves;
236
237                 setRSlavesManager();
238
239         Registry JavaDoc registry =
240             LocateRegistry.createRegistry(
241                 Integer.parseInt(cfg.getProperty("master.bindport", "1099")),
242                 _csf, _ssf);
243         // throws RemoteException
244
try {
245             registry.bind(
246                 cfg.getProperty("master.bindname", "slavemanager"),
247                 this);
248         } catch (Exception JavaDoc t) {
249             throw new FatalException(t);
250         }
251         try {
252             Constructor JavaDoc c =
253                 Class
254                     .forName(
255                         cfg.getProperty(
256                             "slaveselection",
257                             "org.drftpd.slaveselection.def.SlaveSelectionManager"))
258                     .getConstructor(new Class JavaDoc[] { SlaveManagerImpl.class });
259             _slaveSelectionManager =
260                 (SlaveSelectionManagerInterface) c.newInstance(
261                     new Object JavaDoc[] { this });
262         } catch (Exception JavaDoc e) {
263             if (e instanceof RuntimeException JavaDoc)
264                 throw (RuntimeException JavaDoc) e;
265             throw new FatalException(e);
266         }
267         logger.debug("starting slavestatus updater thread");
268         //new Thread(this, "SlaveStatusUpdater").start();
269
new SlaveStatusUpdater().start();
270     }
271
272     protected void addShutdownHook() {
273         //add shutdown hook last
274
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
275             public void run() {
276                 logger.info("Running shutdown hook");
277                 saveFilelist();
278                 try {
279                     getConnectionManager().getUserManager().saveAll();
280                 } catch (UserFileException e) {
281                     logger.warn("", e);
282                 }
283             }
284         });
285     }
286
287     public void addSlave(
288         String JavaDoc slaveName,
289         Slave slave,
290         SlaveStatus status,
291         int maxPath)
292         throws RemoteException JavaDoc {
293
294         slave.ping();
295
296         RemoteSlave rslave = null;
297         for (Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
298             RemoteSlave rslave2 = (RemoteSlave) iter.next();
299             if (rslave2.getName().equals(slaveName)) {
300                 rslave = rslave2;
301                 break;
302             }
303         }
304         if (rslave == null)
305             throw new IllegalArgumentException JavaDoc("Slave not found in slaves.xml");
306
307         if (rslave.isAvailablePing()) {
308             throw new IllegalArgumentException JavaDoc(
309                 rslave.getName() + " is already online");
310         }
311
312         try {
313             InetAddress JavaDoc addr = null;
314             if (slave instanceof SocketSlaveImpl) {
315                 addr = ((SocketSlaveImpl) slave).getPeerAddress();
316             }
317             if (addr == null) {
318                 addr = InetAddress.getByName(RemoteServer.getClientHost());
319             }
320
321             //logger.debug("slave ip address is " + addr);
322
if (addr == null) {
323                 throw new IllegalArgumentException JavaDoc(
324                     rslave.getName() + " has no slave address");
325             }
326             rslave.setSlave(slave, addr, slave.getSlaveStatus(), maxPath);
327         } catch (Throwable JavaDoc e1) {
328             throw new FatalException(e1);
329         }
330         logger.debug("About to remerge(), slave is " + rslave);
331         try {
332             remerge(rslave);
333         } catch (IOException JavaDoc e) {
334             logger.warn("", e);
335             rslave.setOffline("IOException during remerge()");
336             return;
337         } catch (SlaveUnavailableException e) {
338             logger.warn("", e);
339             rslave.setOffline("Slave Unavailable during remerge()");
340             return;
341         }
342
343         logger.info("Slave added: '" + rslave.getName() + "' status: " + status);
344
345         getConnectionManager().dispatchFtpEvent(
346             new SlaveEvent("ADDSLAVE", rslave));
347     }
348
349     public void delSlave(String JavaDoc slaveName, String JavaDoc reason)
350         throws RemoteException JavaDoc {
351
352         RemoteSlave rslave = null;
353         for (Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
354             RemoteSlave rslave2 = (RemoteSlave) iter.next();
355             if (rslave2.getName().equals(slaveName)) {
356                 rslave = rslave2;
357                 break;
358             }
359         }
360         if (rslave == null)
361             throw new IllegalArgumentException JavaDoc(
362                 "Slave not found in slaves.xml (" + slaveName + ")");
363
364         rslave.setOffline(reason);
365     }
366
367         public HashSet JavaDoc findSlavesBySpace(int numOfSlaves, Set JavaDoc exemptSlaves, boolean ascending) {
368         Collection JavaDoc slaveList =
369             getConnectionManager().getSlaveManager().getSlaveList();
370         HashMap JavaDoc map = new HashMap JavaDoc();
371         for (Iterator JavaDoc iter = slaveList.iterator(); iter.hasNext();) {
372             RemoteSlave rslave = (RemoteSlave) iter.next();
373             if (exemptSlaves.contains(rslave))
374                 continue;
375             Long JavaDoc size;
376             try {
377                 size = new Long JavaDoc(rslave.getStatus().getDiskSpaceAvailable());
378             } catch (SlaveUnavailableException e) {
379                 continue;
380             }
381             map.put(size,rslave);
382         }
383         ArrayList JavaDoc sorted = new ArrayList JavaDoc(map.keySet());
384         if (ascending) {
385             Collections.sort(sorted);
386         } else {
387             Collections.sort(sorted, Collections.reverseOrder());
388         }
389         HashSet JavaDoc returnMe = new HashSet JavaDoc();
390         for (ListIterator JavaDoc iter = sorted.listIterator(); iter.hasNext();) {
391             if (iter.nextIndex()==numOfSlaves)
392                 break;
393             Long JavaDoc key = (Long JavaDoc) iter.next();
394             RemoteSlave rslave = (RemoteSlave) map.get(key);
395             returnMe.add(rslave);
396         }
397         return returnMe;
398     }
399
400
401     public RemoteSlave findSmallestFreeSlave() {
402         Collection JavaDoc slaveList =
403             getConnectionManager().getSlaveManager().getSlaveList();
404         long smallSize = Integer.MAX_VALUE;
405         RemoteSlave smallSlave = null;
406         for (Iterator JavaDoc iter = slaveList.iterator(); iter.hasNext();) {
407             RemoteSlave rslave = (RemoteSlave) iter.next();
408             long size = Integer.MAX_VALUE;
409             try {
410                 size = rslave.getStatus().getDiskSpaceAvailable();
411             } catch (SlaveUnavailableException e) {
412                 continue;
413             }
414             if (size < smallSize) {
415                 smallSize = size;
416                 smallSlave = rslave;
417             }
418         }
419         return smallSlave;
420     }
421
422     /**
423      * Not cached at all since RemoteSlave objects cache their SlaveStatus
424      */

425     public SlaveStatus getAllStatus() {
426         SlaveStatus allStatus = new SlaveStatus();
427         for (Iterator JavaDoc iter = getSlaves().iterator(); iter.hasNext();) {
428             RemoteSlave rslave = (RemoteSlave) iter.next();
429             try {
430                 allStatus = allStatus.append(rslave.getStatus());
431             } catch (SlaveUnavailableException e) {
432                 //slave is offline, continue
433
}
434         }
435         return allStatus;
436     }
437
438     // private Random rand = new Random();
439
// public RemoteSlave getASlave() {
440
// ArrayList retSlaves = new ArrayList();
441
// for (Iterator iter = this.rslaves.iterator(); iter.hasNext();) {
442
// RemoteSlave rslave = (RemoteSlave) iter.next();
443
// if (!rslave.isAvailable())
444
// continue;
445
// retSlaves.add(rslave);
446
// }
447
//
448
// int num = rand.nextInt(retSlaves.size());
449
// logger.fine(
450
// "Slave "
451
// + num
452
// + " selected out of "
453
// + retSlaves.size()
454
// + " available slaves");
455
// return (RemoteSlave) retSlaves.get(num);
456
// }
457

458     public Collection JavaDoc getAvailableSlaves() throws NoAvailableSlaveException {
459         ArrayList JavaDoc availableSlaves = new ArrayList JavaDoc();
460         for (Iterator JavaDoc iter = getSlaves().iterator(); iter.hasNext();) {
461             RemoteSlave rslave = (RemoteSlave) iter.next();
462             if (!rslave.isAvailable())
463                 continue;
464             availableSlaves.add(rslave);
465         }
466         if (availableSlaves.isEmpty()) {
467             throw new NoAvailableSlaveException("No slaves online");
468         }
469         return availableSlaves;
470     }
471     
472         public ConnectionManager getConnectionManager() {
473         return _cm;
474     }
475     
476         public RemoteSlave getSlave(String JavaDoc s) throws ObjectNotFoundException {
477         for (Iterator JavaDoc iter = getSlaves().iterator(); iter.hasNext();) {
478             RemoteSlave rslave = (RemoteSlave) iter.next();
479             if (rslave.getName().equals(s))
480                 return rslave;
481         }
482         throw new ObjectNotFoundException(s + ": No such slave");
483     }
484     
485         public List JavaDoc getSlaveList() {
486         return _rslaves;
487     }
488
489     public Collection JavaDoc getSlaves() {
490         return _rslaves;
491     }
492
493     public SlaveSelectionManagerInterface getSlaveSelectionManager() {
494         return _slaveSelectionManager;
495     }
496     /**
497      * @deprecated Use RemoteSlave.handleRemoteException instead
498      */

499     public void handleRemoteException(RemoteException JavaDoc ex, RemoteSlave rslave) {
500         rslave.handleRemoteException(ex);
501     }
502
503     /**
504      * Returns true if one or more slaves are online, false otherwise.
505      * @return true if one or more slaves are online, false otherwise.
506      */

507     public boolean hasAvailableSlaves() {
508         for (Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
509             RemoteSlave rslave = (RemoteSlave) iter.next();
510             if (rslave.isAvailable())
511                 return true;
512         }
513         return false;
514     }
515
516     public void reload() throws FileNotFoundException JavaDoc, IOException JavaDoc {
517         _slaveSelectionManager.reload();
518         reloadRSlaves();
519     }
520     public void reloadRSlaves() throws FileNotFoundException JavaDoc, IOException JavaDoc {
521         Document doc;
522         try {
523             doc = new SAXBuilder().build(new FileReader JavaDoc("conf/slaves.xml"));
524         } catch (JDOMException e) {
525             throw (IOException JavaDoc) new IOException JavaDoc().initCause(e);
526         }
527
528         List JavaDoc slaveElements = doc.getRootElement().getChildren("slave");
529
530         // first, unmerge non-existing slaves
531
synchronized (_rslaves) {
532             nextslave : for (
533                 Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
534                 RemoteSlave rslave = (RemoteSlave) iter.next();
535
536                 for (Iterator JavaDoc iterator = slaveElements.iterator();
537                     iterator.hasNext();
538                     ) {
539                     Element slaveElement = (Element) iterator.next();
540                     if (rslave
541                         .getName()
542                         .equals(slaveElement.getChildText("name"))) {
543                         logger.log(
544                             Level.DEBUG,
545                             rslave.getName() + " still in slaves.xml");
546                         continue nextslave;
547                     }
548                 }
549                 logger.log(
550                     Level.WARN,
551                     rslave.getName() + " no longer in slaves.xml, unmerging");
552                 rslave.setOffline("Slave removed from slaves.xml");
553                 getConnectionManager().getRoot().unmergeDir(rslave);
554                 //rslaves.remove(rslave);
555
iter.remove();
556             }
557         }
558
559         nextelement : for (
560             Iterator JavaDoc iterator = slaveElements.iterator();
561                 iterator.hasNext();
562                 ) {
563             Element slaveElement = (Element) iterator.next();
564
565             for (Iterator JavaDoc iter = _rslaves.iterator(); iter.hasNext();) {
566                 RemoteSlave rslave = (RemoteSlave) iter.next();
567
568                 if (slaveElement
569                     .getChildText("name")
570                     .equals(rslave.getName())) {
571                                             rslave.updateConfig(elementToProps(slaveElement));
572 // List masks = new ArrayList();
573
// List maskElements = slaveElement.getChildren("mask");
574
// for (Iterator i2 = maskElements.iterator();
575
// i2.hasNext();
576
// ) {
577
// masks.add(((Element) i2.next()).getText());
578
// }
579
// rslave.setMasks(masks);
580
continue nextelement;
581                 }
582             } // rslaves.iterator()
583
RemoteSlave rslave = new RemoteSlave(elementToProps(slaveElement));
584             rslave.setManager(this);
585             _rslaves.add(rslave);
586             logger.log(Level.INFO, "Added " + rslave.getName() + " to slaves");
587         }
588         Collections.sort(_rslaves);
589     }
590
591     public void remerge(RemoteSlave rslave)
592         throws IOException JavaDoc, SlaveUnavailableException {
593         LinkedRemoteFile slaveroot;
594         slaveroot = rslave.getSlaveRoot();
595         try {
596             getConnectionManager().getRoot().remerge(slaveroot, rslave);
597         } catch (RuntimeException JavaDoc t) {
598             logger.log(Level.FATAL, "", t);
599             rslave.setOffline(t.getMessage());
600             throw t;
601         }
602     }
603
604     public class SlaveStatusUpdater extends Thread JavaDoc {
605         public SlaveStatusUpdater() {
606             super("SlaveStatusUpdater");
607         }
608         public void run() {
609             logger.debug("started slavestatus updater thread");
610             long low = Integer.MAX_VALUE;
611             long high = 0;
612             while (true) {
613                 try {
614                     for (Iterator JavaDoc iter = getAvailableSlaves().iterator();
615                         iter.hasNext();
616                         ) {
617                         RemoteSlave slave = (RemoteSlave) iter.next();
618                         try {
619                             long time = System.currentTimeMillis();
620                             slave.updateStatus();
621                             long difference = System.currentTimeMillis() - time;
622                             if (difference < low) {
623                                 low = difference;
624                                 logger.debug(
625                                     low
626                                         + " low milliseconds were used to run updateStatus on "
627                                         + slave.getName());
628                             }
629                             if (difference > high) {
630                                 high = difference;
631                                 logger.debug(
632                                     high
633                                         + " high milliseconds were used to run updateStatus on "
634                                         + slave.getName());
635                             }
636                         } catch (SlaveUnavailableException e1) {
637                             continue;
638                         }
639                     }
640                 } catch (NoAvailableSlaveException e) {
641                 }
642                 try {
643                     Thread.sleep(_cm.getConfig().getSlaveStatusUpdateTime());
644                 } catch (InterruptedException JavaDoc e1) {
645                 }
646             }
647         }
648     }
649
650     public void saveFilelist() {
651         try {
652             SafeFileWriter out = new SafeFileWriter("files.mlst");
653             try {
654                 MLSTSerialize.serialize(getConnectionManager().getRoot(), out);
655             } finally {
656                 out.close();
657             }
658         } catch (IOException JavaDoc e) {
659             logger.warn("Error saving files.mlst", e);
660         }
661     }
662
663     /** ping's all slaves, returns number of slaves removed */
664     public int verifySlaves() {
665         int removed = 0;
666         synchronized (_rslaves) {
667             for (Iterator JavaDoc i = _rslaves.iterator(); i.hasNext();) {
668                 RemoteSlave slave = (RemoteSlave) i.next();
669                 if (!slave.isAvailablePing())
670                     removed++;
671             }
672         }
673         return removed;
674     }
675
676          public void updateSlave(String JavaDoc name, Hashtable JavaDoc args) throws IOException JavaDoc {
677         Element tmp;
678         
679         String JavaDoc mask = (String JavaDoc)args.get("mask");
680         String JavaDoc skey = (String JavaDoc)args.get("skey");
681         String JavaDoc mkey = (String JavaDoc)args.get("mkey");
682         String JavaDoc port = (String JavaDoc)args.get("port");
683         String JavaDoc addr = (String JavaDoc)args.get("addr");
684         
685         // create new slaves.xml entry
686
Element slave = new Element("slave");
687         
688         tmp = new Element("name");
689         tmp.setText(name);
690         slave.addContent(tmp);
691         
692         tmp = new Element("addr");
693         if (addr == null) {
694             tmp.setText("Dynamic");
695         } else {
696             tmp.setText(addr);
697         }
698         slave.addContent(tmp);
699         
700         if (mask != null) {
701             String JavaDoc[] masks = mask.split(",");
702             for (int i=0; i<masks.length; i++) {
703                 tmp = new Element("mask");
704                 tmp.setText(masks[i]);
705                 slave.addContent(tmp);
706             }
707         }
708         
709         if (port != null) {
710             tmp = new Element("port");
711             tmp.setText(port);
712             slave.addContent(tmp);
713         }
714         
715         if (skey != null) {
716             tmp = new Element("slavepass");
717             tmp.setText(skey);
718             slave.addContent(tmp);
719         }
720         
721         if (mkey != null) {
722             tmp = new Element("masterpass");
723             tmp.setText(mkey);
724             slave.addContent(tmp);
725         }
726         
727         // get the current slaves.xml file
728
Document doc;
729         try {
730             doc = new SAXBuilder().build(new FileReader JavaDoc("conf/slaves.xml"));
731         } catch (JDOMException e) {
732             throw (IOException JavaDoc) new IOException JavaDoc().initCause(e);
733         }
734         
735         // get the current list of slaves
736
List JavaDoc slaveElements = doc.getRootElement().getChildren("slave");
737         
738         // try to find an existing slave
739
Element conf = null;
740         for (Iterator JavaDoc iterator = slaveElements.iterator(); iterator.hasNext(); ) {
741             Element slaveElement = (Element) iterator.next();
742             if (name.equals(slaveElement.getChildText("name"))) {
743                 conf = slaveElement;
744             }
745         }
746         
747         if (conf == null) {
748             // create new slave entry
749
doc.getRootElement().addContent(slave);
750         } else {
751             // update existing entry
752
slaveElements.remove(conf);
753             slaveElements.add(slave);
754             doc.setContent(slaveElements);
755         }
756         
757         // write slaves.xml
758
XMLOutputter out = new XMLOutputter(" ", true);
759         out.output(doc, new FileWriter JavaDoc("conf/slaves.xml"));
760         out = null;
761         
762         // trigger the normal slave reload process
763
reloadRSlaves();
764         
765     }
766 }
767
Popular Tags