-
Notifications
You must be signed in to change notification settings - Fork 30
/
LogFileProcessor.java
618 lines (527 loc) · 23.5 KB
/
LogFileProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.authentication.test.common.logging.client;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.SequenceInputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author Raja Perumal
*/
/**
* LogFileProcessor does the following operations
*
* 1) Fetches log records from authentication-trace-log.xml
*
* 2) Checks for the existance of search string in the log for example to verify whether server log contains a string
* "Java EE rocks" use the following code
*
* LogFileProcessor logProcessor = new LogFileProcessor(properties); boolean contains =
* logProcessor.verifyLogContains("Java EE rocks");
*
* where "properties" contains the following key value pair 1) log.file.location
*
* 3) Prints the collection of log records.
*
*/
public class LogFileProcessor {
private String logFileLocation;
private Collection recordCollection;
private Collection appIdRecordCollection;
private Collection appSpecificRecordCollection;
private boolean client;
public LogFileProcessor() {
this(false);
}
public LogFileProcessor(boolean client) {
this.client = client;
boolean pass = true;
try {
logFileLocation = System.getProperty("log.file.location");
if (logFileLocation == null)
pass = false;
TestUtil.logMsg("log.file.location = " + logFileLocation);
if (!pass) {
TestUtil.logErr("Setup Failed ");
TestUtil.logErr("Please verify \"log.file.location\"");
}
TestUtil.logMsg("Setup ok");
} catch (Exception e) {
TestUtil.logErr("Setup Failed ");
TestUtil.logErr("Please verify \"log.file.location\"");
}
}
/**
* FetchLogs pull logs from the server.
*
*/
public void fetchLogs() {
File logfile = null;
URL url = null;
try {
String fileName = "authentication-trace-log.xml";
if (client) {
fileName = "client-authentication-trace-log.xml";
}
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
String strFilePathAndName = "";
if (logFileLocation.indexOf(fileName) <= 0) {
strFilePathAndName = logFileLocation + "/" + fileName;
}
System.out.println("Log File = " + strFilePathAndName);
if (strFilePathAndName != null)
logfile = new File(strFilePathAndName);
if (!logfile.exists()) {
String header = "Log file does not exists";
// ostream.writeObject(header);
System.out.println("Log File : " + logfile.getName() + " does not exists");
System.out.println("Check permissions for log file ");
System.out.println("See User guide for Configuring log file permissions");
} else {
// LogRecords will be added to authentication-trace-log.xml as long as the server is
// up and running. Since authentication-trace-log.xml is continuously updated with
// more record there will not be any end tag </log> at the end of the
// log file.
//
// This will cause the SAXParser to throw fatal error message
// "XML Document structures must start and end with the
// same entity"
//
// In order to avoid this error message the FileInputStream
// should have the end tag </log>, this can be achieved by
// creating a SequenceInputStream which includes a
// FileInputStream and a ByteArrayInputStream, where the
// ByteArrayInputStream contains the bytes for </log>
//
String endLogTag = "</log>";
ByteArrayInputStream bais = new ByteArrayInputStream(endLogTag.getBytes());
SequenceInputStream sis = new SequenceInputStream(new FileInputStream(strFilePathAndName), bais);
Document document = documentBuilder.parse(sis);
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes();
String queryString = "pullAllRecords";
String queryParams = "fullLog";
if (queryString.equals("pullAllRecords")) {
recordCollection = pullAllLogRecords(queryParams, nodes);
// printCollection(recordCollection);
}
}
} catch (Exception e) {
TestUtil.logErr(e.getMessage());
TestUtil.printStackTrace(e);
e.printStackTrace();
}
}
/**
* Fetches all JSR196 SPI logs from authentication-trace-log.xml
*/
public static Collection pullAllLogRecords(String queryParams, NodeList nodes) throws Exception {
Node childNode;
Node recordNode;
NodeList recordNodeChildren;
Collection recordCollection = new Vector();
for (int i = 0; i < nodes.getLength(); i++) {
// Take the first record
recordNode = nodes.item(i);
if (recordNode.getNodeName().equals("record")) {
LogRecordEntry recordEntry = new LogRecordEntry(recordNode);
recordCollection.add(recordEntry);
}
}
return recordCollection;
}
public void setAppIdRecordCollection(Collection recordCollection) {
this.appIdRecordCollection = recordCollection;
}
public Collection getAppIdRecordCollection() {
return this.appIdRecordCollection;
}
public void setRecordCollection(Collection recordCollection) {
this.recordCollection = recordCollection;
}
public Collection getRecordCollection() {
return this.recordCollection;
}
public void setAppSpecificRecordCollection(Collection recordCollection) {
this.appSpecificRecordCollection = recordCollection;
}
public Collection getAppSpecificRecordCollection() {
return this.appSpecificRecordCollection;
}
/**
* Checks for the existance of search string in the log. For example to verify whether server log contains a string
* "Java EE rocks" use the following code
*
* LogProcessor logProcessor = new LogProcessor(properties); boolean contains = logProcessor.verifyLogContains("Java EE
* rocks");
*
* where "properties" contains the key value pair for 1) log.file.location
*/
public boolean verifyLogContains(String... args) {
return verifyLogContains(false, args);
}
/**
* Checks for the existance of search string in the log. For example to verify whether server log contains a string
* "Java EE rocks" use the following code
*
* LogProcessor logProcessor = new LogProcessor(properties); boolean contains = logProcessor.verifyLogContains("Java EE
* rocks");
*
* where "properties" contains the key value pair for 1) log.file.location
*/
public boolean verifyLogContains(boolean validateOrder, String... args) {
LogRecordEntry recordEntry = null;
TestUtil.logMsg("Searching log records for record :" + args[0]);
if (recordCollection == null) {
TestUtil.logMsg("Record collection empty : No log records found");
return false;
}
TestUtil.logMsg("Record collection has: " + recordCollection.size() + " records.");
int numberOfArgs = args.length;
int numberOfMatches = 0;
boolean argsMatchIndex[] = new boolean[args.length];
for (int i = 0; i < args.length; i++) {
// initialize all argsMatchIndex to "false" (i.e no match)
argsMatchIndex[i] = false;
// From the given string array(args) if there is a record match
// for the search string, then the corresponding argsMatchIndex[i]
// will be set to true(to indicate a match)
// i.e argsMatchIndex[i] = true;
//
// For example if the string array contains
// String args[]={"JK", "EMERSON", "J.B.Shaw};
//
// And if the string "JK" and "J.B.Shaw" are found in the records
// then the argsMatchIndex will be set as shown below
// argsMatchIndex[] ={true, false, true};
//
}
Iterator iterator = recordCollection.iterator();
if (!validateOrder) {
// we dont care about order, just that all args were found
while (iterator.hasNext()) {
// loop thru all message tag/entries in the log file
recordEntry = (LogRecordEntry) iterator.next();
String message = recordEntry.getMessage();
// loop through all arguments to search for a match
for (int i = 0; i < numberOfArgs; i++) {
// Search only unique record matches ignore repeat occurances
if (argsMatchIndex[i] != true) {
// see if one of the search argument matches with
// the logfile message entry and if so return true
if ((message != null) && message.equals(args[i])) {
TestUtil.logMsg("Matching Record :");
TestUtil.logMsg(recordEntry.getMessage());
// Increment match count
numberOfMatches++;
// Mark the matches in argsMatchIndex
argsMatchIndex[i] = true;
continue;
}
}
}
// Return true if, we found matches for all strings
// in the given string array
if (numberOfMatches == numberOfArgs)
return true;
}
// if here, our non-order-specific search did not find all matches...
// Print unmatched Strings(i.e no matches were found for these strings)
TestUtil.logMsg("No Matching log Record(s) found for the following String(s) :");
for (int i = 0; i < numberOfArgs; i++) {
if (argsMatchIndex[i] == false) {
TestUtil.logMsg(args[i]);
}
}
} else {
// we care about order, so find args and make sure they are in
// same order as the arg[] array
int recordCnt = 0;
int argCount = 0;
// first find how last occurance of our FIRST ITEM. Why? because
// we want to be sure that if we are searching for "a", "b", "c" within
// the authentication-trace-log.xml, we better find the very last and most recent entry
// for "a". For example, if logfile contain "a", "b", "c", "a", "c" due
// to multiple runs, we will erroneously pass a search for "a", "b", "c"
// UNLESS we are careful to find the last "a", then do our check.
int occurances = 0;
while (iterator.hasNext()) {
recordCnt++;
recordEntry = (LogRecordEntry) iterator.next();
String message = recordEntry.getMessage();
if ((message != null) && message.equals(args[argCount])) {
occurances++;
TestUtil.logMsg("verifyLogContains() found " + occurances + "occurances");
}
}
// reset the iterator
iterator = recordCollection.iterator();
while (iterator.hasNext()) {
recordCnt++;
recordEntry = (LogRecordEntry) iterator.next();
String message = recordEntry.getMessage();
if ((message != null) && (message.equals(args[0])) && (occurances > 1)) {
// found a match but not the LAST occurance of it in authentication-trace-log.xml
// so keep looking
occurances--;
continue;
}
if ((message != null) && message.equals(args[argCount])) {
// we found last occurance of first item in args[]
TestUtil.logMsg("Found: " + args[argCount] + " in record entry: " + recordCnt);
argCount++;
if (argCount >= args.length) {
// we are done, found all our matches in order
return true;
}
}
}
// if here, our order-specific-search did not succeed.
TestUtil.logMsg("Failed to find order-specific-records search in verifyLogContains()");
TestUtil.logMsg(" Could not find: " + args[argCount]);
}
return false;
}
/**
* Checks for the existance of one of the search string(from a given String array.
*
* For example to verify whether server log contains one of the following String String[] arr ={"aaa", "bbb", "ccc"};
*
* LogProcessor logProcessor = new LogProcessor(properties); boolean contains =
* logProcessor.verifyLogContainsOneOf(arr);
*
* This method will return true if the log file contains one of the specified String (say "aaa" )
*
* where "properties" contains the key value pair for 1) log.file.location
*/
public boolean verifyLogContainsOneOf(String args[]) {
LogRecordEntry recordEntry = null;
boolean result = false;
TestUtil.logMsg("Searching log records for the presence of one of the String" + " from a given string array");
if (recordCollection == null) {
TestUtil.logMsg("Record collection empty : No log records found");
return false;
} else {
TestUtil.logMsg("Record collection has: " + recordCollection.size() + " records.");
}
int numberOfArgs = args.length;
Iterator iterator = recordCollection.iterator();
searchLabel: while (iterator.hasNext()) {
// loop thru all message tag/entries in the log file
recordEntry = (LogRecordEntry) iterator.next();
String message = recordEntry.getMessage();
// loop through all arguments to search for a match
for (int i = 0; i < numberOfArgs; i++) {
// see if one of the search argument matches with
// the logfile message entry and if so return true
if ((message != null) && message.equals(args[i])) {
TestUtil.logMsg("Matching Record :");
TestUtil.logMsg(recordEntry.getMessage());
result = true;
// If a match is found no need to search further
break searchLabel;
}
}
}
if (!result) {
// Print unmatched Strings(i.e no matches were found for these strings)
TestUtil.logMsg("No Matching log Record(s) found for the following String(s) :");
for (int i = 0; i < numberOfArgs; i++) {
TestUtil.logMsg(args[i]);
}
}
return result;
}
/**
* This method looks for the presence of the given substring (from the array of strings "args") in the serverlog, which
* starts with the given "srchStrPrefix" search-string-prefix.
*
*
* For example to verify whether server log contains one of the following Strings in a server log with appContextId as
* the message prefix we can issue the following command
*
* String[] arr ={"aaa", "bbb", "ccc"}; String srchStrPrefix ="appContextId";
*
* LogProcessor logProcessor = new LogProcessor(properties); boolean contains =
* logProcessor.verifyLogContainsOneOf(arr);
*
* "appContextId= xxxx aaa yyyyyyyyyyyyyyyyy" "appContextId= yyyy bbb xxxxxxxxxxxxxxxxx"
*
* This method will return true if the log file contains one of the specified String (say "aaa" ) in the message log
* with "appContextId" as its message prefix.
*
* where "properties" contains the key value pair for 1) log.file.location
*/
public boolean verifyLogContainsOneOfSubString(String args[], String srchStrPrefix) {
LogRecordEntry recordEntry = null;
boolean result = false;
TestUtil.logMsg("Searching log records for the presence of one of the String" + " from a given string array");
if (recordCollection == null) {
TestUtil.logMsg("Record collection empty : No log records found");
return false;
} else {
TestUtil.logMsg("Record collection has: " + recordCollection.size() + " records.");
}
int numberOfArgs = args.length;
Iterator iterator = recordCollection.iterator();
searchLabel: while (iterator.hasNext()) {
// loop thru all message tag/entries in the log file
recordEntry = (LogRecordEntry) iterator.next();
String message = recordEntry.getMessage();
// loop through all arguments to search for a match
for (int i = 0; i < numberOfArgs; i++) {
// see if one of the search argument matches with
// the logfile message entry and if so return true
if ((message != null) && (message.startsWith(srchStrPrefix, 0)) && (message.indexOf(args[i]) > 0)) {
TestUtil.logMsg("Matching Record :");
TestUtil.logMsg(recordEntry.getMessage());
result = true;
// If a match is found no need to search further
break searchLabel;
}
}
}
if (!result) {
// Print unmatched Strings(i.e no matches were found for these strings)
TestUtil.logMsg("No Matching log Record(s) found for the following String(s) :");
for (int i = 0; i < numberOfArgs; i++) {
TestUtil.logMsg(args[i]);
}
}
return result;
}
public void printCollection(Collection recordCollection) {
LogRecordEntry recordEntry = null;
Iterator iterator = recordCollection.iterator();
while (iterator.hasNext()) {
recordEntry = (LogRecordEntry) iterator.next();
printRecordEntry(recordEntry);
}
}
public void printRecordEntry(LogRecordEntry rec) {
TestUtil.logMsg("*******Log Content*******");
TestUtil.logMsg("Milli Seconds =" + rec.getMilliSeconds());
TestUtil.logMsg("Seqence no =" + rec.getSequenceNumber());
TestUtil.logMsg("Message =" + rec.getMessage());
if (rec.getClassName() != null)
TestUtil.logMsg("Class name =" + rec.getClassName());
if (rec.getMethodName() != null)
TestUtil.logMsg("Method name =" + rec.getMethodName());
if (rec.getLevel() != null)
TestUtil.logMsg("Level =" + rec.getLevel());
if (rec.getThrown() != null)
TestUtil.logMsg("Thrown =" + rec.getThrown());
TestUtil.logMsg("");
}
public String extractQueryToken(String str, String ContextId) {
StringTokenizer strtok;
String DELIMETER = "|";
String qstring = null;
String qparams = null;
strtok = new StringTokenizer(ContextId, DELIMETER);
if (ContextId.indexOf(DELIMETER) > 0) {
qstring = strtok.nextToken();
if (strtok.hasMoreTokens())
qparams = strtok.nextToken();
}
// return query string or query params based on the content
// of the string str
if (str.equals("LogQueryString"))
return qstring;
else
return qparams;
}
// This method tokenize the given string and
// return first token and the remaining
// string a string array based on the given delimeter
public static String[] getTokens(String str, String delimeter) {
String[] array = new String[2];
StringTokenizer strtoken;
// Get first token and the remaining string
strtoken = new StringTokenizer(str, delimeter);
if (str.indexOf(delimeter) > 0) {
array[0] = strtoken.nextToken();
array[1] = str.substring(array[0].length() + 3, str.length());
}
// TestUtil.logMsg("Input String ="+str);
// TestUtil.logMsg("array[0] ="+array[0]);
// TestUtil.logMsg("array[1] ="+array[1]);
return array;
}
//
// Locates the logs based on the given prefix string
//
// For example to locate all commit records i.e records such as
//
// commit :: MyApp1058312446320 , recordTimeStamp=1058312446598
//
// Use the following method to pull all the commit records
//
// fingLogsByPrefix("commit", nodes);
public Collection findLogsByPrefix(String queryParams, NodeList nodes) throws Exception {
Collection recordCollection = new Vector();
String nodeName;
String nodeValue;
Node childNode;
Node recordNode;
NodeList recordNodeChildren;
for (int i = 0; i < nodes.getLength(); i++) {
// Take the first record
recordNode = nodes.item(i);
// get all the child nodes for the first record
recordNodeChildren = recordNode.getChildNodes();
for (int j = 0; j < recordNodeChildren.getLength(); j++) {
childNode = recordNodeChildren.item(j);
nodeName = childNode.getNodeName();
if (nodeName.equals("message")) {
nodeValue = getText(childNode);
if (nodeValue.startsWith(queryParams)) {
// create a new record entry and
// add it to the collection
LogRecordEntry recordEntry = new LogRecordEntry(recordNode);
recordCollection.add(recordEntry);
}
}
}
}
return recordCollection;
}
public String getText(Node textNode) {
String result = "";
NodeList nodes = textNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.TEXT_NODE) {
result = node.getNodeValue();
break;
}
}
return result;
}
}