1 16 package org.apache.myfaces.webapp.webxml; 17 18 import org.apache.myfaces.util.ClassUtils; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.faces.context.ExternalContext; 24 import javax.faces.webapp.FacesServlet; 25 import java.util.*; 26 27 47 public class WebXml 48 { 49 private static final Log log = LogFactory.getLog(WebXmlParser.class); 50 51 private Map _servlets = new HashMap(); 52 private Map _servletMappings = new HashMap(); 53 private List _facesServletMappings = null; 54 55 void addServlet(String servletName, String servletClass) 56 { 57 if (_servlets.get(servletName) != null) 58 { 59 log.warn("Servlet " + servletName + " defined more than once, first definition will be used."); 60 } 61 else 62 { 63 _servlets.put(servletName, servletClass); 64 } 65 } 66 67 boolean containsServlet(String servletName) 68 { 69 return _servlets.containsKey(servletName); 70 } 71 72 void addServletMapping(String servletName, String urlPattern) 73 { 74 List mappings = (List)_servletMappings.get(servletName); 75 if (mappings == null) 76 { 77 mappings = new ArrayList(); 78 _servletMappings.put(servletName, mappings); 79 } 80 mappings.add(urlPattern); 81 } 82 83 public List getFacesServletMappings() 84 { 85 if (_facesServletMappings != null) return _facesServletMappings; 86 87 _facesServletMappings = new ArrayList(); 88 for (Iterator it = _servlets.entrySet().iterator(); it.hasNext(); ) 89 { 90 Map.Entry entry = (Map.Entry)it.next(); 91 String servletName = (String )entry.getKey(); 92 if (null == entry.getValue()) 93 { 94 continue; 101 } 102 Class servletClass = ClassUtils.simpleClassForName((String )entry.getValue()); 103 if (FacesServlet.class.isAssignableFrom(servletClass)) 104 { 105 List urlPatterns = (List)_servletMappings.get(servletName); 106 for (Iterator it2 = urlPatterns.iterator(); it2.hasNext(); ) 107 { 108 String urlpattern = (String )it2.next(); 109 _facesServletMappings.add(new ServletMapping(servletName, 110 servletClass, 111 urlpattern)); 112 if (log.isTraceEnabled()) 113 log.trace("adding mapping for servlet + " + servletName + " urlpattern = " + urlpattern); } 114 } 115 else 116 { 117 if (log.isTraceEnabled()) log.trace("ignoring servlet + " + servletName + " " + servletClass + " (no FacesServlet)"); 118 } 119 } 120 return _facesServletMappings; 121 } 122 123 124 private static final String WEB_XML_ATTR = WebXml.class.getName(); 125 public static WebXml getWebXml(ExternalContext context) 126 { 127 WebXml webXml = (WebXml)context.getApplicationMap().get(WEB_XML_ATTR); 128 if (webXml == null) 129 { 130 log.error(WebXml.class.getName() + ".init must be called before!"); 131 throw new IllegalStateException (WebXml.class.getName() + ".init must be called before!"); 132 } 133 return webXml; 134 } 135 136 140 public static void init(ExternalContext context) 141 { 142 WebXmlParser parser = new WebXmlParser(context); 143 WebXml webXml = parser.parse(); 144 context.getApplicationMap().put(WEB_XML_ATTR, webXml); 145 } 146 } 147 | Popular Tags |