projectresourcebundle.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 432 行 · 第 1/2 页

JAVA
432
字号
                    + 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 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.equals(_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.equals(_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 + =
减小字号Ctrl + -
显示快捷键?