View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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   */
40  public class JettyJasperInitializer extends JasperInitializer
41  {
42     private static final Logger LOG = Log.getLogger(JettyJasperInitializer.class);
43      
44      /**
45       * NullTldScanner
46       *
47       * Does nothing. Used when we can tell that all jsps have been precompiled, in which case
48       * the tlds are not needed.
49       */
50      private final class NullTldScanner extends TldScanner
51      {
52          /**
53           * @param context
54           * @param namespaceAware
55           * @param validation
56           * @param blockExternal
57           */
58          private NullTldScanner(ServletContext context, boolean namespaceAware, boolean validation, boolean blockExternal)
59          {
60              super(context, namespaceAware, validation, blockExternal);
61          }
62  
63          /**
64           * @see org.apache.jasper.servlet.TldScanner#scan()
65           */
66          @Override
67          public void scan() throws IOException, SAXException
68          {
69              return; //do nothing
70          }
71  
72          /**
73           * @see org.apache.jasper.servlet.TldScanner#getListeners()
74           */
75          @Override
76          public List<String> getListeners()
77          {
78              return Collections.emptyList();
79          }
80  
81          /**
82           * @see org.apache.jasper.servlet.TldScanner#scanJars()
83           */
84          @Override
85          public void scanJars()
86          {
87             return; //do nothing
88          }
89      }
90  
91      /**
92       * Make a TldScanner, and prefeed it the tlds that have already been discovered in jar files
93       * by the MetaInfConfiguration.
94       * 
95       * @see org.apache.jasper.servlet.JasperInitializer#prepareScanner(javax.servlet.ServletContext, boolean, boolean, boolean)
96       */
97      @Override
98      public TldScanner newTldScanner(ServletContext context, boolean namespaceAware, boolean validate, boolean blockExternal)
99      {  
100         String tmp = context.getInitParameter("org.eclipse.jetty.jsp.precompiled");
101         if (tmp!=null && !tmp.equals("") && Boolean.valueOf(tmp))
102         {
103             if (LOG.isDebugEnabled()) LOG.debug("Jsp precompilation detected");
104             return new NullTldScanner(context, namespaceAware, validate, blockExternal);
105         }
106         
107         Collection<URL> tldUrls = (Collection<URL>)context.getAttribute("org.eclipse.jetty.tlds");
108         if (tldUrls != null)
109         {
110             if (LOG.isDebugEnabled()) LOG.debug("Tld pre-scan detected");
111             return new TldPreScanned(context,namespaceAware,validate,blockExternal,tldUrls);
112         }
113         
114         if (LOG.isDebugEnabled()) LOG.debug("Defaulting to jasper tld scanning");
115         return super.newTldScanner(context, namespaceAware, validate, blockExternal);
116     }
117     
118 
119 }