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.portal.util;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.CustomJspRegistry;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Ryan Park
029     * @author Brian Wing Shun Chan
030     */
031    @DoPrivileged
032    public class CustomJspRegistryImpl implements CustomJspRegistry {
033    
034            public CustomJspRegistryImpl() {
035                    _servletContextNames = new ConcurrentHashMap<>();
036            }
037    
038            @Override
039            public String getCustomJspFileName(
040                    String servletContextName, String fileName) {
041    
042                    int pos = fileName.lastIndexOf(CharPool.PERIOD);
043    
044                    if (pos == -1) {
045                            return fileName.concat(StringPool.PERIOD).concat(
046                                    servletContextName);
047                    }
048    
049                    StringBundler sb = new StringBundler(4);
050    
051                    sb.append(fileName.substring(0, pos));
052                    sb.append(CharPool.PERIOD);
053                    sb.append(servletContextName);
054                    sb.append(fileName.substring(pos));
055    
056                    return sb.toString();
057            }
058    
059            @Override
060            public String getDisplayName(String servletContextName) {
061                    return _servletContextNames.get(servletContextName);
062            }
063    
064            @Override
065            public Set<String> getServletContextNames() {
066                    return _servletContextNames.keySet();
067            }
068    
069            @Override
070            public void registerServletContextName(
071                    String servletContextName, String displayName) {
072    
073                    _servletContextNames.put(servletContextName, displayName);
074            }
075    
076            @Override
077            public void unregisterServletContextName(String servletContextName) {
078                    _servletContextNames.remove(servletContextName);
079            }
080    
081            private final Map<String, String> _servletContextNames;
082    
083    }