1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
18  import com.liferay.portal.kernel.configuration.Filter;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.util.UniqueList;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * <a href="JavaScriptBundleUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Eduardo Lundgren
30   */
31  public class JavaScriptBundleUtil {
32  
33      public static final String CACHE_NAME =
34          JavaScriptBundleUtil.class.getName();
35  
36      public static void clearCache() {
37          SingleVMPoolUtil.clear(CACHE_NAME);
38      }
39  
40      public static String[] getFileNames(String bundleId) {
41          String[] fileNames = (String[])SingleVMPoolUtil.get(
42              CACHE_NAME, bundleId);
43  
44          if (fileNames == null) {
45              List<String> fileNamesList = new ArrayList<String>();
46  
47              List<String> dependencies = _getDependencies(
48                  bundleId, new UniqueList<String>());
49  
50              for (String dependency : dependencies) {
51                  String[] dependencyFileNames = PropsUtil.getArray(dependency);
52  
53                  for (String dependencyFileName : dependencyFileNames) {
54                      fileNamesList.add(dependencyFileName);
55                  }
56              }
57  
58              fileNames = fileNamesList.toArray(
59                  new String[fileNamesList.size()]);
60  
61              SingleVMPoolUtil.put(CACHE_NAME, bundleId, fileNames);
62          }
63  
64          return fileNames;
65      }
66  
67      private static List<String> _getDependencies(
68          String bundleId, List<String> dependencies) {
69  
70          if (!ArrayUtil.contains(PropsValues.JAVASCRIPT_BUNDLE_IDS, bundleId)) {
71              return dependencies;
72          }
73  
74          String[] bundleDependencies = PropsUtil.getArray(
75              PropsKeys.JAVASCRIPT_BUNDLE_DEPENDENCIES, new Filter(bundleId));
76  
77          for (String bundleDependency : bundleDependencies) {
78              String[] bundleDependencyDependencies = PropsUtil.getArray(
79                  PropsKeys.JAVASCRIPT_BUNDLE_DEPENDENCIES,
80                  new Filter(bundleDependency));
81  
82              if (!ArrayUtil.contains(bundleDependencyDependencies, bundleId)) {
83                  _getDependencies(bundleDependency, dependencies);
84              }
85  
86              dependencies.add(bundleDependency);
87          }
88  
89          dependencies.add(bundleId);
90  
91          return dependencies;
92      }
93  
94  }