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