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.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
019    import com.liferay.portal.kernel.configuration.Filter;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    
023    import java.util.ArrayList;
024    import java.util.LinkedHashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    /**
029     * @author Eduardo Lundgren
030     */
031    public class JavaScriptBundleUtil {
032    
033            public static void clearCache() {
034                    _portalCache.removeAll();
035            }
036    
037            public static String[] getFileNames(String bundleId) {
038                    String[] fileNames = _portalCache.get(bundleId);
039    
040                    if (fileNames == null) {
041                            List<String> fileNamesList = new ArrayList<>();
042    
043                            Set<String> dependencies = _getDependencies(
044                                    bundleId, new LinkedHashSet<String>());
045    
046                            for (String dependency : dependencies) {
047                                    String[] dependencyFileNames = PropsUtil.getArray(dependency);
048    
049                                    for (String dependencyFileName : dependencyFileNames) {
050                                            fileNamesList.add(dependencyFileName);
051                                    }
052                            }
053    
054                            fileNames = fileNamesList.toArray(new String[fileNamesList.size()]);
055    
056                            _portalCache.put(bundleId, fileNames);
057                    }
058    
059                    return fileNames;
060            }
061    
062            private static Set<String> _getDependencies(
063                    String bundleId, Set<String> dependencies) {
064    
065                    if (!ArrayUtil.contains(PropsValues.JAVASCRIPT_BUNDLE_IDS, bundleId)) {
066                            return dependencies;
067                    }
068    
069                    String[] bundleDependencies = PropsUtil.getArray(
070                            PropsKeys.JAVASCRIPT_BUNDLE_DEPENDENCIES, new Filter(bundleId));
071    
072                    for (String bundleDependency : bundleDependencies) {
073                            String[] bundleDependencyDependencies = PropsUtil.getArray(
074                                    PropsKeys.JAVASCRIPT_BUNDLE_DEPENDENCIES,
075                                    new Filter(bundleDependency));
076    
077                            if (!ArrayUtil.contains(bundleDependencyDependencies, bundleId)) {
078                                    _getDependencies(bundleDependency, dependencies);
079                            }
080    
081                            dependencies.add(bundleDependency);
082                    }
083    
084                    dependencies.add(bundleId);
085    
086                    return dependencies;
087            }
088    
089            private static final String _CACHE_NAME =
090                    JavaScriptBundleUtil.class.getName();
091    
092            private static final PortalCache<String, String[]> _portalCache =
093                    SingleVMPoolUtil.getPortalCache(_CACHE_NAME);
094    
095    }