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