Author: nbelaevski
Date: 2010-08-30 12:51:33 -0400 (Mon, 30 Aug 2010)
New Revision: 19022
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/JavaLoggerTest.java
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/LoggerTestMessages.java
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/log/
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/log/Resources.properties
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/JavaLogger.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/LogFactory.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/Logger.java
Log:
https://jira.jboss.org/browse/RFPL-434
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java 2010-08-30
14:56:16 UTC (rev 19021)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -36,18 +36,11 @@
private BundleLoader bundleLoader;
- private boolean checkApplicationBundle;
-
public MessageInterpolator(BundleLoader bundleLoader) {
- this(bundleLoader, false);
- }
-
- public MessageInterpolator(BundleLoader bundleLoader, boolean checkApplicationBundle)
{
super();
this.bundleLoader = bundleLoader;
- this.checkApplicationBundle = checkApplicationBundle;
}
-
+
protected String getMessageKey(Enum<?> key) {
return key.toString();
}
@@ -55,19 +48,6 @@
protected String getPattern(Locale locale, Enum<?> key) {
String messageKey = getMessageKey(key);
- if (checkApplicationBundle) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
-
- if (facesContext != null) {
- try {
- ResourceBundle bundle =
bundleLoader.getApplicationBundle(facesContext, key, locale);
- return bundle.getString(messageKey);
- } catch (MissingResourceException e1) {
- //do nothing
- }
- }
- }
-
try {
ResourceBundle bundle = bundleLoader.getBundle(key, locale);
return bundle.getString(messageKey);
Modified: branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/JavaLogger.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/JavaLogger.java 2010-08-30
14:56:16 UTC (rev 19021)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/JavaLogger.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -24,12 +24,17 @@
package org.richfaces.log;
import java.util.EnumMap;
+import java.util.Locale;
import java.util.Map;
import java.util.logging.LogRecord;
+import org.richfaces.l10n.BundleLoader;
+import org.richfaces.l10n.InterpolationException;
+import org.richfaces.l10n.MessageInterpolator;
+
/**
* <p class="changed_added_4_0">That logger delegates all calls to the
JDK {@link java.util.logging.Logger}</p>
*
@@ -38,11 +43,11 @@
*/
public class JavaLogger implements Logger {
- public static final String RICHFACES_LOG = "org.richfaces.cdk";
+ public static final String RICHFACES_LOG = "org.richfaces";
private static final String CLASS_NAME = JavaLogger.class.getName();
- private static final Map<Level, java.util.logging.Level> LEVELS_MAP =
+ static final Map<Level, java.util.logging.Level> LEVELS_MAP =
new EnumMap<Level, java.util.logging.Level>(Level.class);
static {
@@ -54,8 +59,11 @@
private final java.util.logging.Logger jdkLogger;
+ private MessageInterpolator messageInterpolator;
+
JavaLogger(String category) {
jdkLogger = java.util.logging.Logger.getLogger(category);
+ messageInterpolator = new MessageInterpolator(new BundleLoader());
}
JavaLogger() {
@@ -87,6 +95,14 @@
}
}
+ private String interpolate(Enum<?> messageKey, Object... args) {
+ try {
+ return messageInterpolator.interpolate(Locale.getDefault(), messageKey,
args);
+ } catch (InterpolationException e) {
+ return "???" + e.getMessageKey() + "???";
+ }
+ }
+
private LogRecord createRecord(java.util.logging.Level level, CharSequence message,
Throwable thrown) {
// millis and thread are filled by the constructor
LogRecord record = new LogRecord(level, message != null ? message.toString() :
null);
@@ -107,10 +123,18 @@
log(Level.DEBUG, content);
}
+ public void debug(Enum<?> messageKey, Object... args) {
+ log(Level.DEBUG, messageKey, args);
+ }
+
public void debug(CharSequence content, Throwable thrown) {
log(Level.DEBUG, content, thrown);
}
+ public void debug(Throwable error, Enum<?> messageKey, Object... args) {
+ log(Level.DEBUG, error, messageKey, args);
+ }
+
public void debug(Throwable thrown) {
log(Level.DEBUG, thrown);
}
@@ -123,10 +147,18 @@
log(Level.INFO, content);
}
+ public void info(Enum<?> messageKey, Object... args) {
+ log(Level.INFO, messageKey, args);
+ }
+
public void info(CharSequence content, Throwable thrown) {
log(Level.INFO, content, thrown);
}
+ public void info(Throwable error, Enum<?> messageKey, Object... args) {
+ log(Level.INFO, error, messageKey, args);
+ }
+
public void info(Throwable thrown) {
log(Level.INFO, thrown);
}
@@ -138,11 +170,19 @@
public void warn(CharSequence content) {
log(Level.WARNING, content);
}
+
+ public void warn(Enum<?> messageKey, Object... args) {
+ log(Level.WARNING, messageKey, args);
+ }
public void warn(CharSequence content, Throwable thrown) {
log(Level.WARNING, content, thrown);
}
+ public void warn(Throwable error, Enum<?> messageKey, Object... args) {
+ log(Level.WARNING, error, messageKey, args);
+ }
+
public void warn(Throwable thrown) {
log(Level.WARNING, thrown);
}
@@ -155,10 +195,18 @@
log(Level.ERROR, content);
}
+ public void error(Enum<?> messageKey, Object... args) {
+ log(Level.ERROR, messageKey, args);
+ }
+
public void error(CharSequence content, Throwable thrown) {
log(Level.ERROR, content, thrown);
}
+ public void error(Throwable error, Enum<?> messageKey, Object... args) {
+ log(Level.ERROR, error, messageKey, args);
+ }
+
public void error(Throwable thrown) {
log(Level.ERROR, thrown);
}
@@ -174,6 +222,13 @@
}
}
+ public void log(Level level, Enum<?> messageKey, Object... args) {
+ java.util.logging.Level julLevel = LEVELS_MAP.get(level);
+ if (jdkLogger.isLoggable(julLevel)) {
+ jdkLogger.log(createRecord(julLevel, interpolate(messageKey, args), null));
+ }
+ }
+
public void log(Level level, CharSequence content, Throwable thrown) {
java.util.logging.Level julLevel = LEVELS_MAP.get(level);
if (jdkLogger.isLoggable(julLevel)) {
@@ -181,6 +236,13 @@
}
}
+ public void log(Level level, Throwable thrown, Enum<?> messageKey, Object...
args) {
+ java.util.logging.Level julLevel = LEVELS_MAP.get(level);
+ if (jdkLogger.isLoggable(julLevel)) {
+ jdkLogger.log(createRecord(julLevel, interpolate(messageKey, args),
thrown));
+ }
+ }
+
public void log(Level level, Throwable thrown) {
java.util.logging.Level julLevel = LEVELS_MAP.get(level);
if (jdkLogger.isLoggable(julLevel)) {
Modified: branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/LogFactory.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/LogFactory.java 2010-08-30
14:56:16 UTC (rev 19021)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/LogFactory.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -23,6 +23,8 @@
package org.richfaces.log;
+import org.richfaces.log.Logger.Level;
+
/**
* <p class="changed_added_4_0">This class produces loggers used by whole
RichFaces library.</p>
* @author asmirnov(a)exadel.com
Modified: branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/Logger.java
===================================================================
--- branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/Logger.java 2010-08-30
14:56:16 UTC (rev 19021)
+++ branches/RFPL-434/core/commons/src/main/java/org/richfaces/log/Logger.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -30,7 +30,10 @@
public interface Logger {
public enum Level {
- ERROR, INFO, WARNING, DEBUG
+ DEBUG,
+ INFO,
+ WARNING,
+ ERROR
}
/**
@@ -53,6 +56,15 @@
* <p class="changed_added_4_0">
* </p>
*
+ * @param messageKey
+ * @param args
+ */
+ public void debug(Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
* @param content
* @param error
*/
@@ -63,13 +75,23 @@
* </p>
*
* @param error
+ * @param messageKey
+ * @param args
*/
+ public void debug(Throwable error, Enum<?> messageKey, Object... args);
+
/**
* <p class="changed_added_4_0">
* </p>
*
* @param error
*/
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param error
+ */
public void debug(Throwable error);
/**
@@ -92,6 +114,15 @@
* <p class="changed_added_4_0">
* </p>
*
+ * @param messageKey
+ * @param args
+ */
+ public void info(Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
* @param content
* @param error
*/
@@ -102,7 +133,17 @@
* </p>
*
* @param error
+ * @param messageKey
+ * @param args
*/
+ public void info(Throwable error, Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param error
+ */
public void info(Throwable error);
/**
@@ -125,6 +166,15 @@
* <p class="changed_added_4_0">
* </p>
*
+ * @param messageKey
+ * @param args
+ */
+ public void warn(Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
* @param content
* @param error
*/
@@ -135,7 +185,17 @@
* </p>
*
* @param error
+ * @param messageKey
+ * @param args
*/
+ public void warn(Throwable error, Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param error
+ */
public void warn(Throwable error);
/**
@@ -158,6 +218,15 @@
* <p class="changed_added_4_0">
* </p>
*
+ * @param messageKey
+ * @param args
+ */
+ public void error(Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
* @param content
* @param error
*/
@@ -168,7 +237,17 @@
* </p>
*
* @param error
+ * @param messageKey
+ * @param args
*/
+ public void error(Throwable error, Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param error
+ */
public void error(Throwable error);
/**
@@ -208,5 +287,25 @@
*/
public void log(Level level, Throwable error);
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param level
+ * @param messageKey
+ * @param args
+ */
+ public void log(Level level, Enum<?> messageKey, Object... args);
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param level
+ * @param error
+ * @param messageKey
+ * @param args
+ */
+ public void log(Level level, Throwable error, Enum<?> messageKey, Object...
args);
}
Added: branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/JavaLoggerTest.java
===================================================================
--- branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/JavaLoggerTest.java
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/JavaLoggerTest.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -0,0 +1,194 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.log;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.richfaces.log.Logger.Level;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class JavaLoggerTest {
+
+ private static final String ANOTHER_DUMMY_MESSAGE = "another message";
+
+ private static final String DUMMY_MESSAGE = "message";
+
+ private static final String TEST_MESSAGE_PATTERN = "RF-000000 Test message with
arguments: {0} and {1}";
+
+ private static final String CHAR_SEQUENCE_THROWABLE_PATTERN = "(CharSequence,
Throwable)({0})";
+
+ private static final String CHAR_SEQUENCE_PATTERN = "(CharSequence)({0})";
+
+ /**
+ * @author Nick Belaevski
+ *
+ */
+ private final class TrackingHandler extends Handler {
+ @Override
+ public void publish(LogRecord record) {
+ publishedRecords.add(record);
+ }
+
+ @Override
+ public void flush() {
+ }
+
+ @Override
+ public void close() throws SecurityException {
+ }
+ }
+
+ private java.util.logging.Logger wrappedLogger;
+
+ private JavaLogger logger;
+
+ private List<LogRecord> publishedRecords;
+
+ @Before
+ public void setUp() throws Exception {
+ LogManager.getLogManager().reset();
+ publishedRecords = new ArrayList<LogRecord>();
+
+ wrappedLogger =
java.util.logging.Logger.getLogger("org.richfaces.JavaLoggerTest");
+ wrappedLogger.addHandler(new TrackingHandler());
+
+ logger = new JavaLogger(wrappedLogger.getName());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ publishedRecords = null;
+
+ wrappedLogger = null;
+ logger = null;
+ }
+
+ private void verifyLogRecord(LogRecord logRecord, Level level, String message,
Class<? extends Throwable> thrownClass, String thrownMessage) {
+ assertEquals(JavaLogger.LEVELS_MAP.get(level), logRecord.getLevel());
+ assertEquals(message, logRecord.getMessage());
+
+ if (thrownClass != null && thrownMessage != null) {
+ assertTrue(thrownClass.isInstance(logRecord.getThrown()));
+ assertEquals(thrownMessage, logRecord.getThrown().getMessage());
+ } else {
+ assertNull(logRecord.getThrown());
+ }
+ }
+
+ private void verifyEnabledMethods(Level loggerLevel) throws Exception {
+ String[] levels = {"Debug", "Info", "Warn",
"Error"};
+
+ for (Level level : Level.values()) {
+ boolean enabledValue = (Boolean)
Logger.class.getMethod(MessageFormat.format("is{0}Enabled",
levels[level.ordinal()])).
+ invoke(logger);
+
+ if (level.compareTo(loggerLevel) < 0) {
+ assertFalse(loggerLevel.toString(), enabledValue);
+ } else {
+ assertTrue(loggerLevel.toString(), enabledValue);
+ }
+ }
+ }
+
+ private void verifyLoggingLevels(Level loggerLevel) {
+ for (Level messageLevel : Level.values()) {
+ logger.log(messageLevel, "test");
+
+ if (messageLevel.compareTo(loggerLevel) < 0) {
+ assertTrue(publishedRecords.isEmpty());
+ } else {
+ assertTrue(publishedRecords.size() == 1);
+ publishedRecords.clear();
+ }
+ }
+ }
+
+ @Test
+ public void testLogging() throws Exception {
+ wrappedLogger.setLevel(java.util.logging.Level.ALL);
+
+ String[] levels = {"debug", "info", "warn",
"error"};
+
+ for (String levelName : levels) {
+ Logger.class.getMethod(levelName, CharSequence.class).invoke(logger,
MessageFormat.format(CHAR_SEQUENCE_PATTERN, levelName));
+ Logger.class.getMethod(levelName, CharSequence.class,
Throwable.class).invoke(logger,
+ MessageFormat.format(CHAR_SEQUENCE_THROWABLE_PATTERN, levelName), new
NullPointerException(levelName));
+
+ Logger.class.getMethod(levelName, Enum.class, Object[].class).invoke(logger,
+ LoggerTestMessages.TEST_MESSAGE, new Object[] {levelName,
DUMMY_MESSAGE});
+
+ Logger.class.getMethod(levelName, Throwable.class).invoke(logger, new
IllegalArgumentException(levelName));
+
+ Logger.class.getMethod(levelName, Throwable.class, Enum.class,
Object[].class).invoke(logger,
+ new UnsupportedOperationException(levelName),
LoggerTestMessages.TEST_MESSAGE, new Object[] {levelName,
+ ANOTHER_DUMMY_MESSAGE});
+ }
+
+ Iterator<LogRecord> iterator = publishedRecords.iterator();
+
+ for (Level level: Level.values()) {
+ String levelName = levels[level.ordinal()];
+
+ verifyLogRecord(iterator.next(), level,
MessageFormat.format(CHAR_SEQUENCE_PATTERN, levelName), null, null);
+ verifyLogRecord(iterator.next(), level,
MessageFormat.format(CHAR_SEQUENCE_THROWABLE_PATTERN, levelName),
+ NullPointerException.class, levelName);
+
+ verifyLogRecord(iterator.next(), level,
MessageFormat.format(TEST_MESSAGE_PATTERN,
+ levelName, DUMMY_MESSAGE), null, null);
+
+ verifyLogRecord(iterator.next(), level, null, IllegalArgumentException.class,
levelName);
+
+ verifyLogRecord(iterator.next(), level,
MessageFormat.format(TEST_MESSAGE_PATTERN,
+ levelName, ANOTHER_DUMMY_MESSAGE), UnsupportedOperationException.class,
levelName);
+
+ }
+ }
+
+ @Test
+ public void testLevels() throws Exception {
+ for (Level loggerLevel : Level.values()) {
+ wrappedLogger.setLevel(JavaLogger.LEVELS_MAP.get(loggerLevel));
+
+ verifyEnabledMethods(loggerLevel);
+ verifyLoggingLevels(loggerLevel);
+ }
+ }
+
+}
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/LoggerTestMessages.java
===================================================================
---
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/LoggerTestMessages.java
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/java/org/richfaces/log/LoggerTestMessages.java 2010-08-30
16:51:33 UTC (rev 19022)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.log;
+
+import org.richfaces.l10n.MessageBundle;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+@MessageBundle(baseName = "org.richfaces.log.Resources")
+public enum LoggerTestMessages {
+
+ TEST_MESSAGE("org.richfaces.TestMessage");
+
+
+ private String s;
+
+ private LoggerTestMessages(String s) {
+ this.s = s;
+ }
+
+ @Override
+ public String toString() {
+ return s;
+ }
+}
Added:
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/log/Resources.properties
===================================================================
---
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/log/Resources.properties
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/log/Resources.properties 2010-08-30
16:51:33 UTC (rev 19022)
@@ -0,0 +1 @@
+org.richfaces.TestMessage=RF-000000 Test message with arguments: {0} and {1}
\ No newline at end of file