KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > webapp > webxml > WebXml


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
28  * @author Manfred Geiler (latest modification by $Author: matze $)
29  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:51:01 $
30  * $Log: WebXml.java,v $
31  * Revision 1.3 2004/10/13 11:51:01 matze
32  * renamed packages to org.apache
33  *
34  * Revision 1.2 2004/08/10 10:57:39 manolito
35  * fixed StackOverflow in ClassUtils and cleaned up ClassUtils methods
36  *
37  * Revision 1.1 2004/07/16 15:16:10 royalts
38  * moved org.apache.myfaces.webapp.webxml and org.apache.util.xml to share src-tree (needed WebXml for JspTilesViewHandlerImpl)
39  *
40  * Revision 1.10 2004/07/01 22:05:16 mwessendorf
41  * ASF switch
42  *
43  * Revision 1.9 2004/04/19 13:03:21 manolito
44  * Log
45  *
46  */

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 JavaDoc servletName, String JavaDoc 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 JavaDoc servletName)
68     {
69         return _servlets.containsKey(servletName);
70     }
71
72     void addServletMapping(String JavaDoc servletName, String JavaDoc 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 JavaDoc servletName = (String JavaDoc)entry.getKey();
92             if (null == entry.getValue())
93             {
94                 // the value is null in the case of jsp files listed as servlets
95
// in cactus
96
// <servlet>
97
// <servlet-name>JspRedirector</servlet-name>
98
// <jsp-file>/jspRedirector.jsp</jsp-file>
99
// </servlet>
100
continue;
101             }
102             Class JavaDoc servletClass = ClassUtils.simpleClassForName((String JavaDoc)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 JavaDoc urlpattern = (String JavaDoc)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 JavaDoc 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 JavaDoc(WebXml.class.getName() + ".init must be called before!");
132         }
133         return webXml;
134     }
135
136     /**
137      * should be called when initialising Servlet
138      * @param context
139      */

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