1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  import java.util.TreeSet;
36  
37  import javax.servlet.ServletContext;
38  
39  /**
40   * <a href="ExtRegistry.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class ExtRegistry {
45  
46      public static Map<String, Set<String>> getConflicts(
47              ServletContext servletContext)
48          throws Exception {
49  
50          String servletContextName = servletContext.getServletContextName();
51  
52          Set<String> files = _readExtFiles(
53              servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
54  
55          Iterator<Map.Entry<String, Set<String>>> itr =
56              _extMap.entrySet().iterator();
57  
58          Map<String, Set<String>> conflicts = new HashMap<String, Set<String>>();
59  
60          while (itr.hasNext()) {
61              Map.Entry<String, Set<String>> entry = itr.next();
62  
63              String curServletContextName = entry.getKey();
64              Set<String> curFiles = entry.getValue();
65  
66              for (String file : files) {
67                  if (!curFiles.contains(file)) {
68                      continue;
69                  }
70  
71                  Set<String> conflictFiles = conflicts.get(
72                      curServletContextName);
73  
74                  if (conflictFiles == null) {
75                      conflictFiles = new TreeSet<String>();
76  
77                      conflicts.put(curServletContextName, conflictFiles);
78                  }
79  
80                  conflictFiles.add(file);
81              }
82          }
83  
84          return conflicts;
85      }
86  
87      public static Set<String> getServletContextNames() {
88          return Collections.unmodifiableSet(_extMap.keySet());
89      }
90  
91      public static boolean isRegistered(String servletContextName) {
92          if (_extMap.containsKey(servletContextName)) {
93              return true;
94          }
95          else {
96              return false;
97          }
98      }
99  
100     public static void registerExt(ServletContext servletContext)
101         throws Exception {
102 
103         String servletContextName = servletContext.getServletContextName();
104 
105         Set<String> files = _readExtFiles(
106             servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
107 
108         _extMap.put(servletContextName, files);
109     }
110 
111     public static void registerPortal(ServletContext servletContext)
112         throws Exception {
113 
114         Set<String> resourcePaths = servletContext.getResourcePaths(
115             "/WEB-INF");
116 
117         for (String resourcePath : resourcePaths) {
118             if (resourcePath.startsWith("/WEB-INF/ext-") &&
119                 resourcePath.endsWith("-ext.xml")) {
120 
121                 String servletContextName = resourcePath.substring(
122                     13, resourcePath.length() - 4);
123 
124                 Set<String> files = _readExtFiles(
125                     servletContext, resourcePath);
126 
127                 _extMap.put(servletContextName, files);
128             }
129         }
130     }
131 
132     private static Set<String> _readExtFiles(
133             ServletContext servletContext, String resourcePath)
134         throws Exception {
135 
136         Set<String> files = new TreeSet<String>();
137 
138         Document document = SAXReaderUtil.read(
139             servletContext.getResourceAsStream(resourcePath));
140 
141         Element rootElement = document.getRootElement();
142 
143         Element filesElement = rootElement.element("files");
144 
145         List<Element> fileElements = filesElement.elements("file");
146 
147         for (Element fileElement : fileElements) {
148             files.add(fileElement.getText());
149         }
150 
151         return files;
152     }
153 
154     private static Map<String, Set<String>> _extMap =
155         new HashMap<String, Set<String>>();
156 
157 }