001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.xml.Document;
018 import com.liferay.portal.kernel.xml.Element;
019 import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
020
021 import java.util.Collections;
022 import java.util.HashMap;
023 import java.util.List;
024 import java.util.Map;
025 import java.util.Set;
026 import java.util.TreeSet;
027
028 import javax.servlet.ServletContext;
029
030
034 public class ExtRegistry {
035
036 public static Map<String, Set<String>> getConflicts(
037 ServletContext servletContext)
038 throws Exception {
039
040 String servletContextName = servletContext.getServletContextName();
041
042 Set<String> fileNames = _readExtFileNames(
043 servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
044
045 Map<String, Set<String>> conflicts = new HashMap<String, Set<String>>();
046
047 for (Map.Entry<String, Set<String>> entry : _extMap.entrySet()) {
048 String curServletContextName = entry.getKey();
049 Set<String> curFileNames = entry.getValue();
050
051 for (String fileName : fileNames) {
052 if (!curFileNames.contains(fileName)) {
053 continue;
054 }
055
056 Set<String> conflictFileNames = conflicts.get(
057 curServletContextName);
058
059 if (conflictFileNames == null) {
060 conflictFileNames = new TreeSet<String>();
061
062 conflicts.put(curServletContextName, conflictFileNames);
063 }
064
065 conflictFileNames.add(fileName);
066 }
067 }
068
069 return conflicts;
070 }
071
072 public static Set<String> getServletContextNames() {
073 return Collections.unmodifiableSet(_extMap.keySet());
074 }
075
076 public static boolean isIgnoredFileName(String fileName) {
077 if (isMergedFileName(fileName)) {
078 return true;
079 }
080
081 for (String ignoredFileName : _IGNORED_FILE_NAMES) {
082 if (fileName.contains(ignoredFileName)) {
083 return true;
084 }
085 }
086
087 return false;
088 }
089
090 public static boolean isMergedFileName(String fileName) {
091 for (String mergedFileName : _SUPPORTED_MERGING_FILE_NAMES) {
092 if (fileName.contains(mergedFileName)) {
093 return true;
094 }
095 }
096
097 return false;
098 }
099
100 public static boolean isRegistered(String servletContextName) {
101 if (_extMap.containsKey(servletContextName)) {
102 return true;
103 }
104 else {
105 return false;
106 }
107 }
108
109 public static void registerExt(ServletContext servletContext)
110 throws Exception {
111
112 String servletContextName = servletContext.getServletContextName();
113
114 Set<String> fileNames = _readExtFileNames(
115 servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
116
117 _extMap.put(servletContextName, fileNames);
118 }
119
120 public static void registerPortal(ServletContext servletContext)
121 throws Exception {
122
123 Set<String> resourcePaths = servletContext.getResourcePaths("/WEB-INF");
124
125 if ((resourcePaths == null) || resourcePaths.isEmpty()) {
126 return;
127 }
128
129 for (String resourcePath : resourcePaths) {
130 if (resourcePath.startsWith("/WEB-INF/ext-") &&
131 resourcePath.endsWith("-ext.xml")) {
132
133 String servletContextName = resourcePath.substring(
134 13, resourcePath.length() - 4);
135
136 Set<String> fileNames = _readExtFileNames(
137 servletContext, resourcePath);
138
139 _extMap.put(servletContextName, fileNames);
140 }
141 }
142 }
143
144 private static Set<String> _readExtFileNames(
145 ServletContext servletContext, String resourcePath)
146 throws Exception {
147
148 Set<String> fileNames = new TreeSet<String>();
149
150 Document document = UnsecureSAXReaderUtil.read(
151 servletContext.getResourceAsStream(resourcePath));
152
153 Element rootElement = document.getRootElement();
154
155 Element filesElement = rootElement.element("files");
156
157 List<Element> fileElements = filesElement.elements("file");
158
159 for (Element fileElement : fileElements) {
160 String fileName = fileElement.getText();
161
162 if (!isIgnoredFileName(fileName)) {
163 fileNames.add(fileName);
164 }
165 }
166
167 return fileNames;
168 }
169
170 private static final String[] _IGNORED_FILE_NAMES = new String[] {
171 "log4j.dtd", "service.xml", "sql/"
172 };
173
174 private static final String[] _SUPPORTED_MERGING_FILE_NAMES = new String[] {
175 "content/Language-ext", "ext-hbm.xml", "ext-model-hints.xml",
176 "ext-spring.xml", "portal-log4j-ext.xml"
177 };
178
179 private static Map<String, Set<String>> _extMap =
180 new HashMap<String, Set<String>>();
181
182 }