001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.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 = (String[])_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(
054                                    new String[fileNamesList.size()]);
055    
056                            _portalCache.put(bundleId, fileNames);
057                    }
058    
059                    return fileNames;
060            }
061    
062            private static List<String> _getDependencies(
063                    String bundleId, List<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 PortalCache _portalCache = SingleVMPoolUtil.getCache(
093                    _CACHE_NAME);
094    
095    }