1 package org.csc.phynixx.watchdog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.csc.phynixx.common.logger.IPhynixxLogger;
25 import org.csc.phynixx.common.logger.PhynixxLogManager;
26 import org.csc.phynixx.watchdog.log.CheckConditionFailedLog;
27 import org.csc.phynixx.watchdog.log.ConditionViolatedLog;
28
29
30
31
32
33
34 class RestartCondition extends TimeoutCondition implements IWatchedCondition {
35
36 private IPhynixxLogger log = PhynixxLogManager.getLogger(this.getClass());
37
38 private WatchdogReference watchdogReference = null;
39
40 RestartCondition(long checkInterval, Watchdog wd) {
41 super(checkInterval);
42 this.watchdogReference = new WatchdogReference(wd);
43 }
44
45
46
47
48
49
50 public boolean checkCondition() {
51
52
53 if (super.checkCondition()) {
54 return true;
55 }
56
57 if (this.watchdogReference.isStale()) {
58 return false;
59 }
60
61 Watchdog wd = this.watchdogReference.getWatchdog();
62
63
64 if (!wd.isAlive()) {
65 if (log.isInfoEnabled()) {
66 String logString = "RestartCondition :: Watchdog " + wd.getThreadHandle() + " is not alive ";
67 log.info(new CheckConditionFailedLog(this, logString).toString());
68 }
69 return false;
70 }
71 return true;
72 }
73
74
75 public String toString() {
76 return " RestartCondition referenced Watchdog " + watchdogReference.getId() + " and check interval=" + this.getTimeout();
77 }
78
79
80
81
82
83
84 public void conditionViolated() {
85 if (this.watchdogReference.isStale()) {
86 throw new IllegalStateException("Watchdog is stale and does not exist any longer");
87 }
88
89 Watchdog wd = this.watchdogReference.getWatchdog();
90
91 wd.restart();
92 if (log.isInfoEnabled()) {
93 log.info(new ConditionViolatedLog(this, "Watchdog " + wd.getId() + " is restarted by Condition " + this.toString()).toString());
94 }
95
96 }
97
98
99 public boolean isUseless() {
100 return this.watchdogReference!=null || this.watchdogReference.isStale();
101 }
102
103
104 }