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.service;
21  
22  import org.waarp.common.future.WaarpFuture;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.common.service.EngineAbstract;
26  import org.waarp.common.utility.SystemPropertyUtil;
27  import org.waarp.openr66.protocol.configuration.Configuration;
28  import org.waarp.openr66.protocol.configuration.R66SystemProperties;
29  import org.waarp.openr66.protocol.utils.ChannelUtils;
30  import org.waarp.openr66.server.R66Server;
31  
32  /**
33   * Engine used to start and stop the real R66 service
34   */
35  public class R66Engine extends EngineAbstract {
36    /**
37     * Internal Logger
38     */
39    private static final WaarpLogger logger =
40        WaarpLoggerFactory.getLogger(R66Engine.class);
41  
42    static final WaarpFuture closeFuture = new WaarpFuture(true);
43  
44    @Override
45    public void run() {
46      final String config =
47          SystemPropertyUtil.get(R66SystemProperties.OPENR66_CONFIGFILE);
48      if (config == null) {
49        logger.error("Cannot find " + R66SystemProperties.OPENR66_CONFIGFILE +
50                     " parameter");
51        closeFuture.cancel();
52        shutdown();
53        return;
54      }
55      Configuration.configuration.getShutdownConfiguration().serviceFuture =
56          closeFuture;
57      try {
58        if (!R66Server.initialize(config)) {
59          throw new Exception("Initialization in error");
60        }
61      } catch (final Throwable e) {
62        logger.error("Cannot start R66", e);
63        closeFuture.cancel();
64        shutdown();
65        return;
66      }
67      logger.warn("Service started with " + config);
68    }
69  
70    @Override
71    public final void shutdown() {
72      logger.warn("Shutdown");
73      ChannelUtils.startShutdown();
74      closeFuture.setSuccess();
75      logger.info("Service stopped");
76    }
77  
78    @Override
79    public final boolean isShutdown() {
80      return closeFuture.isDone();
81    }
82  
83    @Override
84    public final boolean waitShutdown() {
85      closeFuture.awaitOrInterruptible();
86      logger.info("Shutdown on going: {}", closeFuture.isSuccess());
87      return closeFuture.isSuccess();
88    }
89  }