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.triggers;
20
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.eclipse.jetty.monitor.jmx.EventState;
25 import org.eclipse.jetty.monitor.jmx.EventTrigger;
26
27
28 /* ------------------------------------------------------------ */
29 /**
30 * AndEventTrigger
31 *
32 * EventTrigger aggregation using logical OR operation
33 * that executes matching of the aggregated event triggers
34 * in left to right order
35 */
36 public class OrEventTrigger
37 extends EventTrigger
38 {
39 private final List<EventTrigger> _triggers;
40
41 /* ------------------------------------------------------------ */
42 /**
43 * Construct an event trigger and associate the list
44 * of event triggers to be aggregated by this trigger
45 *
46 * @param triggers list of event triggers to add
47 */
48 public OrEventTrigger(List<EventTrigger> triggers)
49 {
50 _triggers = triggers;
51 }
52
53 /* ------------------------------------------------------------ */
54 /**
55 * Construct an event trigger and associate the array
56 * of event triggers to be aggregated by this trigger
57 *
58 * @param triggers array of event triggers to add
59 */
60 public OrEventTrigger(EventTrigger... triggers)
61 {
62 _triggers = Arrays.asList(triggers);
63 }
64
65 /* ------------------------------------------------------------ */
66 /**
67 * Verify if the event trigger conditions are in the
68 * appropriate state for an event to be triggered.
69 * This event trigger will match if any of aggregated
70 * event triggers would return a match.
71 *
72 * @see org.eclipse.jetty.monitor.jmx.EventTrigger#match(long)
73 */
74 public boolean match(long timestamp)
75 throws Exception
76 {
77 for(EventTrigger trigger : _triggers)
78 {
79 if (trigger.match(timestamp))
80 return true;
81 }
82 return false;
83 }
84
85 /* ------------------------------------------------------------ */
86 /**
87 * Retrieve the event state associated with specified invocation
88 * of the event trigger match method. This event trigger retrieves
89 * the combined event state of all aggregated event triggers.
90 *
91 * @param timestamp time stamp associated with invocation
92 * @return event state or null if not found
93 *
94 * @see org.eclipse.jetty.monitor.jmx.EventTrigger#getState(long)
95 */
96 @Override
97 @SuppressWarnings("unchecked")
98 public EventState getState(long timestamp)
99 {
100 EventState state = new EventState();
101
102 for (EventTrigger trigger : _triggers)
103 {
104 EventState subState = trigger.getState(timestamp);
105 if (subState!=null)
106 {
107 state.addAll(subState.values());
108 }
109 }
110
111 return state;
112 }
113
114 /* ------------------------------------------------------------ */
115 /**
116 * Returns the string representation of this event trigger
117 * in the format "OR(triger1,trigger2,...)".
118 *
119 * @return string representation of the event trigger
120 *
121 * @see java.lang.Object#toString()
122 */
123 public String toString()
124 {
125 int cnt = 0;
126 StringBuilder result = new StringBuilder();
127
128 result.append("OR(");
129 for (EventTrigger trigger : _triggers)
130 {
131 result.append(cnt++ > 0 ? "," : "");
132 result.append(trigger);
133 }
134 result.append(')');
135
136 return result.toString();
137 }
138 }