[jbossws-commits] JBossWS SVN: r14593 - in api/trunk: src/main/java/org/jboss/ws/api/annotation and 7 other directories.

jbossws-commits at lists.jboss.org jbossws-commits at lists.jboss.org
Tue Jun 21 02:13:28 EDT 2011


Author: jim.ma
Date: 2011-06-21 02:13:28 -0400 (Tue, 21 Jun 2011)
New Revision: 14593

Added:
   api/trunk/src/main/java/org/jboss/ws/api/annotation/Message.properties
   api/trunk/src/main/java/org/jboss/ws/api/util/BundleUtils.java
   api/trunk/src/main/java/org/jboss/ws/api/util/Message.properties
   api/trunk/src/test/java/org/
   api/trunk/src/test/java/org/jboss/
   api/trunk/src/test/java/org/jboss/ws/
   api/trunk/src/test/java/org/jboss/ws/api/
   api/trunk/src/test/java/org/jboss/ws/api/util/
   api/trunk/src/test/java/org/jboss/ws/api/util/BundleUtilsTest.java
   api/trunk/src/test/java/org/jboss/ws/api/util/Message_fr.properties
Modified:
   api/trunk/pom.xml
   api/trunk/src/main/java/org/jboss/ws/api/annotation/AuthMethod.java
   api/trunk/src/main/java/org/jboss/ws/api/util/DOMUtils.java
Log:
[JBWS-3316]:i18n logging and message

Modified: api/trunk/pom.xml
===================================================================
--- api/trunk/pom.xml	2011-06-20 22:52:35 UTC (rev 14592)
+++ api/trunk/pom.xml	2011-06-21 06:13:28 UTC (rev 14593)
@@ -25,6 +25,7 @@
   
   <properties>
     <jboss-logging-spi.version>2.0.5.GA</jboss-logging-spi.version>
+    <junit.version>3.8.2</junit.version>
   </properties>
 
   <!-- Dependencies -->
@@ -37,10 +38,37 @@
       <version>${jboss-logging-spi.version}</version>
       <scope>provided</scope>
     </dependency>
+    
+    <!-- test dependencies -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>${junit.version}</version>
+      <scope>test</scope>
+    </dependency>
+    
   </dependencies>
 
   <!-- Plugins -->
   <build>
+    <resources>
+      <resource>
+        <directory>src/main/java</directory>
+         <includes>
+          <include>**/*.properties</include>
+         </includes>
+      </resource>
+    </resources>
+
+    <testResources>
+      <testResource>
+        <directory>src/test/java</directory>
+        <includes>
+           <include>**</include>
+        </includes>
+      </testResource>
+    </testResources>
+
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>

Modified: api/trunk/src/main/java/org/jboss/ws/api/annotation/AuthMethod.java
===================================================================
--- api/trunk/src/main/java/org/jboss/ws/api/annotation/AuthMethod.java	2011-06-20 22:52:35 UTC (rev 14592)
+++ api/trunk/src/main/java/org/jboss/ws/api/annotation/AuthMethod.java	2011-06-21 06:13:28 UTC (rev 14593)
@@ -21,7 +21,10 @@
  */
 package org.jboss.ws.api.annotation;
 
+import java.util.ResourceBundle;
+
 import org.jboss.logging.Logger;
