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.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.Stack;
27  
28  
29  /***
30   * A Rule class implementing a not null (and not empty string) check.
31   *
32   * @author Scott Deboy (sdeboy@apache.org)
33   */
34  public class ExistsRule extends AbstractRule {
35      /***
36       * Serialization id.
37       */
38    static final long serialVersionUID = -5386265224649967464L;
39      /***
40       * field resolver.
41       */
42    private static final LoggingEventFieldResolver RESOLVER =
43      LoggingEventFieldResolver.getInstance();
44      /***
45       * field name.
46       */
47    private final String field;
48  
49      /***
50       * Create new instance.
51       * @param fld field name.
52       */
53    private ExistsRule(final String fld) {
54      super();
55      if (!RESOLVER.isField(fld)) {
56        throw new IllegalArgumentException(
57          "Invalid EXISTS rule - " + fld + " is not a supported field");
58      }
59  
60      this.field = fld;
61    }
62  
63      /***
64       * Get an instance of ExistsRule.
65       * @param field field.
66       * @return instance of ExistsRule.
67       */
68    public static Rule getRule(final String field) {
69      return new ExistsRule(field);
70    }
71  
72      /***
73       * Create an instance of ExistsRule using the
74       * top name on the stack.
75       * @param stack stack
76       * @return instance of ExistsRule.
77       */
78    public static Rule getRule(final Stack stack) {
79      if (stack.size() < 1) {
80        throw new IllegalArgumentException(
81          "Invalid EXISTS rule - expected one parameter but received "
82          + stack.size());
83      }
84  
85      return new ExistsRule(stack.pop().toString());
86    }
87  
88      /***
89       * {@inheritDoc}
90       */
91    public boolean evaluate(final LoggingEvent event, Map matches) {
92      Object p2 = RESOLVER.getValue(field, event);
93  
94      boolean result = !((p2 == null) || (p2.toString().equals("")));
95      if (result && matches != null) {
96          Set entries = (Set) matches.get(field.toUpperCase());
97          if (entries == null) {
98              entries = new HashSet();
99              matches.put(field.toUpperCase(), entries);
100         }
101         entries.add(p2);
102     }
103     return result;
104   }
105 }