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 equality evaluation for timestamps.
33   *
34   * @author Scott Deboy (sdeboy@apache.org)
35   */
36  public class TimestampEqualsRule extends AbstractRule {
37      /***
38       * Serialization ID.
39       */
40    static final long serialVersionUID = 1639079557187790321L;
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      /***
53       * time stamp.
54       */
55    private long timeStamp;
56  
57      /***
58       * Create new instance.
59       * @param value string representation of date.
60       */
61    private TimestampEqualsRule(final String value) {
62      super();
63      //expects value to be a timestamp value represented as a long
64      try {
65          timeStamp = DATE_FORMAT.parse(value).getTime();
66      } catch (ParseException pe) {
67          throw new IllegalArgumentException("Could not parse date: " + value);
68      }
69    }
70  
71      /***
72       * Create new instance.
73       * @param value string representation of date.
74       * @return new instance
75       */
76    public static Rule getRule(final String value) {
77        return new TimestampEqualsRule(value);
78    }
79  
80      /*** {@inheritDoc} */
81    public boolean evaluate(final LoggingEvent event, Map matches) {
82      String eventTimeStampString = RESOLVER.getValue(LoggingEventFieldResolver.TIMESTAMP_FIELD, event).toString();
83      long eventTimeStamp = Long.parseLong(eventTimeStampString) / 1000 * 1000;
84      boolean result = (eventTimeStamp == timeStamp);
85      if (result && matches != null) {
86          Set entries = (Set) matches.get(LoggingEventFieldResolver.TIMESTAMP_FIELD);
87          if (entries == null) {
88              entries = new HashSet();
89              matches.put(LoggingEventFieldResolver.TIMESTAMP_FIELD, entries);
90          }
91          entries.add(eventTimeStampString);
92      }
93      return result;
94    }
95  
96    /***
97      * Deserialize the state of the object.
98      *
99      * @param in object input stream
100     *
101     * @throws IOException if IO error during deserialization
102     * @throws ClassNotFoundException if class not found during
103    *   deserialization
104     */
105    private void readObject(final java.io.ObjectInputStream in)
106      throws IOException, ClassNotFoundException {
107      timeStamp = in.readLong();
108    }
109 
110    /***
111     * Serialize the state of the object.
112     *
113     * @param out object output stream
114     *
115     * @throws IOException if IO error during serialization
116     */
117    private void writeObject(final java.io.ObjectOutputStream out)
118      throws IOException {
119      out.writeLong(timeStamp);
120    }
121 }