Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
  3.  * Copyright 2009 Jason Mehrens. All Rights Reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *   - Redistributions of source code must retain the above copyright
  10.  *     notice, this list of conditions and the following disclaimer.
  11.  *
  12.  *   - Redistributions in binary form must reproduce the above copyright
  13.  *     notice, this list of conditions and the following disclaimer in the
  14.  *     documentation and/or other materials provided with the distribution.
  15.  *
  16.  *   - Neither the name of Sun Microsystems nor the names of its
  17.  *     contributors may be used to endorse or promote products derived
  18.  *     from this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32.  
  33.  
  34. import java.text.MessageFormat;
  35. import java.util.logging.*;
  36. import com.sun.mail.util.logging.MailHandler;
  37.  
  38. /**
  39.  * Creates an attachment name based on the number of records and errors.
  40.  * The pattern is a <tt>java.text.MesageFormat</tt> with two parameters.
  41.  * The first parameter is the number of records formatted.  The second is the
  42.  * number of records with errors.
  43.  * @author Jason Mehrens
  44.  */
  45. public class SummaryNameFormatter extends Formatter {
  46.  
  47.     private final String pattern;
  48.     private long count;
  49.     private long errors;
  50.  
  51.     /**
  52.      * Creates a simple pattern.
  53.      */
  54.     public SummaryNameFormatter() {
  55.         this("{0} records and {1} errors");
  56.     }
  57.  
  58.     /**
  59.      * Creates formatter using a message format style pattern.
  60.      * @param pattern the pattern.
  61.      * @throws NullPointerException if pattern is null.
  62.      */
  63.     public SummaryNameFormatter(final String pattern) {
  64.         if (pattern == null) {
  65.             throw new NullPointerException();
  66.         }
  67.         this.pattern = pattern;
  68.     }
  69.  
  70.     public synchronized String format(LogRecord r) {
  71.         count++;
  72.         if (r.getThrown() != null) {
  73.             errors++;
  74.         }
  75.         return "";
  76.     }
  77.  
  78.     public synchronized String getTail(Handler h) {
  79.         final long records = this.count; //read
  80.         final long thrown = this.errors;
  81.         this.count = 0; //reset
  82.         this.errors = 0;
  83.         return toString(records, thrown) + extFrom(h);
  84.     }
  85.  
  86.     public synchronized String toString() {
  87.         return toString(count, errors);
  88.     }
  89.  
  90.     private String toString(final long count, final long errors) {
  91.         return MessageFormat.format(pattern, new Object[]{new Long(count), new Long(errors)});
  92.     }
  93.  
  94.     private String extFrom(Handler h) {
  95.         if(h instanceof MailHandler) {
  96.             MailHandler mh = (MailHandler)h;
  97.             Formatter[] content = mh.getAttachmentFormatters();
  98.             Formatter[] names = mh.getAttachmentNames();
  99.             assert content.length == names.length;
  100.             for(int i=0; i<content.length; i++) {
  101.                 if(names[i] == this) {
  102.                     if(content[i] instanceof XMLFormatter) {
  103.                         return ".xml";
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         return ".txt";
  109.     }
  110. }
  111.