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