cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r629531 - in /incubator/cxf/trunk/common/common/src: main/java/org/apache/cxf/common/i18n/ main/java/org/apache/cxf/common/logging/ test/java/org/apache/cxf/common/i18n/ test/java/org/apache/cxf/common/logging/
Date Wed, 20 Feb 2008 16:49:34 GMT
Author: dkulp
Date: Wed Feb 20 08:49:30 2008
New Revision: 629531

URL: http://svn.apache.org/viewvc?rev=629531&view=rev
Log:
[CXF-1432, CXF-1420] Make Message objects serializable, fix issues if message cannot be formatted,
just log as is.

Modified:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/i18n/Message.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
    incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/i18n/MessageTest.java
    incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/logging/LogUtilsTest.java

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/i18n/Message.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/i18n/Message.java?rev=629531&r1=629530&r2=629531&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/i18n/Message.java
(original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/i18n/Message.java
Wed Feb 20 08:49:30 2008
@@ -19,16 +19,20 @@
 
 package org.apache.cxf.common.i18n;
 
+import java.io.IOException;
+import java.io.Serializable;
 import java.text.MessageFormat;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.logging.Logger;
 
-public class Message {
-    String code;
-    Object[] parameters;
-    ResourceBundle bundle;
+public class Message implements Serializable {
+    private static final long serialVersionUID = 42L;
     
+    transient String code;
+    transient Object[] parameters;
+    transient ResourceBundle bundle;
+
     /**
      * Constructor.
      *
@@ -72,5 +76,16 @@
     
     public Object[] getParameters() {
         return parameters;
+    }
+
+    private void writeObject(java.io.ObjectOutputStream out)
+        throws IOException {
+        out.writeUTF(toString());
+    }
+    private void readObject(java.io.ObjectInputStream in)
+        throws IOException, ClassNotFoundException {
+        code = in.readUTF();
+        bundle = null;
+        parameters = null;
     }
 }

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java?rev=629531&r1=629530&r2=629531&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
(original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
Wed Feb 20 08:49:30 2008
@@ -252,12 +252,7 @@
     public static void log(Logger logger, 
                            Level level, 
                            String message) {
-        if (logger.isLoggable(level)) {
-            final String formattedMessage = 
-                MessageFormat.format(localize(logger, message), NO_PARAMETERS);
-            doLog(logger, level, formattedMessage, null);
-        }
-        
+        log(logger, level, message, NO_PARAMETERS);        
     }  
     
     /**
@@ -303,11 +298,14 @@
                            String message, 
                            Object[] parameters) {
         if (logger.isLoggable(level)) {
-            final String formattedMessage = 
-                MessageFormat.format(localize(logger, message), parameters);
-            doLog(logger, level, formattedMessage, null);
-        }
-        
+            String msg = localize(logger, message);
+            try {
+                msg = MessageFormat.format(msg, parameters);
+            } catch (IllegalArgumentException ex) {
+                //ignore, log as is
+            }
+            doLog(logger, level, msg, null);
+        }        
     }
 
     private static void doLog(Logger log, Level level, String msg, Throwable t) {
@@ -345,7 +343,12 @@
      */
     private static String localize(Logger logger, String message) {
         ResourceBundle bundle = logger.getResourceBundle();
-        return bundle != null ? bundle.getString(message) : message;
+        try {
+            return bundle != null ? bundle.getString(message) : message;
+        } catch (MissingResourceException ex) {
+            //string not in the bundle
+            return message;
+        }
     }
 
 }

Modified: incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/i18n/MessageTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/i18n/MessageTest.java?rev=629531&r1=629530&r2=629531&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/i18n/MessageTest.java
(original)
+++ incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/i18n/MessageTest.java
Wed Feb 20 08:49:30 2008
@@ -19,6 +19,10 @@
 
 package org.apache.cxf.common.i18n;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ResourceBundle;
 import java.util.logging.Logger;
 
@@ -49,5 +53,24 @@
         assertEquals("unexpected message string", 
                      "subbed in 4 & 3",
                      msg.toString()); 
+    }
+    
+    @Test
+    public void testExceptionIO() throws java.lang.Exception {
+        ResourceBundle bundle = BundleUtils.getBundle(getClass());
+        UncheckedException ex = new UncheckedException(new Message("SUB2_EXC",
+                                                                   bundle,
+                                                                   new Object[] {3, 4}));
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream(bout);
+        out.writeObject(ex);
+        
+        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+        ObjectInputStream in = new ObjectInputStream(bin);
+        Object o = in.readObject();
+        assertTrue(o instanceof UncheckedException);
+        UncheckedException ex2 = (UncheckedException)o;
+        assertEquals("subbed in 4 & 3", ex2.getMessage());
+        
     }
 }

Modified: incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/logging/LogUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/logging/LogUtilsTest.java?rev=629531&r1=629530&r2=629531&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/logging/LogUtilsTest.java
(original)
+++ incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/logging/LogUtilsTest.java
Wed Feb 20 08:49:30 2008
@@ -125,7 +125,10 @@
         EasyMock.verify(handler);
         LOG.removeHandler(handler);
     }
-    
+    @Test
+    public void testCXF1420() throws Exception {
+        LogUtils.log(LOG, Level.SEVERE, "SQLException for SQL [{call FOO.ping(?, ?)}]");
+    }    
     @Test
     public void testClassMethodNames() throws Exception {
         TestLogHandler handler = new TestLogHandler();



Mime
View raw message