View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.ftp.core.utils;
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  import java.util.TimerTask;
27  
28  /**
29   * Timer Task used mainly when the server is going to shutdown in order to be
30   * sure the program exit.
31   */
32  public class FtpTimerTask extends TimerTask {
33    /**
34     * Internal Logger
35     */
36    private static final WaarpLogger logger =
37        WaarpLoggerFactory.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;
56  
57    /**
58     * Constructor from type
59     *
60     * @param type
61     */
62    public FtpTimerTask(final int type) {
63      this.type = type;
64    }
65  
66    @Override
67    public void run() {
68      switch (type) {
69        case TIMER_EXIT:
70          logger.error("System will force EXIT");
71          //FBGEXIT DetectionUtils.SystemExit(0)
72          break;
73        case TIMER_CONTROL:
74          logger.info("Exit Shutdown Command");
75          FtpChannelUtils.terminateCommandChannels(getConfiguration());
76          logger.warn("Exit end of Command Shutdown");
77          break;
78        default:
79          logger.info("Type unknown in TimerTask");
80      }
81    }
82  
83    /**
84     * @return the configuration
85     */
86    public FtpConfiguration getConfiguration() {
87      return configuration;
88    }
89  
90    /**
91     * @param configuration the configuration to set
92     */
93    public final void setConfiguration(final FtpConfiguration configuration) {
94      this.configuration = configuration;
95    }
96  }