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