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.Version;
27  import org.waarp.common.utility.WaarpShutdownHook;
28  import org.waarp.common.utility.WaarpSystemUtil;
29  import org.waarp.openr66.configuration.FileBasedConfiguration;
30  import org.waarp.openr66.protocol.configuration.Configuration;
31  import org.waarp.openr66.protocol.configuration.Messages;
32  
33  /**
34   * R66Server startup main class
35   */
36  public class R66Server {
37    private static WaarpLogger logger;
38  
39    private R66Server() {
40    }
41  
42    /**
43     * @param args as first argument the configuration file
44     */
45    public static void main(final String[] args) {
46      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
47          new WaarpSlf4JLoggerFactory(null));
48      logger = WaarpLoggerFactory.getLogger(R66Server.class);
49      if (args.length < 1) {
50        logger.error(
51            Messages.getString("Configuration.NeedConfig")); //$NON-NLS-1$
52        return;
53      }
54      try {
55        WaarpShutdownHook.registerMain(R66Server.class, args);
56        if (initialize(args[0])) {
57          logger.warn(Messages.getString("R66Server.ServerStart") +
58                      Configuration.configuration.getHostId() + " : "
59                      //$NON-NLS-1$
60                      + Configuration.configuration + " Version: " +
61                      Version.fullIdentifier());
62          SysErrLogger.FAKE_LOGGER.sysout(
63              Messages.getString("R66Server.ServerStart") +
64              Configuration.configuration.getHostId()); //$NON-NLS-1$
65        } else {
66          logger.error(Messages.getString("R66Server.CannotStart") +
67                       Configuration.configuration.getHostId()); //$NON-NLS-1$
68          SysErrLogger.FAKE_LOGGER.syserr(
69              Messages.getString("R66Server.CannotStart") +
70              Configuration.configuration.getHostId()); //$NON-NLS-1$
71          WaarpSystemUtil.systemExit(1);
72        }
73      } catch (final Throwable e) {
74        logger.error(Messages.getString("R66Server.StartError"), e); //$NON-NLS-1$
75      }
76    }
77  
78    public static boolean initialize(final String config) {
79      if (logger == null) {
80        logger = WaarpLoggerFactory.getLogger(R66Server.class);
81      }
82      if (!FileBasedConfiguration.setConfigurationServerFromXml(
83          Configuration.configuration, config, true)) {
84        logger.error(
85            Messages.getString("Configuration.NeedCorrectConfig")); //$NON-NLS-1$
86        return false;
87      }
88      try {
89        Configuration.configuration.serverStartup();
90      } catch (final Throwable e) {
91        logger.error(Messages.getString("R66Server.StartError"), e); //$NON-NLS-1$
92        WaarpShutdownHook.terminate(false);
93        return false;
94      }
95      return true;
96    }
97  }