1 package net.sf.logdistiller.logtypes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.*;
18 import java.text.SimpleDateFormat;
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.util.Date;
22 import java.util.Locale;
23 import java.util.regex.Pattern;
24
25 import net.sf.logdistiller.LogEvent;
26 import net.sf.logdistiller.LogType;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class OracleAlertLogEvent
54 extends LogEvent
55 implements Comparable
56 {
57 public final static String ID = "oracle-alert";
58
59 public final String timestamp;
60
61 public final Date date;
62
63 public final String message;
64
65 public final static LogType LOGTYPE = new LogType.Basic( ID );
66
67 private final static DateFormat DATE_FORMAT = new SimpleDateFormat( "EEE MMM dd HH:mm:ss yyyy", Locale.US );
68
69 private final static String[] ATTRIBUTE_NAMES =
70 { "logSource", "timestamp", "timestamp.day", "timestamp.time", "message" };
71
72 public final static LogType.Description DESCRIPTION = new Description( (LogType.Basic) LOGTYPE, ATTRIBUTE_NAMES );
73
74 public OracleAlertLogEvent( Factory factory, String firstLine, String message )
75 throws ParseException
76 {
77 super( factory, firstLine + Factory.NEWLINE + message );
78 timestamp = firstLine;
79 date = DATE_FORMAT.parse( timestamp );
80 this.message = message;
81 setAttributes( new String[] { factory.getLogSource(), timestamp,
82 timestamp.substring( 0, 10 ) + timestamp.substring( 19, 24 ), timestamp.substring( 11, 19 ), message } );
83 }
84
85 public int compareTo( Object o )
86 {
87 return date.compareTo( ( (OracleAlertLogEvent) o ).date );
88 }
89
90 private static class Description
91 extends LogType.Description
92 {
93 public Description( LogType.Basic logtype, String[] attributeNames )
94 {
95 super( logtype, attributeNames );
96 logtype.setDescription( this );
97 }
98
99 public LogEvent.Factory newFactory( Reader reader, String logSource )
100 throws IOException
101 {
102 return new Factory( this, reader, logSource );
103 }
104
105 public String getDefaultSpecificGroups()
106 {
107 return " <group id=\"ORA\">\n"
108 + " <description>ORA-* messages</description>\n"
109 + " <condition>\n"
110 + " <match attribute=\"message\" type=\"contains\">ORA-</match>\n"
111 + " </condition>\n"
112 + " <report publisher=\"file\"/>\n"
113 + " <plugin type=\"sampling\">\n"
114 + " <param name=\"attribute\">message</param>\n"
115 + " <param name=\"regexp\">(ORA-\\d+)</param>\n"
116 + " </plugin>\n"
117 + " </group>";
118 }
119 }
120
121 private static class Factory
122 extends LogEvent.Factory
123 {
124 private final LineNumberReader reader;
125
126 private String curLine;
127
128 private final static Pattern DATE_PATTERN =
129 Pattern.compile( "\\w{3} \\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2} \\d{4}" );
130
131 public Factory( Description description, Reader reader, String logSource )
132 throws FileNotFoundException
133 {
134 super( description, logSource );
135 this.reader = new LineNumberReader( reader );
136 }
137
138
139
140
141 protected boolean detectLogEventStart( String line )
142 {
143 return DATE_PATTERN.matcher( line ).matches();
144 }
145
146 protected LogEvent readNextEvent()
147 throws IOException, ParseException
148 {
149 if ( curLine == null )
150 {
151 curLine = reader.readLine();
152 if ( curLine == null )
153 {
154
155 return null;
156 }
157
158 while ( !detectLogEventStart( curLine ) )
159 {
160 curLine = reader.readLine();
161 if ( curLine == null )
162 {
163
164 return null;
165 }
166 }
167 }
168 String firstLine = curLine;
169 StringBuffer buffer = new StringBuffer();
170 while ( ( ( curLine = reader.readLine() ) != null ) && ( !detectLogEventStart( curLine ) ) )
171 {
172 if ( buffer.length() > 0 )
173 {
174 buffer.append( NEWLINE );
175 }
176 buffer.append( curLine );
177 }
178 return new OracleAlertLogEvent( this, firstLine, buffer.toString() );
179 }
180 }
181 }