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