1 5 package org.enhydra.snapper.business; 6 7 import org.enhydra.snapper.Log; 8 9 import java.io.File ; 10 11 import java.io.FileOutputStream ; 12 import java.util.Enumeration ; 13 import java.util.Hashtable ; 14 import java.util.Vector ; 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 22 23 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 LOCAL_STORE = "local"; 38 public static final String FTP_STORE = "FTP"; 39 public static final String UNC_STORE = "UNC"; 40 41 String user; 42 String docStorePath; 43 String host; 44 String port; 45 String login; 46 String password; 47 int type = -1; 48 FTPClient ftp; 49 Vector originalFiles, tempFiles, timestamps; 51 52 public DocumentStore( 53 String currentUser, 54 String path, 55 String type, 56 String host, 57 String port, 58 String login, 59 String password 60 ) throws Exception { 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 ("Invalid DocumentStore type!"); 75 } 76 77 78 79 80 81 82 83 84 85 90 91 public File [] retrieveUNCFiles(String path) throws Exception { 92 File retVal = null; 93 retVal = new File ( this.host + File.separator + path); 94 return retVal.listFiles(); 95 96 } 97 98 public FTPFile[] retrieveFiles(String path) throws Exception { 99 FTPFile[] ftpFiles = null; 100 try { 101 if(this.type == LOCAL) { 102 } else if(this.type == FTP) { 104 FTPClient ftp=new FTPClient(); 106 ftp.connect(this.host); 107 ftp.login(this.login, this.password); 108 ftpFiles = ftp.listFiles(); 110 111 ftp.disconnect(); 112 } 113 } catch (Exception e) { 114 Log.logException(e); 115 } 116 return ftpFiles; 117 } 118 119 120 121 126 private boolean prepareUNC() { 127 boolean retVal = true; 128 173 return retVal; 174 } 175 176 public void retrieveFile(String path) throws Exception { 177 File retVal = null; 178 originalFiles = new Vector (); 179 tempFiles = new Vector (); 180 timestamps = new Vector (); 181 try { 182 if(this.type == LOCAL) { 183 } else if(this.type == FTP) { 185 186 boolean error = false; 187 try { 189 int reply; 190 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 191 FTPFile[] files; 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 (files[i].getName()); 203 FileOutputStream in = new FileOutputStream (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 (files[i].getTimestamp().getTimeInMillis())); 210 tempFiles.add(retVal); 211 } 212 213 } 214 215 220 221 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 ftp.logout(); 232 } catch(Exception e) { 233 error = true; 234 e.printStackTrace(); 235 } finally { 236 if(ftp.isConnected()) { 237 try { 238 ftp.disconnect(); 239 } catch(Exception ioe) { 240 } 242 } 243 } 244 } 245 } catch (Exception 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 e) { 258 Log.logException(e);} 259 } 260 261 public void disconnect(){ 262 try{ 263 if( ftp.isConnected()) 264 ftp.disconnect(); 265 }catch(Exception e) { 266 Log.logException(e);} 267 } 268 269 public static void main(String [] args) { 270 String 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 283 } catch (Exception e) { 284 System.out.println(" caught a " + e.getClass() + 285 "\n with message: " + e.getMessage()); 286 } 287 } 288 289 public void deleteTempFiles(Hashtable ht){ 290 for (int i = 0; i < ht.size(); i++) 291 { 292 Enumeration keys = ht.keys(); 293 while (keys.hasMoreElements()){ 294 File temp = (File ) ht.get(keys.nextElement()); 295 temp.delete(); 296 } 297 } 298 299 } 300 301 public Vector getTempFiles(){ 302 return tempFiles; 303 } 304 305 public Vector getOriginalFiles(){ 306 return originalFiles; 307 } 308 309 public Vector getTimeStamps(){ 310 return timestamps; 311 } 312 313 314 315 public void delTempFiles(Vector files) throws Exception { 316 for (int p = 0; p < files.size(); p++) { 317 ((File )files.elementAt(p)).delete(); 318 } 319 } 320 321 } 322 | Popular Tags |