1 17 package org.apache.geronimo.deployment.plugin.remote; 18 19 import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand; 20 import org.apache.geronimo.util.encoders.Base64; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.DataOutputStream ; 25 import java.io.BufferedInputStream ; 26 import java.io.FileInputStream ; 27 import java.io.BufferedOutputStream ; 28 import java.io.DataInputStream ; 29 import java.net.URL ; 30 import java.net.URLConnection ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Iterator ; 34 35 40 public class RemoteDeployUtil { 41 public static void uploadFilesToServer(File [] files, AbstractDeployCommand progress) { 42 if(files == null) { 43 return; 44 } 45 List valid = new LinkedList (); 46 for(int i=0; i<files.length; i++) { 47 if(files[i] == null) { 48 continue; 49 } 50 File file = files[i]; 51 if(!file.exists() || !file.canRead()) { 52 continue; 53 } 54 valid.add(new Integer (i)); 55 } 56 if(valid.size() > 0) { 57 progress.updateStatus("Uploading "+valid.size()+" file(s) to server"); 58 try { 59 URL url = progress.getRemoteDeployUploadURL(); 60 URLConnection con = connectToServer(url, progress.getCommandContext().getUsername(), progress.getCommandContext().getPassword()); 61 DataOutputStream out = new DataOutputStream (new BufferedOutputStream (con.getOutputStream())); 62 out.writeInt(valid.size()); 63 byte[] buf = new byte[1024]; 64 int size, total, length, threshold, next; 65 for (Iterator it = valid.iterator(); it.hasNext();) { 66 Integer index = (Integer ) it.next(); 67 File file = files[index.intValue()]; 68 out.writeInt(length = (int)file.length()); 69 threshold = Math.max(length / 100, 10240); 70 next = threshold; 71 BufferedInputStream in = new BufferedInputStream (new FileInputStream (file)); 72 total = 0; 73 while((size = in.read(buf)) > -1) { 74 out.write(buf, 0, size); 75 total += size; 76 if(total > next) { 77 progress.updateStatus("Uploading "+file.getName()+": "+(total/1024)+" kB"); 78 while(total > next) next += threshold; 79 } 80 } 81 } 82 out.flush(); 83 out.close(); 84 DataInputStream in = new DataInputStream (new BufferedInputStream (con.getInputStream())); 85 String status = in.readUTF(); 86 if(!status.equals("OK")) { 87 progress.fail("Unable to upload files to server: "+status); 88 return; 89 } 90 progress.updateStatus("File upload complete (Server: "+status+")"); 91 int count = in.readInt(); 92 if(count != valid.size()) { 93 progress.fail("Server did not receive all "+valid.size()+" files ("+count+")"); 94 } 95 for (Iterator it = valid.iterator(); it.hasNext();) { 96 Integer index = (Integer ) it.next(); 97 String serverFileName = in.readUTF(); 98 files[index.intValue()] = new File (serverFileName); 99 } 100 in.close(); 101 progress.updateStatus(count+" file(s) transferred to server. Resuming deployment operation."); 102 } catch (Exception e) { 103 progress.doFail(e); 104 } 105 } 106 } 107 108 private static URLConnection connectToServer(URL url, String username, String password) throws IOException { 109 URLConnection con = url.openConnection(); 110 String auth = username + ":" + password; 111 byte[] data = auth.getBytes(); 112 String s = new String (Base64.encode(data)); 113 while(s.length() % 4 != 0) s += "="; 114 con.setRequestProperty("Authorization", "Basic "+s); 115 con.setDoInput(true); 116 con.setDoOutput(true); 117 con.connect(); 118 return con; 119 } 120 } 121 | Popular Tags |