1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }