Beispiel #1
0
  private static ResourceBundle processGetBundle(
      String baseName,
      Locale targetLocale,
      ClassLoader loader,
      ResourceBundle.Control control,
      boolean expired,
      ResourceBundle result) {
    List<Locale> locales = control.getCandidateLocales(baseName, targetLocale);
    if (null == locales) {
      throw new IllegalArgumentException();
    }
    List<String> formats = control.getFormats(baseName);
    if (Control.FORMAT_CLASS == formats
        || Control.FORMAT_PROPERTIES == formats
        || Control.FORMAT_DEFAULT == formats) {
      throw new IllegalArgumentException();
    }
    ResourceBundle ret = null;
    ResourceBundle currentBundle = null;
    ResourceBundle bundle = null;
    for (Locale locale : locales) {
      for (String format : formats) {
        try {
          if (expired) {
            bundle =
                control.newBundle(
                    baseName,
                    locale,
                    format,
                    loader,
                    control.needsReload(
                        baseName, locale, format, loader, result, System.currentTimeMillis()));

          } else {
            try {
              bundle = control.newBundle(baseName, locale, format, loader, false);
            } catch (IllegalArgumentException e) {
              // do nothing
            }
          }
        } catch (IllegalAccessException e) {
          // do nothing
        } catch (InstantiationException e) {
          // do nothing
        } catch (IOException e) {
          // do nothing
        }
        if (null != bundle) {
          if (null != currentBundle) {
            currentBundle.setParent(bundle);
            currentBundle = bundle;
          } else {
            if (null == ret) {
              ret = bundle;
              currentBundle = ret;
            }
          }
        }
        if (null != bundle) {
          break;
        }
      }
    }

    if ((null == ret)
        || (Locale.ROOT.equals(ret.getLocale())
            && (!(locales.size() == 1 && locales.contains(Locale.ROOT))))) {
      Locale nextLocale = control.getFallbackLocale(baseName, targetLocale);
      if (null != nextLocale) {
        ret = processGetBundle(baseName, nextLocale, loader, control, expired, result);
      }
    }

    return ret;
  }
Beispiel #2
0
  private static ResourceBundle handleGetBundle(
      String base, String locale, boolean loadBase, final ClassLoader loader) {
    ResourceBundle bundle = null;
    String bundleName = base + locale;
    Object cacheKey = loader != null ? (Object) loader : (Object) "null"; // $NON-NLS-1$
    Hashtable<String, ResourceBundle> loaderCache;
    synchronized (cache) {
      loaderCache = cache.get(cacheKey);
      if (loaderCache == null) {
        loaderCache = new Hashtable<String, ResourceBundle>();
        cache.put(cacheKey, loaderCache);
      }
    }
    ResourceBundle result = loaderCache.get(bundleName);
    if (result != null) {
      if (result == MISSINGBASE) {
        return null;
      }
      if (result == MISSING) {
        if (!loadBase) {
          return null;
        }
        String extension = strip(locale);
        if (extension == null) {
          return null;
        }
        return handleGetBundle(base, extension, loadBase, loader);
      }
      return result;
    }

    try {
      Class<?> bundleClass = Class.forName(bundleName, true, loader);

      if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
        bundle = (ResourceBundle) bundleClass.newInstance();
      }
    } catch (LinkageError e) {
    } catch (Exception e) {
    }

    if (bundle != null) {
      bundle.setLocale(locale);
    } else {
      final String fileName = bundleName.replace('.', '/');
      InputStream stream =
          AccessController.doPrivileged(
              new PrivilegedAction<InputStream>() {
                public InputStream run() {
                  return loader == null
                      ? ClassLoader.getSystemResourceAsStream(fileName + ".properties")
                      : loader //$NON-NLS-1$
                          .getResourceAsStream(fileName + ".properties"); // $NON-NLS-1$
                }
              });
      if (stream != null) {
        try {
          try {
            bundle = new PropertyResourceBundle(new InputStreamReader(stream));
          } finally {
            stream.close();
          }
          bundle.setLocale(locale);
        } catch (IOException e) {
          // do nothing
        }
      }
    }

    String extension = strip(locale);
    if (bundle != null) {
      if (extension != null) {
        ResourceBundle parent = handleGetBundle(base, extension, true, loader);
        if (parent != null) {
          bundle.setParent(parent);
        }
      }
      loaderCache.put(bundleName, bundle);
      return bundle;
    }

    if (extension != null && (loadBase || extension.length() > 0)) {
      bundle = handleGetBundle(base, extension, loadBase, loader);
      if (bundle != null) {
        loaderCache.put(bundleName, bundle);
        return bundle;
      }
    }
    loaderCache.put(bundleName, loadBase ? MISSINGBASE : MISSING);
    return null;
  }
 public void setParent(ResourceBundle parent) {
   super.setParent(parent);
 }