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.core.utils;
19  
20  import java.util.TimerTask;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.ftp.core.config.FtpConfiguration;
25  
26  /**
27   * Timer Task used mainly when the server is going to shutdown in order to be sure the program exit.
28   * 
29   * @author Frederic Bregier
30   * 
31   */
32  public class FtpTimerTask extends TimerTask {
33      /**
34       * Internal Logger
35       */
36      private static final WaarpLogger logger = WaarpLoggerFactory
37              .getLogger(FtpTimerTask.class);
38  
39      /**
40       * EXIT type (System.exit(1))
41       */
42      public static final int TIMER_EXIT = 1;
43      /**
44       * Finalize Control connection
45       */
46      public static final int TIMER_CONTROL = 2;
47  
48      /**
49       * Type of execution in run() method
50       */
51      private final int type;
52      /**
53       * Configuration
54       */
55      private FtpConfiguration configuration = null;
56  
57      /**
58       * Constructor from type
59       * 
60       * @param type
61       */
62      public FtpTimerTask(int type) {
63          super();
64          this.type = type;
65      }
66  
67      @Override
68      public void run() {
69          switch (type) {
70              case TIMER_EXIT:
71                  logger.error("System will force EXIT");
72                  System.exit(0);
73                  break;
74              case TIMER_CONTROL:
75                  logger.info("Exit Shutdown Command");
76                  FtpChannelUtils.terminateCommandChannels(getConfiguration());
77                  logger.warn("Exit end of Command Shutdown");
78                  //FtpChannelUtils.stopLogger();
79                  break;
80              default:
81                  logger.info("Type unknown in TimerTask");
82          }
83      }
84  
85      /**
86       * @return the configuration
87       */
88      public FtpConfiguration getConfiguration() {
89          return configuration;
90      }
91  
92      /**
93       * @param configuration the configuration to set
94       */
95      public void setConfiguration(FtpConfiguration configuration) {
96          this.configuration = configuration;
97      }
98  }