168N/A/*
823N/A * Copyright (c) 2009-2016 Oracle and/or its affiliates. All rights reserved.
823N/A * Copyright (c) 2009-2016 Jason Mehrens. All Rights Reserved.
168N/A *
168N/A * Redistribution and use in source and binary forms, with or without
168N/A * modification, are permitted provided that the following conditions
168N/A * are met:
168N/A *
168N/A * - Redistributions of source code must retain the above copyright
168N/A * notice, this list of conditions and the following disclaimer.
168N/A *
168N/A * - Redistributions in binary form must reproduce the above copyright
168N/A * notice, this list of conditions and the following disclaimer in the
168N/A * documentation and/or other materials provided with the distribution.
168N/A *
292N/A * - Neither the name of Oracle nor the names of its
168N/A * contributors may be used to endorse or promote products derived
168N/A * from this software without specific prior written permission.
168N/A *
168N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
168N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
168N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
168N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
168N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
168N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
168N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
168N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
168N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
168N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
168N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168N/A */
168N/A
670N/Aimport com.sun.mail.util.logging.CollectorFormatter;
769N/Aimport com.sun.mail.util.logging.DurationFilter;
168N/Aimport com.sun.mail.util.logging.MailHandler;
670N/Aimport com.sun.mail.util.logging.SeverityComparator;
526N/Aimport java.io.File;
526N/Aimport java.io.FileNotFoundException;
526N/Aimport java.io.IOException;
526N/Aimport java.io.PrintStream;
670N/Aimport java.lang.management.ManagementFactory;
670N/Aimport java.util.*;
526N/Aimport java.util.logging.*;
670N/Aimport javax.activation.DataHandler;
526N/Aimport javax.mail.MessagingException;
526N/Aimport javax.mail.Session;
526N/Aimport javax.mail.internet.InternetAddress;
168N/A
168N/A/**
670N/A * Demo for the different configurations for the MailHandler. If the logging
670N/A * properties file or class is not specified then this demo will apply some
833N/A * default settings to store emails in the user's temp directory.
670N/A *
168N/A * @author Jason Mehrens
168N/A */
168N/Apublic class MailHandlerDemo {
168N/A
613N/A /**
613N/A * This class name.
613N/A */
168N/A private static final String CLASS_NAME = MailHandlerDemo.class.getName();
613N/A /**
613N/A * The logger for this class name.
613N/A */
168N/A private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);
168N/A
168N/A /**
823N/A * Runs the demo.
823N/A *
168N/A * @param args the command line arguments
823N/A * @throws IOException if there is a problem.
168N/A */
823N/A public static void main(String[] args) throws IOException {
670N/A List<String> l = Arrays.asList(args);
670N/A if (l.contains("/?") || l.contains("-?") || l.contains("-help")) {
670N/A LOGGER.info("Usage: java MailHandlerDemo "
823N/A + "[[-all] | [-body] | [-custom] | [-debug] | [-low] "
823N/A + "| [-simple] | [-pushlevel] | [-pushfilter] "
823N/A + "| [-pushnormal] | [-pushonly]] "
670N/A + "\n\n"
670N/A + "-all\t\t: Execute all demos.\n"
670N/A + "-body\t\t: An email with all records and only a body.\n"
823N/A + "-custom\t\t: An email with attachments and dynamic names.\n"
670N/A + "-debug\t\t: Output basic debug information about the JVM "
670N/A + "and log configuration.\n"
670N/A + "-low\t\t: Generates multiple emails due to low capacity."
670N/A + "\n"
670N/A + "-simple\t\t: An email with all records with body and "
670N/A + "an attachment.\n"
670N/A + "-pushlevel\t: Generates high priority emails when the"
670N/A + " push level is triggered and normal priority when "
670N/A + "flushed.\n"
670N/A + "-pushFilter\t: Generates high priority emails when the "
670N/A + "push level and the push filter is triggered and normal "
670N/A + "priority emails when flushed.\n"
670N/A + "-pushnormal\t: Generates multiple emails when the "
670N/A + "MemoryHandler push level is triggered. All generated "
670N/A + "email are sent as normal priority.\n"
670N/A + "-pushonly\t: Generates multiple emails when the "
670N/A + "MemoryHandler push level is triggered. Generates high "
670N/A + "priority emails when the push level is triggered and "
670N/A + "normal priority when flushed.\n");
670N/A } else {
823N/A final boolean debug = init(l); //may create log messages.
670N/A try {
670N/A LOGGER.log(Level.FINEST, "This is the finest part of the demo.",
670N/A new MessagingException("Fake JavaMail issue."));
670N/A LOGGER.log(Level.FINER, "This is the finer part of the demo.",
670N/A new NullPointerException("Fake bug."));
670N/A LOGGER.log(Level.FINE, "This is the fine part of the demo.");
670N/A LOGGER.log(Level.CONFIG, "Logging config file is {0}.",
670N/A getConfigLocation());
670N/A LOGGER.log(Level.INFO, "Your temp directory is {0}, "
670N/A + "please wait...", getTempDir());
168N/A
670N/A try { //Waste some time for the custom formatter.
670N/A Thread.sleep(3L * 1000L);
670N/A } catch (InterruptedException ex) {
670N/A Thread.currentThread().interrupt();
670N/A }
670N/A
670N/A LOGGER.log(Level.WARNING, "This is a warning.",
670N/A new FileNotFoundException("Fake file chooser issue."));
670N/A LOGGER.log(Level.SEVERE, "The end of the demo.",
670N/A new IOException("Fake access denied issue."));
670N/A } finally {
670N/A closeHandlers();
168N/A }
823N/A
823N/A //Force parse errors. This does have side effects.
823N/A if (debug && getConfigLocation() != null) {
823N/A LogManager.getLogManager().readConfiguration();
823N/A }
168N/A }
168N/A }
168N/A
168N/A /**
670N/A * Used debug problems with the logging.properties. The system property
670N/A * java.security.debug=access,stack can be used to trace access to the
670N/A * LogManager reset.
670N/A *
295N/A * @param prefix a string to prefix the output.
295N/A * @param err any PrintStream or null for System.out.
295N/A */
670N/A @SuppressWarnings("UseOfSystemOutOrSystemErr")
295N/A private static void checkConfig(String prefix, PrintStream err) {
295N/A if (prefix == null || prefix.trim().length() == 0) {
295N/A prefix = "DEBUG";
295N/A }
295N/A
295N/A if (err == null) {
295N/A err = System.out;
295N/A }
295N/A
295N/A try {
823N/A err.println(prefix + ": java.version="
823N/A + System.getProperty("java.version"));
670N/A err.println(prefix + ": LOGGER=" + LOGGER.getLevel());
823N/A err.println(prefix + ": JVM id "
823N/A + ManagementFactory.getRuntimeMXBean().getName());
823N/A err.println(prefix + ": java.security.debug="
823N/A + System.getProperty("java.security.debug"));
295N/A SecurityManager sm = System.getSecurityManager();
295N/A if (sm != null) {
823N/A err.println(prefix + ": SecurityManager.class="
823N/A + sm.getClass().getName());
823N/A err.println(prefix + ": SecurityManager classLoader="
823N/A + toString(sm.getClass().getClassLoader()));
295N/A err.println(prefix + ": SecurityManager.toString=" + sm);
295N/A } else {
769N/A err.println(prefix + ": SecurityManager.class=null");
769N/A err.println(prefix + ": SecurityManager.toString=null");
823N/A err.println(prefix + ": SecurityManager classLoader=null");
295N/A }
769N/A
670N/A String policy = System.getProperty("java.security.policy");
670N/A if (policy != null) {
670N/A File f = new File(policy);
670N/A err.println(prefix + ": AbsolutePath=" + f.getAbsolutePath());
670N/A err.println(prefix + ": CanonicalPath=" + f.getCanonicalPath());
670N/A err.println(prefix + ": length=" + f.length());
670N/A err.println(prefix + ": canRead=" + f.canRead());
670N/A err.println(prefix + ": lastModified="
670N/A + new java.util.Date(f.lastModified()));
670N/A }
295N/A
295N/A LogManager manager = LogManager.getLogManager();
295N/A String key = "java.util.logging.config.file";
295N/A String cfg = System.getProperty(key);
295N/A if (cfg != null) {
295N/A err.println(prefix + ": " + cfg);
295N/A File f = new File(cfg);
295N/A err.println(prefix + ": AbsolutePath=" + f.getAbsolutePath());
295N/A err.println(prefix + ": CanonicalPath=" + f.getCanonicalPath());
295N/A err.println(prefix + ": length=" + f.length());
295N/A err.println(prefix + ": canRead=" + f.canRead());
295N/A err.println(prefix + ": lastModified="
295N/A + new java.util.Date(f.lastModified()));
295N/A } else {
823N/A err.println(prefix + ": " + key
823N/A + " is not set as a system property.");
295N/A }
823N/A err.println(prefix + ": LogManager.class="
823N/A + manager.getClass().getName());
823N/A err.println(prefix + ": LogManager classLoader="
823N/A + toString(manager.getClass().getClassLoader()));
295N/A err.println(prefix + ": LogManager.toString=" + manager);
823N/A err.println(prefix + ": MailHandler classLoader="
823N/A + toString(MailHandler.class.getClassLoader()));
823N/A err.println(prefix + ": Context ClassLoader="
823N/A + toString(Thread.currentThread().getContextClassLoader()));
823N/A err.println(prefix + ": Session ClassLoader="
823N/A + toString(Session.class.getClassLoader()));
823N/A err.println(prefix + ": DataHandler ClassLoader="
823N/A + toString(DataHandler.class.getClassLoader()));
295N/A
295N/A final String p = MailHandler.class.getName();
295N/A key = p.concat(".mail.to");
295N/A String to = manager.getProperty(key);
295N/A err.println(prefix + ": TO=" + to);
670N/A if (to != null) {
670N/A err.println(prefix + ": TO="
670N/A + Arrays.toString(InternetAddress.parse(to, true)));
670N/A }
295N/A
295N/A key = p.concat(".mail.from");
295N/A String from = manager.getProperty(key);
295N/A if (from == null || from.length() == 0) {
295N/A Session session = Session.getInstance(new Properties());
295N/A InternetAddress local = InternetAddress.getLocalAddress(session);
295N/A err.println(prefix + ": FROM=" + local);
295N/A } else {
295N/A err.println(prefix + ": FROM="
402N/A + Arrays.asList(InternetAddress.parse(from, false)));
295N/A err.println(prefix + ": FROM="
402N/A + Arrays.asList(InternetAddress.parse(from, true)));
295N/A }
670N/A
670N/A synchronized (manager) {
670N/A final Enumeration<String> e = manager.getLoggerNames();
670N/A while (e.hasMoreElements()) {
670N/A final Logger l = manager.getLogger(e.nextElement());
670N/A if (l != null) {
670N/A final Handler[] handlers = l.getHandlers();
670N/A if (handlers.length > 0) {
670N/A err.println(prefix + ": " + l.getClass().getName()
670N/A + ", " + l.getName());
670N/A for (Handler h : handlers) {
670N/A err.println(prefix + ":\t" + toString(prefix, err, h));
670N/A }
670N/A }
670N/A }
670N/A }
670N/A }
295N/A } catch (Throwable error) {
295N/A err.print(prefix + ": ");
295N/A error.printStackTrace(err);
295N/A }
670N/A err.flush();
295N/A }
295N/A
295N/A /**
823N/A * Gets the class loader list.
823N/A *
823N/A * @param cl the class loader or null.
823N/A * @return the class loader list.
823N/A */
823N/A private static String toString(ClassLoader cl) {
823N/A StringBuilder buf = new StringBuilder();
823N/A buf.append(cl);
823N/A while (cl != null) {
823N/A cl = cl.getParent();
823N/A buf.append("<-").append(cl);
823N/A }
823N/A return buf.toString();
823N/A }
823N/A
823N/A /**
670N/A * Gets a formatting string describing the given handler.
670N/A *
670N/A * @param prefix the output prefix.
670N/A * @param err the error stream.
670N/A * @param h the handler.
670N/A * @return the formatted string.
670N/A */
670N/A private static String toString(String prefix, PrintStream err, Handler h) {
670N/A StringBuilder buf = new StringBuilder();
670N/A buf.append(h.getClass().getName());
670N/A try {
670N/A if (h instanceof MailHandler) {
670N/A MailHandler mh = (MailHandler) h;
670N/A buf.append(", ").append(mh.getSubject());
670N/A }
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
670N/A
670N/A try {
670N/A buf.append(", ").append(h.getFormatter());
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
670N/A
670N/A try {
670N/A if (h instanceof MailHandler) {
670N/A MailHandler mh = (MailHandler) h;
670N/A buf.append(", ").append(Arrays.toString(
670N/A mh.getAttachmentFormatters()));
670N/A }
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
670N/A
670N/A try {
670N/A buf.append(", ").append(h.getLevel());
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
670N/A
670N/A try {
670N/A buf.append(", ").append(h.getFilter());
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
670N/A
670N/A try {
670N/A buf.append(", ").append(h.getErrorManager());
670N/A } catch (SecurityException error) {
670N/A err.print(prefix + ": ");
670N/A error.printStackTrace(err);
670N/A }
823N/A
823N/A buf.append(", ").append(toString(h.getClass().getClassLoader()));
670N/A return buf.toString();
670N/A }
670N/A
670N/A /**
670N/A * Example for body only messages. On close the remaining messages are sent. <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=com.sun.mail.util.logging.MailHandler
670N/A * com.sun.mail.util.logging.MailHandler.subject=Body only demo
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initBodyOnly() {
168N/A MailHandler h = new MailHandler();
168N/A h.setSubject("Body only demo");
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
670N/A * Example showing that when the mail handler reaches capacity it will
670N/A * format and send the current records. Capacity is used to roughly limit
670N/A * the size of an outgoing message. On close any remaining messages are
670N/A * sent. <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=com.sun.mail.util.logging.MailHandler
670N/A * com.sun.mail.util.logging.MailHandler.subject=Low capacity demo
670N/A * com.sun.mail.util.logging.MailHandler.capacity=5
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initLowCapacity() {
168N/A MailHandler h = new MailHandler(5);
168N/A h.setSubject("Low capacity demo");
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
670N/A * Example for body only messages. On close any remaining messages are sent. <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=com.sun.mail.util.logging.MailHandler
670N/A * com.sun.mail.util.logging.MailHandler.subject=Body and attachment demo
670N/A * com.sun.mail.util.logging.MailHandler.attachment.formatters=java.util.logging.XMLFormatter
670N/A * com.sun.mail.util.logging.MailHandler.attachment.names=data.xml
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initSimpleAttachment() {
168N/A MailHandler h = new MailHandler();
168N/A h.setSubject("Body and attachment demo");
670N/A h.setAttachmentFormatters(new XMLFormatter());
670N/A h.setAttachmentNames("data.xml");
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
670N/A * Example setup for priority messages by level. If the push level is
670N/A * triggered the message is high priority. Otherwise, on close any remaining
670N/A * messages are sent. <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=com.sun.mail.util.logging.MailHandler
670N/A * com.sun.mail.util.logging.MailHandler.subject=Push level demo
670N/A * com.sun.mail.util.logging.MailHandler.pushLevel=WARNING
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initWithPushLevel() {
168N/A MailHandler h = new MailHandler();
168N/A h.setSubject("Push level demo");
168N/A h.setPushLevel(Level.WARNING);
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
769N/A * Example for priority messages by generation rate. If the push filter is
670N/A * triggered the message is high priority. Otherwise, on close any remaining
769N/A * messages are sent. If the capacity is set to the <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=com.sun.mail.util.logging.MailHandler
770N/A * com.sun.mail.util.logging.MailHandler.subject=Push filter demo
670N/A * com.sun.mail.util.logging.MailHandler.pushLevel=ALL
769N/A * com.sun.mail.util.logging.MailHandler.pushFilter=com.sun.mail.util.logging.DurationFilter
769N/A * com.sun.mail.util.logging.DurationFilter.records=2
769N/A * com.sun.mail.util.logging.DurationFilter.duration=1 * 60 * 1000
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initWithPushFilter() {
168N/A MailHandler h = new MailHandler();
769N/A h.setSubject("Push filter demo");
168N/A h.setPushLevel(Level.ALL);
769N/A h.setPushFilter(new DurationFilter(2, 1L * 60L * 1000L));
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
670N/A * Example for circular buffer behavior. The level, push level, and capacity
670N/A * are set the same so that the memory handler push results in a mail
670N/A * handler push. All messages are high priority. On close any remaining
670N/A * records are discarded because they never reach the mail handler. <code>
670N/A * ##logging.properties
670N/A * MailHandlerDemo.handlers=java.util.logging.MemoryHandler
670N/A * java.util.logging.MemoryHandler.target=com.sun.mail.util.logging.MailHandler
670N/A * com.sun.mail.util.logging.MailHandler.level=ALL
670N/A * java.util.logging.MemoryHandler.level=ALL
670N/A * java.util.logging.MemoryHandler.push=WARNING
833N/A * com.sun.mail.util.logging.MailHandler.subject=Push only demo
833N/A * com.sun.mail.util.logging.MailHandler.pushLevel=WARNING
670N/A * ##
670N/A * </code>
168N/A */
168N/A private static void initPushOnly() {
168N/A final int capacity = 3;
168N/A final Level pushLevel = Level.WARNING;
168N/A final MailHandler h = new MailHandler(capacity);
168N/A h.setPushLevel(pushLevel);
168N/A h.setSubject("Push only demo");
168N/A MemoryHandler m = new MemoryHandler(h, capacity, pushLevel);
168N/A h.setLevel(m.getLevel());
168N/A LOGGER.addHandler(m);
168N/A pushOnlyHandler = h;
168N/A }
613N/A
613N/A /**
670N/A * Holds on to the push only handler. Only declared here to apply fallback
670N/A * settings.
613N/A */
168N/A private static Handler pushOnlyHandler;
168N/A
168N/A /**
670N/A * Example for circular buffer behavior as normal priority. The push level,
670N/A * and capacity are set the same so that the memory handler push results in
670N/A * a mail handler push. All messages are normal priority. On close any
670N/A * remaining records are discarded because they never reach the mail
670N/A * handler. Use the LogManager config option or extend the MemoryHandler to
670N/A * emulate this behavior via the logging.properties.
168N/A */
168N/A private static void initPushNormal() {
168N/A final int capacity = 3;
168N/A final MailHandler h = new MailHandler(capacity);
168N/A h.setSubject("Push normal demo");
168N/A MemoryHandler m = new MemoryHandler(h, capacity, Level.WARNING) {
168N/A
526N/A @Override
168N/A public void push() {
168N/A super.push(); //push to target.
168N/A super.flush(); //make the target send the email.
168N/A }
168N/A };
168N/A LOGGER.addHandler(m);
168N/A pushNormalHandler = h;
168N/A }
613N/A
613N/A /**
670N/A * Holds on to the push normal handler. Only declared here to apply fallback
670N/A * settings.
613N/A */
168N/A private static Handler pushNormalHandler;
168N/A
168N/A /**
670N/A * Example for various kinds of custom sorting, formatting, and filtering
670N/A * for multiple attachment messages. The subject will contain the most
670N/A * severe record and a count of remaining records. The log records are
670N/A * ordered from most severe to least severe. The body uses a custom
670N/A * formatter that includes a summary by date and time. The attachment use
670N/A * XML and plain text formats. Each attachment has a different set of
670N/A * filtering. The attachment names are generated from either a fixed name or
670N/A * are built using the number and type of the records formatted. On close
670N/A * any remaining messages are sent. Use the LogManager config option or
670N/A * extend the MemoryHandler to emulate this behavior via the
670N/A * logging.properties.
168N/A */
168N/A private static void initCustomAttachments() {
168N/A MailHandler h = new MailHandler();
168N/A
769N/A //Sort records by severity keeping the severe messages at the top.
670N/A h.setComparator(Collections.reverseOrder(new SeverityComparator()));
168N/A
168N/A //Use subject to provide a hint as to what is in the email.
670N/A h.setSubject(new CollectorFormatter());
168N/A
168N/A //Make the body give a simple summary of what happened.
168N/A h.setFormatter(new SummaryFormatter());
168N/A
168N/A //Create 3 attachments.
670N/A h.setAttachmentFormatters(new XMLFormatter(),
670N/A new XMLFormatter(), new SimpleFormatter());
168N/A
670N/A //Filter each attachment differently.
769N/A h.setAttachmentFilters(null,
769N/A new DurationFilter(3L, 1000L),
769N/A new DurationFilter(1L, 15L * 60L * 1000L));
168N/A
670N/A //Creating the attachment name formatters.
670N/A h.setAttachmentNames(new CollectorFormatter("all.xml"),
670N/A new CollectorFormatter("{3} records and {5} errors.xml"),
670N/A new CollectorFormatter("{5,choice,0#no errors|1#1 error|1<"
670N/A + "{5,number,integer} errors}.txt"));
168N/A
168N/A LOGGER.addHandler(h);
168N/A }
168N/A
168N/A /**
168N/A * Sets up the demos that will run.
670N/A *
670N/A * @param l the list of arguments.
823N/A * @return true if debug is on.
168N/A */
823N/A private static boolean init(List<String> l) {
670N/A l = new ArrayList<String>(l);
295N/A Session session = Session.getInstance(System.getProperties());
670N/A boolean all = l.remove("-all") || l.isEmpty();
670N/A if (l.remove("-body") || all) {
670N/A initBodyOnly();
670N/A }
670N/A
670N/A if (l.remove("-custom") || all) {
670N/A initCustomAttachments();
670N/A }
670N/A
670N/A if (l.remove("-low") || all) {
670N/A initLowCapacity();
670N/A }
670N/A
670N/A if (l.remove("-pushfilter") || all) {
670N/A initWithPushFilter();
670N/A }
670N/A
670N/A if (l.remove("-pushlevel") || all) {
670N/A initWithPushLevel();
670N/A }
670N/A
670N/A if (l.remove("-pushnormal") || all) {
670N/A initPushNormal();
670N/A }
670N/A
670N/A if (l.remove("-pushonly") || all) {
670N/A initPushOnly();
670N/A }
670N/A
670N/A if (l.remove("-simple") || all) {
670N/A initSimpleAttachment();
670N/A }
670N/A
670N/A boolean fallback = applyFallbackSettings();
823N/A boolean debug = l.remove("-debug") || session.getDebug();
823N/A if (debug) {
295N/A checkConfig(CLASS_NAME, session.getDebugOut());
295N/A }
295N/A
670N/A if (!l.isEmpty()) {
670N/A LOGGER.log(Level.SEVERE, "Unknown commands: {0}", l);
670N/A }
670N/A
670N/A if (fallback) {
670N/A LOGGER.info("Check your user temp dir for output.");
670N/A }
823N/A return debug;
168N/A }
168N/A
613N/A /**
613N/A * Close and remove all handlers added to the class logger.
613N/A */
168N/A private static void closeHandlers() {
168N/A Handler[] handlers = LOGGER.getHandlers();
670N/A for (Handler h : handlers) {
168N/A h.close();
168N/A LOGGER.removeHandler(h);
168N/A }
168N/A }
168N/A
613N/A /**
613N/A * Apply some fallback settings if no configuration file was specified.
670N/A *
670N/A * @return true if fallback settings were applied.
613N/A */
670N/A private static boolean applyFallbackSettings() {
168N/A if (getConfigLocation() == null) {
168N/A LOGGER.setLevel(Level.ALL);
168N/A Handler[] handlers = LOGGER.getHandlers();
670N/A for (Handler h : handlers) {
168N/A fallbackSettings(h);
168N/A }
168N/A fallbackSettings(pushOnlyHandler);
168N/A fallbackSettings(pushNormalHandler);
670N/A return true;
168N/A }
670N/A return false;
168N/A }
168N/A
613N/A /**
613N/A * Common fallback settings for a single handler.
670N/A *
613N/A * @param h the handler.
613N/A */
168N/A private static void fallbackSettings(Handler h) {
295N/A if (h != null) {
295N/A h.setErrorManager(new FileErrorManager());
295N/A h.setLevel(Level.ALL);
295N/A }
168N/A }
168N/A
613N/A /**
613N/A * Gets the system temp directory.
670N/A *
613N/A * @return the system temp directory.
613N/A */
168N/A private static String getTempDir() {
168N/A return System.getProperty("java.io.tmpdir");
168N/A }
168N/A
613N/A /**
613N/A * Gets the configuration file or class name.
670N/A *
613N/A * @return the file name or class name.
613N/A */
168N/A private static String getConfigLocation() {
168N/A String file = System.getProperty("java.util.logging.config.file");
168N/A if (file == null) {
168N/A return System.getProperty("java.util.logging.config.class");
168N/A }
168N/A return file;
168N/A }
833N/A
833N/A /**
833N/A * No objects are allowed.
833N/A * @throws IllegalAccessException always.
833N/A */
833N/A private MailHandlerDemo() throws IllegalAccessException {
833N/A throw new IllegalAccessException();
833N/A }
168N/A}