/hg/icedtea-web: Moved translator responsibility from JNLPRuntim...

jkang at icedtea.classpath.org jkang at icedtea.classpath.org
Mon Sep 15 15:48:02 UTC 2014


changeset 1e274216ee4c in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=1e274216ee4c
author: Jie Kang <jkang at redhat.com>
date: Mon Sep 15 11:42:08 2014 -0400

	Moved translator responsibility from JNLPRuntime to Translator

	2014-09-15  Jie Kang  <jkang at redhat.com>

	    * netx/net/sourceforge/jnlp/runtime/Translator.java:
	    * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java:
	    (getMessage): moved from JNLPRuntime to Translator
	    * netx/net/sourceforge/jnlp/runtime/TranslatorTest.java:
	    added tests for translating using a ResourceBundle


diffstat:

 ChangeLog                                                        |  10 +
 netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java               |  61 +-------
 netx/net/sourceforge/jnlp/runtime/Translator.java                |  72 +++++++++-
 tests/netx/unit/net/sourceforge/jnlp/runtime/TranslatorTest.java |  60 ++++++++
 4 files changed, 142 insertions(+), 61 deletions(-)

diffs (316 lines):

diff -r 11ebb7204bdc -r 1e274216ee4c ChangeLog
--- a/ChangeLog	Mon Sep 15 10:10:44 2014 -0400
+++ b/ChangeLog	Mon Sep 15 11:42:08 2014 -0400
@@ -1,3 +1,13 @@
+2014-09-15  Jie Kang  <jkang at redhat.com>
+
+    Moved translator responsibility from JNLPRuntime to Translator
+    * netx/net/sourceforge/jnlp/runtime/Translator.java:
+    * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java:
+    (getMessage): moved from JNLPRuntime to Translator
+    * netx/net/sourceforge/jnlp/runtime/TranslatorTest.java:
+    added tests for translating using a ResourceBundle
+
+
 2014-09-15  Jie Kang  <jkang at redhat.com>
 
     * netx/net/sourceforge/jnlp/cache/CacheLRUWrapper.java
diff -r 11ebb7204bdc -r 1e274216ee4c netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java
--- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Mon Sep 15 10:10:44 2014 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Mon Sep 15 11:42:08 2014 -0400
@@ -16,6 +16,8 @@
 
 package net.sourceforge.jnlp.runtime;
 
+import static net.sourceforge.jnlp.runtime.Translator.R;
+
 import java.awt.EventQueue;
 import java.io.File;
 import java.io.FileInputStream;
@@ -35,10 +37,8 @@
 import java.security.Policy;
 import java.security.Security;
 import java.text.DateFormat;
-import java.text.MessageFormat;
 import java.util.Date;
 import java.util.List;
-import java.util.ResourceBundle;
 
 import javax.jnlp.ServiceManager;
 import javax.naming.ConfigurationException;
@@ -67,8 +67,8 @@
 import net.sourceforge.jnlp.services.XServiceManagerStub;
 import net.sourceforge.jnlp.util.FileUtils;
 import net.sourceforge.jnlp.util.logging.JavaConsole;
+import net.sourceforge.jnlp.util.logging.LogConfig;
 import net.sourceforge.jnlp.util.logging.OutputController;
-import net.sourceforge.jnlp.util.logging.LogConfig;
 import sun.net.www.protocol.jar.URLJarFile;
 
 /**
@@ -92,10 +92,6 @@
  */
 public class JNLPRuntime {
 
-    static {
-        loadResources();
-    }
-
     /**
      * java-abrt-connector can print out specific application String method, it is good to save visited urls for reproduce purposes.
      * For javaws we can read the destination jnlp from commandline
@@ -104,9 +100,6 @@
      */
     private static String history = "";
 
-    /** the localized resource strings */
-    private static ResourceBundle resources;
-
     /** the security manager */
     private static JNLPSecurityManager security;
 
@@ -231,7 +224,7 @@
                 //where deployment.system.config points is not readable
                 throw new RuntimeException(getConfiguration().getLoadingException());
             }
