1 // ========================================================================
2 // Copyright (c) Webtide LLC
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.monitor.jmx;
15
16 import static java.util.UUID.randomUUID;
17
18 import java.security.InvalidParameterException;
19
20 /* ------------------------------------------------------------ */
21 /**
22 * MonitorAction
23 *
24 * Abstract base class for all MonitorAction implementations.
25 * Receives notification when an associated EventTrigger is matched.
26 */
27 public abstract class MonitorAction
28 extends NotifierGroup
29 {
30 public static final int DEFAULT_POLL_INTERVAL = 5000;
31
32 private final String _id;
33 private final EventTrigger _trigger;
34 private final EventNotifier _notifier;
35 private final long _pollInterval;
36 private final long _pollDelay;
37
38 /* ------------------------------------------------------------ */
39 /**
40 * Creates a new monitor action
41 *
42 * @param trigger event trigger to be associated with this action
43 * @throws InvalidParameterException
44 */
45 public MonitorAction(EventTrigger trigger)
46 throws InvalidParameterException
47 {
48 this(trigger, null, 0, 0);
49 }
50
51
52 /* ------------------------------------------------------------ */
53 /**
54 * Creates a new monitor action
55 *
56 * @param trigger event trigger to be associated with this action
57 * @param notifier event notifier to be associated with this action
58 * @throws InvalidParameterException
59 */
60 public MonitorAction(EventTrigger trigger, EventNotifier notifier)
61 throws InvalidParameterException
62 {
63 this(trigger, notifier, 0);
64 }
65
66 /* ------------------------------------------------------------ */
67 /**
68 * Creates a new monitor action
69 *
70 * @param trigger event trigger to be associated with this action
71 * @param notifier event notifier to be associated with this action
72 * @param pollInterval interval for polling of the JMX server
73 * @throws InvalidParameterException
74 */
75 public MonitorAction(EventTrigger trigger, EventNotifier notifier, long pollInterval)
76 throws InvalidParameterException
77 {
78 this(trigger, notifier, pollInterval, 0);
79 }
80
81 /* ------------------------------------------------------------ */
82 /**
83 * Creates a new monitor action
84 *
85 * @param trigger event trigger to be associated with this action
86 * @param notifier event notifier to be associated with this action
87 * @param pollInterval interval for polling of the JMX server
88 * @param pollDelay delay before starting to poll the JMX server
89 * @throws InvalidParameterException
90 */
91 public MonitorAction(EventTrigger trigger, EventNotifier notifier, long pollInterval, long pollDelay)
92 throws InvalidParameterException
93 {
94 if (trigger == null)
95 throw new InvalidParameterException("Trigger cannot be null");
96
97 _id = randomUUID().toString();
98 _trigger = trigger;
99 _notifier = notifier;
100 _pollInterval = pollInterval > 0 ? pollInterval : DEFAULT_POLL_INTERVAL;
101 _pollDelay = pollDelay > 0 ? pollDelay : _pollInterval;
102 }
103
104 /* ------------------------------------------------------------ */
105 /**
106 * Retrieve the identification string of the monitor action
107 *
108 * @return unique identification string
109 */
110
111 public final String getID()
112 {
113 return _id;
114 }
115
116
117 /* ------------------------------------------------------------ */
118 /**
119 * Retrieve the event trigger of the monitor action
120 *
121 * @return associated event trigger
122 */
123 public EventTrigger getTrigger()
124 {
125 return _trigger;
126 }
127
128 /* ------------------------------------------------------------ */
129 /**
130 * Retrieve the poll interval
131 *
132 * @return interval value (in milliseconds)
133 */
134 public long getPollInterval()
135 {
136 return _pollInterval;
137 }
138
139
140 /* ------------------------------------------------------------ */
141 /** Retrieve the poll delay
142 * @return delay value (in milliseconds)
143 */
144 public long getPollDelay()
145 {
146 return _pollDelay;
147 }
148
149 /* ------------------------------------------------------------ */
150 /**
151 * This method will be called when event trigger associated
152 * with this monitor action matches its conditions.
153 *
154 * @param timestamp time stamp of the event
155 */
156 public final void doExecute(long timestamp)
157 {
158 EventState<?> state =_trigger.getState(timestamp);
159 if (_notifier != null)
160 _notifier.notify(_trigger, state, timestamp);
161 execute(_trigger, state, timestamp);
162 }
163
164 /* ------------------------------------------------------------ */
165 /**
166 * This method will be called to allow subclass to execute
167 * the desired action in response to the event.
168 *
169 * @param trigger event trigger associated with this monitor action
170 * @param state event state associated with current invocation of event trigger
171 * @param timestamp time stamp of the current invocation of event trigger
172 */
173 public abstract void execute(EventTrigger trigger, EventState<?> state, long timestamp);
174 }