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 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 /**
39  * Initializer for the Jasper JSP Engine.
40  */

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); // must not be static
45
46     /**
47      * Preload classes required at runtime by a JSP servlet so that
48      * we don't get a defineClassInPackage security exception.
49      */

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         // Setup a simple default Instance Manager
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         // scan the application for TLDs
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         // add any listeners defined in TLDs
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