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.openr66.server;
21  
22  import org.waarp.common.database.exception.WaarpDatabaseException;
23  import org.waarp.common.logging.SysErrLogger;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
27  import org.waarp.common.utility.WaarpSystemUtil;
28  import org.waarp.openr66.configuration.FileBasedConfiguration;
29  import org.waarp.openr66.database.data.DbTaskRunner;
30  import org.waarp.openr66.protocol.configuration.Configuration;
31  import org.waarp.openr66.protocol.exception.OpenR66ProtocolBusinessException;
32  import org.waarp.openr66.protocol.localhandler.ServerActions;
33  
34  import java.io.File;
35  
36  import static org.waarp.common.database.DbConstant.*;
37  
38  /**
39   * Server local configuration export to files
40   */
41  public class ServerExportConfiguration {
42    /**
43     * Internal Logger
44     */
45    private static WaarpLogger logger;
46  
47    /**
48     * @param args as configuration file and the directory where to
49     *     export
50     */
51    public static void main(final String[] args) {
52      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
53          new WaarpSlf4JLoggerFactory(null));
54      if (logger == null) {
55        logger = WaarpLoggerFactory.getLogger(ServerExportConfiguration.class);
56      }
57      if (args.length < 2) {
58        SysErrLogger.FAKE_LOGGER.syserr(
59            "Need configuration file and the directory " + "where to " +
60            "export");
61        WaarpSystemUtil.systemExit(1);
62        return;
63      }
64      try {
65        if (!FileBasedConfiguration.setConfigurationServerMinimalFromXml(
66            Configuration.configuration, args[0])) {
67          logger.error("Needs a correct configuration file as first argument");
68          if (admin != null) {
69            admin.close();
70          }
71          WaarpSystemUtil.systemExit(1);
72          return;
73        }
74        final String directory = args[1];
75        final String hostname = Configuration.configuration.getHostId();
76        logger.info("Start of Export");
77        final File dir = new File(directory);
78        if (!dir.isDirectory()) {
79          dir.mkdirs();//NOSONAR
80        }
81        final String[] filenames =
82            ServerActions.staticConfigExport(dir.getAbsolutePath(), true, true,
83                                             true, true, true);
84        for (final String string : filenames) {
85          if (string != null) {
86            logger.info("Export: {}", string);
87          }
88        }
89        final String filename =
90            dir.getAbsolutePath() + File.separator + hostname +
91            "_Runners.run.xml";
92        try {
93          DbTaskRunner.writeXMLWriter(filename);
94        } catch (final WaarpDatabaseException e1) {
95          logger.error("Error", e1);
96          admin.close();
97          WaarpSystemUtil.systemExit(2);
98          return;
99        } catch (final OpenR66ProtocolBusinessException e1) {
100         logger.error("Error", e1);
101         admin.close();
102         WaarpSystemUtil.systemExit(2);
103         return;
104       }
105       logger.info("End of Export");
106     } finally {
107       if (admin != null) {
108         admin.close();
109       }
110       WaarpSystemUtil.systemExit(0);
111     }
112   }
113 
114 }