001package net.sf.logdistiller; 002 003/* 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017import java.io.Serializable; 018import java.util.HashSet; 019import java.util.Set; 020 021import org.apache.commons.lang.ArrayUtils; 022import org.apache.commons.lang.StringUtils; 023import org.jdom.Element; 024 025/** 026 * One condition for a LogEvent to be selected in a group. It consists of matches on attributes of the LogEvent. 027 */ 028public class Condition 029 implements Serializable 030{ 031 private static final long serialVersionUID = 9085774334169039381L; 032 033 private final Match[] matches; 034 035 public static final Condition ACCEPT_ALL = new Condition( null, new Match[0] ); 036 037 /** 038 * @since 1.1 039 */ 040 private final Set<String> tags; 041 042 public Condition( String tags, Match[] matches ) 043 { 044 if ( tags != null ) 045 { 046 this.tags = new HashSet<String>(); 047 String[] values = tags.split( "," ); 048 for ( int i = values.length - 1; i > 0; i-- ) 049 { 050 String tag = values[i].trim(); 051 if ( tag.length() > 0 ) 052 { 053 this.tags.add( tag ); 054 } 055 } 056 } 057 else 058 { 059 this.tags = null; 060 } 061 this.matches = (Match[]) ArrayUtils.clone( matches ); 062 } 063 064 public boolean match( LogEvent le ) 065 { 066 int count = matches.length; 067 for ( int i = 0; i < count; i++ ) 068 { 069 if ( !matches[i].match( le ) ) 070 { 071 return false; 072 } 073 } 074 return true; 075 } 076 077 /** 078 * @since 1.1 079 * @param tag tag value looked for 080 * @return <code>true</code> if this condition was flagged with corresponding tag 081 */ 082 public boolean hasTag( String tag ) 083 { 084 return ( tags == null ) ? false : tags.contains( tag ); 085 } 086 087 public Element dump() 088 { 089 Element elmt = new Element( "condition" ); 090 if ( tags != null ) 091 { 092 elmt.setAttribute( "tags", StringUtils.join( tags, ", " ) ); 093 }; 094 int count = ( matches == null ) ? 0 : matches.length; 095 for ( int i = 0; i < count; i++ ) 096 { 097 elmt.addContent( matches[i].dump() ); 098 } 099 return elmt; 100 } 101}