| Home >> All >> it >> rabellino >> [ toska Javadoc ] |
Source code: it/rabellino/toska/FileDistribution.java
1 package it.rabellino.toska; 2 3 import javax.xml.parsers.SAXParser; 4 import javax.xml.parsers.SAXParserFactory; 5 6 import java.io.File; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.util.Iterator; 10 import java.util.Vector; 11 12 import org.xml.sax.ContentHandler; 13 import org.xml.sax.InputSource; 14 import org.xml.sax.SAXException; 15 import org.xml.sax.XMLReader; 16 17 /** 18 * @version 1.0 19 * @author <a href="mailto:Nito@Qindel.ES">Nito Martinez</a> 20 */ 21 public class FileDistribution { 22 23 private String sourceDir = "/tmp"; 24 private String targetDir = "/tmp"; 25 private ConfigHandler handler; 26 27 public void setSourceDir(String path) { 28 this.sourceDir = path; 29 } 30 31 public void setTargetDir(String target) { 32 this.targetDir = target; 33 } 34 35 public void setConfigHandler(ConfigHandler handler) { 36 this.handler = handler; 37 } 38 39 40 public void scpFiles() throws ScpException { 41 42 Iterator hosts = handler.getHosts().values().iterator(); 43 44 while (hosts.hasNext()) { 45 Host host = (Host) hosts.next(); 46 Iterator users = host.getUsers().values().iterator(); 47 while (users.hasNext()) { 48 User user = (User) users.next(); 49 String sourceFile = sourceDir + File.separator + 50 host.getName() + File.separator + 51 user.getName(); 52 File userFile = new File(sourceFile); 53 54 if (!userFile.exists()) { 55 System.err.println("A supposed created file during the deployment is" + 56 " no longer there(?): <" + sourceFile + ">"); 57 throw new ScpException("Source file <" + sourceFile + 58 "> does not exists"); 59 } 60 61 String target = host.getName() + ":" + targetDir; 62 doScp(sourceFile, target); 63 } 64 } 65 66 } 67 68 /* 69 * doSCP 70 * Input: 71 * - source. The source string in the same way as it is done in the normal scp command 72 * Usually something like "host/*" or "host/key" 73 * - target. The target string in the same way as scp accepts it. 74 * In this environment usually "host:/etc/ssh2/keys" 75 * 76 * Dependencies. scp has to be in the default PATH 77 */ 78 private void doScp(String source, String target) throws ScpException { 79 Process p; 80 Runtime r; 81 82 String commandLine = "scp -q " + source + " " + target; 83 84 try { 85 r = Runtime.getRuntime(); 86 p = r.exec(commandLine); 87 p.waitFor(); 88 if (p.exitValue() == 0) { 89 System.out.println("Transferred " + source + " to " + target); 90 } else { 91 throw new ScpException("Error transferring " + source + " to " + target 92 + ". The command line was: <" + commandLine + ">"); 93 } 94 } catch (SecurityException e) { 95 throw new ScpException("SecurityException: executing <" + commandLine + "> :" + e); 96 } catch (IOException e) { 97 throw new ScpException("IOException: executing <" + commandLine + "> :" + e); 98 } catch (InterruptedException e) { 99 throw new ScpException("InterruptedException: problem during wait while executing <" + 100 commandLine + "> :" + e); 101 } 102 } 103 104 }