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.xml.Document;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.kernel.xml.SAXReaderUtil;
020    
021    import java.util.Collections;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Set;
027    import java.util.TreeSet;
028    
029    import javax.servlet.ServletContext;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
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> files = _readExtFiles(
043                            servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
044    
045                    Iterator<Map.Entry<String, Set<String>>> itr =
046                            _extMap.entrySet().iterator();
047    
048                    Map<String, Set<String>> conflicts = new HashMap<String, Set<String>>();
049    
050                    while (itr.hasNext()) {
051                            Map.Entry<String, Set<String>> entry = itr.next();
052    
053                            String curServletContextName = entry.getKey();
054                            Set<String> curFiles = entry.getValue();
055    
056                            for (String file : files) {
057                                    if (!curFiles.contains(file)) {
058                                            continue;
059                                    }
060    
061                                    Set<String> conflictFiles = conflicts.get(
062                                            curServletContextName);
063    
064                                    if (conflictFiles == null) {
065                                            conflictFiles = new TreeSet<String>();
066    
067                                            conflicts.put(curServletContextName, conflictFiles);
068                                    }
069    
070                                    conflictFiles.add(file);
071                            }
072                    }
073    
074                    return conflicts;
075            }
076    
077            public static Set<String> getServletContextNames() {
078                    return Collections.unmodifiableSet(_extMap.keySet());
079            }
080    
081            public static boolean isRegistered(String servletContextName) {
082                    if (_extMap.containsKey(servletContextName)) {
083                            return true;
084                    }
085                    else {
086                            return false;
087                    }
088            }
089    
090            public static void registerExt(ServletContext servletContext)
091                    throws Exception {
092    
093                    String servletContextName = servletContext.getServletContextName();
094    
095                    Set<String> files = _readExtFiles(
096                            servletContext, "/WEB-INF/ext-" + servletContextName + ".xml");
097    
098                    _extMap.put(servletContextName, files);
099            }
100    
101            public static void registerPortal(ServletContext servletContext)
102                    throws Exception {
103    
104                    Set<String> resourcePaths = servletContext.getResourcePaths(
105                            "/WEB-INF");
106    
107                    for (String resourcePath : resourcePaths) {
108                            if (resourcePath.startsWith("/WEB-INF/ext-") &&
109                                    resourcePath.endsWith("-ext.xml")) {
110    
111                                    String servletContextName = resourcePath.substring(
112                                            13, resourcePath.length() - 4);
113    
114                                    Set<String> files = _readExtFiles(
115                                            servletContext, resourcePath);
116    
117                                    _extMap.put(servletContextName, files);
118                            }
119                    }
120            }
121    
122            private static Set<String> _readExtFiles(
123                            ServletContext servletContext, String resourcePath)
124                    throws Exception {
125    
126                    Set<String> files = new TreeSet<String>();
127    
128                    Document document = SAXReaderUtil.read(
129                            servletContext.getResourceAsStream(resourcePath));
130    
131                    Element rootElement = document.getRootElement();
132    
133                    Element filesElement = rootElement.element("files");
134    
135                    List<Element> fileElements = filesElement.elements("file");
136    
137                    for (Element fileElement : fileElements) {
138                            files.add(fileElement.getText());
139                    }
140    
141                    return files;
142            }
143    
144            private static Map<String, Set<String>> _extMap =
145                    new HashMap<String, Set<String>>();
146    
147    }