KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.snapper.business;
2
3 /**
4  * @author Milin Radivoj
5  *
6  * TODO To change the template for this generated type comment go to
7  * Window - Preferences - Java - Code Style - Code Templates
8  */

9
10 import org.apache.commons.net.ftp.FTPClient;
11 import org.enhydra.dods.DODS;
12 import org.enhydra.snapper.Log;
13 import org.enhydra.snapper.spec.*;
14
15 import com.lutris.appserver.server.sql.DBTransaction;
16
17 import java.io.File JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19
20 /**
21  * @author Milin Radivoj
22  *
23  */

24
25 public class DownloadImpl implements Download
26  {
27     
28     public File JavaDoc downloadFtpFile(String JavaDoc id, String JavaDoc type){
29         try{
30             FTPClient ftp=new FTPClient();
31             String JavaDoc temp = id.replaceFirst("ftp://","");
32             temp = temp.replaceAll("\\\\","/");
33             
34             
35             String JavaDoc host = temp.substring(0,temp.indexOf("/"));
36             String JavaDoc filePath = temp.substring(temp.indexOf("/"));
37             
38             String JavaDoc user = new String JavaDoc();
39             String JavaDoc pass = new String JavaDoc();
40
41      try{
42         DBTransaction dbTransaction = DODS.getDatabaseManager().createTransaction();
43            
44         SiteList sl = SiteListFactory.getSiteList("org.enhydra.snapper.business.SiteListImpl");
45         
46         Site[] list = sl.getList(dbTransaction);
47
48
49     for(int i=0;i<list.length;i++)
50     {
51         Site currentSite = list[i];
52           
53         String JavaDoc idd = currentSite.getID();
54         String JavaDoc name = currentSite.getName();
55         
56                 Path pt;
57                 Path[] pathArray;
58                 
59                 pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl");
60             
61                 pathArray = pt.getListForID(dbTransaction, idd);
62                 
63                         
64              for(int j=0;j<pathArray.length;j++)
65              {
66                 String JavaDoc hostDB = pathArray[j].getHost();
67                 String JavaDoc rootDB = pathArray[j].getRoot();
68                 String JavaDoc r = filePath.replaceFirst("/","");
69                 
70                 if(hostDB.equals(host)&&r.startsWith(rootDB))
71                  {
72                     
73                     user = pathArray[j].getUser();
74                     pass = pathArray[j].getPass();
75                     
76                     break;
77                  }
78                 
79              }
80             }
81            dbTransaction.release();
82             
83             }catch (Exception JavaDoc e){
84                 Log.logException(e);
85              return null;
86             }
87            ftp = new FTPClient();
88             ftp.connect(host);
89             ftp.login(user, pass);
90             ftp.enterLocalActiveMode();
91             File JavaDoc retVal = new File JavaDoc("temp."+type);
92             FileOutputStream JavaDoc in = new FileOutputStream JavaDoc(retVal);
93             ftp.retrieveFile(ftp.printWorkingDirectory() + filePath, in);
94             in.close();
95             in=null;
96             ftp.disconnect();
97            return retVal;
98         } catch (Exception JavaDoc e) {
99             Log.logException(e);
100              return null;
101           }
102                
103            
104         
105        }
106    
107     
108  }
Popular Tags