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