MailHandlerDemo.java revision 292
569c3090f5818228805d517e135aa3799732292aRyan Grove * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Redistribution and use in source and binary forms, with or without
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * modification, are permitted provided that the following conditions
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * - Redistributions of source code must retain the above copyright
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * notice, this list of conditions and the following disclaimer.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * - Redistributions in binary form must reproduce the above copyright
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * notice, this list of conditions and the following disclaimer in the
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * documentation and/or other materials provided with the distribution.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * - Neither the name of Oracle nor the names of its
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * contributors may be used to endorse or promote products derived
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * from this software without specific prior written permission.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Demo for the different configurations for the MailHandler.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * If the logging properties file or class is not specified then this
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * demo will apply some default settings to store emails in the users temp dir.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * @author Jason Mehrens
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static final String CLASS_NAME = MailHandlerDemo.class.getName();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * @param args the command line arguments
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove LOGGER.log(Level.FINEST, "This is the finest part of the demo.",
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.log(Level.FINER, "This is the finer part of the demo.",
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo LOGGER.log(Level.FINE, "This is the fine part of the demo.");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.log(Level.CONFIG, "Logging config file is {0}.", getConfigLocation());
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.log(Level.INFO, "Your temp directory is {0}, please wait...", getTempDir());
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove try { //waste some time for the custom formatter.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.log(Level.WARNING, "This is a warning.", new FileNotFoundException("Fake"));
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.log(Level.SEVERE, "The end of the demo.", new IOException("Fake"));
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Example for body only messages.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * On close the remaining messages are sent.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void initBodyOnly() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * Example showing that when the mail handler reaches capacity it
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * will format and send the current records. Capacity is used to roughly
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * limit the size of an outgoing message.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * On close any remaining messages are sent.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static void initLowCapacity() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * Example for body only messages.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * On close any remaining messages are sent.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static void initSimpleAttachment() {
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove h.setAttachmentFormatters(new Formatter[]{new XMLFormatter()});
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Example setup for priority messages by level.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * If the push level is triggered the message is high priority.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Otherwise, on close any remaining messages are sent.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void initWithPushLevel() {
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove * Example for priority messages by custom trigger.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove * If the push filter is triggered the message is high priority.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * Otherwise, on close any remaining messages are sent.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void initWithPushFilter() {
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * Example for circular buffer behavior. The level, push level, and
569c3090f5818228805d517e135aa3799732292aRyan Grove * capacity are set the same so that the memory handler push results
569c3090f5818228805d517e135aa3799732292aRyan Grove * in a mail handler push. All messages are high priority.
569c3090f5818228805d517e135aa3799732292aRyan Grove * On close any remaining records are discarded because they never reach
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * the mail handler.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void initPushOnly() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove MemoryHandler m = new MemoryHandler(h, capacity, pushLevel);
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * Example for circular buffer behavior as normal priority. The push level,
569c3090f5818228805d517e135aa3799732292aRyan Grove * and capacity are set the same so that the memory handler push results
569c3090f5818228805d517e135aa3799732292aRyan Grove * in a mail handler push. All messages are normal priority.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * On close any remaining records are discarded because they never reach
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * the mail handler.
569c3090f5818228805d517e135aa3799732292aRyan Grove private static void initPushNormal() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove MemoryHandler m = new MemoryHandler(h, capacity, Level.WARNING) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove public void push() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Example for various kinds of custom sorting, formatting, and filtering
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove * for multiple attachment messages.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * On close any remaining messages are sent.
569c3090f5818228805d517e135aa3799732292aRyan Grove private static void initCustomAttachments() {
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove //Sort records by level keeping the severe messages at the top.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setComparator(new LevelAndSeqComparator(true));
569c3090f5818228805d517e135aa3799732292aRyan Grove //Use subject to provide a hint as to what is in the email.
569c3090f5818228805d517e135aa3799732292aRyan Grove h.setSubject(new SummaryNameFormatter("Log containing {0} records with {1} errors"));
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove //Make the body give a simple summary of what happened.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove //Create 3 attachments.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setAttachmentFormatters(new Formatter[]{new XMLFormatter(), new XMLFormatter(), new SimpleFormatter()});
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove //filter each attachment differently.
569c3090f5818228805d517e135aa3799732292aRyan Grove h.setAttachmentFilters(new Filter[]{null, new MessageErrorsFilter(false),
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove //create simple names.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setAttachmentNames(new String[]{"all.xml", "errors.xml", "errors.txt"});
569c3090f5818228805d517e135aa3799732292aRyan Grove //extract simple name, replace the rest with formatters.
569c3090f5818228805d517e135aa3799732292aRyan Grove h.setAttachmentNames(new Formatter[]{h.getAttachmentNames()[0],
569c3090f5818228805d517e135aa3799732292aRyan Grove new SummaryNameFormatter("{0} records and {1} errors"),
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove new SummaryNameFormatter("{0,choice,0#no records|1#1 record|" +
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove "1<{0,number,integer} records} and " +
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove "{1,choice,0#no errors|1#1 error|1<" +
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove "{1,number,integer} errors}")});
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove * Sets up the demos that will run.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void init() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void closeHandlers() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void applyFallbackSettings() {
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.info("Check your user temp dir for output.");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void fallbackSettings(Handler h) {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo String file = System.getProperty("java.util.logging.config.file");
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo return System.getProperty("java.util.logging.config.class");
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove private static final class MessageErrorsFilter implements Filter {
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove private final boolean complement;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove return r.getThrown() instanceof MessagingException == complement;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Orders log records by level then sequence number.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static final class LevelAndSeqComparator
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo implements Comparator, java.io.Serializable {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static final long serialVersionUID = 6269562326337300267L;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private final boolean reverse;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove this(false);