// ever-increasing amount of server resources.
//this.log.add(logEntry);
String status = getStatusString(statusCode);
Persister persister = new Persister(database);
try {
persister.update(logEntry);
List messages;
boolean mailAddressNotSet =((getMailHost() == null || getRecipientAddress() == null) ||
(getMailHost().length() == 0 || getRecipientAddress().length() == 0));
// Notification via email only if host and email address are present
if(!mailAddressNotSet&&!(getTransferLogNotificationLevel()==MessageLogger.MAIL_NONE)) {
String date = dateFormat.format(new Date());
String subject = this.getName() + " " + status + " " + date + " (" + database + ")";
String message = "";
// Get number of log messages at or below transferLogNotificationLevel.
int entries=persister.getLogMessageEntries(logEntry.getId(), getTransferLogNotificationLevel()).size();
//Generate mail message
if(entries > 0) {
messages = persister.getLogMessageEntries(
logEntry.getId(),
getLoggingVerbosityLevel());
for(Iterator m = messages.iterator(); m.hasNext();) {
LogMessageEntry messageEntry = (LogMessageEntry) m.next();
message += (messageEntry.getMessage()) + "\n";
}
} else {
message += MAIL_MESSAGE_NO_ENTRIES;
}
// Send notification email except when the message is
// MAIL_MESSAGE_NO_ENTRIES or the pipe has aborted and
// isAbortMailEnabled()==true
if (!message.equals(MAIL_MESSAGE_NO_ENTRIES)
|| (statusCode==LogEntry.STATUS_ABORTED && isAbortMailEnabled())) {
try {
Properties props = new Properties();
props.put("mail.host", getMailHost());
Session mailConnection = Session.getInstance(props,
null);
Message msg = new MimeMessage(mailConnection);
Address sender = new InternetAddress(MAIL_SENDER + "@"
+ getMailHost(), MAIL_SENDER);
Address[] receivers = receiverAddresses(getRecipientAddress());
// Set mail content and subject
msg.setContent(message, "text/plain");
msg.setFrom(sender);
msg.setRecipients(Message.RecipientType.TO, receivers);
msg.setSubject(subject);
// Send the mail
Transport.send(msg);
} catch (MessagingException e) {
String error = "An error occurred when sending mail report from "
+ MAIL_SENDER
+ "@"
+ getMailHost()
+ " to "
+ getRecipientAddress()
+ ":\n"
+ e.getMessage();
Environment.getInstance().log(error);
logEntry.logMessage(error, this, MessageLogger.ERROR);
persister.update(logEntry);
} catch (RuntimeException ex) {
Environment.getInstance().log(
"A RuntimeException has occurred: "
+ ex.getMessage()
+ ex.getStackTrace().toString());
}
}
}
// Remove unnecessary (debug level) messages from the LogEntry if Transfer log
// verbosity level is set to DYNAMIC and the current LogEntry's status is either
// OK or ABORTED
if(getLoggingVerbosityLevel() == MessageLogger.LOG_DYNAMIC
&& (statusCode == LogEntry.STATUS_OK || statusCode == LogEntry.STATUS_ABORTED) ) {
messages = persister.getLogMessageEntries(logEntry.getId(), MessageLogger.DEBUG);
if(messages.size() > 0) {
for(Iterator m = messages.iterator(); m.hasNext();) {
LogMessageEntry messageEntry = (LogMessageEntry) m.next();
if(messageEntry.getMessageType() == MessageLogger.DEBUG) {
persister.delete(messageEntry);
}
}
}
}
} catch(Exception e) {
Environment.getInstance().log(e.getMessage());
} finally {
persister.close();
}
}