KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > DocumentStore


1 /*
2  *
3  * Created on March 14, 2004
4  */

5 package org.enhydra.snapper.business;
6
7 import org.enhydra.snapper.Log;
8
9 import java.io.File JavaDoc;
10
11 import java.io.FileOutputStream JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Hashtable JavaDoc;
14 import java.util.Vector JavaDoc;
15
16
17 import org.apache.commons.net.ftp.FTPClient;
18 import org.apache.commons.net.ftp.FTPFile;
19 import org.apache.commons.net.ftp.FTPReply;
20 //import org.apache.commons.net.ftp.FTPFile;
21

22
23 /**
24  *
25  * Class is client for comunicate with file server(s) over net.
26  * This class is used by application to retrive files
27  *
28  *
29  * @author Igor Smirnov
30  */

31 public class DocumentStore {
32     
33     private static final int LOCAL = 0;
34     private static final int FTP = 1;
35     private static final int UNC = 2;
36     
37     public static final String JavaDoc LOCAL_STORE = "local";
38     public static final String JavaDoc FTP_STORE = "FTP";
39     public static final String JavaDoc UNC_STORE = "UNC";
40     
41     String JavaDoc user;
42     String JavaDoc docStorePath;
43     String JavaDoc host;
44     String JavaDoc port;
45     String JavaDoc login;
46     String JavaDoc password;
47     int type = -1;
48     FTPClient ftp;
49     //Hashtable ht;
50
Vector JavaDoc originalFiles, tempFiles, timestamps;
51     
52     public DocumentStore(
53             String JavaDoc currentUser,
54             String JavaDoc path,
55             String JavaDoc type,
56             String JavaDoc host,
57             String JavaDoc port,
58             String JavaDoc login,
59             String JavaDoc password
60             ) throws Exception JavaDoc {
61         this.user = currentUser;
62         this.docStorePath = path;
63         this.host = host;
64         this.port = port;
65         this.login = login;
66         this.password = password;
67         if(type.equals("local"))
68             this.type = LOCAL;
69         else if(type.equals("FTP"))
70             this.type = FTP;
71         else if(type.equals("UNC"))
72             this.type = UNC;
73         else
74             throw new Exception JavaDoc("Invalid DocumentStore type!");
75     }
76
77
78      
79     
80     /* WORK WITH FILES */
81
82     
83
84     
85     /**
86      * Retrieve file from server(ftp,...)
87      * @param path path in document stores
88      * @return object File for current version of document
89      */

90     
91     public File JavaDoc[] retrieveUNCFiles(String JavaDoc path) throws Exception JavaDoc {
92         File JavaDoc retVal = null;
93         retVal = new File JavaDoc( this.host + File.separator + path);
94         return retVal.listFiles();
95         
96     }
97     
98     public FTPFile[] retrieveFiles(String JavaDoc path) throws Exception JavaDoc {
99         FTPFile[] ftpFiles = null;
100         try {
101             if(this.type == LOCAL) {
102                 //retVal = new File( "/" + path);
103
} else if(this.type == FTP) {
104                 //retVal = File.createTempFile("tempFile","tmp");
105
FTPClient ftp=new FTPClient();
106                 ftp.connect(this.host);
107                 ftp.login(this.login, this.password);
108                 //ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
109
ftpFiles = ftp.listFiles();
110                 
111                 ftp.disconnect();
112             }
113         } catch (Exception JavaDoc e) {
114             Log.logException(e);
115         }
116         return ftpFiles;
117     }
118    
119
120     
121     /**
122      * Login to UNC path.
123      *
124      * @return true if OK, false if not.
125      */

126     private boolean prepareUNC() {
127         boolean retVal = true;
128         /*
129         String[] letters = { "I","M","K","L","S","O" };
130         try {
131             int index = 0;
132             boolean finished = false;
133             while(!finished) {
134                 try {
135                     String command = "net use "+letters[index]+": "+this.docStorePath+" "+this.password+" /USER:"+this.login;
136                     Log.log("comand = "+command);
137                     int exVal;
138                     try {
139                         Process p = Runtime.getRuntime().exec(command);
140                         exVal = p.exitValue();
141                         Log.log("exVal = "+exVal);
142                         if(exVal == 0)
143                             finished = true;
144                     } catch (Exception e) {
145                         index--;
146                         }
147                 } catch(Exception e) {
148                     Log.logException(e);
149                     index--;
150                 }
151                 if(!finished)
152                     index++;
153             }
154             finished = false;
155             while(!finished) {
156                 try {
157                     String command = "net use "+letters[index]+": /DELETE";
158                     Log.log("comand = "+command);
159                     Process p = Runtime.getRuntime().exec(command);
160                     int exVal = p.exitValue();
161                     Log.log("exVal = "+exVal);
162                     if(exVal == 0 || index>=letters.length)
163                         finished = true;
164                 } catch(Exception e) {
165                     Log.logException(e);
166                 }
167             
168             }
169         } catch (Exception e) {
170             retVal = false;
171         }
172         */

173         return retVal;
174     }
175     
176     public void retrieveFile(String JavaDoc path) throws Exception JavaDoc {
177         File JavaDoc retVal = null;
178         originalFiles = new Vector JavaDoc();
179         tempFiles = new Vector JavaDoc();
180         timestamps = new Vector JavaDoc();
181         try {
182             if(this.type == LOCAL) {
183                 //retVal = new File( this.docStorePath + "/" + path);
184
} else if(this.type == FTP) {
185                 
186                 boolean error = false;
187                 //FTPClient ftp = null;
188
try {
189                   int reply;
190                   ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
191                   FTPFile[] files;// = ftp.listFiles();
192
//for (int i=0; i<files.length; i++ ){
193
//System.out.println(files[i].getName());
194
//}
195
//FTPFile[] files = ftp.listFiles("/home/igor");
196
ftp.cwd(path);
197                   files = ftp.listFiles();
198                   for (int i=0; i<files.length; i++ ){
199                     
200                   if(!files[i].isDirectory())
201                   {
202                   retVal = new File JavaDoc(files[i].getName());
203                   //System.out.println(files[i].getName());
204
FileOutputStream JavaDoc in = new FileOutputStream JavaDoc(retVal);
205                   ftp.retrieveFile(ftp.printWorkingDirectory() + "/" + files[i].getName(), in);
206                   in.close();
207                   in=null;
208                   originalFiles.add("ftp://" + this.host + File.separator + path + File.separator + files[i].getName());
209                   timestamps.add(new Long JavaDoc(files[i].getTimestamp().getTimeInMillis()));
210                   tempFiles.add(retVal);
211                   }
212                   
213                   }
214                               
215                   //retVal.delete();
216
// System.out.println("Connected to vajat");
217
// System.out.print(ftp.getReplyString());
218
// System.out.println(ftp.printWorkingDirectory());
219

220
221                   // After connection attempt, you should check the reply code to verify
222
// success.
223
reply = ftp.getReplyCode();
224
225                   if(!FTPReply.isPositiveCompletion(reply)) {
226                     ftp.disconnect();
227                     System.err.println("FTP server refused connection.");
228                     System.exit(1);
229                   }
230                   //... // transfer files
231
ftp.logout();
232                 } catch(Exception JavaDoc e) {
233                   error = true;
234                   e.printStackTrace();
235                 } finally {
236                   if(ftp.isConnected()) {
237                     try {
238                       ftp.disconnect();
239                     } catch(Exception JavaDoc ioe) {
240                       // do nothing
241
}
242                   }
243             }
244             }
245         } catch (Exception JavaDoc e) {
246             Log.logException(e);
247         }
248         return;
249     }
250     
251     public void connect(){
252         try{
253             ftp = new FTPClient();
254             ftp.connect(this.host);
255             ftp.login(this.login, this.password);
256             ftp.enterLocalActiveMode();
257     }catch(Exception JavaDoc e) {
258         Log.logException(e);}
259     }
260     
261     public void disconnect(){
262         try{
263             if( ftp.isConnected())
264                 ftp.disconnect();
265          }catch(Exception JavaDoc e) {
266             Log.logException(e);}
267     }
268     
269     public static void main(String JavaDoc[] args) {
270         String JavaDoc usage = "java " + DocumentStore.class + " <user> <path> <type> <host> <port> <login> <pass>";
271         if (args.length == 0) {
272           System.err.println("Usage: " + usage);
273           System.exit(1);
274         }
275         
276         
277         try {
278          
279           DocumentStore ds = new DocumentStore("igor", "home/igor", "FTP", "vajat", "", "igor", "f22light" );
280           //File file = ds.retrieveFiles("/home/igor");
281
//System.out.println(file.getName());
282

283         } catch (Exception JavaDoc e) {
284           System.out.println(" caught a " + e.getClass() +
285            "\n with message: " + e.getMessage());
286         }
287       }
288     
289     public void deleteTempFiles(Hashtable JavaDoc ht){
290         for (int i = 0; i < ht.size(); i++)
291         {
292             Enumeration JavaDoc keys = ht.keys();
293             while (keys.hasMoreElements()){
294             File JavaDoc temp = (File JavaDoc) ht.get(keys.nextElement());
295             temp.delete();
296             }
297         }
298         
299     }
300     
301     public Vector JavaDoc getTempFiles(){
302         return tempFiles;
303     }
304     
305     public Vector JavaDoc getOriginalFiles(){
306         return originalFiles;
307     }
308     
309     public Vector JavaDoc getTimeStamps(){
310         return timestamps;
311     }
312     
313
314     
315     public void delTempFiles(Vector JavaDoc files) throws Exception JavaDoc {
316          for (int p = 0; p < files.size(); p++) {
317             ((File JavaDoc)files.elementAt(p)).delete();
318          }
319     }
320     
321 }
322
Popular Tags