MailHandlerDemo.java revision 292
569c3090f5818228805d517e135aa3799732292aRyan Grove/*
569c3090f5818228805d517e135aa3799732292aRyan Grove * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove *
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 * are met:
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove *
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * - Redistributions of source code must retain the above copyright
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * notice, this list of conditions and the following disclaimer.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove *
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 *
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 *
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 */
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Groveimport com.sun.mail.util.logging.MailHandler;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Groveimport java.util.logging.*;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Groveimport javax.mail.*;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Groveimport java.util.Comparator;
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Groveimport java.io.*;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove/**
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 */
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grovepublic class MailHandlerDemo {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static final String CLASS_NAME = MailHandlerDemo.class.getName();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * @param args the command line arguments
569c3090f5818228805d517e135aa3799732292aRyan Grove */
fe23a9535206b8c78aff2e73f8d0d0106a148a41Ryan Grove public static void main(String[] args) {
569c3090f5818228805d517e135aa3799732292aRyan Grove init(); //may create log messages.
569c3090f5818228805d517e135aa3799732292aRyan Grove try {
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove LOGGER.log(Level.FINEST, "This is the finest part of the demo.",
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove new MessagingException("Fake"));
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.log(Level.FINER, "This is the finer part of the demo.",
569c3090f5818228805d517e135aa3799732292aRyan Grove new NullPointerException("Fake"));
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
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove try { //waste some time for the custom formatter.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove Thread.sleep(3L * 1000L);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove } catch (InterruptedException ex) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove Thread.currentThread().interrupt();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
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 } finally {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove closeHandlers();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Example for body only messages.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * On close the remaining messages are sent.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove */
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void initBodyOnly() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo MailHandler h = new MailHandler();
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo h.setSubject("Body only demo");
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo LOGGER.addHandler(h);
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo }
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo /**
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 */
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static void initLowCapacity() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo MailHandler h = new MailHandler(5);
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo h.setSubject("Low capacity demo");
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo LOGGER.addHandler(h);
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo }
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo /**
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * Example for body only messages.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo * On close any remaining messages are sent.
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo */
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static void initSimpleAttachment() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo MailHandler h = new MailHandler();
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo h.setSubject("Body and attachment demo");
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove h.setAttachmentFormatters(new Formatter[]{new XMLFormatter()});
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setAttachmentNames(new String[]{"data.xml"});
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.addHandler(h);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
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.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove */
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void initWithPushLevel() {
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove MailHandler h = new MailHandler();
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove h.setSubject("Push level demo");
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove h.setPushLevel(Level.WARNING);
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.addHandler(h);
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove }
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove /**
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 */
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void initWithPushFilter() {
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove MailHandler h = new MailHandler();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setSubject("Push on MessagingException demo");
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setPushLevel(Level.ALL);
569c3090f5818228805d517e135aa3799732292aRyan Grove h.setPushFilter(new MessageErrorsFilter(true));
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.addHandler(h);
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove }
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove /**
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 */
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void initPushOnly() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove final int capacity = 3;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove final Level pushLevel = Level.WARNING;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove final MailHandler h = new MailHandler(capacity);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setPushLevel(pushLevel);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setSubject("Push only demo");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove MemoryHandler m = new MemoryHandler(h, capacity, pushLevel);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setLevel(m.getLevel());
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.addHandler(m);
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove pushOnlyHandler = h;
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove }
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove private static Handler pushOnlyHandler;
569c3090f5818228805d517e135aa3799732292aRyan Grove
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove /**
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 */
569c3090f5818228805d517e135aa3799732292aRyan Grove private static void initPushNormal() {
569c3090f5818228805d517e135aa3799732292aRyan Grove final int capacity = 3;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove final MailHandler h = new MailHandler(capacity);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setSubject("Push normal demo");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove MemoryHandler m = new MemoryHandler(h, capacity, Level.WARNING) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove public void push() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove super.push(); //push to target.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove super.flush(); //make the target send the email.
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove }
569c3090f5818228805d517e135aa3799732292aRyan Grove };
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.addHandler(m);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove pushNormalHandler = h;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static Handler pushNormalHandler;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
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 */
569c3090f5818228805d517e135aa3799732292aRyan Grove private static void initCustomAttachments() {
569c3090f5818228805d517e135aa3799732292aRyan Grove MailHandler h = new MailHandler();
569c3090f5818228805d517e135aa3799732292aRyan Grove
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove //Sort records by level keeping the severe messages at the top.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setComparator(new LevelAndSeqComparator(true));
569c3090f5818228805d517e135aa3799732292aRyan Grove
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
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove //Make the body give a simple summary of what happened.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setFormatter(new SummaryFormatter());
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove //Create 3 attachments.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setAttachmentFormatters(new Formatter[]{new XMLFormatter(), new XMLFormatter(), new SimpleFormatter()});
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove //filter each attachment differently.
569c3090f5818228805d517e135aa3799732292aRyan Grove h.setAttachmentFilters(new Filter[]{null, new MessageErrorsFilter(false),
569c3090f5818228805d517e135aa3799732292aRyan Grove new MessageErrorsFilter(true)});
569c3090f5818228805d517e135aa3799732292aRyan Grove
569c3090f5818228805d517e135aa3799732292aRyan Grove
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove //create simple names.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove h.setAttachmentNames(new String[]{"all.xml", "errors.xml", "errors.txt"});
569c3090f5818228805d517e135aa3799732292aRyan Grove
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
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove LOGGER.addHandler(h);
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove }
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove /**
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove * Sets up the demos that will run.
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove */
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove private static void init() {
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initBodyOnly();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initLowCapacity();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initSimpleAttachment();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initWithPushLevel();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initWithPushFilter();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initCustomAttachments();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initPushOnly();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove initPushNormal();
da8d2cc08af9bbf979ee87a14059ac8e0b74cee8Ryan Grove applyFallbackSettings();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void closeHandlers() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove Handler[] handlers = LOGGER.getHandlers();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove for (int i = 0; i < handlers.length; i++) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove Handler h = handlers[i];
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.close();
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LOGGER.removeHandler(h);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void applyFallbackSettings() {
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove if (getConfigLocation() == null) {
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove LOGGER.setLevel(Level.ALL);
569c3090f5818228805d517e135aa3799732292aRyan Grove LOGGER.info("Check your user temp dir for output.");
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove Handler[] handlers = LOGGER.getHandlers();
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove for (int i = 0; i < handlers.length; i++) {
569c3090f5818228805d517e135aa3799732292aRyan Grove Handler h = handlers[i];
569c3090f5818228805d517e135aa3799732292aRyan Grove fallbackSettings(h);
569c3090f5818228805d517e135aa3799732292aRyan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove fallbackSettings(pushOnlyHandler);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove fallbackSettings(pushNormalHandler);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static void fallbackSettings(Handler h) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setErrorManager(new FileErrorManager());
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove h.setLevel(Level.ALL);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private static String getTempDir() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove return System.getProperty("java.io.tmpdir");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static String getConfigLocation() {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo String file = System.getProperty("java.util.logging.config.file");
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo if (file == null) {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo return System.getProperty("java.util.logging.config.class");
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove return file;
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove }
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove private static final class MessageErrorsFilter implements Filter {
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove
722482d912ef5ee813fa1aa0d584c0ae461d4f62Ryan Grove private final boolean complement;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove MessageErrorsFilter(boolean complement) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove this.complement = complement;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove public boolean isLoggable(LogRecord r) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove return r.getThrown() instanceof MessagingException == complement;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove * Orders log records by level then sequence number.
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove */
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static final class LevelAndSeqComparator
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo implements Comparator, java.io.Serializable {
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo
60721add39f50a2f904fa5736c6d04bb7aecdb63Eric Ferraiuolo private static final long serialVersionUID = 6269562326337300267L;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove private final boolean reverse;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LevelAndSeqComparator() {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove this(false);
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove LevelAndSeqComparator(final boolean reverse) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove this.reverse = reverse;
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove }
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove
569c3090f5818228805d517e135aa3799732292aRyan Grove public int compare(Object o1, Object o2) {
569c3090f5818228805d517e135aa3799732292aRyan Grove LogRecord r1 = (LogRecord) o1;
569c3090f5818228805d517e135aa3799732292aRyan Grove LogRecord r2 = (LogRecord) o2;
569c3090f5818228805d517e135aa3799732292aRyan Grove final int first = r1.getLevel().intValue();
569c3090f5818228805d517e135aa3799732292aRyan Grove final int second = r2.getLevel().intValue();
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove if (first < second) {
return reverse ? 1 : -1;
} else if (first > second) {
return reverse ? -1 : 1;
} else {
return compareSeq(r1, r2);
}
}
private int compareSeq(LogRecord r1, LogRecord r2) {
final long first = r1.getSequenceNumber();
final long second = r2.getSequenceNumber();
if (first < second) {
return reverse ? 1 : -1;
} else if (first > second) {
return reverse ? -1 : 1;
} else {
return 0;
}
}
}
}