📄 projectresourcebundle.java
字号:
* It must be a proper prefix of the caller's package. * * @param caller The calling class. * This is used to get the package name to further construct * the basename as well as to get the proper ClassLoader. * * @param resourceName The name of the resource without the * ".properties" extension * * @param locale The locale * * @param extendsBundle If non-null, then this ExtendMessages will * default to extendsBundle. * * @throws MissingResourceException if projectName is not a prefix of * the caller's package name, or if the resource could not be * found/loaded. */ public static ProjectResourceBundle getBundle(String projectName, String packageName, String resourceName, Locale locale, ClassLoader loader, ResourceBundle extendsBundle) throws MissingResourceException { if (log.isDebugEnabled()) { log.debug("getBundle(" + projectName + "," + packageName + "," + resourceName + "," + String.valueOf(locale) + ",...)"); } Context context = new Context(); context.setLocale(locale); context.setLoader(loader); context.setProjectName(projectName); context.setResourceName(resourceName); context.setParentBundle(extendsBundle); packageName = context.validate(packageName); ProjectResourceBundle bundle = null; try { bundle = getBundle(context, packageName); } catch (RuntimeException e) { log.debug("Exception: ", e); throw e; } if (bundle == null) { throw new MissingResourceException("Cannot find resource '" + packageName + '.' + resourceName + "'", resourceName, ""); } return bundle; } /** * get bundle... * - check cache * - try up hierarchy * - if at top of hierarchy, use (link to) context.getParentBundle() */ private static synchronized ProjectResourceBundle getBundle(Context context, String packageName) throws MissingResourceException { String cacheKey = context.getCacheKey(packageName); ProjectResourceBundle prb = (ProjectResourceBundle)bundleCache.get(cacheKey); if (prb == null) { String name = packageName + '.' + context.getResourceName(); ResourceBundle rb = context.loadBundle(packageName); ResourceBundle parent = context.getParentBundle(packageName); if (rb != null) { prb = new ProjectResourceBundle(name, rb); prb.setParent(parent); if (log.isDebugEnabled()) { log.debug("Created " + prb + ", linked to parent " + String.valueOf(parent)); } } else { if (parent != null) { if (parent instanceof ProjectResourceBundle) { prb = (ProjectResourceBundle)parent; } else { prb = new ProjectResourceBundle(name, parent); } if (log.isDebugEnabled()) { log.debug("Root package not found, cross link to " + parent); } } } if (prb != null) { // Cache the resource bundleCache.put(cacheKey, prb); } } return prb; } private static final String getPackage(String name) { return name.substring(0, name.lastIndexOf('.')).intern(); } /** * Construct a new ProjectResourceBundle */ private ProjectResourceBundle(String name, ResourceBundle bundle) throws MissingResourceException { this.resourceBundle = bundle; this.resourceName = name; } public String getResourceName() { return resourceName; } /** * Clears the internal cache */ public static void clearCache() { bundleCache.clear(); } public String toString() { return resourceName; } private static class Context { private Locale _locale; private ClassLoader _loader; private String _projectName; private String _resourceName; private ResourceBundle _parent; void setLocale(Locale l) { /* 1. Docs indicate that if locale is not specified, * then the default local is used in it's place. * 2. A null value for locale is invalid. * * Therefore, default... */ _locale = (l == null) ? defaultLocale : l; } void setLoader(ClassLoader l) { _loader = (l != null) ? l : this.getClass().getClassLoader(); // START FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16868 if (_loader == null) { _loader = ClassLoader.getSystemClassLoader(); } // END FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16868 } void setProjectName(String name) { _projectName = name.intern(); } void setResourceName(String name) { _resourceName = name.intern(); } void setParentBundle(ResourceBundle b) { _parent = b; } Locale getLocale() { return _locale; } ClassLoader getLoader() { return _loader; } String getProjectName() { return _projectName; } String getResourceName() { return _resourceName; } ResourceBundle getParentBundle() { return _parent; } String getCacheKey(String packageName) { String loaderName = (_loader == null) ? "" : (":" + _loader.hashCode()); return packageName + "." + _resourceName + ":" + _locale + ":" + defaultLocale + loaderName; } ResourceBundle loadBundle(String packageName) { try { return ResourceBundle.getBundle(packageName + '.' + _resourceName, _locale, _loader); } catch (MissingResourceException e) { // Deliberately surpressing print stack.. just the string for info. log.debug("loadBundle: Ignoring MissingResourceException: " + e.getMessage()); } return null; } ResourceBundle getParentBundle(String packageName) { ResourceBundle p; if (packageName != _projectName) { p = getBundle(this, getPackage(packageName)); } else { p = _parent; _parent = null; } return p; } String validate(String packageName) throws MissingResourceException { if (_projectName == null || _projectName.length() == 0) { log.debug("Project name not specified"); throw new MissingResourceException("Project name not specified", "", ""); } if (packageName == null || packageName.length() == 0) { log.debug("Package name not specified"); throw new MissingResourceException("Package not specified", packageName, ""); } packageName = packageName.intern(); /* Ensure that project is a proper prefix of class. * Terminate project name with '.' to ensure proper match. */ if (packageName != _projectName && !packageName.startsWith(_projectName + '.')) { log.debug("Project not a prefix of Package"); throw new MissingResourceException("Project '" + _projectName + "' must be a prefix of Package '" + packageName + "'", packageName + '.' + _resourceName, ""); } return packageName; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -