bb.util.logging
Class HandlerAbstract

java.lang.Object
  extended by java.util.logging.Handler
      extended by bb.util.logging.HandlerAbstract
Direct Known Subclasses:
HandlerAudio, HandlerConsole, HandlerEmail, HandlerGui

public abstract class HandlerAbstract
extends Handler

Abstract subclass of Handler which makes it easier to write further subclasses.

The chief reason why this class was written is because Sun annoyingly hid key functionality for Handler subclasses. First, from ConsoleHandler, this class copied and modified configure. Second, from LogManager, this class copied and modified some code, which it exposes in the following methods: getLevelProperty, getFilterProperty, getFormatterProperty, getStringProperty. These methods are needed by configure. Also to support configure, this class introduces the methods getEncodingDefault, getFilterDefault, getFormatterDefault, and getLevelDefault.

This class introduces a new field, alive, along with accessor (isAlive) and mutator (setAlive) methods for it. This field is used by close, which this class overrides from its superclass (Handler). Also, this class overrides isLoggable, since its superclass method does not detect null record params.

It is unclear whether or not this class is multithread safe. This is because it is unclear whether or not its superclass, Handler, is multithread safe: its javadocs do not mention thread safety, and Sun's source code is bizarre (as of JDK 1.6.0_11, two of Handler's methods are synchronized, but many others are not even tho they involve shared mutable state). There exists a bug report with Sun on this matter (status as of 2009-03-23: "Your report has been assigned an internal review ID of 1484784, which is NOT visible on the Sun Developer Network (SDN).") Every method added by this class is synchronized.

Author:
Brent Boyer

Nested Class Summary
static class HandlerAbstract.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  boolean alive
          Records whether or not this instance is considered alive.
 
Constructor Summary
HandlerAbstract()
          Constructor.
 
Method Summary
 void close()
          If isAlive returns true, then calls super.close before calling setAlive(false).
protected  void configure()
          Initializes an instance using the following LogManager configuration properties: className.encoding the name of the character set encoding to use (default: getEncodingDefault) className.filter specifies the name of a Filter class to use (default: getFilterDefault) className.formatter specifies the name of a Formatter class to use (default: getFormatterDefault) className.level specifies the default level for the Handler (default: getLevelDefault) If any property is not defined (or has an invalid value) then the specified default value is used.
protected  String getEncodingDefault()
          Returns the default value to use for the encoding.
protected  Filter getFilterDefault()
          Returns the default value to use for the filter.
protected  Filter getFilterProperty(String name, Filter defaultValue)
           
protected  Formatter getFormatterDefault()
          Returns the default value to use for the formatter.
protected  Formatter getFormatterProperty(String name, Formatter defaultValue)
           
protected  int getIntProperty(String name, int defaultValue)
           
protected  Level getLevelDefault()
          Returns the default value to use for the level.
protected  Level getLevelProperty(String name, Level defaultValue)
           
protected  String getStringProperty(String name, String defaultValue)
           
protected  boolean isAlive()
          Accessor for alive.
 boolean isLoggable(LogRecord record)
          If record is null, immediately returns false., Otherwise, returns super.isLoggable(record).
protected  void setAlive(boolean alive)
          Mutator for alive.
 
Methods inherited from class java.util.logging.Handler
flush, getEncoding, getErrorManager, getFilter, getFormatter, getLevel, publish, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

alive

private boolean alive
Records whether or not this instance is considered alive. Is initially true, then should be set to false once and for all when close is first called.

Constructor Detail

HandlerAbstract

public HandlerAbstract()
Constructor.

Method Detail

configure

protected void configure()
                  throws RuntimeException
Initializes an instance using the following LogManager configuration properties:
  1. className.encoding the name of the character set encoding to use (default: getEncodingDefault)
  2. className.filter specifies the name of a Filter class to use (default: getFilterDefault)
  3. className.formatter specifies the name of a Formatter class to use (default: getFormatterDefault)
  4. className.level specifies the default level for the Handler (default: getLevelDefault)
If any property is not defined (or has an invalid value) then the specified default value is used.

Throws:
RuntimeException - (or some subclass) if any problem occurs

getEncodingDefault

protected String getEncodingDefault()
Returns the default value to use for the encoding. This method is called by configure(). Implementation here returns null (which means use the platform default).


getFilterDefault

protected Filter getFilterDefault()
Returns the default value to use for the filter. This method is called by configure(). Implementation here returns null (which means use no Filter).


getFormatterDefault

protected Formatter getFormatterDefault()
Returns the default value to use for the formatter. This method is called by configure(). Implementation here returns a new FormatterShort instance.


getLevelDefault

protected Level getLevelDefault()
Returns the default value to use for the level. This method is called by configure(). Implementation here returns Level.INFO.


getLevelProperty

protected Level getLevelProperty(String name,
                                 Level defaultValue)

getFilterProperty

protected Filter getFilterProperty(String name,
                                   Filter defaultValue)

getFormatterProperty

protected Formatter getFormatterProperty(String name,
                                         Formatter defaultValue)

getStringProperty

protected String getStringProperty(String name,
                                   String defaultValue)

getIntProperty

protected int getIntProperty(String name,
                             int defaultValue)

isAlive

protected boolean isAlive()
Accessor for alive.


setAlive

protected void setAlive(boolean alive)
Mutator for alive.


close

public void close()
If isAlive returns true, then calls super.close before calling setAlive(false).

Specified by:
close in class Handler

isLoggable

public boolean isLoggable(LogRecord record)
If record is null, immediately returns false., Otherwise, returns super.isLoggable(record).

Overrides:
isLoggable in class Handler