KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > mom > dest > ftp > TransferImplJftp


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2007 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package com.scalagent.joram.mom.dest.ftp;
24
25 import java.io.File JavaDoc;
26
27 import net.sf.jftp.config.Settings;
28 import net.sf.jftp.net.BasicConnection;
29 import net.sf.jftp.net.ConnectionHandler;
30 import net.sf.jftp.net.ConnectionListener;
31 import net.sf.jftp.net.FtpConnection;
32
33 public class TransferImplJftp
34   implements TransferItf, ConnectionListener {
35
36   private boolean established = false;
37
38   // connection pool, not necessary but you should take a look at this class
39
// if you want to use multiple event based ftp transfers.
40
private ConnectionHandler handler = new ConnectionHandler();
41
42   public String JavaDoc getFile(String JavaDoc protocol,
43                         String JavaDoc host,
44                         int port,
45                         String JavaDoc user,
46                         String JavaDoc pass,
47                         String JavaDoc remotePath,
48                         String JavaDoc localPath,
49                         String JavaDoc remoteFileName,
50                         String JavaDoc localFileName,
51                         String JavaDoc type,
52                         long crc) throws Exception JavaDoc {
53     
54     Settings.setProperty("jftp.disableLog","true");
55     Settings.setProperty("jftp.enableMultiThreading","true");
56     Settings.maxConnections = 5;
57     Settings.enableResuming = true;
58     
59     // create a FtpConnection
60
FtpConnection con = null;
61     if (port > -1)
62       con = new FtpConnection(host,port,remotePath);
63     else
64       con = new FtpConnection(host);
65
66     // set updatelistener, interface methods are below
67
con.addConnectionListener(this);
68
69     // set handler
70
con.setConnectionHandler(handler);
71
72     // connect and login.
73
con.login(user,pass);
74
75     // login calls connectionInitialized() below which sets established to true
76
while(!established) {
77       try {
78         Thread.sleep(10);
79       } catch(Exception JavaDoc exc) {
80         throw exc;
81       }
82     }
83
84     if (remotePath != null)
85       con.chdirRaw(remotePath);
86
87     if (localPath != null)
88       con.setLocalPath(localPath);
89
90     //System.out.println("LocalPath = " + con.getLocalPath());
91

92     // which spawns a new thread for the download
93
con.download(remoteFileName);
94     
95     File JavaDoc file = new File JavaDoc(con.getLocalPath(),localFileName);
96     
97     if (crc > 0 && crc != file.length())
98       throw new Exception JavaDoc("CRC ERROR.");
99     
100     return file.getAbsolutePath();
101   }
102
103
104   //------ needed by ConnectionListener interface ------
105

106   // called if the remote directory has changed
107
public void updateRemoteDirectory(BasicConnection con) {}
108
109   // called if a connection has been established
110
public void connectionInitialized(BasicConnection con) {
111     established = true;
112   }
113  
114   // called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)
115
public void updateProgress(String JavaDoc file, String JavaDoc type, long bytes) {}
116
117   // called if connection fails
118
public void connectionFailed(BasicConnection con, String JavaDoc why) {System.out.println("connection failed! " + why);}
119
120   // up- or download has finished
121
public void actionFinished(BasicConnection con) {}
122 }
123
Popular Tags