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