View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.rule;
19  
20  import org.apache.log4j.spi.LoggingEvent;
21  import org.apache.log4j.spi.LoggingEventFieldResolver;
22  
23  import java.io.IOException;
24  import java.util.Stack;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  import java.util.regex.PatternSyntaxException;
28  
29  /***
30   * A Rule class supporting java.util.regex regular expression syntax.
31   *
32   * @author Scott Deboy (sdeboy@apache.org)
33   */
34  public class LikeRule extends AbstractRule {
35      /***
36       * Serialization ID.
37       */
38    static final long serialVersionUID = -3375458885595683156L;
39  
40      /***
41       * Resolver.
42       */
43    private static final LoggingEventFieldResolver RESOLVER =
44              LoggingEventFieldResolver.getInstance();
45      /***
46       * Pattern.
47       */
48    private transient Pattern pattern;
49      /***
50       * Regular expression matcher.
51       */
52    private transient Matcher matcher = null;
53      /***
54       * Field.
55       */
56    private transient String field;
57  
58      /***
59       * Create new instance.
60       * @param field field
61       * @param pattern pattern
62       */
63    private LikeRule(final String field, final Pattern pattern) {
64      super();
65      if (!RESOLVER.isField(field)) {
66          throw new IllegalArgumentException(
67                  "Invalid LIKE rule - " + field + " is not a supported field");
68      }
69  
70      this.field = field;
71      this.pattern = pattern;
72    }
73  
74      /***
75       * Create new instance from top two elements of stack.
76       * @param stack stack
77       * @return new instance
78       */
79    public static Rule getRule(final Stack stack) {
80        if (stack.size() < 2) {
81            throw new IllegalArgumentException(
82                    "Invalid LIKE rule - expected two parameters but received "
83                            + stack.size());
84        }
85  
86        String p2 = stack.pop().toString();
87        String p1 = stack.pop().toString();
88        return getRule(p1, p2);
89    }
90  
91      /***
92       * Create new instance.
93       * @param field field
94       * @param pattern pattern
95       * @return new instance
96       */
97    public static Rule getRule(final String field, final String pattern) {
98      try {
99          return new LikeRule(field, Pattern.compile(pattern, Pattern.CASE_INSENSITIVE));
100     } catch (PatternSyntaxException e) {
101         throw new IllegalArgumentException(
102                 "Invalid LIKE rule - " + e.getMessage());
103     }
104   }
105 
106     /*** {@inheritDoc} */
107   public boolean evaluate(final LoggingEvent event) {
108     Object input = RESOLVER.getValue(field, event);
109     if((input != null) && (pattern != null)) {
110         if (matcher == null) {
111             matcher = pattern.matcher(input.toString());
112         } else {
113             matcher.reset(input.toString());
114         }
115         return matcher.matches();
116     }
117     return false;
118   }
119 
120   /***
121     * Deserialize the state of the object.
122     *
123     * @param in object input stream
124     *
125     * @throws IOException if IOException during deserialization
126     * @throws ClassNotFoundException if class not found.
127     */
128    private void readObject(final java.io.ObjectInputStream in)
129      throws IOException, ClassNotFoundException {
130          try {
131            field = (String) in.readObject();
132            String patternString = (String) in.readObject();
133            pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
134          } catch (PatternSyntaxException e) {
135              throw new IOException("Invalid LIKE rule - " + e.getMessage());
136          }
137    }
138 
139    /***
140     * Serialize the state of the object.
141     *
142     * @param out object output stream
143     *
144     * @throws IOException if IOException during serialization
145     */
146    private void writeObject(final java.io.ObjectOutputStream out)
147      throws IOException {
148      out.writeObject(field);
149      out.writeObject(pattern.pattern());
150    }
151 }