Issue #2937 Consolidated LogEntry to string and from string conversions in utility

methods.  Made LogEntry immutable.  Error handling updates processing LogEntries
read from the log and when processing room updates.

Amend: Create new DateFormatter for the the DetailedFeedLogPlugin purge thread.
       Added patterns and pipe constant to LogEntry.

Change-Id: I0d4963a54f5cf9fe236fef46fdd41f6c6822a0db

Former-commit-id: 19f4b1b64a [formerly fe95275a34] [formerly 056dbdbdff] [formerly 19f4b1b64a [formerly fe95275a34] [formerly 056dbdbdff] [formerly d50f39e98d [formerly 056dbdbdff [formerly 38594a026e343bcf9db7d55816ae29fa77716f05]]]]
Former-commit-id: d50f39e98d
Former-commit-id: ce28c5b32a [formerly 7472fb8084] [formerly fdd1fbec562ec709c2e1ddc6bf77a84a8df0074a [formerly 178e4fe8bb]]
Former-commit-id: 594fbd7a5cd20c9d9e2c10b81e484a4e90c1d0e0 [formerly ebcb2cb414]
Former-commit-id: 60f66cda85
This commit is contained in:
Brad Gonzales 2014-04-09 10:09:10 -05:00
parent 81522eea15
commit c091089ad7
3 changed files with 125 additions and 73 deletions

View file