+import org.jboss.ws.api.util.BundleUtils;
 
 /**
  * The authMethod is used to configure the authentication mechanism for the web service. 
@@ -33,6 +36,7 @@
 public final class AuthMethod
 {
    private static final Logger log = Logger.getLogger(AuthMethod.class);
+   private static final ResourceBundle bundle = BundleUtils.getBundle(AuthMethod.class);
    
    /**
     * Basic authentication.
@@ -73,11 +77,11 @@
          {
             return AuthMethod.CLIENT_CERT;
          }
-         log.warn("Non-standard auth method value: " + s);
+         log.warn(BundleUtils.getMessage(bundle, "NON_STANDARD_METHOD", s));
          return s;         
       }
       
-      throw new IllegalArgumentException("Illegal auth method value: " + s);
+      throw new IllegalArgumentException(BundleUtils.getMessage(bundle, "ILLEGAL_AUTH_METHOD", s));
    }
    
 }

Added: api/trunk/src/main/java/org/jboss/ws/api/annotation/Message.properties
===================================================================
--- api/trunk/src/main/java/org/jboss/ws/api/annotation/Message.properties	                        (rev 0)
+++ api/trunk/src/main/java/org/jboss/ws/api/annotation/Message.properties	2011-06-21 06:13:28 UTC (rev 14593)
@@ -0,0 +1,2 @@
+NON_STANDARD_METHOD=Non-standard auth method value : {0}
+ILLEGAL_AUTH_METHOD= Illegal auth method : {0}
\ No newline at end of file

Added: api/trunk/src/main/java/org/jboss/ws/api/util/BundleUtils.java
===================================================================
--- api/trunk/src/main/java/org/jboss/ws/api/util/BundleUtils.java	                        (rev 0)
+++ api/trunk/src/main/java/org/jboss/ws/api/util/BundleUtils.java	2011-06-21 06:13:28 UTC (rev 14593)
@@ -0,0 +1,82 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.jboss.ws.api.util;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+/**
+ * Utility class to get resource bundle and i18n message
+ * @author <a href="mailto:ema at redhat.com">Jim Ma</a>
+ * 
+ */
+public final class BundleUtils {
+   private static final String BUNDLE_FILE_NAME = ".Message";
+   
+   private BundleUtils() {
+   }
+
+   /**Get resource bundle with a class
+    * @param cls the class to find the resource bundle
+    * @return ResourceBundle if exists, otherwise null
+    */
+   public static ResourceBundle getBundle(Class<?> cls)
+   {
+      String bundleName = cls.getPackage().getName() + BUNDLE_FILE_NAME;
+      try
+      {
+         return ResourceBundle.getBundle(bundleName, Locale.getDefault(), cls.getClassLoader());
+      }
+      catch (MissingResourceException ex)
+      {
+         return ResourceBundle.getBundle(bundleName, Locale.getDefault(), Thread.currentThread()
+               .getContextClassLoader());
+
+      }
+   }
+
+   /**
+    * Get the i18n string message with an associated resource bundle
+    * @param bundle resourceBundle
+    * @param key    the key for the desired string
+    * @param params the message substitution parameters
+    * @return the formated string for the given key
+    */
+   public static String getMessage(ResourceBundle bundle, String key, Object... params)
+   {
+      String fmt = null;
+      try
+      {
+         if (null == bundle)
+         {
+            return key;
+         }
+         fmt = bundle.getString(key);
+      }
+      catch (MissingResourceException ex)
+      {
+         return key;
+      }
+      return MessageFormat.format(fmt, params);
+   }
+}

Modified: api/trunk/src/main/java/org/jboss/ws/api/util/DOMUtils.java
===================================================================
--- api/trunk/src/main/java/org/jboss/ws/api/util/DOMUtils.java	2011-06-20 22:52:35 UTC (rev 14592)
+++ api/trunk/src/main/java/org/jboss/ws/api/util/DOMUtils.java	2011-06-21 06:13:28 UTC (rev 14593)
@@ -31,6 +31,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.ResourceBundle;
 
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
@@ -64,6 +65,7 @@
 public abstract class DOMUtils
 {
    private static Logger log = Logger.getLogger(DOMUtils.class);
+   private static ResourceBundle bundle = BundleUtils.getBundle(DOMUtils.class);
 
    /** Get the qname of the given node.
     */
@@ -567,6 +569,7 @@
       }
       catch (IOException e)
       {
+         log.error(BundleUtils.getMessage(bundle, "CAN_NOT_PARSE", xmlString));
          log.error("Cannot parse: " + xmlString);
          throw e;
       }

Added: api/trunk/src/main/java/org/jboss/ws/api/util/Message.properties
===================================================================
--- api/trunk/src/main/java/org/jboss/ws/api/util/Message.properties	                        (rev 0)
+++ api/trunk/src/main/java/org/jboss/ws/api/util/Message.properties	2011-06-21 06:13:28 UTC (rev 14593)
@@ -0,0 +1 @@
+CAN_NOT_PARSE = Cannot parse: {0}
\ No newline at end of file

Added: api/trunk/src/test/java/org/jboss/ws/api/util/BundleUtilsTest.java
===================================================================
--- api/trunk/src/test/java/org/jboss/ws/api/util/BundleUtilsTest.java	                        (rev 0)
+++ api/trunk/src/test/java/org/jboss/ws/api/util/BundleUtilsTest.java	2011-06-21 06:13:28 UTC (rev 14593)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.jboss.ws.api.util;
+
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+public class BundleUtilsTest extends TestCase
+{
+   public void testI18NMessage() throws Exception
+   {
+      String message = BundleUtils.getMessage(BundleUtils.getBundle(this.getClass()), "CAN_NOT_PARSE", "file");
+      assertTrue("English error message is expected", message.indexOf("Cannot parse") > -1);
+      Locale.setDefault(java.util.Locale.FRANCE);
+      message = BundleUtils.getMessage(BundleUtils.getBundle(this.getClass()), "CAN_NOT_PARSE", "file");
+      assertTrue("French error message is expected", message.contains("ne peut pas analyser"));
+   }
+}

Added: api/trunk/src/test/java/org/jboss/ws/api/util/Message_fr.properties
===================================================================
--- api/trunk/src/test/java/org/jboss/ws/api/util/Message_fr.properties	                        (rev 0)
+++ api/trunk/src/test/java/org/jboss/ws/api/util/Message_fr.properties	2011-06-21 06:13:28 UTC (rev 14593)
@@ -0,0 +1 @@
+CAN_NOT_PARSE = ne peut pas analyser: {0}
\ No newline at end of file



More information about the jbossws-commits mailing list