View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.apache.jsp;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import javax.servlet.ServletContext;
28  
29  import org.apache.jasper.servlet.JasperInitializer;
30  import org.apache.jasper.servlet.TldPreScanned;
31  import org.apache.jasper.servlet.TldScanner;
32  import org.eclipse.jetty.util.log.Log;
33  import org.eclipse.jetty.util.log.Logger;
34  import org.xml.sax.SAXException;
35  
36  /**
37   * JettyJasperInitializer
38   */
39  public class JettyJasperInitializer extends JasperInitializer
40  {
41     private static final Logger LOG = Log.getLogger(JettyJasperInitializer.class);
42      
43      /**
44       * NullTldScanner
45       *
46       * Does nothing. Used when we can tell that all jsps have been precompiled, in which case
47       * the tlds are not needed.
48       */
49      private final class NullTldScanner extends TldScanner
50      {
51          /**
52           * @param context
53           * @param namespaceAware
54           * @param validation
55           * @param blockExternal
56           */
57          private NullTldScanner(ServletContext context, boolean namespaceAware, boolean validation, boolean blockExternal)
58          {
59              super(context, namespaceAware, validation, blockExternal);
60          }
61  
62          /**
63           * @see org.apache.jasper.servlet.TldScanner#scan()
64           */
65          @Override
66          public void scan() throws IOException, SAXException
67          {
68              return; //do nothing
69          }
70  
71          /**
72           * @see org.apache.jasper.servlet.TldScanner#getListeners()
73           */
74          @Override
75          public List<String> getListeners()
76          {
77              return Collections.emptyList();
78          }
79  
80          /**
81           * @see org.apache.jasper.servlet.TldScanner#scanJars()
82           */
83          @Override
84          public void scanJars()
85          {
86             return; //do nothing
87          }
88      }
89  
90      /**
91       * Make a TldScanner, and prefeed it the tlds that have already been discovered in jar files
92       * by the MetaInfConfiguration.
93       */
94      @Override
95      public TldScanner newTldScanner(ServletContext context, boolean namespaceAware, boolean validate, boolean blockExternal)
96      {  
97          String tmp = context.getInitParameter("org.eclipse.jetty.jsp.precompiled");
98          if (tmp!=null && !tmp.equals("") && Boolean.valueOf(tmp))
99          {
100             if (LOG.isDebugEnabled()) LOG.debug("Jsp precompilation detected");
101             return new NullTldScanner(context, namespaceAware, validate, blockExternal);
102         }
103         
104         Collection<URL> tldUrls = (Collection<URL>)context.getAttribute("org.eclipse.jetty.tlds");
105         if (tldUrls != null)
106         {
107             if (LOG.isDebugEnabled()) LOG.debug("Tld pre-scan detected");
108             return new TldPreScanned(context,namespaceAware,validate,blockExternal,tldUrls);
109         }
110         
111         if (LOG.isDebugEnabled()) LOG.debug("Defaulting to jasper tld scanning");
112         return super.newTldScanner(context, namespaceAware, validate, blockExternal);
113     }
114     
115 
116 }