View Javadoc

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