You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2011/12/22 21:25:49 UTC

svn commit: r1222426 - in /incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core: i18n/Messages.java system/ManifoldCFResourceLoader.java

Author: kwright
Date: Thu Dec 22 20:25:49 2011
New Revision: 1222426

URL: http://svn.apache.org/viewvc?rev=1222426&view=rev
Log:
Add classloader support to Message class

Modified:
    incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/i18n/Messages.java
    incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCFResourceLoader.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=1222426&r1=1222425&r2=1222426&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 Thu Dec 22 20:25:49 2011
@@ -23,10 +23,12 @@ import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
+import org.apache.manifoldcf.core.interfaces.*;
+import org.apache.manifoldcf.core.system.Logging;
 
 public final class Messages
 {
-public static final String DEFAULT_BUNDLE_NAME="common";
+  public static final String DEFAULT_BUNDLE_NAME="org.apache.manifoldcf.core.i18n.common";
 
   /** Constructor - do no instantiate
   */
@@ -34,6 +36,9 @@ public static final String DEFAULT_BUNDL
   {
   }
   
+  // These four have limited applicability since they are all local to the core jar, which generally does not render
+  // text.
+  
   public static String getString(String messageKey)
   {
     return getString(DEFAULT_BUNDLE_NAME, Locale.getDefault(), messageKey, null);
@@ -54,50 +59,87 @@ public static final String DEFAULT_BUNDL
     return getString(DEFAULT_BUNDLE_NAME, locale, messageKey, args);
   }
   
+  // More general methods which allow bundlenames and class loaders to be specified.
+  
   public static String getString(String bundleName, String messageKey)
   {
     return getString(bundleName, Locale.getDefault(), messageKey, null);
   }
+
+  public static String getString(ClassLoader classLoader, String bundleName, String messageKey)
+  {
+    return getString(classLoader, bundleName, Locale.getDefault(), messageKey, null);
+  }
   
   public static String getString(String bundleName, String messageKey, Object[] args)
   {
     return getString(bundleName, Locale.getDefault(), messageKey, args);
   }
+
+  public static String getString(ClassLoader classLoader, String bundleName, String messageKey, Object[] args)
+  {
+    return getString(classLoader, bundleName, Locale.getDefault(), messageKey, args);
+  }
   
   public static String getString(String bundleName, Locale locale, String messageKey)
   {
     return getString(bundleName, locale, messageKey, null);
   }
+
+  public static String getString(ClassLoader classLoader, String bundleName, Locale locale, String messageKey)
+  {
+    return getString(classLoader, bundleName, locale, messageKey, null);
+  }
   
   public static String getString(String bundleName, Locale locale, String messageKey, Object[] args)
   {
-    ResourceBundle resources = null;
-    String message = null;
-    String formatMessage = null;
+    return getString(Messages.class.getClassLoader(), bundleName, locale, messageKey, args);
+  }
+  
+  public static String getString(ClassLoader classLoader, String bundleName, Locale locale,
+    String messageKey, Object[] args)
+  {
+    ResourceBundle resources;
+    try
+    {
+      resources = ResourceBundle.getBundle(bundleName, locale, classLoader);
+    }
+    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()+"'");
+      resources = ResourceBundle.getBundle(bundleName, Locale.US, classLoader);
+    }
+    if (resources == null)
+    {
+      Logging.misc.fatal("No US bundle found!");
+      return messageKey;
+    }
+    
+    String message;
     try
     {
-      resources = getBundle(bundleName, locale);
       message = resources.getString(messageKey);
-      if (args != null)
-       {
-         formatMessage = MessageFormat.format(message, args);
-       }
-       else
-       {
-         formatMessage = message;
-       }
-     }
-     catch (MissingResourceException e)
-     {
-       System.out.println("Missing resource bundle:" + messageKey);
-     }
-     return formatMessage;
-   }
-   
-   protected static ResourceBundle getBundle(String bundleName, Locale locale)
-   {
-     String path = "org.apache.manifoldcf.core.i18n." + bundleName;
-     return ResourceBundle.getBundle(path, locale);   
     }
+    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()+"'");
+      message = messageKey;
+    }
+
+    // Format the message
+    String formatMessage;
+    if (args != null)
+    {
+      formatMessage = MessageFormat.format(message, args);
+    }
+    else
+    {
+      formatMessage = message;
+    }
+    return formatMessage;
+  }
+   
 }
 

Modified: incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCFResourceLoader.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCFResourceLoader.java?rev=1222426&r1=1222425&r2=1222426&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCFResourceLoader.java (original)
+++ incubator/lcf/branches/CONNECTORS-335/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCFResourceLoader.java Thu Dec 22 20:25:49 2011
@@ -138,7 +138,7 @@ public class ManifoldCFResourceLoader
     }
     return classLoader;
   }
-
+  
   /** Get the specified class using the proper classloader.
   *@param cname is the fully-qualified class name.
   */