001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.util.ResourceBundleThreadLocal;
018    import com.liferay.portal.kernel.util.ResourceBundleUtil;
019    
020    import java.util.Enumeration;
021    import java.util.MissingResourceException;
022    import java.util.ResourceBundle;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class NullSafeResourceBundle extends ResourceBundle {
028    
029            public NullSafeResourceBundle(ResourceBundle resourceBundle) {
030                    if (resourceBundle == null) {
031                            throw new NullPointerException();
032                    }
033    
034                    setParent(resourceBundle);
035            }
036    
037            @Override
038            public Enumeration<String> getKeys() {
039                    return parent.getKeys();
040            }
041    
042            @Override
043            protected Object handleGetObject(String key) {
044                    if (key == null) {
045                            throw new NullPointerException();
046                    }
047    
048                    try {
049                            return parent.getString(key);
050                    }
051                    catch (MissingResourceException mre) {
052                            if (ResourceBundleThreadLocal.isReplace()) {
053                                    return ResourceBundleUtil.NULL_VALUE;
054                            }
055                            else {
056                                    throw mre;
057                            }
058                    }
059            }
060    
061    }