View Javadoc

1   // ========================================================================
2   // Copyright (c) 2003-2009 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  import java.net.URL;
18  import java.util.Collections;
19  
20  import org.eclipse.jetty.security.ConstraintAware;
21  import org.eclipse.jetty.security.ConstraintMapping;
22  import org.eclipse.jetty.security.SecurityHandler;
23  import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
24  import org.eclipse.jetty.servlet.ServletHandler;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.resource.Resource;
27  
28  /* ------------------------------------------------------------------------------- */
29  /**
30   * Configure by parsing default web.xml and web.xml
31   * 
32   * 
33   */
34  public class WebXmlConfiguration implements Configuration
35  {
36      
37      
38    
39     
40      /* ------------------------------------------------------------------------------- */
41      /**
42       * Process webdefaults.xml
43       * 
44       * 
45       */
46      public void preConfigure (WebAppContext context) throws Exception
47      {
48          // cannot configure if the context is already started
49          if (context.isStarted())
50          {
51              if (Log.isDebugEnabled())
52              {
53                  Log.debug("Cannot configure webapp after it is started");
54              }
55              return;
56          }
57          
58          //Get or create a processor to handle webdefaults, web.xml and the fragments
59          WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR); 
60          if (processor == null)
61          {
62              processor = new WebXmlProcessor (context);
63              context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
64          }
65          
66          //handle webdefault.xml
67          String defaultsDescriptor = context.getDefaultsDescriptor();
68          if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
69          {
70              Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
71              if (dftResource == null) 
72                  dftResource = context.newResource(defaultsDescriptor);
73              processor.parseDefaults (dftResource);
74              processor.processDefaults();
75          }
76          
77          //parse, but don't process web.xml
78          Resource webxml = findWebXml(context);
79          if (webxml != null) 
80          {      
81              processor.parseWebXml(webxml);
82          }
83      }
84  
85      /* ------------------------------------------------------------------------------- */
86      /**
87       * Process web.xml, web-fragment.xml(s), override-web.xml
88       * 
89       * 
90       */
91      public void configure (WebAppContext context) throws Exception
92      {
93          // cannot configure if the context is already started
94          if (context.isStarted())
95          {
96              if (Log.isDebugEnabled()) Log.debug("Cannot configure webapp after it is started");
97              return;
98          }
99          
100         WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR); 
101         if (processor == null)
102         {
103             processor = new WebXmlProcessor (context);
104             context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
105         }
106 
107         //process web.xml (the effective web.xml???)
108         processor.processWebXml();
109      
110         
111         //process override-web.xml
112         String overrideDescriptor = context.getOverrideDescriptor();
113         if (overrideDescriptor != null && overrideDescriptor.length() > 0)
114         {
115             Resource orideResource = Resource.newSystemResource(overrideDescriptor);
116             if (orideResource == null) 
117                 orideResource = context.newResource(overrideDescriptor);
118             processor.parseOverride(orideResource);
119             processor.processOverride();
120         }
121     }
122     
123 
124     public void postConfigure(WebAppContext context) throws Exception
125     {
126         context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, null); 
127         context.setAttribute(WebXmlProcessor.METADATA_COMPLETE, null);
128         context.setAttribute(WebXmlProcessor.WEBXML_VERSION, null);
129         context.setAttribute(WebXmlProcessor.WEBXML_CLASSNAMES, null); 
130     }
131 
132   
133 
134     /* ------------------------------------------------------------------------------- */
135     protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
136     {
137         String descriptor = context.getDescriptor();
138         if (descriptor != null)
139         {
140             Resource web = context.newResource(descriptor);
141             if (web.exists() && !web.isDirectory()) return web;
142         }
143 
144         Resource web_inf = context.getWebInf();
145         if (web_inf != null && web_inf.isDirectory())
146         {
147             // do web.xml file
148             Resource web = web_inf.addPath("web.xml");
149             if (web.exists()) return web;
150             Log.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
151         }
152         return null;
153     }
154 
155 
156     /* ------------------------------------------------------------------------------- */
157     public void deconfigure (WebAppContext context) throws Exception
158     {
159         // TODO preserve any configuration that pre-existed.
160 
161         ServletHandler _servletHandler = context.getServletHandler();
162         SecurityHandler _securityHandler = (SecurityHandler)context.getSecurityHandler();
163        
164         _servletHandler.setFilters(null);
165         _servletHandler.setFilterMappings(null);
166         _servletHandler.setServlets(null);
167         _servletHandler.setServletMappings(null);
168 
169         context.setEventListeners(null);
170         context.setWelcomeFiles(null);
171         if (_securityHandler instanceof ConstraintAware) 
172             ((ConstraintAware) _securityHandler).setConstraintMappings(new ConstraintMapping[]{}, Collections.EMPTY_SET);
173 
174         if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
175             ((ErrorPageErrorHandler) 
176                     context.getErrorHandler()).setErrorPages(null);
177 
178 
179         // TODO remove classpaths from classloader
180 
181     }
182 }