MailHandlerDemo.java revision 295
/*
* Copyright (c) 2009-2010 Jason Mehrens. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Demo for the different configurations for the MailHandler.
* If the logging properties file or class is not specified then this
* demo will apply some default settings to store emails in the users temp dir.
* @author Jason Mehrens
*/
public class MailHandlerDemo {
/**
* @param args the command line arguments
*/
init(); //may create log messages.
try {
new MessagingException("Fake"));
new NullPointerException("Fake"));
try { //waste some time for the custom formatter.
} catch (InterruptedException ex) {
}
} finally {
}
}
/**
* Used debug problems with the logging.properties.
* @param prefix a string to prefix the output.
* @param err any PrintStream or null for System.out.
*/
prefix = "DEBUG";
}
}
try {
} else {
}
//force any errors, only safe is key is present.
} else {
}
} else {
}
}
}
/**
* Example for body only messages.
* On close the remaining messages are sent.
*/
private static void initBodyOnly() {
MailHandler h = new MailHandler();
h.setSubject("Body only demo");
LOGGER.addHandler(h);
}
/**
* Example showing that when the mail handler reaches capacity it
* will format and send the current records. Capacity is used to roughly
* limit the size of an outgoing message.
* On close any remaining messages are sent.
*/
private static void initLowCapacity() {
h.setSubject("Low capacity demo");
LOGGER.addHandler(h);
}
/**
* Example for body only messages.
* On close any remaining messages are sent.
*/
private static void initSimpleAttachment() {
MailHandler h = new MailHandler();
h.setSubject("Body and attachment demo");
LOGGER.addHandler(h);
}
/**
* Example setup for priority messages by level.
* If the push level is triggered the message is high priority.
* Otherwise, on close any remaining messages are sent.
*/
private static void initWithPushLevel() {
MailHandler h = new MailHandler();
h.setSubject("Push level demo");
LOGGER.addHandler(h);
}
/**
* Example for priority messages by custom trigger.
* If the push filter is triggered the message is high priority.
* Otherwise, on close any remaining messages are sent.
*/
private static void initWithPushFilter() {
MailHandler h = new MailHandler();
h.setSubject("Push on MessagingException demo");
h.setPushFilter(new MessageErrorsFilter(true));
LOGGER.addHandler(h);
}
/**
* Example for circular buffer behavior. The level, push level, and
* capacity are set the same so that the memory handler push results
* in a mail handler push. All messages are high priority.
* On close any remaining records are discarded because they never reach
* the mail handler.
*/
private static void initPushOnly() {
final int capacity = 3;
h.setSubject("Push only demo");
LOGGER.addHandler(m);
pushOnlyHandler = h;
}
private static Handler pushOnlyHandler;
/**
* Example for circular buffer behavior as normal priority. The push level,
* and capacity are set the same so that the memory handler push results
* in a mail handler push. All messages are normal priority.
* On close any remaining records are discarded because they never reach
* the mail handler.
*/
private static void initPushNormal() {
final int capacity = 3;
h.setSubject("Push normal demo");
public void push() {
super.push(); //push to target.
super.flush(); //make the target send the email.
}
};
LOGGER.addHandler(m);
pushNormalHandler = h;
}
private static Handler pushNormalHandler;
/**
* Example for various kinds of custom sorting, formatting, and filtering
* for multiple attachment messages.
* On close any remaining messages are sent.
*/
private static void initCustomAttachments() {
MailHandler h = new MailHandler();
//Sort records by level keeping the severe messages at the top.
h.setComparator(new LevelAndSeqComparator(true));
//Use subject to provide a hint as to what is in the email.
//Make the body give a simple summary of what happened.
h.setFormatter(new SummaryFormatter());
//Create 3 attachments.
h.setAttachmentFormatters(new Formatter[]{new XMLFormatter(), new XMLFormatter(), new SimpleFormatter()});
//filter each attachment differently.
new MessageErrorsFilter(true)});
//create simple names.
//extract simple name, replace the rest with formatters.
new SummaryNameFormatter("{0} records and {1} errors"),
new SummaryNameFormatter("{0,choice,0#no records|1#1 record|"
+ "1<{0,number,integer} records} and "
+ "{1,choice,0#no errors|1#1 error|1<"
+ "{1,number,integer} errors}")});
LOGGER.addHandler(h);
}
/**
* Sets up the demos that will run.
*/
private static void init() {
}
initBodyOnly();
initPushOnly();
}
private static void closeHandlers() {
h.close();
LOGGER.removeHandler(h);
}
}
private static void applyFallbackSettings() {
if (getConfigLocation() == null) {
fallbackSettings(h);
}
}
}
private static void fallbackSettings(Handler h) {
if (h != null) {
h.setErrorManager(new FileErrorManager());
}
}
private static String getTempDir() {
}
private static String getConfigLocation() {
}
return file;
}
private static final class MessageErrorsFilter implements Filter {
private final boolean complement;
MessageErrorsFilter(final boolean complement) {
this.complement = complement;
}
public boolean isLoggable(LogRecord r) {
}
}
/**
* Orders log records by level then sequence number.
*/
private static final class LevelAndSeqComparator
private static final long serialVersionUID = 6269562326337300267L;
private final boolean reverse;
this(false);
}
LevelAndSeqComparator(final boolean reverse) {
}
} else {
}
}
} else {
return 0;
}
}
}
}