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.CharPool;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.registry.Registry;
023    import com.liferay.registry.RegistryUtil;
024    import com.liferay.registry.ServiceReference;
025    import com.liferay.registry.collections.ServiceReferenceMapper;
026    import com.liferay.registry.collections.ServiceTrackerCollections;
027    import com.liferay.registry.collections.ServiceTrackerMap;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    /**
036     * @author Carlos Sierra Andr??s
037     * @author Raymond Aug??
038     */
039    public class TagDynamicIncludeUtil {
040    
041            public static List<TagDynamicInclude> getTagDynamicIncludes(
042                    String tagClassName, String tagDynamicId, String tagPoint) {
043    
044                    String key = _getKey(tagClassName, tagDynamicId, tagPoint);
045    
046                    return _instance._tagDynamicIncludes.getService(key);
047            }
048    
049            public static boolean hasTagDynamicInclude(
050                    String tagClassName, String tagDynamicId, String tagPoint) {
051    
052                    List<TagDynamicInclude> tagDynamicIncludes = getTagDynamicIncludes(
053                            tagClassName, tagDynamicId, tagPoint);
054    
055                    if ((tagDynamicIncludes == null) || tagDynamicIncludes.isEmpty()) {
056                            return false;
057                    }
058    
059                    return true;
060            }
061    
062            public static void include(
063                    HttpServletRequest request, HttpServletResponse response,
064                    String tagClassName, String tagDynamicId, String tagPoint,
065                    boolean ascendingPriority) {
066    
067                    List<TagDynamicInclude> tagDynamicIncludes = getTagDynamicIncludes(
068                            tagClassName, tagDynamicId, tagPoint);
069    
070                    if ((tagDynamicIncludes == null) || tagDynamicIncludes.isEmpty()) {
071                            return;
072                    }
073    
074                    Iterator<TagDynamicInclude> iterator = null;
075    
076                    if (ascendingPriority) {
077                            iterator = tagDynamicIncludes.iterator();
078                    }
079                    else {
080                            iterator = ListUtil.reverseIterator(tagDynamicIncludes);
081                    }
082    
083                    while (iterator.hasNext()) {
084                            TagDynamicInclude tagDynamicInclude = iterator.next();
085    
086                            try {
087                                    tagDynamicInclude.include(
088                                            request, response, tagClassName, tagDynamicId, tagPoint);
089                            }
090                            catch (Exception e) {
091                                    _log.error(e, e);
092                            }
093                    }
094            }
095    
096            private static String _getKey(
097                    String tagClassName, String tagDynamicId, String tagPoint) {
098    
099                    StringBundler sb = new StringBundler(5);
100    
101                    sb.append(tagClassName);
102                    sb.append(CharPool.POUND);
103                    sb.append(tagPoint);
104                    sb.append(CharPool.POUND);
105                    sb.append(tagDynamicId);
106    
107                    return sb.toString();
108            }
109    
110            private TagDynamicIncludeUtil() {
111                    _tagDynamicIncludes = ServiceTrackerCollections.openMultiValueMap(
112                            TagDynamicInclude.class, null,
113                            new ServiceReferenceMapper<String, TagDynamicInclude>() {
114    
115                                    @Override
116                                    public void map(
117                                            ServiceReference<TagDynamicInclude> serviceReference,
118                                            final Emitter<String> emitter) {
119    
120                                            Registry registry = RegistryUtil.getRegistry();
121    
122                                            TagDynamicInclude tagDynamicInclude = registry.getService(
123                                                    serviceReference);
124    
125                                            try {
126                                                    tagDynamicInclude.register(
127                                                            new TagDynamicInclude.TagDynamicIncludeRegistry() {
128    
129                                                                    @Override
130                                                                    public void register(
131                                                                            String tagClassName, String tagDynamicId,
132                                                                            String tagPoint) {
133    
134                                                                            String key = _getKey(
135                                                                                    tagClassName, tagDynamicId, tagPoint);
136    
137                                                                            emitter.emit(key);
138                                                                    }
139    
140                                                            });
141                                            }
142                                            finally {
143                                                    registry.ungetService(serviceReference);
144                                            }
145                                    }
146    
147                            });
148            }
149    
150            private static final Log _log = LogFactoryUtil.getLog(
151                    TagDynamicIncludeUtil.class);
152    
153            private static final TagDynamicIncludeUtil _instance =
154                    new TagDynamicIncludeUtil();
155    
156            private final ServiceTrackerMap<String, List<TagDynamicInclude>>
157                    _tagDynamicIncludes;
158    
159    }