1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.compiler;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.xml.sax.InputSource;
import javax.servlet.ServletContext;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.xmlparser.ParserUtils;
import org.apache.jasper.xmlparser.TreeNode;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* A container for all tag libraries that are defined "globally"
* for the web application.
*
* Tag Libraries can be defined globally in one of two ways:
* 1. Via <taglib> elements in web.xml:
* the uri and location of the tag-library are specified in
* the <taglib> element.
* 2. Via packaged jar files that contain .tld files
* within the META-INF directory, or some subdirectory
* of it. The taglib is 'global' if it has the <uri>
* element defined.
*
* A mapping between the taglib URI and its associated TaglibraryInfoImpl
* is maintained in this container.
* Actually, that's what we'd like to do. However, because of the
* way the classes TagLibraryInfo and TagInfo have been defined,
* it is not currently possible to share an instance of TagLibraryInfo
* across page invocations. A bug has been submitted to the spec lead.
* In the mean time, all we do is save the 'location' where the
* TLD associated with a taglib URI can be found.
*
* When a JSP page has a taglib directive, the mappings in this container
* are first searched (see method getLocation()).
* If a mapping is found, then the location of the TLD is returned.
* If no mapping is found, then the uri specified
* in the taglib directive is to be interpreted as the location for
* the TLD of this tag library.
*
* @author Pierre Delisle
* @author Jan Luehe
*/
public class TldLocationsCache {
// Logger
private Log log = LogFactory.getLog(TldLocationsCache.class);
/**
* The types of URI one may specify for a tag library
*/
public static final int ABS_URI = 0;
public static final int ROOT_REL_URI = 1;
public static final int NOROOT_REL_URI = 2;
private static final String WEB_XML = "/WEB-INF/web.xml";
private static final String FILE_PROTOCOL = "file:";
private static final String JAR_FILE_SUFFIX = ".jar";
// Names of JARs that are known not to contain any TLDs
private static HashSet<String> noTldJars;
/**
* The mapping of the 'global' tag library URI to the location (resource
* path) of the TLD associated with that tag library. The location is
* returned as a String array:
* [0] The location
* [1] If the location is a jar file, this is the location of the tld.
*/
private Hashtable mappings;
private boolean initialized;
private ServletContext ctxt;
private boolean redeployMode;
//*********************************************************************
// Constructor and Initilizations
/*
* Initializes the set of JARs that are known not to contain any TLDs
*/
static {
noTldJars = new HashSet<String>();
// Bootstrap JARs
noTldJars.add("bootstrap.jar");
noTldJars.add("commons-daemon.jar");
noTldJars.add("tomcat-juli.jar");
// Main JARs
noTldJars.add("annotations-api.jar");
noTldJars.add("catalina.jar");
noTldJars.add("catalina-ant.jar");
noTldJars.add("catalina-ha.jar");
noTldJars.add("catalina-tribes.jar");
noTldJars.add("el-api.jar");
noTldJars.add("jasper.jar");
noTldJars.add("jasper-el.jar");
noTldJars.add("ecj-3.7.jar");
noTldJars.add("ecj-3.7.1.jar");
noTldJars.add("ecj-3.7.2.jar");
noTldJars.add("ecj-4.2.1.jar");
noTldJars.add("ecj-4.2.2.jar");
noTldJars.add("ecj-4.3.1.jar");
noTldJars.add("ecj-4.3.2.jar");
noTldJars.add("ecj-P20140317-1600.jar");
noTldJars.add("jsp-api.jar");
noTldJars.add("servlet-api.jar");
noTldJars.add("tomcat-coyote.jar");
noTldJars.add("tomcat-dbcp.jar");
// i18n JARs
noTldJars.add("tomcat-i18n-en.jar");
noTldJars.add("tomcat-i18n-es.jar");
noTldJars.add("tomcat-i18n-fr.jar");
noTldJars.add("tomcat-i18n-ja.jar");
// Misc JARs not included with Tomcat
noTldJars.add("ant.jar");
noTldJars.add("commons-dbcp.jar");
noTldJars.add("commons-beanutils.jar");
noTldJars.add("commons-fileupload-1.0.jar");
noTldJars.add("commons-pool.jar");
noTldJars.add("commons-digester.jar");
noTldJars.add("commons-logging.jar");
noTldJars.add("commons-collections.jar");
noTldJars.add("jmx.jar");
noTldJars.add("jmx-tools.jar");
noTldJars.add("xercesImpl.jar");
noTldJars.add("xmlParserAPIs.jar");
noTldJars.add("xml-apis.jar");
// JARs from J2SE runtime
noTldJars.add("sunjce_provider.jar");
noTldJars.add("ldapsec.jar");
noTldJars.add("localedata.jar");
noTldJars.add("dnsns.jar");
noTldJars.add("tools.jar");
noTldJars.add("sunpkcs11.jar");
}
public TldLocationsCache(ServletContext ctxt) {
this(ctxt, true);
}
/** Constructor.
*
* @param ctxt the servlet context of the web application in which Jasper
* is running
* @param redeployMode if true, then the compiler will allow redeploying
* a tag library from the same jar, at the expense of slowing down the
* server a bit. Note that this may only work on JDK 1.3.1_01a and later,
* because of JDK bug 4211817 fixed in this release.
* If redeployMode is false, a faster but less capable mode will be used.
*/
public TldLocationsCache(ServletContext ctxt, boolean redeployMode) {
this.ctxt = ctxt;
this.redeployMode = redeployMode;
mappings = new Hashtable();
initialized = false;
}
/**
* Sets the list of JARs that are known not to contain any TLDs.
*
* @param jarNames List of comma-separated names of JAR files that are
* known not to contain any TLDs
*/
public static void setNoTldJars(String jarNames) {
if (jarNames != null) {
noTldJars.clear();
StringTokenizer tokenizer = new StringTokenizer(jarNames, ",");
while (tokenizer.hasMoreElements()) {
noTldJars.add(tokenizer.nextToken());
}
}
}
/**
* Gets the 'location' of the TLD associated with the given taglib 'uri'.
*
* Returns null if the uri is not associated with any tag library 'exposed'
* in the web application. A tag library is 'exposed' either explicitly in
* web.xml or implicitly via the uri tag in the TLD of a taglib deployed
* in a jar file (WEB-INF/lib).
*
* @param uri The taglib uri
*
* @return An array of two Strings: The first element denotes the real
* path to the TLD. If the path to the TLD points to a jar file, then the
* second element denotes the name of the TLD entry in the jar file.
* Returns null if the uri is not associated with any tag library 'exposed'
* in the web application.
*/
public String[] getLocation(String uri) throws JasperException {
if (!initialized) {
init();
}
return (String[]) mappings.get(uri);
}
/**
* Returns the type of a URI:
* ABS_URI
* ROOT_REL_URI
* NOROOT_REL_URI
*/
public static int uriType(String uri) {
if (uri.indexOf(':') != -1) {
return ABS_URI;
} else if (uri.startsWith("/")) {
return ROOT_REL_URI;
} else {
return NOROOT_REL_URI;
}
}
private void init() throws JasperException {
if (initialized) return;
try {
processWebDotXml();
scanJars();
processTldsInFileSystem("/WEB-INF/");
initialized = true;
} catch (Exception ex) {
throw new JasperException(Localizer.getMessage(
"jsp.error.internal.tldinit", ex.getMessage()));
}
}
/*
* Populates taglib map described in web.xml.
*/
private void processWebDotXml() throws Exception {
InputStream is = null;
try {
// Acquire input stream to web application deployment descriptor
String altDDName = (String)ctxt.getAttribute(
Constants.ALT_DD_ATTR);
URL uri = null;
if (altDDName != null) {
try {
uri = new URL(FILE_PROTOCOL+altDDName.replace('\\', '/'));
} catch (MalformedURLException e) {
if (log.isWarnEnabled()) {
log.warn(Localizer.getMessage(
"jsp.error.internal.filenotfound",
altDDName));
}
}
} else {
uri = ctxt.getResource(WEB_XML);
if (uri == null && log.isWarnEnabled()) {
log.warn(Localizer.getMessage(
"jsp.error.internal.filenotfound",
WEB_XML));
}
}
if (uri == null) {
return;
}
is = uri.openStream();
InputSource ip = new InputSource(is);
ip.setSystemId(uri.toExternalForm());
boolean validate = Boolean.parseBoolean(
ctxt.getInitParameter(
Constants.XML_VALIDATION_INIT_PARAM));
String blockExternalString = ctxt.getInitParameter(
Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
boolean blockExternal;
if (blockExternalString == null) {
blockExternal = true;
} else {
blockExternal = Boolean.parseBoolean(blockExternalString);
}
// Parse the web application deployment descriptor
ParserUtils pu = new ParserUtils(validate, blockExternal);
TreeNode webtld = null;
// altDDName is the absolute path of the DD
if (altDDName != null) {
webtld = pu.parseXMLDocument(altDDName, ip);
} else {
webtld = pu.parseXMLDocument(WEB_XML, ip);
}
// Allow taglib to be an element of the root or jsp-config (JSP2.0)
TreeNode jspConfig = webtld.findChild("jsp-config");
if (jspConfig != null) {
webtld = jspConfig;
}
Iterator taglibs = webtld.findChildren("taglib");
while (taglibs.hasNext()) {
// Parse the next <taglib> element
TreeNode taglib = (TreeNode) taglibs.next();
String tagUri = null;
String tagLoc = null;
TreeNode child = taglib.findChild("taglib-uri");
if (child != null)
tagUri = child.getBody();
child = taglib.findChild("taglib-location");
if (child != null)
tagLoc = child.getBody();
// Save this location if appropriate
if (tagLoc == null)
continue;
if (uriType(tagLoc) == NOROOT_REL_URI)
tagLoc = "/WEB-INF/" + tagLoc;
String tagLoc2 = null;
if (tagLoc.endsWith(JAR_FILE_SUFFIX)) {
tagLoc = ctxt.getResource(tagLoc).toString();
tagLoc2 = "META-INF/taglib.tld";
}
mappings.put(tagUri, new String[] { tagLoc, tagLoc2 });
}
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable t) {}
}
}
}
/**
* Scans the given JarURLConnection for TLD files located in META-INF
* (or a subdirectory of it), adding an implicit map entry to the taglib
* map for any TLD that has a <uri> element.
*
* @param conn The JarURLConnection to the JAR file to scan
* @param ignore true if any exceptions raised when processing the given
* JAR should be ignored, false otherwise
*/
private void scanJar(JarURLConnection conn, boolean ignore)
throws JasperException {
JarFile jarFile = null;
String resourcePath = conn.getJarFileURL().toString();
try {
if (redeployMode) {
conn.setUseCaches(false);
}
jarFile = conn.getJarFile();
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
String name = entry.getName();
if (!name.startsWith("META-INF/")) continue;
if (!name.endsWith(".tld")) continue;
InputStream stream = jarFile.getInputStream(entry);
try {
String uri = getUriFromTld(resourcePath, stream);
// Add implicit map entry only if its uri is not already
// present in the map
if (uri != null && mappings.get(uri) == null) {
mappings.put(uri, new String[]{ resourcePath, name });
}
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
// do nothing
}
}
}
}
} catch (Exception ex) {
if (!redeployMode) {
// if not in redeploy mode, close the jar in case of an error
if (jarFile != null) {
try {
jarFile.close();
} catch (Throwable t) {
// ignore
}
}
}
if (!ignore) {
throw new JasperException(ex);
}
} finally {
if (redeployMode) {
// if in redeploy mode, always close the jar
if (jarFile != null) {
try {
jarFile.close();
} catch (Throwable t) {
// ignore
}
}
}
}
}
/*
* Searches the filesystem under /WEB-INF for any TLD files, and adds
* an implicit map entry to the taglib map for any TLD that has a <uri>
* element.
*/
private void processTldsInFileSystem(String startPath)
throws Exception {
Set dirList = ctxt.getResourcePaths(startPath);
if (dirList != null) {
Iterator it = dirList.iterator();
while (it.hasNext()) {
String path = (String) it.next();
if (path.endsWith("/")) {
processTldsInFileSystem(path);
}
if (!path.endsWith(".tld")) {
continue;
}
InputStream stream = ctxt.getResourceAsStream(path);
String uri = null;
try {
uri = getUriFromTld(path, stream);
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
// do nothing
}
}
}
// Add implicit map entry only if its uri is not already
// present in the map
if (uri != null && mappings.get(uri) == null) {
mappings.put(uri, new String[] { path, null });
}
}
}
}
/*
* Returns the value of the uri element of the given TLD, or null if the
* given TLD does not contain any such element.
*/
private String getUriFromTld(String resourcePath, InputStream in)
throws JasperException
{
boolean validate = Boolean.parseBoolean(
ctxt.getInitParameter(
Constants.XML_VALIDATION_TLD_INIT_PARAM));
String blockExternalString = ctxt.getInitParameter(
Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
boolean blockExternal;
if (blockExternalString == null) {
blockExternal = true;
} else {
blockExternal = Boolean.parseBoolean(blockExternalString);
}
ParserUtils pu = new ParserUtils(validate, blockExternal);
TreeNode tld = pu.parseXMLDocument(resourcePath, in);
TreeNode uri = tld.findChild("uri");
if (uri != null) {
String body = uri.getBody();
if (body != null)
return body;
}
return null;
}
/*
* Scans all JARs accessible to the webapp's classloader and its
* parent classloaders for TLDs.
*
* The list of JARs always includes the JARs under WEB-INF/lib, as well as
* all shared JARs in the classloader delegation chain of the webapp's
* classloader.
*
* Considering JARs in the classloader delegation chain constitutes a
* Tomcat-specific extension to the TLD search
* order defined in the JSP spec. It allows tag libraries packaged as JAR
* files to be shared by web applications by simply dropping them in a
* location that all web applications have access to (e.g.,
* <CATALINA_HOME>/common/lib).
*
* The set of shared JARs to be scanned for TLDs is narrowed down by
* the <tt>noTldJars</tt> class variable, which contains the names of JARs
* that are known not to contain any TLDs.
*/
private void scanJars() throws Exception {
ClassLoader webappLoader
= Thread.currentThread().getContextClassLoader();
ClassLoader loader = webappLoader;
while (loader != null) {
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
for (int i=0; i<urls.length; i++) {
URLConnection conn = urls[i].openConnection();
if (conn instanceof JarURLConnection) {
if (needScanJar(loader, webappLoader,
((JarURLConnection) conn).getJarFile().getName())) {
scanJar((JarURLConnection) conn, true);
}
} else {
String urlStr = urls[i].toString();
if (urlStr.startsWith(FILE_PROTOCOL)
&& urlStr.endsWith(JAR_FILE_SUFFIX)
&& needScanJar(loader, webappLoader, urlStr)) {
URL jarURL = new URL("jar:" + urlStr + "!/");
scanJar((JarURLConnection) jarURL.openConnection(),
true);
}
}
}
}
loader = loader.getParent();
}
}
/*
* Determines if the JAR file with the given <tt>jarPath</tt> needs to be
* scanned for TLDs.
*
* @param loader The current classloader in the parent chain
* @param webappLoader The webapp classloader
* @param jarPath The JAR file path
*
* @return TRUE if the JAR file identified by <tt>jarPath</tt> needs to be
* scanned for TLDs, FALSE otherwise
*/
private boolean needScanJar(ClassLoader loader, ClassLoader webappLoader,
String jarPath) {
if (loader == webappLoader) {
// JARs under WEB-INF/lib must be scanned unconditionally according
// to the spec.
return true;
} else {
String jarName = jarPath;
int slash = jarPath.lastIndexOf('/');
if (slash >= 0) {
jarName = jarPath.substring(slash + 1);
}
return (!noTldJars.contains(jarName));
}
}
}
|