KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > remote > RemoteDeployUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.DataOutputStream JavaDoc;
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.DataInputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * Knows how to upload files to a server
37  *
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class RemoteDeployUtil {
41     public static void uploadFilesToServer(File JavaDoc[] files, AbstractDeployCommand progress) {
42         if(files == null) {
43             return;
44         }
45         List JavaDoc valid = new LinkedList JavaDoc();
46         for(int i=0; i<files.length; i++) {
47             if(files[i] == null) {
48                 continue;
49             }
50             File JavaDoc file = files[i];
51             if(!file.exists() || !file.canRead()) {
52                 continue;
53             }
54             valid.add(new Integer JavaDoc(i));
55         }
56         if(valid.size() > 0) {
57             progress.updateStatus("Uploading "+valid.size()+" file(s) to server");
58             try {
59                 URL JavaDoc url = progress.getRemoteDeployUploadURL();
60                 URLConnection JavaDoc con = connectToServer(url, progress.getCommandContext().getUsername(), progress.getCommandContext().getPassword());
61                 DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(new BufferedOutputStream JavaDoc(con.getOutputStream()));
62                 out.writeInt(valid.size());
63                 byte[] buf = new byte[1024];
64                 int size, total, length, threshold, next;
65                 for (Iterator JavaDoc it = valid.iterator(); it.hasNext();) {
66                     Integer JavaDoc index = (Integer JavaDoc) it.next();
67                     File JavaDoc file = files[index.intValue()];
68                     out.writeInt(length = (int)file.length());
69                     threshold = Math.max(length / 100, 10240);
70                     next = threshold;
71                     BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(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 JavaDoc in = new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(con.getInputStream()));
85                 String JavaDoc 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 JavaDoc it = valid.iterator(); it.hasNext();) {
96                     Integer JavaDoc index = (Integer JavaDoc) it.next();
97                     String JavaDoc serverFileName = in.readUTF();
98                     files[index.intValue()] = new File JavaDoc(serverFileName);
99                 }
100                 in.close();
101                 progress.updateStatus(count+" file(s) transferred to server. Resuming deployment operation.");
102             } catch (Exception JavaDoc e) {
103                 progress.doFail(e);
104             }
105         }
106     }
107
108     private static URLConnection JavaDoc connectToServer(URL JavaDoc url, String JavaDoc username, String JavaDoc password) throws IOException JavaDoc {
109         URLConnection JavaDoc con = url.openConnection();
110         String JavaDoc auth = username + ":" + password;
111         byte[] data = auth.getBytes();
112         String JavaDoc s = new String JavaDoc(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