1
17 package org.apache.jasper.servlet;
18
19 import java.io.IOException;
20 import java.util.Set;
21
22 import javax.servlet.ServletContainerInitializer;
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletException;
25 import javax.servlet.jsp.JspFactory;
26
27 import org.apache.jasper.Constants;
28 import org.apache.jasper.compiler.Localizer;
29 import org.apache.jasper.compiler.TldCache;
30 import org.apache.jasper.runtime.JspFactoryImpl;
31 import org.apache.jasper.security.SecurityClassLoad;
32 import org.apache.juli.logging.Log;
33 import org.apache.juli.logging.LogFactory;
34 import org.apache.tomcat.InstanceManager;
35 import org.apache.tomcat.SimpleInstanceManager;
36 import org.xml.sax.SAXException;
37
38
41 public class JasperInitializer implements ServletContainerInitializer {
42
43 private static final String MSG = "org.apache.jasper.servlet.JasperInitializer";
44 private final Log log = LogFactory.getLog(JasperInitializer.class);
45
46
50 static {
51 JspFactoryImpl factory = new JspFactoryImpl();
52 SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader());
53 if (JspFactory.getDefaultFactory() == null) {
54 JspFactory.setDefaultFactory(factory);
55 }
56 }
57
58 @Override
59 public void onStartup(Set<Class<?>> types, ServletContext context) throws ServletException {
60 if (log.isDebugEnabled()) {
61 log.debug(Localizer.getMessage(MSG + ".onStartup", context.getServletContextName()));
62 }
63
64
65 if (context.getAttribute(InstanceManager.class.getName())==null) {
66 context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
67 }
68
69 boolean validate = Boolean.parseBoolean(
70 context.getInitParameter(Constants.XML_VALIDATION_TLD_INIT_PARAM));
71 String blockExternalString = context.getInitParameter(
72 Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
73 boolean blockExternal;
74 if (blockExternalString == null) {
75 blockExternal = true;
76 } else {
77 blockExternal = Boolean.parseBoolean(blockExternalString);
78 }
79
80
81 TldScanner scanner = newTldScanner(context, true, validate, blockExternal);
82 try {
83 scanner.scan();
84 } catch (IOException | SAXException e) {
85 throw new ServletException(e);
86 }
87
88
89 for (String listener : scanner.getListeners()) {
90 context.addListener(listener);
91 }
92
93 context.setAttribute(TldCache.SERVLET_CONTEXT_ATTRIBUTE_NAME,
94 new TldCache(context, scanner.getUriTldResourcePathMap(),
95 scanner.getTldResourcePathTaglibXmlMap()));
96 }
97
98 protected TldScanner newTldScanner(ServletContext context, boolean namespaceAware,
99 boolean validate, boolean blockExternal) {
100 return new TldScanner(context, namespaceAware, validate, blockExternal);
101 }
102 }
103