Methods Summary |
---|
private void | appendFileName(javax.mail.Part part, java.lang.String chunk)Constructs a file name from a formatter.
It is assumed that file names are short (less than 32 chars) and that in
most cases getTail will be the only method that will produce a result.
if (chunk != null) {
if (chunk.length() > 0) {
try {
final String old = part.getFileName();
part.setFileName(old != null ? old.concat(chunk) : chunk);
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
} else {
reportError("null", new NullPointerException(), ErrorManager.FORMAT_FAILURE);
}
|
private void | appendSubject(Message msg, java.lang.String chunk)Constructs a subject line from a formatter.
It is assumed that subject lines are short (less than 32 chars) and that
in most cases getTail will be the only method that will produce a result.
if (chunk != null) {
if (chunk.length() > 0) {
try {
final String old = msg.getSubject();
msg.setSubject(old != null ? old.concat(chunk) : chunk);
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
} else {
reportError("null", new NullPointerException(), ErrorManager.FORMAT_FAILURE);
}
|
private static java.lang.String | atIndexMsg(int i)
return "At index: " + i + '.";
|
private static java.lang.RuntimeException | attachmentMismatch(java.lang.String msg)
return new IndexOutOfBoundsException(msg);
|
private static java.lang.RuntimeException | attachmentMismatch(int expected, int found)
return attachmentMismatch("Attachments mismatched, expected " +
expected + " but given " + found + '.");
|
final void | checkAccess()
if (sealed) {
LogManagerProperties.manager.checkAccess();
}
|
public synchronized void | close()Prevents any other records from being published.
Pushes any buffered records to the email server as normal priority.
The internal buffer is then cleared. Once this handler is closed it
will remain closed.
super.setLevel(Level.OFF); //security check first.
push(ErrorManager.CLOSE_FAILURE, false);
/**
* The sign bit of the capacity is set to ensure that records that
* have passed isLoggable, but have yet to be added to the internal
* buffer, are immediately pushed as an email.
*/
if (this.capacity > 0) {
this.capacity = -this.capacity;
if (this.data.isEmpty()) { //ensure not inside a push.
this.data = newData(1);
}
}
assert this.capacity < 0;
|
private java.lang.String | contentTypeOf(java.lang.String head)Determines the mimeType of a formatter from the getHead call.
This could be made protected, or a new class could be created to do
this type of conversion. Currently, this is only used for the body.
if (head != null) {
final Locale locale = Locale.ENGLISH;
if (head.trim().toUpperCase(locale).indexOf("<HTML") > -1) {
return "text/html";
} else if (head.trim().toUpperCase(locale).indexOf("<XML") > -1) {
return "text/xml";
}
}
return null; //text/plain
|
private static java.lang.Object[] | copyOf(java.lang.Object[] a, int size)Copies the given array. Can be removed when Java Mail requires Java 1.6.
Object[] copy = (Object[]) Array.newInstance(a.getClass().getComponentType(), size);
System.arraycopy(a, 0, copy, 0, Math.min(a.length, size));
return copy;
|
private javax.mail.BodyPart | createBodyPart()
final MimeBodyPart part = new MimeBodyPart();
part.setDisposition(Part.INLINE);
part.setDescription(descriptionFrom(getFormatter(), getFilter()));
return part;
|
private javax.mail.BodyPart | createBodyPart(int index)
assert Thread.holdsLock(this);
final MimeBodyPart part = new MimeBodyPart();
part.setDisposition(Part.ATTACHMENT);
part.setDescription(descriptionFrom(attachmentFormatters[index], attachmentFilters[index]));
return part;
|
private Message | createMessage()Creates the Message object. Keep private.
assert Thread.holdsLock(this);
final Properties proxyProps = new LogManagerProperties(mailProps, getClass().getName());
final Session session = Session.getInstance(proxyProps, auth);
final MimeMessage msg = new MimeMessage(session);
setFrom(msg, proxyProps);
setRecipient(msg, proxyProps, "mail.to", Message.RecipientType.TO);
setRecipient(msg, proxyProps, "mail.cc", Message.RecipientType.CC);
setRecipient(msg, proxyProps, "mail.bcc", Message.RecipientType.BCC);
setReplyTo(msg, proxyProps);
setSender(msg, proxyProps);
setMailer(msg);
return msg;
|
private java.lang.String | descriptionFrom(java.util.logging.Formatter formatter, java.util.logging.Filter filter)
return "Formatted using " + formatter.getClass().getName() +
" and filtered with " + (filter == null ? "no filter"
: filter.getClass().getName()) + '.";
|
private boolean | fixUpAttachmentFilters()Expand or shrink the attachment filters.
assert Thread.holdsLock(this);
final int expect = this.attachmentFormatters.length;
final int current = this.attachmentFilters.length;
if (current != expect) {
this.attachmentFilters = (Filter[]) copyOf(attachmentFilters, expect);
return current != 0;
}
return false;
|
private boolean | fixUpAttachmentNames()Expand or shrink the attachment name formatters.
assert Thread.holdsLock(this);
final int expect = this.attachmentFormatters.length;
final int current = this.attachmentNames.length;
if (current != expect) {
this.attachmentNames = (Formatter[]) copyOf(attachmentNames, expect);
for (int i = 0; i < expect; i++) {
if (this.attachmentNames[i] == null) {
//use String.valueOf to ensure non null string.
this.attachmentNames[i] = new TailNameFormatter(
String.valueOf(this.attachmentFormatters[i]));
}
}
return current != 0;
}
return false;
|
public void | flush()Pushes any buffered records to the email server as normal priority.
The internal buffer is then cleared. Does nothing if called from inside
a push.
push(ErrorManager.FLUSH_FAILURE, false);
|
private java.lang.String | format(java.util.logging.Formatter f, java.util.logging.LogRecord r)
try {
return f.format(r);
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
return "";
}
|
public final java.util.logging.Filter[] | getAttachmentFilters()Gets the attachment filters. If the attachment filter does not
allow any LogRecord to be formatted, the attachment may
be omitted from the email.
return (Filter[]) readOnlyAttachmentFilters().clone();
|
public final java.util.logging.Formatter[] | getAttachmentFormatters()Gets the attachment formatters. This Handler is using
attachments only if the returned array length is non zero.
Formatter[] formatters;
synchronized (this) {
formatters = this.attachmentFormatters;
}
return (Formatter[]) formatters.clone();
|
public final java.util.logging.Formatter[] | getAttachmentNames()Gets the attachment name formatters.
If the attachment names were set using explicit names then
the names can be returned by calling toString on each
attachment name formatter.
final Formatter[] formatters;
synchronized (this) {
formatters = this.attachmentNames;
}
return (Formatter[]) formatters.clone();
|
public final synchronized javax.mail.Authenticator | getAuthenticator()Gets the Authenticator used to login to the email server.
checkAccess();
return this.auth;
|
public final synchronized int | getCapacity()Gets the number of log records the internal buffer can hold. When
capacity is reached, Handler will format all LogRecord
objects into one email message.
assert capacity != Integer.MIN_VALUE;
return Math.abs(capacity);
|
public final synchronized java.util.Comparator | getComparator()Gets the comparator used to order all LogRecord objects prior
to formatting. If null then the order is unspecified.
return this.comparator;
|
public final java.util.Properties | getMailProperties()Gets a copy of the mail properties used for the session.
checkAccess();
final Properties props;
synchronized (this) {
props = this.mailProps;
}
return (Properties) props.clone();
|
public final synchronized java.util.logging.Filter | getPushFilter()Gets the push filter. The default is null.
return this.pushFilter;
|
public final synchronized java.util.logging.Level | getPushLevel()Gets the push level. The default is Level.OFF meaning that
this Handler will only push when the internal buffer is full.
return this.pushLevel;
|
public final synchronized java.util.logging.Formatter | getSubject()Gets the formatter used to create the subject line.
If the subject was created using a literal string then
the toString method can be used to get the subject line.
return this.subjectFormatter;
|
private java.lang.String | head(java.util.logging.Formatter f)
try {
return f.getHead(this);
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
return "";
}
|
private synchronized void | init()
final LogManager manager = LogManagerProperties.manager;
final String p = getClass().getName();
this.mailProps = new Properties();
//Assign any custom error manager first so it can detect all failures.
ErrorManager em = (ErrorManager) initObject(p.concat(".errorManager"), ErrorManager.class);
if (em != null) {
setErrorManager(em);
}
try {
final String val = manager.getProperty(p.concat(".level"));
if (val != null) {
super.setLevel(Level.parse(val));
} else {
super.setLevel(Level.WARNING);
}
} catch (final SecurityException SE) {
throw SE;
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
try {
super.setLevel(Level.WARNING);
} catch (RuntimeException fail) {
reportError(fail.getMessage(), fail, ErrorManager.OPEN_FAILURE);
}
}
try {
super.setFilter((Filter) initObject(p.concat(".filter"), Filter.class));
} catch (final SecurityException SE) {
throw SE;
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}
final int DEFAULT_CAPACITY = 1000;
try {
final String value = manager.getProperty(p.concat(".capacity"));
if (value != null) {
this.setCapacity(Integer.parseInt(value));
} else {
this.setCapacity(DEFAULT_CAPACITY);
}
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}
if (capacity == 0) { //ensure non zero
capacity = DEFAULT_CAPACITY;
}
this.data = newData(10);
this.auth = (Authenticator) initObject(p.concat(".authenticator"), Authenticator.class);
try {
super.setEncoding(manager.getProperty(p.concat(".encoding")));
} catch (final SecurityException SE) {
throw SE;
} catch (final UnsupportedEncodingException UEE) {
reportError(UEE.getMessage(), UEE, ErrorManager.OPEN_FAILURE);
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}
try {
final Formatter formatter = (Formatter) initObject(p.concat(".formatter"), Formatter.class);
if (formatter != null) {
super.setFormatter(formatter);
} else {
super.setFormatter(new SimpleFormatter());
}
} catch (final SecurityException SE) {
throw SE;
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
try {
super.setFormatter(new SimpleFormatter());
} catch (RuntimeException fail) {
reportError(fail.getMessage(), fail, ErrorManager.OPEN_FAILURE);
}
}
try {
this.comparator = initComparator(p.concat(".comparator"));
} catch (final Exception RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}
/*try {
final String reverse = manager.getProperty(p.concat(".comparator.reverse"));
if (reverse != null) {
if (Boolean.parseBoolean(reverse)) {
if (this.comparator != null) {
this.comparator = Collections.reverseOrder(this.comparator);
}
else {
throw new IllegalArgumentException("No comparator to reverse.");
}
}
}
}
catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}*/
try {
final String val = manager.getProperty(p.concat(".pushLevel"));
if (val != null) {
this.pushLevel = Level.parse(val);
}
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.OPEN_FAILURE);
}
if (this.pushLevel == null) {
this.pushLevel = Level.OFF;
}
this.pushFilter = (Filter) initObject(p.concat(".pushFilter"), Filter.class);
this.subjectFormatter = (Formatter) initObject(p.concat(".subject"), Formatter.class);
if (this.subjectFormatter == null) {
this.subjectFormatter = new TailNameFormatter("");
}
this.attachmentFormatters = (Formatter[]) initArray(p.concat(".attachment.formatters"), Formatter.class);
this.attachmentFilters = (Filter[]) initArray(p.concat(".attachment.filters"), Filter.class);
this.attachmentNames = (Formatter[]) initArray(p.concat(".attachment.names"), Formatter.class);
final int attachments = attachmentFormatters.length;
for (int i = 0; i < attachments; i++) {
if (attachmentFormatters[i] == null) {
final NullPointerException NPE = new NullPointerException(atIndexMsg(i));
attachmentFormatters[i] = new SimpleFormatter();
reportError("attachment formatter.", NPE, ErrorManager.OPEN_FAILURE);
} else if (attachmentFormatters[i] instanceof TailNameFormatter) {
final ClassNotFoundException CNFE =
new ClassNotFoundException(attachmentFormatters[i].toString());
attachmentFormatters[i] = new SimpleFormatter();
reportError("attachment formatter.", CNFE, ErrorManager.OPEN_FAILURE);
}
}
if (fixUpAttachmentFilters()) {
reportError("attachment filters.",
attachmentMismatch("length mismatch"), ErrorManager.OPEN_FAILURE);
}
if (fixUpAttachmentNames()) {
reportError("attachment names.",
attachmentMismatch("length mismatch"), ErrorManager.OPEN_FAILURE);
}
|
private java.lang.Object[] | initArray(java.lang.String key, java.lang.Class type)
final String list = LogManagerProperties.manager.getProperty(key);
if (list != null && list.length() > 0) {
final String[] names = list.split(",");
Object[] a = (Object[]) Array.newInstance(type, names.length);
for (int i = 0; i < a.length; i++) {
names[i] = names[i].trim();
if (!"null".equalsIgnoreCase(names[i])) {
try {
a[i] = objectFromNew(names[i], type);
} catch (NoSuchMethodException E) {
reportError(E.getMessage(), E, ErrorManager.OPEN_FAILURE);
}
}
}
return a;
} else {
return /*(T[])*/ (Object[]) Array.newInstance(type, 0);
}
|
private java.util.Comparator | initComparator(java.lang.String key)
return (Comparator) this.initObject(key, Comparator.class);
|
private java.lang.Object | initObject(java.lang.String key, java.lang.Class type)
String name = LogManagerProperties.manager.getProperty(key);
if (name != null && name.length() > 0 && !"null".equalsIgnoreCase(name)) {
try {
return objectFromNew(name, type);
} catch (NoSuchMethodException E) {
reportError(E.getMessage(), E, ErrorManager.OPEN_FAILURE);
}
}
return null;
|
private boolean | isAttachmentLoggable(java.util.logging.LogRecord record)Check if any attachment would actually format the given
LogRecord. Because this method is private, it avoids checking
the handler level for OFF.
final Filter[] filters = readOnlyAttachmentFilters();
for (int i = 0; i < filters.length; i++) {
final Filter f = filters[i];
if (f == null || f.isLoggable(record)) {
return true;
}
}
return false;
|
public boolean | isLoggable(java.util.logging.LogRecord record)Check if this Handler would actually log a given
LogRecord into its internal buffer.
This method checks if the LogRecord has an appropriate level and
whether it satisfies any Filter including any attachment filters.
However it does not check whether the LogRecord would
result in a "push" of the buffer contents.
int levelValue = getLevel().intValue();
if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
return false;
}
Filter filter = getFilter();
if (filter == null || filter.isLoggable(record)) {
return true;
}
return isAttachmentLoggable(record);
|
private boolean | isPushable(java.util.logging.LogRecord record)Check if this Handler would push after storing the
LogRecord into its internal buffer.
assert Thread.holdsLock(this);
final int value = getPushLevel().intValue();
if (value == offValue || record.getLevel().intValue() < value) {
return false;
}
final Filter filter = getPushFilter();
return filter == null || filter.isLoggable(record);
|
private java.util.Collection | newData(int initialCapacity)
return new ArrayList/*<LogRecord>*/(initialCapacity);
|
private java.lang.Object | objectFromNew(java.lang.String name, java.lang.Class type)
Object obj = null;
try {
try {
try {
Class clazz = LogManagerProperties.findClass(name);
return clazz.getConstructor((Class[]) null).newInstance((Object[]) null);
} catch (final NoClassDefFoundError NCDFE) {
throw (ClassNotFoundException) new ClassNotFoundException(NCDFE.getMessage()).initCause(NCDFE);
}
} catch (final ClassNotFoundException CNFE) {
if (type == Formatter.class) {
return /*type.cast(*/ new TailNameFormatter(name);
} else {
throw CNFE;
}
}
} catch (final NoSuchMethodException NSME) {
throw NSME; //avoid catch all.
} catch (final Exception E) {
reportError(E.getMessage(), E, ErrorManager.OPEN_FAILURE);
}
return obj;
|
public void | publish(java.util.logging.LogRecord record)Stores a LogRecord in the internal buffer.
The isLoggable method is called to check if the given log record
is loggable. If the given record is loggable, it is copied into
an internal buffer. Then the record's level property is compared with
the push level. If the given level of the LogRecord
is greater than or equal to the push level then the push filter is
called. If no push filter exists, the push filter returns true,
or the capacity of the internal buffer has been reached then all buffered
records are formatted into one email and sent to the server.
/**
* It is possible for the handler to be closed after the
* call to isLoggable. In that case, the current thread
* will push to ensure that all published records are sent.
* See close().
*/
if (isLoggable(record)) {
record.getSourceMethodName(); //infer caller
synchronized (this) {
data.add(record);
final boolean priority = isPushable(record);
if (priority || data.size() >= capacity) {
push(ErrorManager.WRITE_FAILURE, priority);
}
}
}
|
private void | push(int code, boolean priority)Performs the push using the given parameters.
If the push fails then the raw email is written to the ErrorManager.
Message msg = null;
try {
msg = writeLogRecords(priority);
if (msg != null) {
Transport.send(msg);
}
} catch (final Exception E) {
try { //use super call so we do not prefix raw email.
super.reportError(toRawString(msg), E, code);
} catch (final MessagingException rawMe) {
reportError(rawMe.toString(), E, code); //report original cause.
} catch (final IOException rawIo) {
reportError(rawIo.toString(), E, code); //report original cause.
}
}
|
public void | push()Pushes any buffered records to the email server as high priority.
The internal buffer is then cleared. Does nothing if called from inside
a push.
push(ErrorManager.FLUSH_FAILURE, true);
|
private synchronized java.util.logging.Filter[] | readOnlyAttachmentFilters()Gets the attachment filters under a lock. The attachment filters
are treated as copy-on-write, so the returned array must never be
modified or published outside this class.
return this.attachmentFilters;
|
protected void | reportError(java.lang.String msg, java.lang.Exception ex, int code)Protected convenience method to report an error to this Handler's
ErrorManager. This method will prefix all non null error messages with
Level.SEVERE.getName(). This allows the receiving error
manager to determine if the msg parameter is a simple error
message or a raw email message.
if (msg != null) {
super.reportError(Level.SEVERE.getName() + ": " + msg, ex, code);
} else {
super.reportError(null, ex, code);
}
|
public final void | setAttachmentFilters(java.util.logging.Filter[] filters)Sets the attachment filters.
checkAccess();
filters = (Filter[]) filters.clone();
synchronized (this) {
if (this.attachmentFormatters.length != filters.length) {
throw attachmentMismatch(this.attachmentFormatters.length, filters.length);
}
if (isWriting) {
throw new IllegalStateException();
}
this.attachmentFilters = filters;
}
|
public final void | setAttachmentFormatters(java.util.logging.Formatter[] formatters)Sets the attachment Formatter object for this handler.
The number of formatters determines the number of attachments per
email. This method should be the first attachment method called.
To remove all attachments, call this method with empty array.
checkAccess();
formatters = (Formatter[]) formatters.clone();
for (int i = 0; i < formatters.length; i++) {
if (formatters[i] == null) {
throw new NullPointerException(atIndexMsg(i));
}
}
synchronized (this) {
if (isWriting) {
throw new IllegalStateException();
}
this.attachmentFormatters = formatters;
this.fixUpAttachmentFilters();
this.fixUpAttachmentNames();
}
|
public final void | setAttachmentNames(java.lang.String[] names)Sets the attachment file name for each attachment. This method will
create a set of custom formatters.
checkAccess();
Formatter[] formatters = new Formatter[names.length];
for (int i = 0; i < names.length; i++) {
final String name = names[i];
if (name != null) {
if (name.length() > 0) {
formatters[i] = new TailNameFormatter(name);
} else {
throw new IllegalArgumentException(atIndexMsg(i));
}
} else {
throw new NullPointerException(atIndexMsg(i));
}
}
synchronized (this) {
if (this.attachmentFormatters.length != names.length) {
throw attachmentMismatch(this.attachmentFormatters.length, names.length);
}
if (isWriting) {
throw new IllegalStateException();
}
this.attachmentNames = formatters;
}
|
public final void | setAttachmentNames(java.util.logging.Formatter[] formatters)Sets the attachment file name formatters. The format method of each
attachment formatter will see only the LogRecord objects that
passed its attachment filter during formatting. The format method should
always return the empty string. Instead of being used to format records,
it is used to gather information about the contents of an attachment.
The getTail method should be used to construct the attachment
file name and reset any formatter collected state.
checkAccess();
formatters = (Formatter[]) formatters.clone();
for (int i = 0; i < formatters.length; i++) {
if (formatters[i] == null) {
throw new NullPointerException(atIndexMsg(i));
}
}
synchronized (this) {
if (this.attachmentFormatters.length != formatters.length) {
throw attachmentMismatch(this.attachmentFormatters.length, formatters.length);
}
if (isWriting) {
throw new IllegalStateException();
}
this.attachmentNames = formatters;
}
|
public final synchronized void | setAuthenticator(javax.mail.Authenticator auth)Gets the Authenticator used to login to the email server.
checkAccess();
if (isWriting) {
throw new IllegalStateException();
}
this.auth = auth;
|
private final synchronized void | setCapacity(int newCapacity)Sets the capacity for this handler. This method is kept private
because we would have to define a public policy for when the size is
greater than the capacity.
I.E. do nothing, flush now, truncate now, push now and resize.
if (newCapacity <= 0) {
throw new IllegalArgumentException("Capacity must be greater than zero.");
}
if (isWriting) {
throw new IllegalStateException();
}
if (this.capacity < 0) { //if closed, remain closed.
this.capacity = -newCapacity;
} else {
this.capacity = newCapacity;
}
|
public final synchronized void | setComparator(java.util.Comparator c)Sets the comparator used to order all LogRecord objects prior
to formatting. If null then the order is unspecified.
checkAccess();
if (isWriting) {
throw new IllegalStateException();
}
this.comparator = c;
|
private void | setContent(javax.mail.Part part, java.lang.StringBuffer buf, java.lang.String type)Set the content for a part.
if (type != null && !"text/plain".equals(type)) {
try {
DataSource source = new ByteArrayDataSource(buf.toString(), type);
part.setDataHandler(new DataHandler(source));
} catch (final IOException IOE) {
reportError(IOE.getMessage(), IOE, ErrorManager.FORMAT_FAILURE);
part.setText(buf.toString());
}
} else {
part.setText(buf.toString());
}
|
private void | setDefaultFrom(Message msg)
try {
msg.setFrom();
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
|
private void | setFrom(Message msg, java.util.Properties props)
final String from = props.getProperty("mail.from");
if (from != null && from.length() > 0) {
try {
final InternetAddress[] address = InternetAddress.parse(from, false);
if (address == null || address.length == 0) {
setDefaultFrom(msg);
} else {
if (address.length == 1) {
msg.setFrom(address[0]);
} else { //greater than 1 address.
msg.addFrom(address);
}
}
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
setDefaultFrom(msg);
}
} else {
setDefaultFrom(msg);
}
|
public synchronized void | setLevel(java.util.logging.Level newLevel)Set the log level specifying which message levels will be
logged by this Handler. Message levels lower than this
value will be discarded.
if (this.capacity > 0) {
super.setLevel(newLevel);
} else { //don't allow a closed handler to be opened (half way).
if (newLevel == null) {
throw new NullPointerException();
}
checkAccess();
}
|
public final void | setMailProperties(java.util.Properties props)Sets the mail properties used for the session. The key/value pairs
are defined in the Java Mail API documentation. This
Handler will also search the LogManager for defaults
if needed.
checkAccess();
props = (Properties) props.clone();
synchronized (this) {
if (isWriting) {
throw new IllegalStateException();
}
this.mailProps = props;
}
|
private void | setMailer(Message msg)
try {
final Class mail = MailHandler.class;
final Class k = getClass();
final String value;
if (k == mail) {
value = mail.getName();
} else {
value = mail.getName() + " using the " + k.getName() + " extension.";
}
msg.setHeader("X-Mailer", value);
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
|
private void | setPriority(Message msg, boolean priority)
if (priority) {
try {
msg.setHeader("X-Priority", "2"); //High
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
|
public final synchronized void | setPushFilter(java.util.logging.Filter filter)Sets the push filter. This filter is only called if the given
LogRecord level was greater than the push level. If this
filter returns true, all pending records are formatted and sent
to the email server. When the push filter triggers a send, the resulting
email is flagged as high priority.
checkAccess();
if (isWriting) {
throw new IllegalStateException();
}
this.pushFilter = filter;
|
public final synchronized void | setPushLevel(java.util.logging.Level level)Sets the push level. This level is used to trigger a push so that
all pending records are formatted and sent to the email server. When
the push level triggers a send, the resulting email is flagged as
high priority.
checkAccess();
if (level == null) {
throw new NullPointerException();
}
if (isWriting) {
throw new IllegalStateException();
}
this.pushLevel = level;
|
private void | setRecipient(Message msg, java.util.Properties props, java.lang.String key, javax.mail.Message$RecipientType type)
final String value = props.getProperty(key);
if (value != null && value.length() > 0) {
try {
final InternetAddress[] address = InternetAddress.parse(value, false);
if (address != null && address.length > 0) {
msg.setRecipients(type, address);
}
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
|
private void | setReplyTo(Message msg, java.util.Properties props)
final String reply = props.getProperty("mail.reply.to");
if (reply != null && reply.length() > 0) {
try {
final InternetAddress[] address = InternetAddress.parse(reply, false);
if (address != null && address.length > 0) {
msg.setReplyTo(address);
}
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
|
private void | setSender(javax.mail.internet.MimeMessage msg, java.util.Properties props)
final String sender = props.getProperty("mail.sender");
if (sender != null && sender.length() > 0) {
try {
final InternetAddress[] address = InternetAddress.parse(sender, false);
if (address != null && address.length > 0) {
msg.setSender(address[0]);
if (address.length > 1) {
reportError("Ignoring other senders.",
new AddressException(Arrays.asList(address).subList(1, address.length).toString()), ErrorManager.FORMAT_FAILURE);
}
}
} catch (final MessagingException ME) {
reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
}
}
|
public final void | setSubject(java.lang.String subject)Sets a literal string for the email subject.
if (subject != null) {
this.setSubject(new TailNameFormatter(subject));
} else {
throw new NullPointerException();
}
|
public final synchronized void | setSubject(java.util.logging.Formatter format)Sets the subject formatter for email. The format method of the subject
formatter will see all LogRecord objects that were published to
this Handler during formatting and should always return the empty
string. This formatter is used to gather information to create a summary
about what information is contained in the email. The getTail
method should be used to construct the subject and reset any
formatter collected state. The toString method of the given
formatter should be overridden to provide a useful subject, if possible.
checkAccess();
if (format == null) {
throw new NullPointerException();
}
if (isWriting) {
throw new IllegalStateException();
}
this.subjectFormatter = format;
|
private java.util.Collection | sortAsReadOnlyData()
Collection/*<LogRecord>*/ records;
if (comparator != null) {
LogRecord[] a = (LogRecord[]) data.toArray(new LogRecord[data.size()]);
try {
Arrays.sort(a, comparator);
records = Arrays.asList(a);
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
records = data;
}
} else {
records = data;
}
return records;
|
private java.lang.String | tail(java.util.logging.Formatter f, java.lang.String def)
try {
return f.getTail(this);
} catch (final RuntimeException RE) {
reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
return def;
}
|
private java.lang.String | toRawString(Message msg)Converts an email message to a raw string. This raw string
is passed to the error manager to allow custom error managers
to recreate the original MimeMessage object.
if (msg != null) {
final int size = Math.max(msg.getSize() + 1024, 1024);
final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
msg.writeTo(out);
return out.toString("US-ASCII");
} else { //Must match this.reportError behavior, see push method.
return null; //null is the safe choice.
}
|
private synchronized Message | writeLogRecords(boolean priority)
if (data.isEmpty()) {
return null;
}
if (isWriting) {
return null;
}
final Message msg;
isWriting = true;
try {
msg = createMessage();
setPriority(msg, priority);
Collection/*<LogRecord>*/ records = sortAsReadOnlyData();
/**
* Parts are lazily created when an attachment performs a getHead
* call. Therefore, a null part at an index means that the head is
* required.
*/
BodyPart[] parts = new BodyPart[attachmentFormatters.length];
/**
* The buffers are lazily created when the part requires a getHead.
*/
StringBuffer[] buffers = new StringBuffer[parts.length];
String contentType = null;
StringBuffer buf = null;
appendSubject(msg, head(subjectFormatter));
final Formatter bodyFormat = getFormatter();
final Filter bodyFilter = getFilter();
for (Iterator it = records.iterator(); it.hasNext();) {
LogRecord r = (LogRecord) it.next();
appendSubject(msg, format(subjectFormatter, r));
if (bodyFilter == null || bodyFilter.isLoggable(r)) {
if (buf == null) {
buf = new StringBuffer();
final String head = head(bodyFormat);
buf.append(head);
contentType = contentTypeOf(head);
}
buf.append(format(bodyFormat, r));
}
for (int i = 0; i < parts.length; i++) {
if (attachmentFilters[i] == null ||
attachmentFilters[i].isLoggable(r)) {
if (parts[i] == null) {
parts[i] = createBodyPart(i);
buffers[i] = new StringBuffer();
buffers[i].append(head(attachmentFormatters[i]));
appendFileName(parts[i], head(attachmentNames[i]));
}
appendFileName(parts[i], format(attachmentNames[i], r));
buffers[i].append(format(attachmentFormatters[i], r));
}
}
}
for (int i = parts.length - 1; i >= 0; i--) {
if (parts[i] != null) {
appendFileName(parts[i], tail(attachmentNames[i], "err"));
buffers[i].append(tail(attachmentFormatters[i], ""));
final String content = buffers[i].toString();
if (content.length() > 0) {
String name = parts[i].getFileName();
if (name == null || name.length() == 0) {
parts[i].setFileName(attachmentFormatters[i].toString());
}
parts[i].setText(content);
} else {
parts[i] = null;
}
buffers[i] = null;
}
}
if (buf != null) {
buf.append(tail(bodyFormat, ""));
} else {
buf = new StringBuffer(0);
}
appendSubject(msg, tail(subjectFormatter, ""));
records = null;
data.clear();
if (parts.length > 0) {
Multipart multipart = new MimeMultipart();
if (buf.length() > 0) {
BodyPart body = createBodyPart();
setContent(body, buf, contentType);
multipart.addBodyPart(body);
}
buf = null;
for (int i = 0; i < parts.length; i++) {
if (parts[i] != null) {
multipart.addBodyPart(parts[i]);
}
}
parts = null;
msg.setContent(multipart);
} else {
setContent(msg, buf, contentType);
buf = null;
}
} finally {
isWriting = false;
if (!data.isEmpty()) {
data.clear();
}
}
msg.setSentDate(new java.util.Date());
msg.saveChanges();
return msg;
|