-            OutputController.getLogger().log(OutputController.Level.WARNING_ALL, getMessage("RConfigurationError")+": "+getConfiguration().getLoadingException().getMessage());
+            OutputController.getLogger().log(OutputController.Level.WARNING_ALL, R("RConfigurationError")+": "+getConfiguration().getLoadingException().getMessage());
         }
         KeyStores.setConfiguration(getConfiguration());
 
@@ -453,16 +446,16 @@
                 config.load();
                 config.copyTo(System.getProperties());
             } catch (ConfigurationException ex) {
-                OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, Translator.R("RConfigurationError"));
+                OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, R("RConfigurationError"));
                 //mark this exceptionas we can die on it later
                 config.setLoadingException(ex);
                 //to be sure - we MUST die - http://docs.oracle.com/javase/6/docs/technotes/guides/deployment/deployment-guide/properties.html
             }catch(Exception t){
                 //all exceptions are causing InstantiatizationError so this do it much more readble
                 OutputController.getLogger().log(OutputController.Level.ERROR_ALL, t);
-                OutputController.getLogger().log(OutputController.Level.WARNING_ALL, Translator.R("RFailingToDefault"));
+                OutputController.getLogger().log(OutputController.Level.WARNING_ALL, R("RFailingToDefault"));
                 if (!JNLPRuntime.isHeadless()){
-                    JOptionPane.showMessageDialog(null, getMessage("RFailingToDefault")+"\n"+t.toString());
+                    JOptionPane.showMessageDialog(null, R("RFailingToDefault")+"\n"+t.toString());
                 }
                 //try to survive this unlikely exception
                 config.resetToDefaults();
@@ -678,40 +671,11 @@
         return indicator;
     }
 
