001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.util;
032:
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: import com.jgoodies.binding.beans.BeanUtils;
039:
040: /**
041: * Assists in logging changes in bound bean properties.
042: *
043: * @author Andrej Golovnin
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.5 $
046: *
047: * @see Logger
048: */
049: public final class LoggingUtils {
050:
051: private static final Logger LOGGER = Logger
052: .getLogger(LoggingUtils.class.getName());
053:
054: private static Level defaultLevel = Level.FINE;
055:
056: private LoggingUtils() {
057: // Override default constructor; prevents instantiation.
058: }
059:
060: /**
061: * Sets the default log level to be used when logging PropertyChangeEvents.
062: * The initial default level is {@link Level#FINE}.
063: *
064: * @param level the default level to be used if no custom level
065: * has been provided
066: *
067: * @throws NullPointerException if the new defaultLevel is null
068: */
069: public static void setDefaultLevel(Level level) {
070: if (level == null) {
071: throw new NullPointerException(
072: "The log level must not be null.");
073: }
074: LoggingUtils.defaultLevel = level;
075: }
076:
077: /**
078: * Registers a PropertyChangeListener with the specified bean
079: * that logs all PropertyChangeEvents fired by this bean
080: * using the default Logger and default log level.
081: *
082: * @param bean the bean to log PropertyChangeEvents from
083: *
084: * @throws NullPointerException if the bean is null
085: */
086: public static void logPropertyChanges(Object bean) {
087: logPropertyChanges(bean, LOGGER);
088: }
089:
090: /**
091: * Registers a PropertyChangeListener with the specified bean, which logs
092: * all PropertyChangeEvents fired by the given bean using the specified
093: * Logger and the default log level.
094: *
095: * @param bean the bean to log PropertyChangeEvents from
096: * @param logger the Logger to be used to log PropertyChangeEvents
097: *
098: * @throws NullPointerException if the bean or logger is null
099: */
100: public static void logPropertyChanges(Object bean, Logger logger) {
101: logPropertyChanges(bean, logger, defaultLevel);
102: }
103:
104: /**
105: * Registers a PropertyChangeListener with the specified bean, which logs
106: * all PropertyChangeEvents fired by the given bean using the specified
107: * Logger and log level.
108: *
109: * @param bean the bean to log PropertyChangeEvents from
110: * @param logger the Logger to be used to log PropertyChangeEvents
111: * @param level the log level
112: *
113: * @throws NullPointerException if the bean, logger, or level is null
114: */
115: public static void logPropertyChanges(Object bean, Logger logger,
116: Level level) {
117: BeanUtils.addPropertyChangeListener(bean, new LogHandler(
118: logger, level));
119: }
120:
121: /**
122: * Registers a named PropertyChangeListener with the specified bean,
123: * which logs all PropertyChangeEvents of the given property using
124: * the default Logger and default log level.
125: *
126: * @param bean the bean to log PropertyChangeEvents from
127: * @param propertyName the name of the property which PropertyChangeEvents
128: * should be logged
129: *
130: * @throws NullPointerException if the bean or propertyName is null
131: */
132: public static void logPropertyChanges(Object bean,
133: String propertyName) {
134: logPropertyChanges(bean, propertyName, LOGGER);
135: }
136:
137: /**
138: * Registers a named PropertyChangeListener with the specified bean,
139: * which logs all PropertyChangeEvents of the given property using
140: * the specified Logger and the default log level.
141: *
142: * @param bean the bean to log PropertyChangeEvents from
143: * @param propertyName the name of the property which PropertyChangeEvents
144: * should be logged
145: * @param logger the Logger to be used to log PropertyChangeEvents
146: *
147: * @throws NullPointerException if the bean, propertyName, or logger is null
148: */
149: public static void logPropertyChanges(Object bean,
150: String propertyName, Logger logger) {
151: logPropertyChanges(bean, propertyName, logger, defaultLevel);
152: }
153:
154: /**
155: * Registers a named PropertyChangeListener with the specified bean,
156: * which logs all PropertyChangeEvents of the given property, Logger,
157: * and log level.
158: *
159: * @param bean the bean to log PropertyChangeEvents from
160: * @param propertyName the name of the property which PropertyChangeEvents
161: * should be logged
162: * @param logger the Logger to be used to log PropertyChangeEvents
163: * @param level the log level
164: *
165: * @throws NullPointerException if the bean, propertyName, logger,
166: * or level is null
167: */
168: public static void logPropertyChanges(Object bean,
169: String propertyName, Logger logger, Level level) {
170: BeanUtils.addPropertyChangeListener(bean, propertyName,
171: new LogHandler(logger, level));
172: }
173:
174: // Helper code ************************************************************
175:
176: /**
177: * A listener which logs PropertyChangeEvents.
178: */
179: private static final class LogHandler implements
180: PropertyChangeListener {
181:
182: /**
183: * Logger to be used to log PropertyChangeEvents.
184: */
185: private final Logger logger;
186:
187: /**
188: * The log level used for the logging.
189: */
190: private final Level level;
191:
192: /**
193: * Creates and returns LoggingListener, which uses the given Logger
194: * to log PropertyChangeEvents.
195: *
196: * @param logger the logger to be used to log PropertyChangeEvents
197: * @param level the level used for the logging
198: */
199: LogHandler(Logger logger, Level level) {
200: if (logger == null) {
201: throw new NullPointerException(
202: "The logger must not be null.");
203: }
204: if (level == null) {
205: throw new NullPointerException(
206: "The level must not be null.");
207: }
208: this .logger = logger;
209: this .level = level;
210: }
211:
212: /**
213: * Logs the given event.
214: */
215: public void propertyChange(PropertyChangeEvent e) {
216: if (!logger.isLoggable(level))
217: return;
218:
219: Object newValue = e.getNewValue();
220: Object oldValue = e.getOldValue();
221: StringBuilder builder = new StringBuilder(e.getSource()
222: .toString());
223: builder.append(" [propertyName=");
224: builder.append(e.getPropertyName());
225: builder.append(", oldValue=");
226: builder.append(oldValue);
227: if (oldValue != null) {
228: builder.append(", oldValueType=");
229: builder.append(oldValue.getClass());
230: }
231: builder.append(", newValue=");
232: builder.append(newValue);
233: if (newValue != null) {
234: builder.append(", newValueType=");
235: builder.append(newValue.getClass());
236: }
237: logger.log(level, builder.toString());
238: }
239:
240: }
241:
242: }
|