View Javadoc

1   // ========================================================================
2   // Copyright (c) 2003-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.webapp;
14  
15  import java.io.IOException;
16  import java.net.MalformedURLException;
17  
18  import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
19  import org.eclipse.jetty.servlet.ServletHandler;
20  import org.eclipse.jetty.util.log.Log;
21  import org.eclipse.jetty.util.log.Logger;
22  import org.eclipse.jetty.util.resource.Resource;
23  
24  /* ------------------------------------------------------------------------------- */
25  /**
26   * Configure by parsing default web.xml and web.xml
27   * 
28   */
29  public class WebXmlConfiguration extends AbstractConfiguration
30  {
31      private static final Logger LOG = Log.getLogger(WebXmlConfiguration.class);
32  
33      
34      /* ------------------------------------------------------------------------------- */
35      /**
36       * 
37       */
38      @Override
39      public void preConfigure (WebAppContext context) throws Exception
40      {
41          //parse webdefault.xml
42          String defaultsDescriptor = context.getDefaultsDescriptor();
43          if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
44          {
45              Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
46              if (dftResource == null) 
47                  dftResource = context.newResource(defaultsDescriptor);
48              context.getMetaData().setDefaults (dftResource);
49          }
50          
51          //parse, but don't process web.xml
52          Resource webxml = findWebXml(context);
53          if (webxml != null) 
54          {      
55              context.getMetaData().setWebXml(webxml);
56              context.getServletContext().setEffectiveMajorVersion(context.getMetaData().getWebXml().getMajorVersion());
57              context.getServletContext().setEffectiveMinorVersion(context.getMetaData().getWebXml().getMinorVersion());
58          }
59          
60          //parse but don't process override-web.xml
61          for (String overrideDescriptor : context.getOverrideDescriptors())
62          {
63              if (overrideDescriptor != null && overrideDescriptor.length() > 0)
64              {
65                  Resource orideResource = Resource.newSystemResource(overrideDescriptor);
66                  if (orideResource == null) 
67                      orideResource = context.newResource(overrideDescriptor);
68                  context.getMetaData().addOverride(orideResource);
69              }
70          }
71      }
72  
73      /* ------------------------------------------------------------------------------- */
74      /**
75       * Process web-default.xml, web.xml, override-web.xml
76       * 
77       */
78      @Override
79      public void configure (WebAppContext context) throws Exception
80      {
81          // cannot configure if the context is already started
82          if (context.isStarted())
83          {
84              LOG.debug("Cannot configure webapp after it is started");
85              return;
86          }
87  
88          context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
89      }
90      
91      /* ------------------------------------------------------------------------------- */
92      protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
93      {
94          String descriptor = context.getDescriptor();
95          if (descriptor != null)
96          {
97              Resource web = context.newResource(descriptor);
98              if (web.exists() && !web.isDirectory()) return web;
99          }
100 
101         Resource web_inf = context.getWebInf();
102         if (web_inf != null && web_inf.isDirectory())
103         {
104             // do web.xml file
105             Resource web = web_inf.addPath("web.xml");
106             if (web.exists()) return web;
107             if (LOG.isDebugEnabled())
108                 LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
109         }
110         return null;
111     }
112 
113 
114     /* ------------------------------------------------------------------------------- */
115     @Override
116     public void deconfigure (WebAppContext context) throws Exception
117     {
118         // TODO preserve any configuration that pre-existed.
119 
120         ServletHandler _servletHandler = context.getServletHandler();
121        
122         _servletHandler.setFilters(null);
123         _servletHandler.setFilterMappings(null);
124         _servletHandler.setServlets(null);
125         _servletHandler.setServletMappings(null);
126 
127         context.setEventListeners(null);
128         context.setWelcomeFiles(null);
129 
130         if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
131             ((ErrorPageErrorHandler) 
132                     context.getErrorHandler()).setErrorPages(null);
133 
134 
135         // TODO remove classpaths from classloader
136 
137     }
138 }