Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fba3e6a718226c47922fa6f25803474bb4f3da04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//


package org.eclipse.jetty.monitor.triggers;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jetty.monitor.jmx.EventState;
import org.eclipse.jetty.monitor.jmx.EventTrigger;


/* ------------------------------------------------------------ */
/**
 * AndEventTrigger 
 *
 * EventTrigger aggregation using logical AND operation 
 * that executes matching of the aggregated event triggers
 * in left to right order  
 */
public class AndEventTrigger extends EventTrigger
{
    protected final List<EventTrigger> _triggers;
    
    /* ------------------------------------------------------------ */
    /**
     * Construct an event trigger and associate the list 
     * of event triggers to be aggregated by this trigger
     * 
     * @param triggers list of event triggers to add
     */
    public AndEventTrigger(List<EventTrigger> triggers)
    {
        _triggers = triggers;
    }

    /* ------------------------------------------------------------ */
    /**
     * Construct an event trigger and associate the array 
     * of event triggers to be aggregated by this trigger
     * 
     * @param triggers array of event triggers to add
     */
    public AndEventTrigger(EventTrigger... triggers)
    {
        _triggers = Arrays.asList(triggers);
    }

    /* ------------------------------------------------------------ */
    /**
     * Verify if the event trigger conditions are in the 
     * appropriate state for an event to be triggered.
     * This event trigger will match if all aggregated 
     * event triggers would return a match.
     * 
     * @see org.eclipse.jetty.monitor.jmx.EventTrigger#match(long)
     */
    public boolean match(long timestamp)
        throws Exception
    {
        for(EventTrigger trigger : _triggers)
        {
            if (!trigger.match(timestamp))
                return false;
        }
        return true;
    }

    /* ------------------------------------------------------------ */
    /**
     * Retrieve the event state associated with specified invocation
     * of the event trigger match method. This event trigger retrieves
     * the combined event state of all aggregated event triggers.
     * 
     * @param timestamp time stamp associated with invocation
     * @return event state or null if not found
     *
     * @see org.eclipse.jetty.monitor.jmx.EventTrigger#getState(long)
     */
    @Override
    public EventState getState(long timestamp)
    {
       EventState state = new EventState();
       
       for (EventTrigger trigger : _triggers)
       {
           EventState subState = trigger.getState(timestamp);
           state.addAll(subState.values());
       }
       
       return state;
    }

    /* ------------------------------------------------------------ */
    /**
     * Returns the string representation of this event trigger
     * in the format "AND(triger1,trigger2,...)". 
     * 
     * @return string representation of the event trigger
     * 
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        int cnt = 0;
        StringBuilder result = new StringBuilder();
        
        result.append("AND(");
        for (EventTrigger trigger : _triggers)
        {
            result.append(cnt++ > 0 ? "," : "");
            result.append(trigger);
        }
        result.append(')');
        
        return result.toString();
    }
}

Back to the top