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          }
57          
58          //parse but don't process override-web.xml
59          for (String overrideDescriptor : context.getOverrideDescriptors())
60          {
61              if (overrideDescriptor != null && overrideDescriptor.length() > 0)
62              {
63                  Resource orideResource = Resource.newSystemResource(overrideDescriptor);
64                  if (orideResource == null) 
65                      orideResource = context.newResource(overrideDescriptor);
66                  context.getMetaData().addOverride(orideResource);
67              }
68          }
69      }
70  
71      /* ------------------------------------------------------------------------------- */
72      /**
73       * Process web-default.xml, web.xml, override-web.xml
74       * 
75       */
76      @Override
77      public void configure (WebAppContext context) throws Exception
78      {
79          // cannot configure if the context is already started
80          if (context.isStarted())
81          {
82              if (LOG.isDebugEnabled()) LOG.debug("Cannot configure webapp after it is started");
83              return;
84          }
85  
86          context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
87      }
88      
89      /* ------------------------------------------------------------------------------- */
90      protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
91      {
92          String descriptor = context.getDescriptor();
93          if (descriptor != null)
94          {
95              Resource web = context.newResource(descriptor);
96              if (web.exists() && !web.isDirectory()) return web;
97          }
98  
99          Resource web_inf = context.getWebInf();
100         if (web_inf != null && web_inf.isDirectory())
101         {
102             // do web.xml file
103             Resource web = web_inf.addPath("web.xml");
104             if (web.exists()) return web;
105             LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
106         }
107         return null;
108     }
109 
110 
111     /* ------------------------------------------------------------------------------- */
112     @Override
113     public void deconfigure (WebAppContext context) throws Exception
114     {
115         // TODO preserve any configuration that pre-existed.
116 
117         ServletHandler _servletHandler = context.getServletHandler();
118        
119         _servletHandler.setFilters(null);
120         _servletHandler.setFilterMappings(null);
121         _servletHandler.setServlets(null);
122         _servletHandler.setServletMappings(null);
123 
124         context.setEventListeners(null);
125         context.setWelcomeFiles(null);
126 
127         if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
128             ((ErrorPageErrorHandler) 
129                     context.getErrorHandler()).setErrorPages(null);
130 
131 
132         // TODO remove classpaths from classloader
133 
134     }
135 }