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