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.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceReference;
023    import com.liferay.registry.collections.ServiceReferenceMapper;
024    import com.liferay.registry.collections.ServiceTrackerCollections;
025    import com.liferay.registry.collections.ServiceTrackerMap;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Carlos Sierra Andr??s
035     * @author Raymond Aug??
036     */
037    public class DynamicIncludeUtil {
038    
039            public static List<DynamicInclude> getDynamicIncludes(String key) {
040                    return _instance._dynamicIncludes.getService(key);
041            }
042    
043            public static boolean hasDynamicInclude(String key) {
044                    List<DynamicInclude> dynamicIncludes = getDynamicIncludes(key);
045    
046                    if ((dynamicIncludes == null) || dynamicIncludes.isEmpty()) {
047                            return false;
048                    }
049    
050                    return true;
051            }
052    
053            public static void include(
054                    HttpServletRequest request, HttpServletResponse response, String key,
055                    boolean ascendingPriority) {
056    
057                    List<DynamicInclude> dynamicIncludes = getDynamicIncludes(key);
058    
059                    if ((dynamicIncludes == null) || dynamicIncludes.isEmpty()) {
060                            return;
061                    }
062    
063                    Iterator<DynamicInclude> iterator = null;
064    
065                    if (ascendingPriority) {
066                            iterator = dynamicIncludes.iterator();
067                    }
068                    else {
069                            iterator = ListUtil.reverseIterator(dynamicIncludes);
070                    }
071    
072                    while (iterator.hasNext()) {
073                            DynamicInclude dynamicInclude = iterator.next();
074    
075                            try {
076                                    dynamicInclude.include(request, response, key);
077                            }
078                            catch (Exception e) {
079                                    _log.error(e, e);
080                            }
081                    }
082            }
083    
084            private DynamicIncludeUtil() {
085                    _dynamicIncludes = ServiceTrackerCollections.openMultiValueMap(
086                            DynamicInclude.class, null,
087                            new ServiceReferenceMapper<String, DynamicInclude>() {
088    
089                                    @Override
090                                    public void map(
091                                            ServiceReference<DynamicInclude> serviceReference,
092                                            final Emitter<String> emitter) {
093    
094                                            Registry registry = RegistryUtil.getRegistry();
095    
096                                            DynamicInclude dynamicInclude = registry.getService(
097                                                    serviceReference);
098    
099                                            dynamicInclude.register(
100                                                    new DynamicInclude.DynamicIncludeRegistry() {
101    
102                                                            @Override
103                                                            public void register(String key) {
104                                                                    emitter.emit(key);
105                                                            }
106    
107                                                    });
108    
109                                            registry.ungetService(serviceReference);
110                                    }
111    
112                            });
113            }
114    
115            private static final Log _log = LogFactoryUtil.getLog(
116                    DynamicIncludeUtil.class);
117    
118            private static final DynamicIncludeUtil _instance =
119                    new DynamicIncludeUtil();
120    
121            private final ServiceTrackerMap<String, List<DynamicInclude>>
122                    _dynamicIncludes;
123    
124    }