⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagecache.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
字号:
/*
 * @author                        : Neelesh
 * @Version                       : 1.0
 *
 * Development Environment        : Oracle 9i JDeveloper
 * Name of the File               : MessageCache.java
 * Creation/Modification History  :
 *
 * Neelesh    03-March-2002      Created
 *
 */
package oracle.otnsamples.util;

import java.util.Hashtable;
import java.util.Locale;

// Java util
import java.util.Properties;
import java.util.StringTokenizer;

// VSM Util class
import oracle.otnsamples.util.Utilities;


/**
 * This class provides a set of methods to retrieve messages from resource
 * bundles. The class uses Misc.properties to load the set of resource bundles
 * and creates a table of resource bundles indexed by language. The set of
 * getMessage methods can be used to access these cached resource bundles. The
 * cache can be refreshed at run time by invoking the refresh() method.
 *
 * @author Neelesh
 * @version 2.0
 */
public final class MessageCache {

  // Table of resource bundles, stored against the language ids
  private static Hashtable allMessages = new Properties();

  /* The static block initialises the cache of resource bundles */
  static {

    try {
      refresh();
    } catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * The constructor is made private so that no other classes can instantiate
   * this class. Instances of this class are not needed , since all methods are
   * static final.
   */
  private MessageCache() {
  }

  /**
   * The method refreshes the resource bundles from disk. The method looks for
   * the names of resource bundles in Misc.properties file. The list of
   * resources to be loaded must be a space separated string, where each token
   * is the name of the resource without the extension.
   */
  public static synchronized void refresh() {

    try {

      // Load meta data
      Properties properties = Utilities.loadParams("Misc");

      // Find the resources to load using this meta data
      // The list is space separated, so extract each resource
      // name using a tokenizer
      StringTokenizer tokenizer =
        new StringTokenizer(
                            properties.getProperty("resource.files"), " ", false);

      Properties messages = null;
      String token        = null;
      String langID       = null;

      // load each resource in to the allMessages table against the language id.
      // The language id will be "en","ru","fr" etc. This is extracted using the
      // name of the resources. eg: Messages_en.
      // If there is no language associated,(default resource bundle),the language
      // is "default"
      while(tokenizer.hasMoreTokens()) {
        token = tokenizer.nextToken();

        int lastIndex = token.lastIndexOf("_");

        if(lastIndex >= 0 && lastIndex <= token.length()) {
          langID = token.substring(lastIndex + 1, token.length());
        } else {
          langID = "default";
        }

        // add to the list of messages, if a set of messages already exists
        // for a language
        if(allMessages.containsKey(langID)) {
          messages = (Properties) allMessages.get(langID);
        } else {
          messages = new Properties();
          allMessages.put(langID, messages);
        }

        messages.putAll(Utilities.loadParams(token));
      }
    } catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * This method returns a message for a given key and language id.
   *
   * @param <b>key</b> - The message key
   * @param <b>langID</b> - The language id
   *
   * @return <b>String</b> - The corresponding message or null, if not present
   */
  public static String getMessage(String key, String langID) {

    Properties messages =
      (Properties) allMessages.get(langID == null ? "default" : langID);

    if(messages != null) {

      return messages.getProperty(key);
    } else {
      messages = (Properties) allMessages.get("default");

      return messages.getProperty(key);
    }
  }
  /**
   * This method returns a message for a given key and the default language id.
   *
   * @param <b>key</b> - The message key
   *
   * @return <b>String</b> - The corresponding message
   */
  public static String getMessage(String key) {

    return getMessage(key, "default");
  }
  /**
   * This method returns a message for a given key and locale.
   *
   * @param <b>key</b> - The message key
   * @param <b>locale</b> - The locale
   *
   * @return <b>String</b> - The corresponding message
   */
  public static String getMessage(String key, Locale locale) {

    return getMessage(key, locale.getLanguage());
  }
  /**
   * This method returns a list of messages for a given set of  keys and
   * language id.
   *
   * @param <b>keys</b> - The set of message keys
   * @param <b>langID</b> - The language id
   *
   * @return <b>String[]</b> - The corresponding set of messages.
   */
  public static String[] getMessages(String[] keys, String langID) {

    int len         = keys.length;
    String[] values = new String[ len ];

    for(int i = 0; i < len; i++) {
      values [ i ] = getMessage(keys [ i ], langID);
    }

    return values;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -