001    /**
002     * Copyright (c) 2000-2011 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.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    }