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