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.logging.SysErrLogger;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
26  import org.waarp.common.utility.WaarpSystemUtil;
27  import org.waarp.openr66.configuration.FileBasedConfiguration;
28  import org.waarp.openr66.database.data.DbTaskRunner;
29  import org.waarp.openr66.protocol.configuration.Configuration;
30  import org.waarp.openr66.protocol.exception.OpenR66ProtocolBusinessException;
31  
32  import java.io.File;
33  
34  import static org.waarp.common.database.DbConstant.*;
35  
36  /**
37   * To import logs that were exported (one should not try to import on the same
38   * server to prevent strange
39   * behavior).
40   */
41  public class LogImport {
42    /**
43     * Internal Logger
44     */
45    private static WaarpLogger logger;
46  
47    /**
48     * @param args
49     */
50    public static void main(final String[] args) {
51      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
52          new WaarpSlf4JLoggerFactory(null));
53      if (logger == null) {
54        logger = WaarpLoggerFactory.getLogger(LogImport.class);
55      }
56      if (args.length < 2) {
57        SysErrLogger.FAKE_LOGGER.syserr(
58            "Need configuration file and the logfile to import");
59        WaarpSystemUtil.systemExit(1);
60        return;
61      }
62      try {
63        if (!FileBasedConfiguration.setConfigurationServerMinimalFromXml(
64            Configuration.configuration, args[0])) {
65          logger.error("Needs a correct configuration file as first argument");
66          if (admin != null) {
67            admin.close();
68          }
69          WaarpSystemUtil.systemExit(1);
70          return;
71        }
72        final long time1 = System.currentTimeMillis();
73        final File logsFile = new File(args[1]);
74        try {
75          DbTaskRunner.loadXml(logsFile);
76        } catch (final OpenR66ProtocolBusinessException e) {
77          logger.error("Cannot load the logs from " + logsFile.getAbsolutePath() +
78                       " since: " + e.getMessage(), e);
79          if (admin != null) {
80            admin.close();
81          }
82          WaarpSystemUtil.systemExit(1);
83          return;
84        }
85        final long time2 = System.currentTimeMillis();
86        final long delay = time2 - time1;
87        logger.warn("LogFile imported in " + delay + " ms");
88      } finally {
89        if (admin != null) {
90          admin.close();
91        }
92        WaarpSystemUtil.systemExit(0);
93      }
94    }
95  }