@ -70,6 +70,9 @@ import com.raytheon.openfire.plugin.detailedfeedlog.listener.DetailedFeedLogEven
* in a 'Rooms' subdirectory so that readLogInfo()
* only parses room logs. Add finally block to close
* the buffered readers.
* Apr 07, 2014 2937 bgonzale Use new LogEntry toString and fromString methods
* and changed error handling to log each bad line.
* Create new DateFormatter for the purge thread.
*
* </pre>
*
@ -117,8 +120,9 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
private static final String ROOM_DIR = "Rooms";
private DateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy h:mm a");
private static final String DATE_FORMAT_STR = "MM/dd/yyyy h:mm a";
private DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_STR);
private File ROOM_LOG_DIR;
@ -172,7 +176,7 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
// flush all the current information out of log and write it to the feed
// log file
writeToFile();
writeToFile(dateFormat);
entries.clear();
// cancel the task so we aren't trying to purge when there is no plugin
@ -187,30 +191,27 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
for (File file : files) {
BufferedReader bReader = null;
try {
logger.info("Reading " + file.getName());
FileReader reader = new FileReader(file);
bReader = new BufferedReader(reader);
while (bReader.ready()) {
String line = bReader.readLine();
String[] splitLine = line.split("\\|");
String dateString = splitLine[0];
// replace the first parentheses and the second one with
// nothing, so the date can be formatted
dateString = dateString.replaceAll("\\(|\\)", "");
Date date = dateFormat.parse(dateString);
String user = splitLine[1];
String message = splitLine[3];
addToMemoryLog(date, user, message, file.getName()
.replaceAll(".log", ""));
try {
LogEntry entry = LogEntry.fromString(line, dateFormat);
addToMemoryLog(entry,
file.getName()
.replaceAll(".log", ""));
} catch (ParseException e) {
logger.error("Failed in log " + file.getName()
+ " to parse date from line: " + line, e);
} catch (ArrayIndexOutOfBoundsException e) {
logger.info("Failed in log " + file.getName()
+ " to read line:" + line, e);
}
}
} catch (FileNotFoundException e) {
logger.error("Unable to find " + file.getName(), e);
} catch (IOException e) {
logger.error("Unable to read from " + file.getName(), e);
} catch (ParseException e) {
logger.error("Unable to parse date", e);
} catch (ArrayIndexOutOfBoundsException e) {
logger.info("Unable to read " + file.getName(), e);
} finally {
if (bReader != null) {
try {
@ -244,7 +245,7 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
String site = getSiteFromPresence(user);
// format it with the site so the site can be sent with the packet later
log(user.getUsername() + "|" + site, message.getBody(), room);
log(user.getUsername(), site, message.getBody(), room);
}
/**
@ -254,23 +255,22 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
* - fqname
* @param message
*/
private static void log(String user, String message, String room) {
logger.info("Logging : " + user + message);
private static void log(String user, String site, String message,
String room) {
logger.info("Logging : " + user + " \"" + message + "\"");
Date date = new Date();
addToMemoryLog(date, user, message, room);
LogEntry entry = new LogEntry(date, site, user, message);
addToMemoryLog(entry, room);
}
/**
* Add the log message to memory
*
* @param date
* @param user
* @param message
* @param entry
* @param room
*/
private static void addToMemoryLog(Date date, String user, String message,
private static void addToMemoryLog(LogEntry entry,
String room) {
LogEntry entry = new LogEntry(date, user, message);
Queue<LogEntry> queue = null;
if (entries.containsKey(room)) {
queue = entries.get(room);
@ -314,7 +314,7 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
/**
* Write out the current in memory log to a file
*/
private void writeToFile() {
private void writeToFile(DateFormat dateFormat) {
try {
// writes out each log file for each room, all active and permanent,
// and for rooms that the purge has not removed all logs yet
@ -323,9 +323,7 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
FileWriter writer = new FileWriter(file);
for (LogEntry entry : entries.get(room)) {
writer.write("(" + dateFormat.format(entry.getDate()) + ")");
writer.write("|" + entry.getUsername() + "|");
writer.write(entry.getMessage());
writer.write(entry.toString(dateFormat));
writer.write("\n");
}
writer.close();
@ -356,6 +354,9 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
Date latestTimeToKeep = new Date(
System.currentTimeMillis()
- (purgeLogTime * SECONDS_TO_MILLIS));
// DateFormat for this thread
DateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy h:mm a");
String dateText = dateFormat.format(latestTimeToKeep);
logger.info("Purging the log of anything before "
+ dateText);
@ -374,7 +375,7 @@ public class DetailedFeedLogPlugin implements Plugin, PropertyEventListener {
}
// write to file now since we have possibly purged items
writeToFile();
writeToFile(dateFormat);
}
});
thread.run();

View file

@ -19,7 +19,10 @@
**/
package com.raytheon.openfire.plugin.detailedfeedlog;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Pattern;
/**
* Contains all the necessary information to be stored in memory for useful
@ -31,7 +34,9 @@ import java.util.Date;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 27, 2012 mnash Initial creation
* Jul 27, 2012 mnash Initial creation
* Apr 07, 2014 2937 bgonzale Changed to immutable. Use predefined patterns
* and a constant for pipe.
*
* </pre>
*
@ -41,17 +46,28 @@ import java.util.Date;
public class LogEntry {
private Date date;
private static final String PIPE = "|";
private String username;
private static final Pattern PIPE_PATTERN = Pattern.compile("\\|");
private String message;
private static final Pattern AT_PATTERN = Pattern.compile("@");
private static final Pattern DATE_PATTERN = Pattern.compile("\\(|\\)");
private final Date date;
private final String site;
private final String username;
private final String message;
/**
*
* Initialization Constructor.
*/
public LogEntry(Date date, String username, String message) {
public LogEntry(Date date, String site, String username, String message) {
this.date = date;
this.site = site;
this.username = username;
this.message = message;
}
@ -63,14 +79,6 @@ public class LogEntry {
return date;
}
/**
* @param date
* the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @return the username
*/
@ -79,11 +87,10 @@ public class LogEntry {
}
/**
* @param username
* the username to set
* @return the user without host information
*/
public void setUsername(String username) {
this.username = username;
public String getUser() {
return AT_PATTERN.split(username)[0];
}
/**
@ -94,10 +101,48 @@ public class LogEntry {
}
/**
* @param message
* the message to set
* @return the site
*/
public void setMessage(String message) {
this.message = message;
public String getSite() {
return site;
}
/**
* Parse a String and create a LineEntry.
*
* @param line
* @param dateFormat
* @return a LineEntry
* @throws ParseException
* @throws ArrayIndexOutOfBoundsException
*/
public static LogEntry fromString(String line, DateFormat dateFormat)
throws ParseException, ArrayIndexOutOfBoundsException {
String[] splitLine = PIPE_PATTERN.split(line);
String dateString = splitLine[0];
// get the string date without parenthesis
dateString = DATE_PATTERN.split(dateString)[1];
Date date = dateFormat.parse(dateString);
String username = splitLine[1];
String site = splitLine[2];
String message = splitLine[3];
return new LogEntry(date, site, username, message);
}
/**
* Generate a formatted LineEntry String that is readable by the fromString
* method.
*
* @param dateFormat
* @return formated String representing a LineEntry
*/
public String toString(DateFormat dateFormat) {
StringBuilder sb = new StringBuilder("(")
.append(dateFormat.format(date)).append(")").append(PIPE)
.append(username).append(PIPE).append(site).append(PIPE)
.append(message);
return sb.toString();
}
}

View file

@ -50,7 +50,8 @@ import com.raytheon.openfire.plugin.detailedfeedlog.LogEntry;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 25, 2012 mnash Initial creation
* Jul 25, 2012 mnash Initial creation
* Apr 07, 2014 2937 bgonzale Handle errors processing room events.
*
* </pre>
*
@ -91,28 +92,33 @@ public class DetailedFeedLogEventListener implements MUCEventListener {
Message message = new Message();
message.setTo(roomJID + "/" + nickname);
String usr = entry.getUsername();
String[] info = usr.split("\\|");
User sendUser = null;
try {
sendUser = userManager
.getUser(info[0].split("@")[0]);
User sendUser = userManager
.getUser(entry.getUser());
// set all the necessary values in the message to be
// sent out
message.setTo(user);
message.setFrom(roomJID + "/" + nickname);
String body = HISTORY_START
+ entry.getDate().getTime() + "|"
+ sendUser + "|" + entry.getSite() + "|"
+ entry.getMessage();
message.setBody(body);
message.setType(Type.groupchat);
router.route(message);
logger.info("Routed message : " + message.getBody()
+ " from " + sendUser + " to " + user);
} catch (UserNotFoundException e) {
logger.error(usr + " not found", e);
} catch (ArrayIndexOutOfBoundsException e) {
logger.error(
"Failed to parse log history for Routing. Bad Entry was date: "
+ entry.getDate() + " username: "
+ entry.getUsername()
+ " message: " + entry.getMessage(),
e);
}
String site = info[1];
// set all the necessary values in the message to be
// sent out
message.setTo(user);
message.setFrom(roomJID + "/" + nickname);
String body = HISTORY_START + entry.getDate().getTime()
+ "|" + sendUser + "|" + site + "|"
+ entry.getMessage();
message.setBody(body);
message.setType(Type.groupchat);
router.route(message);
logger.info("Routed message : " + message.getBody()
+ " from " + sendUser + " to " + user);
}
}
};