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.webapp;
20  
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  
24  import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.log.Logger;
27  import org.eclipse.jetty.util.resource.Resource;
28  
29  /* ------------------------------------------------------------------------------- */
30  /**
31   * Configure by parsing default web.xml and web.xml
32   * 
33   */
34  public class WebXmlConfiguration extends AbstractConfiguration
35  {
36      private static final Logger LOG = Log.getLogger(WebXmlConfiguration.class);
37  
38      
39      /* ------------------------------------------------------------------------------- */
40      /**
41       * 
42       */
43      @Override
44      public void preConfigure (WebAppContext context) throws Exception
45      {
46          //parse webdefault.xml
47          String defaultsDescriptor = context.getDefaultsDescriptor();
48          if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
49          {
50              Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
51              if (dftResource == null) 
52                  dftResource = context.newResource(defaultsDescriptor);
53              context.getMetaData().setDefaults (dftResource);
54          }
55          
56          //parse, but don't process web.xml
57          Resource webxml = findWebXml(context);
58          if (webxml != null) 
59          {      
60              context.getMetaData().setWebXml(webxml);
61              context.getServletContext().setEffectiveMajorVersion(context.getMetaData().getWebXml().getMajorVersion());
62              context.getServletContext().setEffectiveMinorVersion(context.getMetaData().getWebXml().getMinorVersion());
63          }
64          
65          //parse but don't process override-web.xml
66          for (String overrideDescriptor : context.getOverrideDescriptors())
67          {
68              if (overrideDescriptor != null && overrideDescriptor.length() > 0)
69              {
70                  Resource orideResource = Resource.newSystemResource(overrideDescriptor);
71                  if (orideResource == null) 
72                      orideResource = context.newResource(overrideDescriptor);
73                  context.getMetaData().addOverride(orideResource);
74              }
75          }
76      }
77  
78      /* ------------------------------------------------------------------------------- */
79      /**
80       * Process web-default.xml, web.xml, override-web.xml
81       * 
82       */
83      @Override
84      public void configure (WebAppContext context) throws Exception
85      {
86          // cannot configure if the context is already started
87          if (context.isStarted())
88          {
89              LOG.debug("Cannot configure webapp after it is started");
90              return;
91          }
92  
93          context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
94      }
95      
96      /* ------------------------------------------------------------------------------- */
97      protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
98      {
99          String descriptor = context.getDescriptor();
100         if (descriptor != null)
101         {
102             Resource web = context.newResource(descriptor);
103             if (web.exists() && !web.isDirectory()) return web;
104         }
105 
106         Resource web_inf = context.getWebInf();
107         if (web_inf != null && web_inf.isDirectory())
108         {
109             // do web.xml file
110             Resource web = web_inf.addPath("web.xml");
111             if (web.exists()) return web;
112             if (LOG.isDebugEnabled())
113                 LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
114         }
115         return null;
116     }
117 
118 
119     /* ------------------------------------------------------------------------------- */
120     @Override
121     public void deconfigure (WebAppContext context) throws Exception
122     {      
123         context.setWelcomeFiles(null);
124 
125         if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
126             ((ErrorPageErrorHandler) 
127                     context.getErrorHandler()).setErrorPages(null);
128 
129         // TODO remove classpaths from classloader
130     }
131 }