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.context.task;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.common.role.RoleDefault.ROLE;
25  import org.waarp.common.utility.WaarpShutdownHook;
26  import org.waarp.openr66.context.R66Session;
27  import org.waarp.openr66.protocol.configuration.Configuration;
28  import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
29  import org.waarp.openr66.protocol.utils.ChannelUtils;
30  
31  import java.util.concurrent.TimeUnit;
32  
33  /**
34   * Command to Restart the R66 server (for instance after upgrade of jar sent by
35   * administrative operations,
36   * unzipped in the library directory)
37   */
38  public class RestartServerTask extends AbstractTask {
39  
40    /**
41     * Internal Logger
42     */
43    private static final WaarpLogger logger =
44        WaarpLoggerFactory.getLogger(RestartServerTask.class);
45  
46    /**
47     * @param argRule
48     * @param delay
49     * @param argTransfer
50     * @param session
51     */
52    public RestartServerTask(final String argRule, final int delay,
53                             final String argTransfer, final R66Session session) {
54      super(TaskType.RESTART, delay, argRule, argTransfer, session);
55    }
56  
57    @Override
58    public void run() {
59      // check if allowed to do restart
60      // SYSTEM authorization
61      final boolean isAdmin = session.getAuth().isValidRole(ROLE.SYSTEM);
62      if (!isAdmin) {
63        // not allowed
64        logger.error("Shutdown order asked through task but unallowed: " +
65                     session.getAuth().getUser());
66        futureCompletion.setFailure(new OpenR66ProtocolSystemException(
67            "Shutdown order asked through task but unallowed: " +
68            session.getAuth().getUser()));
69        return;
70      }
71      if (!Configuration.configuration.isServer()) {
72        logger.error(
73            "Shutdown order asked through task but this is a client, not a server");
74        futureCompletion.setFailure(new OpenR66ProtocolSystemException(
75            "Shutdown order asked through task but this is a client, not a server"));
76        return;
77      }
78      // now start the process
79      logger.warn("Shutdown order received and going from: " +
80                  session.getAuth().getUser());
81      WaarpShutdownHook.setRestart(true);
82      futureCompletion.setSuccess();
83      final Thread thread =
84          new Thread(new ChannelUtils(), "R66 Shutdown and Restart Thread");
85      thread.setDaemon(true);
86      // give time for the task to finish correctly
87      Configuration.configuration.launchInFixedDelay(thread, 1000,
88                                                     TimeUnit.MILLISECONDS);
89    }
90  
91  }