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