incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1222613 - /incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java
Date Fri, 23 Dec 2011 09:04:28 GMT
Author: kwright
Date: Fri Dec 23 09:04:28 2011
New Revision: 1222613

URL: http://svn.apache.org/viewvc?rev=1222613&view=rev
Log:
Make i18n support resilient about when it is called, and make it less noisy, so people can
still use ManifoldCF even if no current-locale translation exists.

Modified:
    incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java

Modified: incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java?rev=1222613&r1=1222612&r2=1222613&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java
(original)
+++ incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java
Fri Dec 23 09:04:28 2011
@@ -22,14 +22,20 @@ import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.HashSet;
 
-import org.apache.manifoldcf.core.interfaces.*;
 import org.apache.manifoldcf.core.system.Logging;
 
 public class Messages
 {
   public static final String DEFAULT_BUNDLE_NAME="org.apache.manifoldcf.core.i18n.common";
 
+  // Keep track of messages and bundles we've already complained about.
+  
+  protected static Set<BundleKey> bundleSet = new HashSet<BundleKey>();
+  protected static Set<MessageKey> messageSet = new HashSet<MessageKey>();
+  
   /** Constructor - do no instantiate
   */
   public Messages()
@@ -107,14 +113,15 @@ public class Messages
     catch (MissingResourceException e)
     {
       // Use English if we don't have a bundle for the current locale
-      Logging.misc.warn("Missing resource bundle '" + bundleName + "' for locale '"+locale.toString()+"':
"+e.getMessage());
+      complainMissingBundle("Missing resource bundle '" + bundleName + "' for locale '"+locale.toString()+"':
"+e.getMessage()+"; trying en_US",
+        e,bundleName,locale);
       try
       {
         resources = ResourceBundle.getBundle(bundleName, Locale.US, classLoader);
       }
       catch (MissingResourceException e2)
       {
-        Logging.misc.fatal("No US bundle found either!");
+        complainMissingBundle("No backup US bundle found either! "+e.getMessage(),e,bundleName,Locale.US);
         return messageKey;
       }
     }
@@ -126,8 +133,8 @@ public class Messages
     }
     catch (MissingResourceException e)
     {
-      // Use English if we don't have a bundle for the current locale
-      Logging.misc.error("Missing resource '" + messageKey + "' in bundle '" + bundleName
+ "' for locale '"+locale.toString()+"'");
+      complainMissingMessage("Missing resource '" + messageKey + "' in bundle '" + bundleName
+ "' for locale '"+locale.toString()+"'",
+        e,bundleName,locale,messageKey);
       message = messageKey;
     }
 
@@ -143,6 +150,99 @@ public class Messages
     }
     return formatMessage;
   }
-   
+
+  protected static void complainMissingBundle(String errorMessage, Throwable exception, String
bundleName, Locale locale)
+  {
+    String localeName = locale.toString();
+    BundleKey bk = new BundleKey(bundleName,localeName);
+    synchronized (bundleSet)
+    {
+      if (bundleSet.contains(bk))
+        return;
+      bundleSet.add(bk);
+    }
+    logError(errorMessage,exception);
+  }
+  
+  protected static void complainMissingMessage(String errorMessage, Throwable exception,
String bundleName, Locale locale, String messageKey)
+  {
+    String localeName = locale.toString();
+    MessageKey bk = new MessageKey(bundleName,localeName,messageKey);
+    synchronized (messageSet)
+    {
+      if (messageSet.contains(bk))
+        return;
+      messageSet.add(bk);
+    }
+    logError(errorMessage,exception);
+  }
+
+  protected static void logError(String errorMessage, Throwable exception)
+  {
+    if (Logging.misc == null)
+    {
+      System.err.println(errorMessage);
+      exception.printStackTrace(System.err);
+    }
+    else
+      Logging.misc.error(errorMessage,exception);
+  }
+
+  /** Class to help keep track of the missing resource bundles we've already complained about,
+  * so we don't fill up the standard out log with repetitive stuff. */
+  protected static class BundleKey
+  {
+    protected String bundleName;
+    protected String localeName;
+    
+    public BundleKey(String bundleName, String localeName)
+    {
+      this.bundleName = bundleName;
+      this.localeName = localeName;
+    }
+    
+    public int hashCode()
+    {
+      return bundleName.hashCode() + localeName.hashCode();
+    }
+    
+    public boolean equals(Object o)
+    {
+      if (!(o instanceof BundleKey))
+        return false;
+      BundleKey b = (BundleKey)o;
+      return b.bundleName.equals(bundleName) && b.localeName.equals(localeName);
+    }
+  }
+
+  /** Class to help keep track of the missing messages we've already complained about,
+  * so we don't fill up the standard out log with repetitive stuff. */
+  protected static class MessageKey
+  {
+    protected String bundleName;
+    protected String localeName;
+    protected String messageKey;
+    
+    public MessageKey(String bundleName, String localeName, String messageKey)
+    {
+      this.bundleName = bundleName;
+      this.localeName = localeName;
+      this.messageKey = messageKey;
+    }
+    
+    public int hashCode()
+    {
+      return bundleName.hashCode() + localeName.hashCode() + messageKey.hashCode();
+    }
+    
+    public boolean equals(Object o)
+    {
+      if (!(o instanceof MessageKey))
+        return false;
+      MessageKey b = (MessageKey)o;
+      return b.bundleName.equals(bundleName) && b.localeName.equals(localeName) &&
b.messageKey.equals(messageKey);
+    }
+  }
+
 }
 



Mime
View raw message