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 inequality evaluation for timestamps.
30   *
31   * @author Scott Deboy (sdeboy@apache.org)
32   */
33  public class TimestampInequalityRule extends AbstractRule {
34      /***
35       * Serialization ID.
36       */
37    static final long serialVersionUID = -4642641663914789241L;
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       * Inequality symbol.
50       */
51    private transient String inequalitySymbol;
52      /***
53       * Timestamp.
54       */
55    private long timeStamp;
56  
57      /***
58       * Create new instance.
59       * @param inequalitySymbol inequality symbol.
60       * @param value string representation of date.
61       */
62    private TimestampInequalityRule(
63      final String inequalitySymbol, final String value) {
64      super();
65      this.inequalitySymbol = inequalitySymbol;
66      try {
67          timeStamp = DATE_FORMAT.parse(value).getTime();
68      } catch (ParseException pe) {
69          throw new IllegalArgumentException("Could not parse date: " + value);
70      }
71    }
72  
73      /***
74       * Create new instance.
75       * @param inequalitySymbol inequality symbol
76       * @param value string representation of date
77       * @return new instance
78       */
79    public static Rule getRule(final String inequalitySymbol,
80                               final String value) {
81        return new TimestampInequalityRule(inequalitySymbol, value);
82    }
83  
84      /*** {@inheritDoc} */
85    public boolean evaluate(final LoggingEvent event) {
86      long eventTimeStamp = Long.parseLong(
87              RESOLVER.getValue("TIMESTAMP", event).toString()) / 1000 * 1000;
88      boolean result = false;
89      long first = eventTimeStamp;
90      long second = timeStamp;
91  
92      if ("<".equals(inequalitySymbol)) {
93        result = first < second;
94      } else if (">".equals(inequalitySymbol)) {
95        result = first > second;
96      } else if ("<=".equals(inequalitySymbol)) {
97        result = first <= second;
98      } else if (">=".equals(inequalitySymbol)) {
99        result = first >= second;
100     }
101 
102     return result;
103   }
104 
105   /***
106     * Deserialize the state of the object.
107     *
108     * @param in object input stream
109     *
110     * @throws IOException if IO error during deserialization
111     * @throws ClassNotFoundException if class not found
112     */
113    private void readObject(final java.io.ObjectInputStream in)
114      throws IOException, ClassNotFoundException {
115      inequalitySymbol = (String) in.readObject();
116      timeStamp = in.readLong();
117    }
118 
119    /***
120     * Serialize the state of the object.
121     *
122     * @param out object output stream
123     *
124     * @throws IOException if IO error during serialization
125     */
126    private void writeObject(final java.io.ObjectOutputStream out)
127      throws IOException {
128      out.writeObject(inequalitySymbol);
129      out.writeLong(timeStamp);
130    }
131 }