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.gateway.ftp.service;
21  
22  import org.waarp.common.future.WaarpFuture;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.common.service.EngineAbstract;
26  import org.waarp.common.utility.SystemPropertyUtil;
27  import org.waarp.ftp.core.config.FtpConfiguration;
28  import org.waarp.ftp.core.utils.FtpChannelUtils;
29  import org.waarp.gateway.ftp.ExecGatewayFtpServer;
30  import org.waarp.gateway.ftp.config.FileBasedConfiguration;
31  
32  /**
33   * Engine used to start and stop the real Gateway Ftp service
34   */
35  public class FtpEngine extends EngineAbstract {
36    /**
37     * Internal Logger
38     */
39    private static final WaarpLogger logger =
40        WaarpLoggerFactory.getLogger(FtpEngine.class);
41  
42    public static final WaarpFuture closeFuture = new WaarpFuture(true);
43  
44    public static final String CONFIGFILE = "org.waarp.gateway.ftp.config.file";
45  
46    public static final String R66CONFIGFILE = "org.waarp.r66.config.file";
47  
48    @Override
49    public final void run() {
50      final String ftpfile = SystemPropertyUtil.get(CONFIGFILE);
51      final String r66file = SystemPropertyUtil.get(R66CONFIGFILE);
52      if (ftpfile == null) {
53        logger.error("Cannot find " + CONFIGFILE + " parameter");
54        shutdown();
55        return;
56      }
57      try {
58        if (!ExecGatewayFtpServer.initialize(ftpfile, r66file)) {
59          logger.error("Cannot start Gateway FTP");
60          shutdown();
61          return;
62        }
63      } catch (final Throwable e) {
64        logger.error("Cannot start Gateway FTP", e);
65        shutdown();
66        return;
67      }
68      logger.warn("Service started with " + ftpfile);
69    }
70  
71    private static void exit(final FtpConfiguration configuration) {
72      final FtpChannelUtils util = new FtpChannelUtils(configuration);
73      util.run();
74    }
75  
76    @Override
77    public final void shutdown() {
78      exit(FileBasedConfiguration.fileBasedConfiguration);
79      closeFuture.setSuccess();
80      logger.info("Service stopped");
81    }
82  
83    @Override
84    public final boolean isShutdown() {
85      return closeFuture.isDone();
86    }
87  
88    @Override
89    public final boolean waitShutdown() {
90      closeFuture.awaitOrInterruptible();
91      return closeFuture.isSuccess();
92    }
93  }