| Method from org.apache.log4j.net.SMTPAppender Detail: |
public void activateOptions() {
Session session = createSession();
msg = new MimeMessage(session);
try {
addressMessage(msg);
if(subject != null) {
msg.setSubject(subject);
}
} catch(MessagingException e) {
LogLog.error("Could not activate SMTPAppender options.", e );
}
if (evaluator instanceof OptionHandler) {
((OptionHandler) evaluator).activateOptions();
}
}
Activate the specified options, such as the smtp host, the
recipient, from, etc. |
protected void addressMessage(Message msg) throws MessagingException {
if (from != null) {
msg.setFrom(getAddress(from));
} else {
msg.setFrom();
}
if (to != null && to.length() > 0) {
msg.setRecipients(Message.RecipientType.TO, parseAddress(to));
}
//Add CC receipients if defined.
if (cc != null && cc.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, parseAddress(cc));
}
//Add BCC receipients if defined.
if (bcc != null && bcc.length() > 0) {
msg.setRecipients(Message.RecipientType.BCC, parseAddress(bcc));
}
}
|
public void append(LoggingEvent event) {
if(!checkEntryConditions()) {
return;
}
event.getThreadName();
event.getNDC();
event.getMDCCopy();
if(locationInfo) {
event.getLocationInformation();
}
cb.add(event);
if(evaluator.isTriggeringEvent(event)) {
sendBuffer();
}
}
Perform SMTPAppender specific appending actions, mainly adding
the event to a cyclic buffer and checking if the event triggers
an e-mail to be sent. |
protected boolean checkEntryConditions() {
if(this.msg == null) {
errorHandler.error("Message object not configured.");
return false;
}
if(this.evaluator == null) {
errorHandler.error("No TriggeringEventEvaluator is set for appender ["+
name+"].");
return false;
}
if(this.layout == null) {
errorHandler.error("No layout set for appender named ["+name+"].");
return false;
}
return true;
}
This method determines if there is a sense in attempting to append.
It checks whether there is a set output target and also if
there is a set layout. If these checks fail, then the boolean
value false is returned. |
public synchronized void close() {
this.closed = true;
}
|
protected Session createSession() {
Properties props = null;
try {
props = new Properties (System.getProperties());
} catch(SecurityException ex) {
props = new Properties();
}
if (smtpHost != null) {
props.put("mail.smtp.host", smtpHost);
}
Authenticator auth = null;
if(smtpPassword != null && smtpUsername != null) {
props.put("mail.smtp.auth", "true");
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
};
}
Session session = Session.getInstance(props, auth);
if (smtpDebug) {
session.setDebug(smtpDebug);
}
return session;
}
|
InternetAddress getAddress(String addressStr) {
try {
return new InternetAddress(addressStr);
} catch(AddressException e) {
errorHandler.error("Could not parse address ["+addressStr+"].", e,
ErrorCode.ADDRESS_PARSE_FAILURE);
return null;
}
}
|
public String getBcc() {
return bcc;
}
Get the bcc recipient addresses. |
public int getBufferSize() {
return bufferSize;
}
Returns value of the BufferSize option. |
public String getCc() {
return cc;
}
Get the cc recipient addresses. |
public final TriggeringEventEvaluator getEvaluator() {
return evaluator;
}
Get triggering evaluator. |
public String getEvaluatorClass() {
return evaluator == null ? null : evaluator.getClass().getName();
}
Returns value of the EvaluatorClass option. |
public String getFrom() {
return from;
}
Returns value of the From option. |
public boolean getLocationInfo() {
return locationInfo;
}
Returns value of the LocationInfo option. |
public boolean getSMTPDebug() {
return smtpDebug;
}
|
public String getSMTPHost() {
return smtpHost;
}
Returns value of the SMTPHost option. |
public String getSMTPPassword() {
return smtpPassword;
}
|
public String getSMTPUsername() {
return smtpUsername;
}
|
public String getSubject() {
return subject;
}
Returns value of the Subject option. |
public String getTo() {
return to;
}
Returns value of the To option. |
InternetAddress[] parseAddress(String addressStr) {
try {
return InternetAddress.parse(addressStr, true);
} catch(AddressException e) {
errorHandler.error("Could not parse address ["+addressStr+"].", e,
ErrorCode.ADDRESS_PARSE_FAILURE);
return null;
}
}
|
public boolean parseUnrecognizedElement(Element element,
Properties props) throws Exception {
if ("triggeringPolicy".equals(element.getNodeName())) {
Object triggerPolicy =
org.apache.log4j.xml.DOMConfigurator.parseElement(
element, props, TriggeringEventEvaluator.class);
if (triggerPolicy instanceof TriggeringEventEvaluator) {
setEvaluator((TriggeringEventEvaluator) triggerPolicy);
}
return true;
}
return false;
}
|
public boolean requiresLayout() {
return true;
}
The SMTPAppender requires a layout . |
protected void sendBuffer() {
// Note: this code already owns the monitor for this
// appender. This frees us from needing to synchronize on 'cb'.
try {
MimeBodyPart part = new MimeBodyPart();
StringBuffer sbuf = new StringBuffer();
String t = layout.getHeader();
if(t != null)
sbuf.append(t);
int len = cb.length();
for(int i = 0; i < len; i++) {
//sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
LoggingEvent event = cb.get();
sbuf.append(layout.format(event));
if(layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
if (s != null) {
for(int j = 0; j < s.length; j++) {
sbuf.append(s[j]);
sbuf.append(Layout.LINE_SEP);
}
}
}
}
t = layout.getFooter();
if(t != null)
sbuf.append(t);
part.setContent(sbuf.toString(), layout.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
} catch(Exception e) {
LogLog.error("Error occured while sending e-mail notification.", e);
}
}
Send the contents of the cyclic buffer as an e-mail message. |
public void setBcc(String addresses) {
this.bcc = addresses;
}
Set the bcc recipient addresses. |
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
cb.resize(bufferSize);
}
The BufferSize option takes a positive integer
representing the maximum number of logging events to collect in a
cyclic buffer. When the BufferSize is reached,
oldest events are deleted as new events are added to the
buffer. By default the size of the cyclic buffer is 512 events. |
public void setCc(String addresses) {
this.cc = addresses;
}
Set the cc recipient addresses. |
public final void setEvaluator(TriggeringEventEvaluator trigger) {
if (trigger == null) {
throw new NullPointerException("trigger");
}
this.evaluator = trigger;
}
Sets triggering evaluator. |
public void setEvaluatorClass(String value) {
evaluator = (TriggeringEventEvaluator)
OptionConverter.instantiateByClassName(value,
TriggeringEventEvaluator.class,
evaluator);
}
The EvaluatorClass option takes a string value
representing the name of the class implementing the TriggeringEventEvaluator interface. A corresponding object will
be instantiated and assigned as the triggering event evaluator
for the SMTPAppender. |
public void setFrom(String from) {
this.from = from;
}
The From option takes a string value which should be a
e-mail address of the sender. |
public void setLocationInfo(boolean locationInfo) {
this.locationInfo = locationInfo;
}
The LocationInfo option takes a boolean value. By
default, it is set to false which means there will be no effort
to extract the location information related to the event. As a
result, the layout that formats the events as they are sent out
in an e-mail is likely to place the wrong location information
(if present in the format).
Location information extraction is comparatively very slow and
should be avoided unless performance is not a concern. |
public void setSMTPDebug(boolean debug) {
this.smtpDebug = debug;
}
Setting the SmtpDebug option to true will cause the mail session to log its server interaction to stdout.
This can be useful when debuging the appender but should not be used during production because username and
password information is included in the output. |
public void setSMTPHost(String smtpHost) {
this.smtpHost = smtpHost;
}
The SMTPHost option takes a string value which should be a
the host name of the SMTP server that will send the e-mail message. |
public void setSMTPPassword(String password) {
this.smtpPassword = password;
}
The SmtpPassword option takes a string value which should be the password required to authenticate against
the mail server. |
public void setSMTPUsername(String username) {
this.smtpUsername = username;
}
The SmtpUsername option takes a string value which should be the username required to authenticate against
the mail server. |
public void setSubject(String subject) {
this.subject = subject;
}
The Subject option takes a string value which should be a
the subject of the e-mail message. |
public void setTo(String to) {
this.to = to;
}
The To option takes a string value which should be a
comma separated list of e-mail address of the recipients. |