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  
21  package org.waarp.gateway.ftp;
22  
23  import org.waarp.common.file.filesystembased.FilesystemBasedFileParameterImpl;
24  import org.waarp.common.logging.SysErrLogger;
25  import org.waarp.common.logging.WaarpLogger;
26  import org.waarp.common.logging.WaarpLoggerFactory;
27  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
28  import org.waarp.ftp.core.exception.FtpNoConnectionException;
29  import org.waarp.gateway.ftp.config.FileBasedConfiguration;
30  import org.waarp.gateway.ftp.control.ExecBusinessHandler;
31  import org.waarp.gateway.ftp.data.FileSystemBasedDataBusinessHandler;
32  import org.waarp.gateway.ftp.exec.AbstractExecutor;
33  import org.waarp.gateway.ftp.service.FtpEngine;
34  import org.waarp.openr66.protocol.configuration.Configuration;
35  
36  /**
37   * Exec FTP Server using simple authentication (XML FileInterface based), and
38   * standard Directory and
39   * FileInterface implementation (Filesystem based).
40   */
41  public class ExecGatewayFtpServer {
42    /**
43     * Internal Logger
44     */
45    private static WaarpLogger logger;
46  
47    private ExecGatewayFtpServer() {
48    }
49  
50    /**
51     * Take a simple XML file as configuration.
52     *
53     * @param args
54     */
55    public static void main(final String[] args) {
56      if (args.length < 1) {
57        SysErrLogger.FAKE_LOGGER.syserr(
58            "Usage: " + ExecGatewayFtpServer.class.getName() +
59            " <config-file> [<r66config-file>]");
60        return;
61      }
62      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
63          new WaarpSlf4JLoggerFactory(null));
64      logger = WaarpLoggerFactory.getLogger(ExecGatewayFtpServer.class);
65      initialize(args[0], args.length > 1? args[1] : null);
66    }
67  
68    public static boolean initialize(final String config, final String r66file) {
69      boolean asAService = false;
70      if (logger == null) {
71        // Called as a service
72        logger = WaarpLoggerFactory.getLogger(ExecGatewayFtpServer.class);
73        asAService = true;
74      }
75      final FileBasedConfiguration configuration =
76          new FileBasedConfiguration(ExecGatewayFtpServer.class,
77                                     ExecBusinessHandler.class,
78                                     FileSystemBasedDataBusinessHandler.class,
79                                     new FilesystemBasedFileParameterImpl());
80      if (asAService) {
81        configuration.getShutdownConfiguration().serviceFuture =
82            FtpEngine.closeFuture;
83      }
84      if (!configuration.setConfigurationServerFromXml(config)) {
85        SysErrLogger.FAKE_LOGGER.syserr("Bad main configuration");
86        return false;
87      }
88      Configuration.configuration.setUseLocalExec(configuration.isUseLocalExec());
89      if (AbstractExecutor.useDatabase) {
90        // Use R66 module
91        if (r66file != null) {
92          if (!org.waarp.openr66.configuration.FileBasedConfiguration.setSubmitClientConfigurationFromXml(
93              Configuration.configuration, r66file)) {
94            SysErrLogger.FAKE_LOGGER.syserr("Bad R66 configuration");
95            return false;
96          }
97        } else {
98          // Cannot get R66 functional
99          SysErrLogger.FAKE_LOGGER.syserr(
100             "No R66PrepareTransfer configuration file");
101       }
102     } else {
103       SysErrLogger.FAKE_LOGGER.syserr("No R66PrepareTransfer support");
104     }
105     FileBasedConfiguration.fileBasedConfiguration = configuration;
106     // Start server.
107     configuration.configureLExec();
108     try {
109       configuration.serverStartup();
110     } catch (final FtpNoConnectionException e1) {
111       SysErrLogger.FAKE_LOGGER.syserr(e1);
112       configuration.releaseResources();
113       return false;
114     }
115     configuration.configureHttps();
116     configuration.configureConstraint();
117     try {
118       configuration.configureSnmp();
119     } catch (final FtpNoConnectionException e) {
120       SysErrLogger.FAKE_LOGGER.syserr(
121           "Cannot start SNMP support: " + e.getMessage());
122     }
123     logger.warn("FTP started " +
124                 (configuration.getFtpInternalConfiguration().isUsingNativeSsl()?
125                     "Implicit SSL On" :
126                     configuration.getFtpInternalConfiguration()
127                                  .isAcceptAuthProt()? "Explicit SSL On" :
128                         "with SSL Off"));
129     return true;
130   }
131 
132 }