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  
25  import org.apache.log4j.spi.LoggingEvent;
26  import org.apache.log4j.spi.LoggingEventFieldResolver;
27  
28  /***
29   * A Rule class implementing equality evaluation for timestamps.
30   *
31   * @author Scott Deboy (sdeboy@apache.org)
32   */
33  public class TimestampEqualsRule extends AbstractRule {
34      /***
35       * Serialization ID.
36       */
37    static final long serialVersionUID = 1639079557187790321L;
38      /***
39       * Resolver.
40       */
41    private static final LoggingEventFieldResolver RESOLVER =
42            LoggingEventFieldResolver.getInstance();
43      /***
44       * Date format.
45       */
46    private static final DateFormat DATE_FORMAT =
47            new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
48  
49      /***
50       * time stamp.
51       */
52    private long timeStamp;
53  
54      /***
55       * Create new instance.
56       * @param value string representation of date.
57       */
58    private TimestampEqualsRule(final String value) {
59      super();
60      //expects value to be a timestamp value represented as a long
61      try {
62          timeStamp = DATE_FORMAT.parse(value).getTime();
63      } catch (ParseException pe) {
64          throw new IllegalArgumentException("Could not parse date: " + value);
65      }
66    }
67  
68      /***
69       * Create new instance.
70       * @param value string representation of date.
71       * @return new instance
72       */
73    public static Rule getRule(final String value) {
74        return new TimestampEqualsRule(value);
75    }
76  
77      /*** {@inheritDoc} */
78    public boolean evaluate(final LoggingEvent event) {
79      long eventTimeStamp = Long.parseLong(
80              RESOLVER.getValue("TIMESTAMP", event).toString()) / 1000 * 1000;
81      return eventTimeStamp == timeStamp;
82    }
83  
84    /***
85      * Deserialize the state of the object.
86      *
87      * @param in object input stream
88      *
89      * @throws IOException if IO error during deserialization
90      * @throws ClassNotFoundException if class not found during
91     *   deserialization
92      */
93     private void readObject(final java.io.ObjectInputStream in)
94       throws IOException, ClassNotFoundException {
95       timeStamp = in.readLong();
96     }
97  
98     /***
99      * Serialize the state of the object.
100     *
101     * @param out object output stream
102     *
103     * @throws IOException if IO error during serialization
104     */
105    private void writeObject(final java.io.ObjectOutputStream out)
106      throws IOException {
107      out.writeLong(timeStamp);
108    }
109 }