1 16 package org.jboss.web.tomcat.tc6.jasper; 17 18 import java.util.Set ; 19 import java.util.Iterator ; 20 import java.util.HashMap ; 21 import java.util.ArrayList ; 22 import java.util.jar.JarEntry ; 23 import java.util.jar.JarInputStream ; 24 import java.io.InputStream ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 import java.net.MalformedURLException ; 31 import javax.servlet.ServletContext ; 32 import java.util.Map ; 34 import org.apache.jasper.compiler.TldLocationsCache; 35 import org.apache.jasper.compiler.Localizer; 36 import org.apache.jasper.JasperException; 37 import org.apache.jasper.Constants; 38 import org.apache.jasper.xmlparser.TreeNode; 39 import org.apache.jasper.xmlparser.ParserUtils; 40 import org.jboss.logging.Logger; 41 42 48 public class TagLibCache extends TldLocationsCache 49 { 50 private static final String WEB_XML = "/WEB-INF/web.xml"; 51 private static final String JAR_FILE_SUFFIX = ".jar"; 52 53 private static Logger log = Logger.getLogger(TagLibCache.class); 54 55 private ServletContext ctx; 56 private HashMap mappings; 57 private ArrayList tagLibJars; 58 59 public TagLibCache(ServletContext ctx, ArrayList tagLibJars) 60 { 61 super(ctx, true); 62 this.ctx = ctx; 63 this.tagLibJars = tagLibJars; 64 } 65 66 80 public String [] getLocation(String uri) throws JasperException 81 { 82 if (mappings == null) 83 init(); 84 String [] locations = (String []) mappings.get(uri); 85 return locations; 86 } 87 88 private synchronized void init() throws JasperException 89 { 90 if (mappings != null) 91 { 92 return; 93 } 94 95 HashMap tmpMappings = null; 96 try 97 { 98 tmpMappings = new HashMap (); 99 processWebDotXml(tmpMappings); 100 loadStandardTlds(tmpMappings); 101 processTldsInFileSystem("/WEB-INF/", tmpMappings); 102 } 103 catch (Exception ex) 104 { 105 String msg = Localizer.getMessage("jsp.error.internal.tldinit", ex.getMessage()); 106 throw new JasperException(msg, ex); 107 } 108 finally 109 { 110 mappings = tmpMappings; 111 } 112 } 113 114 117 protected void processWebDotXml(Map tmpMappings) throws Exception 118 { 119 120 InputStream is = null; 121 122 try 123 { 124 String altDDName = (String ) ctx.getAttribute(Constants.ALT_DD_ATTR); 126 if (altDDName != null) 127 { 128 try 129 { 130 is = new FileInputStream (altDDName); 131 } 132 catch (FileNotFoundException e) 133 { 134 log.warn(Localizer.getMessage("jsp.error.internal.filenotfound", 135 altDDName)); 136 } 137 } 138 else 139 { 140 is = ctx.getResourceAsStream(WEB_XML); 141 if (is == null) 142 { 143 log.warn(Localizer.getMessage("jsp.error.internal.filenotfound", 144 WEB_XML)); 145 } 146 } 147 148 if (is == null) 149 { 150 return; 151 } 152 153 TreeNode webtld = null; 155 if (altDDName != null) 157 { 158 webtld = new ParserUtils().parseXMLDocument(altDDName, is); 159 } 160 else 161 { 162 webtld = new ParserUtils().parseXMLDocument(WEB_XML, is); 163 } 164 165 TreeNode jspConfig = webtld.findChild("jsp-config"); 167 if (jspConfig != null) 168 { 169 webtld = jspConfig; 170 } 171 Iterator taglibs = webtld.findChildren("taglib"); 172 while (taglibs.hasNext()) 173 { 174 175 TreeNode taglib = (TreeNode) taglibs.next(); 177 String tagUri = null; 178 String tagLoc = null; 179 TreeNode child = taglib.findChild("taglib-uri"); 180 if (child != null) 181 tagUri = child.getBody(); 182 child = taglib.findChild("taglib-location"); 183 if (child != null) 184 tagLoc = child.getBody(); 185 186 if (tagLoc == null) 188 continue; 189 if (uriType(tagLoc) == NOROOT_REL_URI) 190 tagLoc = "/WEB-INF/" + tagLoc; 191 String tagLoc2 = null; 192 if (tagLoc.endsWith(JAR_FILE_SUFFIX)) 193 { 194 tagLoc = ctx.getResource(tagLoc).toString(); 195 tagLoc2 = "META-INF/taglib.tld"; 196 } 197 tmpMappings.put(tagUri, new String []{tagLoc, tagLoc2}); } 199 } 200 finally 201 { 202 if (is != null) 203 { 204 try 205 { 206 is.close(); 207 } 208 catch (Throwable t) 209 { 210 } 211 } 212 } 213 } 214 215 protected void loadStandardTlds(Map tmpMappings) throws MalformedURLException { 217 if( tagLibJars.size() == 0 ) 218 return; 219 220 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 222 URL web = loader.getResource("conf/web.xml"); 223 URL sarURL = new URL (web, ".."); 224 for(int n = 0; n < tagLibJars.size(); n ++) 225 { 226 String jarPath = (String ) tagLibJars.get(n); 227 try 228 { 229 URL url = new URL (sarURL, jarPath); 230 String resourcePath = url.toString(); 231 log.debug("Scanning for tlds in: "+resourcePath); 232 URLConnection conn = url.openConnection(); 233 conn.setUseCaches(false); 234 scanJar(conn, resourcePath, true, tmpMappings); } 236 catch (Exception e) 237 { 238 log.debug("Failed to scan: "+jarPath, e); 239 } 240 } 241 } 242 243 248 protected void processTldsInFileSystem(String startPath, Map tmpMappings) throws Exception 250 { 251 252 Set dirList = ctx.getResourcePaths(startPath); 253 if (dirList != null) 254 { 255 Iterator it = dirList.iterator(); 256 while (it.hasNext()) 257 { 258 String path = (String ) it.next(); 259 if (path.endsWith("/")) 260 { 261 processTldsInFileSystem(path, tmpMappings); } 263 if( path.endsWith(".jar") ) 264 { 265 URL resURL = ctx.getResource(path); 266 URLConnection conn = resURL.openConnection(); 267 conn.setUseCaches(false); 268 this.scanJar(conn, resURL.toString(), false, tmpMappings); } 270 else if ( path.endsWith(".tld") == true ) 271 { 272 InputStream stream = ctx.getResourceAsStream(path); 273 String uri = null; 274 try 275 { 276 uri = getUriFromTld(path, stream); 277 } 278 finally 279 { 280 if (stream != null) 281 { 282 try 283 { 284 stream.close(); 285 } 286 catch (Throwable t) 287 { 288 } 290 } 291 } 292 if (uri != null && tmpMappings.get(uri) == null) { 296 tmpMappings.put(uri, new String []{path, null}); } 298 } 299 } 300 } 301 } 302 303 311 private void scanJar(URLConnection conn, String resourcePath, boolean ignore, Map tmpMappings) throws JasperException, IOException 313 { 314 InputStream connIS = conn.getInputStream(); 315 JarInputStream jis = new JarInputStream (connIS); 316 try 317 { 318 JarEntry entry = jis.getNextJarEntry(); 319 while( entry != null ) 320 { 321 String name = entry.getName(); 322 if( name.endsWith(".tld") == false ) 323 { 324 entry = jis.getNextJarEntry(); 325 continue; 326 } 327 328 EntryInputStream eis = new EntryInputStream(jis); 329 String uri = getUriFromTld(resourcePath, eis); 330 if (uri != null && tmpMappings.get(uri) == null) { 334 tmpMappings.put(uri, new String []{resourcePath, name}); } 336 entry = jis.getNextJarEntry(); 337 } 338 } 339 catch (Exception ex) 340 { 341 if (!ignore) 342 { 343 throw new JasperException(ex); 344 } 345 } 346 finally 347 { 348 if( jis != null ) 349 { 350 try 351 { 352 jis.close(); 353 } 354 catch (Throwable t) 355 { 356 } 358 } 359 360 if (connIS != null) 361 { 362 try 363 { 364 connIS.close(); 365 } 366 catch (Throwable t) 367 { 368 } 370 } 371 } 372 } 373 374 378 private String getUriFromTld(String resourcePath, InputStream in) 379 throws JasperException 380 { 381 TreeNode tld = new ParserUtils().parseXMLDocument(resourcePath, in); 383 TreeNode uri = tld.findChild("uri"); 384 if (uri != null) 385 { 386 String body = uri.getBody(); 387 if (body != null) 388 return body; 389 } 390 391 return null; 392 } 393 394 398 static class EntryInputStream extends InputStream 399 { 400 private JarInputStream jis; 401 EntryInputStream(JarInputStream jis) 402 { 403 this.jis = jis; 404 } 405 406 public int read() throws IOException 407 { 408 return jis.read(); 409 } 410 411 public int available() throws IOException 412 { 413 return jis.available(); 414 } 415 416 public void close() throws IOException 417 { 418 419 } 420 421 public void reset() throws IOException 422 { 423 jis.reset(); 424 } 425 426 public boolean markSupported() 427 { 428 return jis.markSupported(); 429 } 430 431 public synchronized void mark(int readlimit) 432 { 433 jis.mark(readlimit); 434 } 435 436 public long skip(long n) throws IOException 437 { 438 return jis.skip(n); 439 } 440 441 public int read(byte b[], int off, int len) throws IOException 442 { 443 return jis.read(b, off, len); 444 } 445 } 446 } 447 | Popular Tags |