Author: jboynes
Date: Sat Apr  8 16:01:03 2006
New Revision: 392626

URL: http://svn.apache.org/viewcvs?rev=392626&view=rev
Log:
simplify I18N by searching for resource bundles in all parent packages

Added:
    
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
   (with props)
Removed:
    incubator/tuscany/java/sca/common/src/main/resources/org/
Modified:
    
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
    
incubator/tuscany/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java

Modified: 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java?rev=392626&r1=392625&r2=392626&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
 (original)
+++ 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
 Sat Apr  8 16:01:03 2006
@@ -24,8 +24,12 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.WeakHashMap;
+import java.util.ResourceBundle;
+import java.util.Locale;
+import java.util.MissingResourceException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.logging.LogRecord;
 
 import org.apache.tuscany.common.monitor.LogLevel;
 import org.apache.tuscany.common.monitor.MonitorFactory;
@@ -88,7 +92,7 @@
 
     private <T>T createMonitor(Class<T> monitorInterface, String bundleName) {
         String className = monitorInterface.getName();
-        Logger logger = Logger.getLogger(className, bundleName);
+        Logger logger = Logger.getLogger(className);
         Method[] methods = monitorInterface.getMethods();
         Map<String, Level> levels = new HashMap<String, Level>(methods.length);
         for (Method method : methods) {
@@ -112,17 +116,44 @@
             }
             levels.put(method.getName(), level);
         }
-        InvocationHandler handler = new LoggingHandler(logger, levels);
+
+        ResourceBundle bundle = locateBundle(monitorInterface, bundleName);
+
+        InvocationHandler handler = new LoggingHandler(logger, levels, bundle);
         return 
monitorInterface.cast(Proxy.newProxyInstance(monitorInterface.getClassLoader(), 
new Class<?>[]{monitorInterface}, handler));
     }
 
+    private static <T>ResourceBundle locateBundle(Class<T> monitorInterface, 
String bundleName) {
+        Locale locale = Locale.getDefault();
+        ClassLoader cl = monitorInterface.getClassLoader();
+        String packageName = monitorInterface.getPackage().getName();
+        while (true) {
+            try {
+                return ResourceBundle.getBundle(packageName + '.' + 
bundleName, locale, cl);
+            } catch (MissingResourceException e) {
+            }
+            int index = packageName.lastIndexOf('.');
+            if (index == -1) {
+                break;
+            }
+            packageName = packageName.substring(0, index);
+        }
+        try {
+            return ResourceBundle.getBundle(bundleName, locale, cl);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
     private static final class LoggingHandler implements InvocationHandler {
         private final Logger logger;
         private final Map<String, Level> methodLevels;
+        private final ResourceBundle bundle;
 
-        public LoggingHandler(Logger logger, Map<String, Level> methodLevels) {
+        public LoggingHandler(Logger logger, Map<String, Level> methodLevels, 
ResourceBundle bundle) {
             this.logger = logger;
             this.methodLevels = methodLevels;
+            this.bundle = bundle;
         }
 
         public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
@@ -133,12 +164,21 @@
                 String className = logger.getName();
                 String key = className + '#' + sourceMethod;
 
-                // if the only argument is a Throwable use the special logger 
for it
-                if (args != null && args.length == 1 && args[0] instanceof 
Throwable) {
-                    logger.logp(level, className, sourceMethod, key, 
(Throwable) args[0]);
-                } else {
-                    logger.logp(level, className, sourceMethod, key, args);
+                LogRecord logRecord = new LogRecord(level, key);
+                logRecord.setLoggerName(className);
+                logRecord.setSourceClassName(className);
+                logRecord.setSourceMethodName(sourceMethod);
+                logRecord.setParameters(args);
+                if (args != null) {
+                    for (Object o : args) {
+                        if (o instanceof Throwable) {
+                            logRecord.setThrown((Throwable) o);
+                            break;
+                        }
+                    }
                 }
+                logRecord.setResourceBundle(bundle);
+                logger.log(logRecord);
             }
             return null;
         }

Modified: 
incubator/tuscany/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java?rev=392626&r1=392625&r2=392626&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java
 Sat Apr  8 16:01:03 2006
@@ -107,7 +107,6 @@
         assertEquals(1, handler.logs.size());
         LogRecord record = handler.logs.get(0);
         assertEquals(Monitor.class.getName() + "#eventWithOneArg", 
record.getMessage());
-        assertEquals(Monitor.class.getName(), record.getResourceBundleName());
     }
 
     protected void setUp() throws Exception {
@@ -121,7 +120,7 @@
         levels.setProperty(sourceClass + "#eventWithNoArgs", "INFO");
         levels.setProperty(sourceClass + "#eventWithOneArg", "INFO");
         levels.setProperty(sourceClass + "#eventWithThrowable", "WARNING");
-        factory = new JavaLoggingMonitorFactory(levels, Level.FINE, 
sourceClass);
+        factory = new JavaLoggingMonitorFactory(levels, Level.FINE, 
"TestMessages");
     }
 
     protected void tearDown() throws Exception {

Added: 
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties?rev=392626&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
 (added)
+++ 
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
 Sat Apr  8 16:01:03 2006
@@ -0,0 +1,16 @@
+#  Copyright (c) 2006 The Apache Software Foundation or its licensors, as 
applicable.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+#
+#  $Rev$ $Date$
+#

Propchange: 
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/java/sca/common/src/test/resources/org/apache/tuscany/common/TestMessages.properties
------------------------------------------------------------------------------
    svn:keywords = Rev,Date


Reply via email to