1 16 package org.mortbay.jetty.servlet; 17 18 import java.util.EventListener ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Set ; 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 47 public class TagLibConfiguration implements Configuration 48 { 49 private static Log log=LogFactory.getLog(TagLibConfiguration.class); 50 WebApplicationContext _context; 51 52 53 55 public TagLibConfiguration() 56 { 57 super(); 58 } 59 60 61 64 public void setWebApplicationContext(WebApplicationContext context) 65 { 66 this._context=context; 67 } 68 69 70 73 public WebApplicationContext getWebApplicationContext() 74 { 75 return _context; 76 } 77 78 79 82 public void configureClassPath() throws Exception 83 { 84 } 85 86 87 90 public void configureDefaults() throws Exception 91 { 92 } 93 94 95 98 public void configureWebApp() throws Exception 99 { 100 Set tlds = new HashSet (); 101 102 if (_context.getResourceAliases()!=null) 107 { 108 Iterator iter=_context.getResourceAliases().values().iterator(); 109 while(iter.hasNext()) 110 { 111 String location = (String )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 if (_context.getWebInf()!=null) 124 { 125 String [] 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 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 [] 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 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 Iterator 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 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 className=node.getString("listener-class",false,true); 192 if (log.isDebugEnabled()) log.debug("listener="+className); 193 194 try 195 { 196 Class listenerClass=getWebApplicationContext().loadClass(className); 197 EventListener l=(EventListener )listenerClass.newInstance(); 198 _context.addEventListener(l); 199 } 200 catch(Exception e) 201 { 202 log.warn("Could not instantiate listener "+className,e); 203 } 204 } 205 } 206 } 207 } 208 catch(Exception e) 209 { 210 log.warn(e); 211 } 212 } 213 } 214 215 } 216 | Popular Tags |