View Javadoc
1   package net.sf.logdistiller;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.Serializable;
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import org.apache.commons.lang.ArrayUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.jdom.Element;
24  
25  /**
26   * One condition for a LogEvent to be selected in a group. It consists of matches on attributes of the LogEvent.
27   */
28  public class Condition
29      implements Serializable
30  {
31      private static final long serialVersionUID = 9085774334169039381L;
32  
33      private final Match[] matches;
34  
35      public static final Condition ACCEPT_ALL = new Condition( null, new Match[0] );
36  
37      /**
38       * @since 1.1
39       */
40      private final Set<String> tags;
41  
42      public Condition( String tags, Match[] matches )
43      {
44          if ( tags != null )
45          {
46              this.tags = new HashSet<String>();
47              String[] values = tags.split( "," );
48              for ( int i = values.length - 1; i > 0; i-- )
49              {
50                  String tag = values[i].trim();
51                  if ( tag.length() > 0 )
52                  {
53                      this.tags.add( tag );
54                  }
55              }
56          }
57          else
58          {
59              this.tags = null;
60          }
61          this.matches = (Match[]) ArrayUtils.clone( matches );
62      }
63  
64      public boolean match( LogEvent le )
65      {
66          int count = matches.length;
67          for ( int i = 0; i < count; i++ )
68          {
69              if ( !matches[i].match( le ) )
70              {
71                  return false;
72              }
73          }
74          return true;
75      }
76  
77      /**
78       * @since 1.1
79       * @param tag tag value looked for
80       * @return <code>true</code> if this condition was flagged with corresponding tag
81       */
82      public boolean hasTag( String tag )
83      {
84          return ( tags == null ) ? false : tags.contains( tag );
85      }
86  
87      public Element dump()
88      {
89          Element elmt = new Element( "condition" );
90          if ( tags != null )
91          {
92              elmt.setAttribute( "tags", StringUtils.join( tags, ", " ) );
93          };
94          int count = ( matches == null ) ? 0 : matches.length;
95          for ( int i = 0; i < count; i++ )
96          {
97              elmt.addContent( matches[i].dump() );
98          }
99          return elmt;
100     }
101 }