1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36
37
38 public class RestartServerTask extends AbstractTask {
39
40
41
42
43 private static final WaarpLogger logger =
44 WaarpLoggerFactory.getLogger(RestartServerTask.class);
45
46
47
48
49
50
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
60
61 final boolean isAdmin = session.getAuth().isValidRole(ROLE.SYSTEM);
62 if (!isAdmin) {
63
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
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
87 Configuration.configuration.launchInFixedDelay(thread, 1000,
88 TimeUnit.MILLISECONDS);
89 }
90
91 }