001    /**
002     * Copyright (c) 2000-present 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.JavaConstants;
018    import com.liferay.portal.kernel.util.SetUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.language.LanguageResources;
021    import com.liferay.portal.language.ResourceBundleEnumeration;
022    
023    import java.util.Enumeration;
024    import java.util.HashSet;
025    import java.util.Locale;
026    import java.util.MissingResourceException;
027    import java.util.ResourceBundle;
028    import java.util.Set;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Eduardo Lundgren
033     */
034    public class StrutsResourceBundle extends ResourceBundle {
035    
036            public StrutsResourceBundle(String portletName, Locale locale) {
037                    _portletName = portletName;
038                    _locale = locale;
039    
040                    setParent(LanguageResources.getResourceBundle(locale));
041            }
042    
043            @Override
044            public boolean containsKey(String key) {
045                    if (key == null) {
046                            throw new NullPointerException();
047                    }
048    
049                    if (_keys.contains(key)) {
050                            key = _buildKey(key);
051                    }
052    
053                    return parent.containsKey(key);
054            }
055    
056            @Override
057            public Enumeration<String> getKeys() {
058                    Set<String> keys = new HashSet<>();
059    
060                    for (String key : _keys) {
061                            if (parent.containsKey(_buildKey(key))) {
062                                    keys.add(key);
063                            }
064                    }
065    
066                    return new ResourceBundleEnumeration(keys, parent.getKeys());
067            }
068    
069            @Override
070            public Locale getLocale() {
071                    return _locale;
072            }
073    
074            @Override
075            protected Object handleGetObject(String key) {
076                    if (key == null) {
077                            throw new NullPointerException();
078                    }
079    
080                    if (_keys.contains(key)) {
081                            key = _buildKey(key);
082                    }
083    
084                    if (parent.containsKey(key)) {
085                            try {
086                                    return parent.getObject(key);
087                            }
088                            catch (MissingResourceException mre) {
089                                    return null;
090                            }
091                    }
092    
093                    return null;
094            }
095    
096            private String _buildKey(String key) {
097                    return key.concat(StringPool.PERIOD).concat(_portletName);
098            }
099    
100            private static final Set<String> _keys = SetUtil.fromArray(
101                    new String[] {
102                            JavaConstants.JAVAX_PORTLET_DESCRIPTION,
103                            JavaConstants.JAVAX_PORTLET_KEYWORDS,
104                            JavaConstants.JAVAX_PORTLET_LONG_TITLE,
105                            JavaConstants.JAVAX_PORTLET_SHORT_TITLE,
106                            JavaConstants.JAVAX_PORTLET_TITLE
107                    });
108    
109            private final Locale _locale;
110            private final String _portletName;
111    
112    }