1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.xml.Document;
18  import com.liferay.portal.kernel.xml.Element;
19  import com.liferay.portal.kernel.xml.SAXReaderUtil;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.TreeSet;
28  
29  import javax.servlet.ServletContext;
30  
31  /**
32   * <a href="ExtRegistry.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ExtRegistry {
37  
38      public static Map<String, Set<String>> getConflicts(
39              ServletContext servletContext)
40          throws Exception {
41  
42          String servletContextName = servletContext.getServletContextName();
43  
44          Set<String> files = _readExtFiles(
45              servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
46  
47          Iterator<Map.Entry<String, Set<String>>> itr =
48              _extMap.entrySet().iterator();
49  
50          Map<String, Set<String>> conflicts = new HashMap<String, Set<String>>();
51  
52          while (itr.hasNext()) {
53              Map.Entry<String, Set<String>> entry = itr.next();
54  
55              String curServletContextName = entry.getKey();
56              Set<String> curFiles = entry.getValue();
57  
58              for (String file : files) {
59                  if (!curFiles.contains(file)) {
60                      continue;
61                  }
62  
63                  Set<String> conflictFiles = conflicts.get(
64                      curServletContextName);
65  
66                  if (conflictFiles == null) {
67                      conflictFiles = new TreeSet<String>();
68  
69                      conflicts.put(curServletContextName, conflictFiles);
70                  }
71  
72                  conflictFiles.add(file);
73              }
74          }
75  
76          return conflicts;
77      }
78  
79      public static Set<String> getServletContextNames() {
80          return Collections.unmodifiableSet(_extMap.keySet());
81      }
82  
83      public static boolean isRegistered(String servletContextName) {
84          if (_extMap.containsKey(servletContextName)) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      public static void registerExt(ServletContext servletContext)
93          throws Exception {
94  
95          String servletContextName = servletContext.getServletContextName();
96  
97          Set<String> files = _readExtFiles(
98              servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
99  
100         _extMap.put(servletContextName, files);
101     }
102 
103     public static void registerPortal(ServletContext servletContext)
104         throws Exception {
105 
106         Set<String> resourcePaths = servletContext.getResourcePaths(
107             "/WEB-INF");
108 
109         for (String resourcePath : resourcePaths) {
110             if (resourcePath.startsWith("/WEB-INF/ext-") &&
111                 resourcePath.endsWith("-ext.xml")) {
112 
113                 String servletContextName = resourcePath.substring(
114                     13, resourcePath.length() - 4);
115 
116                 Set<String> files = _readExtFiles(
117                     servletContext, resourcePath);
118 
119                 _extMap.put(servletContextName, files);
120             }
121         }
122     }
123 
124     private static Set<String> _readExtFiles(
125             ServletContext servletContext, String resourcePath)
126         throws Exception {
127 
128         Set<String> files = new TreeSet<String>();
129 
130         Document document = SAXReaderUtil.read(
131             servletContext.getResourceAsStream(resourcePath));
132 
133         Element rootElement = document.getRootElement();
134 
135         Element filesElement = rootElement.element("files");
136 
137         List<Element> fileElements = filesElement.elements("file");
138 
139         for (Element fileElement : fileElements) {
140             files.add(fileElement.getText());
141         }
142 
143         return files;
144     }
145 
146     private static Map<String, Set<String>> _extMap =
147         new HashMap<String, Set<String>>();
148 
149 }