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.kernel.servlet.taglib;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.registry.collections.ServiceTrackerCollections;
020    import com.liferay.registry.collections.ServiceTrackerMap;
021    
022    import java.util.List;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Carlos Sierra Andr??s
029     * @author Raymond Aug??
030     */
031    public class DynamicIncludeUtil {
032    
033            public static List<DynamicInclude> getDynamicIncludes(String key) {
034                    return _instance._dynamicIncludes.getService(key);
035            }
036    
037            public static boolean hasDynamicInclude(String key) {
038                    List<DynamicInclude> dynamicIncludes = getDynamicIncludes(key);
039    
040                    if ((dynamicIncludes == null) || dynamicIncludes.isEmpty()) {
041                            return false;
042                    }
043    
044                    return true;
045            }
046    
047            public static void include(
048                    HttpServletRequest request, HttpServletResponse response, String key) {
049    
050                    List<DynamicInclude> dynamicIncludes = getDynamicIncludes(key);
051    
052                    if ((dynamicIncludes != null) && !dynamicIncludes.isEmpty()) {
053                            for (DynamicInclude dynamicInclude : dynamicIncludes) {
054                                    try {
055                                            dynamicInclude.include(request, response);
056                                    }
057                                    catch (Exception e) {
058                                            _log.error(e, e);
059                                    }
060                            }
061                    }
062            }
063    
064            private DynamicIncludeUtil() {
065                    _dynamicIncludes = ServiceTrackerCollections.multiValueMap(
066                            DynamicInclude.class, "key");
067    
068                    _dynamicIncludes.open();
069            }
070    
071            private static final Log _log = LogFactoryUtil.getLog(
072                    DynamicIncludeUtil.class);
073    
074            private static final DynamicIncludeUtil _instance =
075                    new DynamicIncludeUtil();
076    
077            private final ServiceTrackerMap<String, List<DynamicInclude>>
078                    _dynamicIncludes;
079    
080    }