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 java.io.IOException;
21  import java.text.DateFormat;
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.log4j.spi.LoggingEvent;
29  import org.apache.log4j.spi.LoggingEventFieldResolver;
30  
31  /***
32   * A Rule class implementing inequality evaluation for timestamps.
33   *
34   * @author Scott Deboy (sdeboy@apache.org)
35   */
36  public class TimestampInequalityRule extends AbstractRule {
37      /***
38       * Serialization ID.
39       */
40    static final long serialVersionUID = -4642641663914789241L;
41      /***
42       * Resolver.
43       */
44    private static final LoggingEventFieldResolver RESOLVER =
45              LoggingEventFieldResolver.getInstance();
46      /***
47       * Date format.
48       */
49    private static final DateFormat DATE_FORMAT =
50            new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
51      /***
52       * Inequality symbol.
53       */
54    private transient String inequalitySymbol;
55      /***
56       * Timestamp.
57       */
58    private long timeStamp;
59  
60      /***
61       * Create new instance.
62       * @param inequalitySymbol inequality symbol.
63       * @param value string representation of date.
64       */
65    private TimestampInequalityRule(
66      final String inequalitySymbol, final String value) {
67      super();
68      this.inequalitySymbol = inequalitySymbol;
69      try {
70          timeStamp = DATE_FORMAT.parse(value).getTime();
71      } catch (ParseException pe) {
72          throw new IllegalArgumentException("Could not parse date: " + value);
73      }
74    }
75  
76      /***
77       * Create new instance.
78       * @param inequalitySymbol inequality symbol
79       * @param value string representation of date
80       * @return new instance
81       */
82    public static Rule getRule(final String inequalitySymbol,
83                               final String value) {
84        return new TimestampInequalityRule(inequalitySymbol, value);
85    }
86  
87      /*** {@inheritDoc} */
88    public boolean evaluate(final LoggingEvent event, Map matches) {
89      String eventTimeStampString = RESOLVER.getValue(LoggingEventFieldResolver.TIMESTAMP_FIELD, event).toString();
90      long eventTimeStamp = Long.parseLong(
91                  eventTimeStampString) / 1000 * 1000;
92      boolean result = false;
93      long first = eventTimeStamp;
94      long second = timeStamp;
95  
96      if ("<".equals(inequalitySymbol)) {
97        result = first < second;
98      } else if (">".equals(inequalitySymbol)) {
99        result = first > second;
100     } else if ("<=".equals(inequalitySymbol)) {
101       result = first <= second;
102     } else if (">=".equals(inequalitySymbol)) {
103       result = first >= second;
104     }
105     if (result && matches != null) {
106         Set entries = (Set) matches.get(LoggingEventFieldResolver.TIMESTAMP_FIELD);
107         if (entries == null) {
108             entries = new HashSet();
109             matches.put(LoggingEventFieldResolver.TIMESTAMP_FIELD, entries);
110         }
111         entries.add(eventTimeStampString);
112     }
113     return result;
114   }
115 
116   /***
117     * Deserialize the state of the object.
118     *
119     * @param in object input stream
120     *
121     * @throws IOException if IO error during deserialization
122     * @throws ClassNotFoundException if class not found
123     */
124    private void readObject(final java.io.ObjectInputStream in)
125      throws IOException, ClassNotFoundException {
126      inequalitySymbol = (String) in.readObject();
127      timeStamp = in.readLong();
128    }
129 
130    /***
131     * Serialize the state of the object.
132     *
133     * @param out object output stream
134     *
135     * @throws IOException if IO error during serialization
136     */
137    private void writeObject(final java.io.ObjectOutputStream out)
138      throws IOException {
139      out.writeObject(inequalitySymbol);
140      out.writeLong(timeStamp);
141    }
142 }