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