View Javadoc
1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.client;
19  
20  import java.io.IOException;
21  import java.util.Timer;
22  import java.util.TimerTask;
23  
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  
27  import it.sauronsoftware.ftp4j.FTPClient;
28  import it.sauronsoftware.ftp4j.FTPDataTransferListener;
29  import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
30  
31  /**
32   * Timeout listener for Data connection for FTP4J model
33   * 
34   * @author "Frederic Bregier"
35   * 
36   */
37  public class DataTimeOutListener implements FTPDataTransferListener {
38      /**
39       * Internal Logger
40       */
41      private static final WaarpLogger logger = WaarpLoggerFactory.getLogger(DataTimeOutListener.class);
42  
43      private final FTPClient client;
44      private Timer timer;
45      private long timeout = 10000;
46      private long last = System.currentTimeMillis();
47      private boolean finished = false;
48      private String command;
49      private String file;
50  
51      public DataTimeOutListener(FTPClient client, long timeout, String command, String file) {
52          this.client = client;
53          timer = new Timer(true);
54          this.timeout = timeout;
55          this.command = command;
56          this.file = file;
57      }
58  
59      private void renewTask() {
60          TimerTask task = new TimerTask() {
61              public void run() {
62                  if (finished) {
63                      return;
64                  }
65                  long now = System.currentTimeMillis();
66                  if (now - last - timeout > 0) {
67                      try {
68                          logger.warn("Timeout during file transfer: " + command + " " + file);
69                          client.abortCurrentDataTransfer(true);
70                      } catch (IOException e) {
71                      } catch (FTPIllegalReplyException e) {
72                      }
73                  } else {
74                      renewTask();
75                  }
76              }
77          };
78          timer.schedule(task, timeout);
79      }
80  
81      @Override
82      public void started() {
83          renewTask();
84          last = System.currentTimeMillis();
85      }
86  
87      @Override
88      public void transferred(int length) {
89          last = System.currentTimeMillis();
90      }
91  
92      @Override
93      public void completed() {
94          finished = true;
95          last = System.currentTimeMillis();
96          timer.cancel();
97      }
98  
99      @Override
100     public void aborted() {
101         finished = true;
102         last = System.currentTimeMillis();
103         timer.cancel();
104     }
105 
106     @Override
107     public void failed() {
108         finished = true;
109         last = System.currentTimeMillis();
110         timer.cancel();
111     }
112 
113 }