Skip to content

Commit bb15c1b

Browse files
committed
New helper class to provide web.xml to those Jasper classes that need to parse it. JspConfig was not consistent with TldLocationsCache. These are now consistent. JspConfig now has access to the merged web.xml generated by Catalina from web-fragment.xml files and/or annotations.
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@894672 13f79535-47bb-0310-9956-ffa450edef68
1 parent c493cd4 commit bb15c1b

4 files changed

Lines changed: 136 additions & 86 deletions

File tree

java/org/apache/jasper/compiler/JspConfig.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
package org.apache.jasper.compiler;
1919

20-
import java.io.InputStream;
2120
import java.util.Iterator;
2221
import java.util.Vector;
23-
import java.net.URL;
2422

2523
import javax.servlet.ServletContext;
2624

@@ -29,7 +27,6 @@
2927
import org.apache.jasper.xmlparser.TreeNode;
3028
import org.apache.juli.logging.Log;
3129
import org.apache.juli.logging.LogFactory;
32-
import org.xml.sax.InputSource;
3330

3431
/**
3532
* Handles the jsp-config element in WEB_INF/web.xml. This is used
@@ -41,8 +38,6 @@
4138

4239
public class JspConfig {
4340

44-
private static final String WEB_XML = "/WEB-INF/web.xml";
45-
4641
// Logger
4742
private final Log log = LogFactory.getLog(JspConfig.class);
4843

@@ -75,23 +70,16 @@ private double getVersion(TreeNode webApp) {
7570
return 2.3;
7671
}
7772

78-
private void processWebDotXml(ServletContext ctxt) throws JasperException {
73+
private void processWebDotXml() throws JasperException {
7974

80-
InputStream is = null;
75+
WebXml webXml = null;
8176

8277
try {
83-
URL uri = ctxt.getResource(WEB_XML);
84-
if (uri == null) {
85-
// no web.xml
86-
return;
87-
}
88-
89-
is = uri.openStream();
90-
InputSource ip = new InputSource(is);
91-
ip.setSystemId(uri.toExternalForm());
92-
78+
webXml = new WebXml(ctxt);
79+
9380
ParserUtils pu = new ParserUtils();
94-
TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip);
81+
TreeNode webApp = pu.parseXMLDocument(webXml.getSystemId(),
82+
webXml.getInputSource());
9583

9684
if (webApp == null
9785
|| getVersion(webApp) < 2.4) {
@@ -222,10 +210,8 @@ else if ("error-on-undeclared-namespace".equals(tname))
222210
} catch (Exception ex) {
223211
throw new JasperException(ex);
224212
} finally {
225-
if (is != null) {
226-
try {
227-
is.close();
228-
} catch (Throwable t) {}
213+
if (webXml != null) {
214+
webXml.close();
229215
}
230216
}
231217
}
@@ -235,7 +221,7 @@ private void init() throws JasperException {
235221
if (!initialized) {
236222
synchronized (this) {
237223
if (!initialized) {
238-
processWebDotXml(ctxt);
224+
processWebDotXml();
239225
defaultJspProperty = new JspProperty(defaultIsXml,
240226
defaultIsELIgnored,
241227
defaultIsScriptingInvalid,

java/org/apache/jasper/compiler/TldLocationsCache.java

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@
8181

8282
public class TldLocationsCache {
8383

84-
// Logger
85-
private final Log log = LogFactory.getLog(TldLocationsCache.class);
86-
8784
/**
8885
* The types of URI one may specify for a tag library
8986
*/
@@ -93,8 +90,6 @@ public class TldLocationsCache {
9390

9491
private static final String WEB_INF = "/WEB-INF/";
9592
private static final String WEB_INF_LIB = "/WEB-INF/lib/";
96-
private static final String WEB_XML = "/WEB-INF/web.xml";
97-
private static final String FILE_PROTOCOL = "file:";
9893
private static final String JAR_EXT = ".jar";
9994
private static final String TLD_EXT = ".tld";
10095

@@ -238,67 +233,17 @@ public void scan(File file) throws IOException {
238233
* This is not kept in sync with o.a.c.startup.TldConfig as the Jasper only
239234
* needs the URI to TLD mappings from scan web.xml whereas TldConfig needs
240235
* to scan the actual TLD files.
241-
*
242-
* Search order is:
243-
* - web.xml scanned by Tomcat and placed in context attribute
244-
* - location specified by ALT_DD_ATTR
245-
* - /WEB-INF/web.xml
246236
*/
247237
private void tldScanWebXml() throws Exception {
248238

249-
InputStream is = null;
250-
String systemId = null;
251-
239+
WebXml webXml = null;
252240
try {
253-
// Is a web.xml provided as context attribute?
254-
String webXml = (String) ctxt.getAttribute(
255-
org.apache.tomcat.util.scan.Constants.MERGED_WEB_XML);
256-
if (webXml != null) {
257-
is = new ByteArrayInputStream(webXml.getBytes());
258-
systemId = org.apache.tomcat.util.scan.Constants.MERGED_WEB_XML;
259-
}
260-
261-
// If not available as context attribute, look for an alternative
262-
// location
263-
if (is == null) {
264-
// Acquire input stream to web application deployment descriptor
265-
String altDDName = (String)ctxt.getAttribute(
266-
Constants.ALT_DD_ATTR);
267-
if (altDDName != null) {
268-
try {
269-
URL uri =
270-
new URL(FILE_PROTOCOL+altDDName.replace('\\', '/'));
271-
is = uri.openStream();
272-
systemId = uri.toExternalForm();
273-
} catch (MalformedURLException e) {
274-
log.warn(Localizer.getMessage(
275-
"jsp.error.internal.filenotfound",
276-
altDDName));
277-
}
278-
}
279-
}
241+
webXml = new WebXml(ctxt);
280242

281-
// Finally, try the default /WEB-INF/web.xml
282-
if (is == null) {
283-
URL uri = ctxt.getResource(WEB_XML);
284-
if (uri == null) {
285-
log.warn(Localizer.getMessage(
286-
"jsp.error.internal.filenotfound", WEB_XML));
287-
} else {
288-
is = uri.openStream();
289-
systemId = uri.toExternalForm();
290-
}
291-
}
292-
293-
if (is == null) {
294-
return;
295-
}
296-
InputSource ip = new InputSource(is);
297-
ip.setSystemId(systemId);
298-
299243
// Parse the web application deployment descriptor
300244
TreeNode webtld = null;
301-
webtld = new ParserUtils().parseXMLDocument(systemId, ip);
245+
webtld = new ParserUtils().parseXMLDocument(webXml.getSystemId(),
246+
webXml.getInputSource());
302247

303248
// Allow taglib to be an element of the root or jsp-config (JSP2.0)
304249
TreeNode jspConfig = webtld.findChild("jsp-config");
@@ -332,10 +277,8 @@ private void tldScanWebXml() throws Exception {
332277
mappings.put(tagUri, new String[] { tagLoc, tagLoc2 });
333278
}
334279
} finally {
335-
if (is != null) {
336-
try {
337-
is.close();
338-
} catch (Throwable t) {}
280+
if (webXml != null) {
281+
webXml.close();
339282
}
340283
}
341284
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.jasper.compiler;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.net.MalformedURLException;
24+
import java.net.URL;
25+
26+
import javax.servlet.ServletContext;
27+
28+
import org.apache.jasper.Constants;
29+
import org.apache.juli.logging.Log;
30+
import org.apache.juli.logging.LogFactory;
31+
import org.xml.sax.InputSource;
32+
33+
/**
34+
* Provides Jasper with a standard mechanism for gaining access to the web.xml
35+
* file associated with the current application. This isn't as simple as looking
36+
* in /WEB-INF/web.xml. In embedded scenarios, an alternative web.xml may be
37+
* provided and in Servlet 3.0 / JSP 2.2 environments an application's web.xml
38+
* may be the result of merging a number of web-fragment.xml files and/or
39+
* annotations with the main web.xml
40+
*
41+
* Clients *must* ensure that they call {@link #close()} to clean up resources.
42+
*/
43+
public class WebXml {
44+
private static final String FILE_PROTOCOL = "file:";
45+
private static final String WEB_XML = "/WEB-INF/web.xml";
46+
47+
private final Log log = LogFactory.getLog(WebXml.class);
48+
49+
private InputStream is;
50+
private InputSource ip;
51+
private String systemId;
52+
53+
public WebXml(ServletContext ctxt) throws IOException {
54+
// Is a web.xml provided as context attribute?
55+
String webXml = (String) ctxt.getAttribute(
56+
org.apache.tomcat.util.scan.Constants.MERGED_WEB_XML);
57+
if (webXml != null) {
58+
is = new ByteArrayInputStream(webXml.getBytes());
59+
systemId = org.apache.tomcat.util.scan.Constants.MERGED_WEB_XML;
60+
}
61+
62+
// If not available as context attribute, look for an alternative
63+
// location
64+
if (is == null) {
65+
// Acquire input stream to web application deployment descriptor
66+
String altDDName = (String)ctxt.getAttribute(
67+
Constants.ALT_DD_ATTR);
68+
if (altDDName != null) {
69+
try {
70+
URL uri =
71+
new URL(FILE_PROTOCOL+altDDName.replace('\\', '/'));
72+
is = uri.openStream();
73+
systemId = uri.toExternalForm();
74+
} catch (MalformedURLException e) {
75+
log.warn(Localizer.getMessage(
76+
"jsp.error.internal.filenotfound",
77+
altDDName));
78+
}
79+
}
80+
}
81+
82+
// Finally, try the default /WEB-INF/web.xml
83+
if (is == null) {
84+
URL uri = ctxt.getResource(WEB_XML);
85+
if (uri == null) {
86+
log.warn(Localizer.getMessage(
87+
"jsp.error.internal.filenotfound", WEB_XML));
88+
} else {
89+
is = uri.openStream();
90+
systemId = uri.toExternalForm();
91+
}
92+
}
93+
94+
if (is == null) {
95+
systemId = null;
96+
} else {
97+
ip = new InputSource(is);
98+
ip.setSystemId(systemId);
99+
}
100+
}
101+
102+
public String getSystemId() {
103+
return systemId;
104+
}
105+
106+
public InputSource getInputSource() {
107+
return ip;
108+
}
109+
110+
public void close() {
111+
if (is != null) {
112+
try {
113+
is.close();
114+
} catch (IOException e) {
115+
log.error(Localizer.getMessage(
116+
"jsp.error.stream.close.failed"));
117+
}
118+
}
119+
}
120+
}

java/org/apache/jasper/resources/LocalStrings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ jsp.error.not.impl.taglib=Internal error: Tag extensions not implemented
8787
jsp.error.include.missing.file=Missing file argument to include
8888
jsp.error.include.bad.file=Bad file argument to include
8989
jsp.error.include.exception=Unable to include {0}
90+
jsp.error.stream.close.failed=Failed to close stream
9091
jsp.error.stream.closed=Stream closed
9192
jsp.error.invalid.forward=Invalid forward tag
9293
jsp.error.unknownException=Unhandled error! You might want to consider having an error page \

0 commit comments

Comments
 (0)