KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > TagLibConfiguration


1 //========================================================================
2
//$Id: TagLibConfiguration.java,v 1.4 2005/08/13 00:01:27 gregwilkins Exp $
3
//Copyright 2004 Mort Bay Consulting Pty. Ltd.
4
//------------------------------------------------------------------------
5
//Licensed under the Apache License, Version 2.0 (the "License");
6
//you may not use this file except in compliance with the License.
7
//You may obtain a copy of the License at
8
//http://www.apache.org/licenses/LICENSE-2.0
9
//Unless required by applicable law or agreed to in writing, software
10
//distributed under the License is distributed on an "AS IS" BASIS,
11
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//See the License for the specific language governing permissions and
13
//limitations under the License.
14
//========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.util.EventListener JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.mortbay.log.LogFactory;
25 import org.mortbay.jetty.servlet.WebApplicationContext.Configuration;
26 import org.mortbay.util.Resource;
27 import org.mortbay.xml.XmlParser;
28
29 /* ------------------------------------------------------------ */
30 /** TagLibConfiguration.
31  *
32  * The class searches for TLD descriptors found in web.xml, in WEB-INF/*.tld files of the web app
33  * or *.tld files withing jars found in WEB-INF/lib of the webapp. Any listeners defined in these
34  * tld's are added to the context.
35  *
36  * <bile>This is total rubbish special case for JSPs! If there was a general use-case for web app
37  * frameworks to register listeners directly, then a generic mechanism could have been added to the servlet
38  * spec. Instead some special purpose JSP support is required that breaks all sorts of encapsualtion rules as
39  * the servlet container must go searching for and then parsing the descriptors for one particular framework.
40  * It only appears to be used by JSF, which is being developed by the same developer who implemented this
41  * feature in the first place!
42  * </bile>
43  *
44  * @author gregw
45  *
46  */

