001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }