com/scalagent/joram/mom/dest/ftp/TransferImplJftp.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - 2007 ScalAgent Distributed Technologies
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00018  * USA.
00019  *
00020  * Initial developer(s): Nicolas Tachker (ScalAgent)
00021  * Contributor(s): 
00022  */
00023 package com.scalagent.joram.mom.dest.ftp;
00024 
00025 import java.io.File;
00026 
00027 import net.sf.jftp.config.Settings;
00028 import net.sf.jftp.net.BasicConnection;
00029 import net.sf.jftp.net.ConnectionHandler;
00030 import net.sf.jftp.net.ConnectionListener;
00031 import net.sf.jftp.net.FtpConnection;
00032 
00033 public class TransferImplJftp 
00034   implements TransferItf, ConnectionListener {
00035 
00036   private boolean established = false;
00037 
00038   // connection pool, not necessary but you should take a look at this class
00039   // if you want to use multiple event based ftp transfers.
00040   private ConnectionHandler handler = new ConnectionHandler();
00041 
00042   public String getFile(String protocol,
00043                         String host,
00044                         int port,
00045                         String user,
00046                         String pass,
00047                         String remotePath, 
00048                         String localPath, 
00049                         String remoteFileName,
00050                         String localFileName,
00051                         String type,
00052                         long crc) throws Exception {
00053     
00054     Settings.setProperty("jftp.disableLog","true");
00055     Settings.setProperty("jftp.enableMultiThreading","true");
00056     Settings.maxConnections = 5;
00057     Settings.enableResuming = true;
00058     
00059     // create a FtpConnection
00060     FtpConnection con = null;
00061     if (port > -1) 
00062       con = new FtpConnection(host,port,remotePath);
00063     else
00064       con = new FtpConnection(host);
00065 
00066     // set updatelistener, interface methods are below
00067     con.addConnectionListener(this);
00068 
00069     // set handler
00070     con.setConnectionHandler(handler);
00071 
00072     // connect and login. 
00073     con.login(user,pass);
00074 
00075     // login calls connectionInitialized() below which sets established to true
00076     while(!established) {
00077       try { 
00078         Thread.sleep(10); 
00079       } catch(Exception exc) { 
00080         throw exc; 
00081       }
00082     }
00083 
00084     if (remotePath != null)
00085       con.chdirRaw(remotePath);
00086 
00087     if (localPath != null)
00088       con.setLocalPath(localPath);
00089 
00090     //System.out.println("LocalPath = " + con.getLocalPath());
00091 
00092     // which spawns a new thread for the download
00093     con.download(remoteFileName);
00094     
00095     File file = new File(con.getLocalPath(),localFileName);
00096     
00097     if (crc > 0 && crc != file.length())
00098       throw new Exception("CRC ERROR.");
00099     
00100     return file.getAbsolutePath();
00101   }
00102 
00103 
00104   //------ needed by ConnectionListener interface ------
00105   
00106   // called if the remote directory has changed
00107   public void updateRemoteDirectory(BasicConnection con) {}
00108 
00109   // called if a connection has been established
00110   public void connectionInitialized(BasicConnection con) {
00111     established = true;
00112   }
00113  
00114   // called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)
00115   public void updateProgress(String file, String type, long bytes) {}
00116 
00117   // called if connection fails
00118   public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed! " + why);}
00119 
00120   // up- or download has finished
00121   public void actionFinished(BasicConnection con) {}
00122 }

Generated on Tue Sep 16 16:14:22 2008 for joram by  doxygen 1.5.0