47 public class TagLibConfiguration implements Configuration
48 {
49     private static Log log=LogFactory.getLog(TagLibConfiguration.class);
50     WebApplicationContext _context;
51     
52     /* ------------------------------------------------------------ */
53     /**
54      */

55     public TagLibConfiguration()
56     {
57         super();
58     }
59
60     /* ------------------------------------------------------------ */
61     /*
62      * @see org.mortbay.jetty.servlet.WebApplicationContext.Configuration#setWebApplicationContext(org.mortbay.jetty.servlet.WebApplicationContext)
63      */

64     public void setWebApplicationContext(WebApplicationContext context)
65     {
66         this._context=context;
67     }
68
69     /* ------------------------------------------------------------ */
70     /*
71      * @see org.mortbay.jetty.servlet.WebApplicationContext.Configuration#getWebApplicationContext()
72      */

73     public WebApplicationContext getWebApplicationContext()
74     {
75         return _context;
76     }
77
78     /* ------------------------------------------------------------ */
79     /*
80      * @see org.mortbay.jetty.servlet.WebApplicationContext.Configuration#configureClassPath()
81      */

82     public void configureClassPath() throws Exception JavaDoc
83     {
84     }
85
86     /* ------------------------------------------------------------ */
87     /*
88      * @see org.mortbay.jetty.servlet.WebApplicationContext.Configuration#configureDefaults()
89      */

90     public void configureDefaults() throws Exception JavaDoc
91     {
92     }
93
94     /* ------------------------------------------------------------ */
95     /*
96      * @see org.mortbay.jetty.servlet.WebApplicationContext.Configuration#configureWebApp()
97      */

98     public void configureWebApp() throws Exception JavaDoc
99     {
100         Set JavaDoc tlds = new HashSet JavaDoc();
101         
102         // Find tld's from web.xml
103
// When the XMLConfigurator (or other configurator) parsed the web.xml,
104
// It should have created aliases for all TLDs. So search resources aliases
105
// for aliases ending in tld
106
if (_context.getResourceAliases()!=null)
107         {
108             Iterator JavaDoc iter=_context.getResourceAliases().values().iterator();
109             while(iter.hasNext())
110             {
111                 String JavaDoc location = (String JavaDoc)iter.next();
112                 if (location!=null && location.toLowerCase().endsWith(".tld"))
113                 {
114                     if (!location.startsWith("/"))
115                         location="/WEB-INF/"+location;
116                     Resource l=_context.getBaseResource().addPath(location);
117                     tlds.add(l);
118                 }
119             }
120         }
121         
122         // Look for any tlds in WEB-INF directly.
123
if (_context.getWebInf()!=null)
124         {
125             String JavaDoc[] contents = _context.getWebInf().list();
126             for (int i=0;i<contents.length;i++)
127             {
128                 if (contents[i]!=null && contents[i].toLowerCase().endsWith(".tld"))
129                 {
130                     Resource l=_context.getWebInf().addPath(contents[i]);
131                     tlds.add(l);
132                 }
133                 
134             }
135
136             // Look for any tlds in the META-INF of included jars
137
Resource lib=_context.getWebInf().addPath("lib/");
138             if (lib.exists() && lib.isDirectory())
139             {
140                 contents = lib.list();
141                 for (int i=0;i<contents.length;i++)
142                 {
143                     if (contents[i]!=null && contents[i].toLowerCase().endsWith(".jar"))
144                     {
145                         Resource l=lib.addPath(contents[i]);
146                         Resource meta=Resource.newResource("jar:"+l+"!/META-INF/");
147                         if (meta.exists())
148                         {
149                             String JavaDoc[] meta_contents=meta.list();
150                             
151                             for (int j=0;j<meta_contents.length;j++)
152                             {
153                                 if (meta_contents[j]!=null && meta_contents[j].toLowerCase().endsWith(".tld"))
154                                 {
155                                     Resource t=meta.addPath(meta_contents[j]);
156                                     tlds.add(t);
157                                 }
158                             }
159                         }
160                     }
161                 }
162             }
163         }
164         
165         // Create a TLD parser
166
XmlParser parser = new XmlParser(false);
167         
168         parser.redirectEntity("web-jsptaglibrary_1_1.dtd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_1_1.dtd"));
169         parser.redirectEntity("web-jsptaglibrary_1_2.dtd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd"));
170         parser.redirectEntity("web-jsptaglibrary_2_0.xsd",WebApplicationContext.class.getResource("/javax/servlet/jsp/resources/web-jsptaglibrary_2_0.xsd"));
171         parser.setXpath("/taglib/listener/listener-class");
172         // Parse all the discovered TLDs
173
Iterator JavaDoc iter = tlds.iterator();
174         while (iter.hasNext())
175         {
176             try
177             {
178                 Resource tld = (Resource)iter.next();
179                 if (log.isDebugEnabled()) log.debug("TLD="+tld);
180                 
181                 XmlParser.Node root = parser.parse(tld.getURL());
182                 
183                 for (int i=0;i<root.size();i++)
184                 {
185                     Object JavaDoc o=root.get(i);
186                     if (o instanceof XmlParser.Node)
187                     {
188                         XmlParser.Node node = (XmlParser.Node)o;
189                         if ("listener".equals(node.getTag()))
190                         {
191                             String JavaDoc className=node.getString("listener-class",false,true);
192                             if (log.isDebugEnabled()) log.debug("listener="+className);
193                             
194                             try
195                             {
196                                 Class JavaDoc listenerClass=getWebApplicationContext().loadClass(className);
197                                 EventListener JavaDoc l=(EventListener JavaDoc)listenerClass.newInstance();
198                                 _context.addEventListener(l);
199                             }
200                             catch(Exception JavaDoc e)
201                             {
202                                 log.warn("Could not instantiate listener "+className,e);
203                             }
204                         }
205                     }
206                 }
207             }
208             catch(Exception JavaDoc e)
209             {
210                 log.warn(e);
211             }
212         }
213     }
214
215 }
216
Popular Tags