-    /**
-     * Returns the localized resource string identified by the
-     * specified key. If the message is empty, a null is
-     * returned.
-     */
-    public static String getMessage(String key) {
-        try {
-            String result = resources.getString(key);
-            if (result.length() == 0)
-                return null;
-            else
-                return result;
-        } catch (Exception ex) {
-            if (!key.equals("RNoResource"))
-                return getMessage("RNoResource", new Object[] { key });
-            else
-                return "Missing resource: " + key;
-        }
-    }
-    
     public static String getLocalisedTimeStamp(Date timestamp) {
         return DateFormat.getInstance().format(timestamp);
     }
 
     /**
-     * Returns the localized resource string using the specified arguments.
-     *
-     * @param args the formatting arguments to the resource string
-     */
-    public static String getMessage(String key, Object... args) {
-        return MessageFormat.format(getMessage(key), args);
-    }
-
-    /**
      * Returns {@code true} if the current runtime will fork
      */
     public static boolean getForksAllowed() {
@@ -755,17 +719,6 @@
     }
 
     /**
-     * Load the resources.
-     */
-    private static void loadResources() {
-        try {
-            resources = ResourceBundle.getBundle("net.sourceforge.jnlp.resources.Messages");
-        } catch (Exception ex) {
-            throw new IllegalStateException("Missing resource bundle in netx.jar:net/sourceforge/jnlp/resource/Messages.properties");
-        }
-    }
-
-    /**
      * @return {@code true} if running on Windows
      */
     public static boolean isWindows() {
diff -r 11ebb7204bdc -r 1e274216ee4c netx/net/sourceforge/jnlp/runtime/Translator.java
--- a/netx/net/sourceforge/jnlp/runtime/Translator.java	Mon Sep 15 10:10:44 2014 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/Translator.java	Mon Sep 15 11:42:08 2014 -0400
@@ -16,16 +16,32 @@
 
 package net.sourceforge.jnlp.runtime;
 
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
 /**
  * Utility class to provide simple methods to help localize messages
  */
-public class Translator {
+public enum Translator {
 
-    /**
-     * @return the localized string for the message
-     */
-    public static String R(String message, Object... params) {
-        return JNLPRuntime.getMessage(message, params);
+    INSTANCE;
+
+    /** the localized resource strings */
+    private ResourceBundle resources;
+
+    private Translator() {
+        try {
+            resources = ResourceBundle.getBundle("net.sourceforge.jnlp.resources.Messages");
+        } catch (Exception ex) {
+            throw new IllegalStateException("No bundles found for Locale: " + Locale.getDefault().toString() +
+                    "and missing base resource bundle in netx.jar:net/sourceforge/jnlp/resource/Messages.properties");
+        }
+    }
+
+    public static Translator getInstance() {
+        return Translator.INSTANCE;
     }
 
     /**
@@ -34,7 +50,49 @@
      * @return a string representing the localized message
      */
     public static String R(String message) {
-        return JNLPRuntime.getMessage(message);
+        return R(message, new Object[0]);
     }
 
+    /**
+     * @return the localized string for the message
+     */
+    public static String R(String message, Object... params) {
+        return getInstance().getMessage(message, params);
+    }
+
+    protected void loadResourceBundle(ResourceBundle bundle) {
+        this.resources = bundle;
+    }
+
+    /**
+     * Returns the localized resource string using the specified arguments.
+     *
+     * @param args the formatting arguments to the resource string
+     */
+    private String getMessage(String key, Object... args) {
+        return MessageFormat.format(getMessage(key), args);
+    }
+
+    /**
+     * Returns the localized resource string identified by the
+     * specified key. If the message is empty, a null is
+     * returned.
+     */
+    private String getMessage(String key) {
+        try {
+            String result = resources.getString(key);
+            if (result.length() == 0)
+                return "";
+            else
+                return result;
+        } catch (NullPointerException e) {
+            return getMessage("RNoResource", new Object[]{key});
+        } catch (MissingResourceException | ClassCastException e) {
+            if (key == "RNoResource") {
+                return "No localized text found";
+            } else {
+                return getMessage("RNoResource", new Object[]{key});
+            }
+        }
+    }
 }
diff -r 11ebb7204bdc -r 1e274216ee4c tests/netx/unit/net/sourceforge/jnlp/runtime/TranslatorTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/TranslatorTest.java	Mon Sep 15 11:42:08 2014 -0400
@@ -0,0 +1,60 @@
+package net.sourceforge.jnlp.runtime;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class TranslatorTest {
+
+    @Before
+    public void setup() throws IOException {
+        File f = new File(System.getProperty("java.io.tmpdir"), "test.properties");
+        f.createNewFile();
+        f.deleteOnExit();
+
+        FileOutputStream fos = new FileOutputStream(f);
+        String message = "key=value\n"
+                + "argkey=value {0}\n"
+                + "RNoResource=no-resource\n";
+        fos.write(message.getBytes());
+
+        URL u = f.getParentFile().toURI().toURL();
+        ClassLoader loader = new URLClassLoader(new URL[] {u});
+
+        ResourceBundle bundle = ResourceBundle.getBundle("test", Locale.getDefault(), loader);
+        Translator.getInstance().loadResourceBundle(bundle);
+    }
+
+    @Test
+    public void testTranslateNonExistingMessage() {
+        String message = Translator.R("doesn't-exist");
+        assertEquals("no-resource", message);
+    }
+
+    @Test
+    public void testTranslateNullMessage() {
+        String message = Translator.R(null);
+        assertEquals("no-resource", message);
+    }
+
+    @Test
+    public void testTranslateMessage() {
+        String message = Translator.R("key");
+        assertEquals("value", message);
+    }
+
+    @Test
+    public void testTranslateMessageWithArgs() {
+        String message = Translator.R("argkey", new Object[] {"Hello"});
+        assertEquals("value Hello", message);
+    }
+}


More information about the distro-pkg-dev mailing list