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;
21  
22  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
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.common.utility.WaarpSystemUtil;
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.database.DbConstantFtp;
33  
34  /**
35   * Program to initialize the database for Waarp Ftp Exec
36   */
37  public class ServerInitDatabase {
38    /**
39     * Internal Logger
40     */
41    static volatile WaarpLogger logger;
42  
43    static String sxml;
44    static boolean database;
45  
46    private ServerInitDatabase() {
47    }
48  
49    protected static boolean getParams(final String[] args) {
50      if (args.length < 1) {
51        logger.error(
52            "Need at least the configuration file as first argument then optionally\n" +
53            "    -initdb");
54        return false;
55      }
56      sxml = args[0];
57      for (int i = 1; i < args.length; i++) {
58        if ("-initdb".equalsIgnoreCase(args[i])) {
59          database = true;
60          break;
61        }
62      }
63      return true;
64    }
65  
66    /**
67     * @param args as config_database file [rules_directory host_authent
68     *     limit_configuration]
69     */
70    public static void main(final String[] args) {
71      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
72          new WaarpSlf4JLoggerFactory(null));
73      if (logger == null) {
74        logger = WaarpLoggerFactory.getLogger(ServerInitDatabase.class);
75      }
76      if (!getParams(args)) {
77        logger.error(
78            "Need at least the configuration file as first argument then optionally\n" +
79            "    -initdb");
80        if (DbConstantFtp.gatewayAdmin != null) {
81          DbConstantFtp.gatewayAdmin.close();
82        }
83        WaarpSystemUtil.systemExit(1);
84        return;
85      }
86      final FileBasedConfiguration configuration =
87          new FileBasedConfiguration(ExecGatewayFtpServer.class,
88                                     ExecBusinessHandler.class,
89                                     FileSystemBasedDataBusinessHandler.class,
90                                     new FilesystemBasedFileParameterImpl());
91      try {
92        if (!configuration.setConfigurationServerFromXml(args[0])) {
93          SysErrLogger.FAKE_LOGGER.syserr("Bad main configuration");
94          if (DbConstantFtp.gatewayAdmin != null) {
95            DbConstantFtp.gatewayAdmin.close();
96          }
97          WaarpSystemUtil.systemExit(1);
98          return;
99        }
100       if (database) {
101         // Init database
102         try {
103           initdb();
104         } catch (final WaarpDatabaseNoConnectionException e) {
105           logger.error("Cannot connect to database");
106           return;
107         }
108         SysErrLogger.FAKE_LOGGER.sysout("End creation");
109       }
110       SysErrLogger.FAKE_LOGGER.sysout("Load done");
111     } finally {
112       if (DbConstantFtp.gatewayAdmin != null) {
113         DbConstantFtp.gatewayAdmin.close();
114       }
115     }
116   }
117 
118   public static void initdb() throws WaarpDatabaseNoConnectionException {
119     // Create tables: configuration, hosts, rules, runner, cptrunner
120     DbConstantFtp.gatewayAdmin.getDbModel().createTables(
121         DbConstantFtp.gatewayAdmin.getSession());
122   }
123 
124 }