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