View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.webapp;
20  
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  
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.log.Logger;
28  import org.eclipse.jetty.util.resource.Resource;
29  
30  /* ------------------------------------------------------------------------------- */
31  /**
32   * Configure by parsing default web.xml and web.xml
33   * 
34   */
35  public class WebXmlConfiguration extends AbstractConfiguration
36  {
37      private static final Logger LOG = Log.getLogger(WebXmlConfiguration.class);
38  
39      
40      /* ------------------------------------------------------------------------------- */
41      /**
42       * 
43       */
44      @Override
45      public void preConfigure (WebAppContext context) throws Exception
46      {
47          //parse webdefault.xml
48          String defaultsDescriptor = context.getDefaultsDescriptor();
49          if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
50          {
51              Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
52              if (dftResource == null) 
53                  dftResource = context.newResource(defaultsDescriptor);
54              context.getMetaData().setDefaults (dftResource);
55          }
56          
57          //parse, but don't process web.xml
58          Resource webxml = findWebXml(context);
59          if (webxml != null) 
60          {      
61              context.getMetaData().setWebXml(webxml);
62          }
63          
64          //parse but don't process override-web.xml
65          for (String overrideDescriptor : context.getOverrideDescriptors())
66          {
67              if (overrideDescriptor != null && overrideDescriptor.length() > 0)
68              {
69                  Resource orideResource = Resource.newSystemResource(overrideDescriptor);
70                  if (orideResource == null) 
71                      orideResource = context.newResource(overrideDescriptor);
72                  context.getMetaData().addOverride(orideResource);
73              }
74          }
75      }
76  
77      /* ------------------------------------------------------------------------------- */
78      /**
79       * Process web-default.xml, web.xml, override-web.xml
80       * 
81       */
82      @Override
83      public void configure (WebAppContext context) throws Exception
84      {
85          // cannot configure if the context is already started
86          if (context.isStarted())
87          {
88              LOG.debug("Cannot configure webapp after it is started");
89              return;
90          }
91  
92          context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
93      }
94      
95      /* ------------------------------------------------------------------------------- */
96      protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
97      {
98          String descriptor = context.getDescriptor();
99          if (descriptor != null)
100         {
101             Resource web = context.newResource(descriptor);
102             if (web.exists() && !web.isDirectory()) return web;
103         }
104 
105         Resource web_inf = context.getWebInf();
106         if (web_inf != null && web_inf.isDirectory())
107         {
108             // do web.xml file
109             Resource web = web_inf.addPath("web.xml");
110             if (web.exists()) return web;
111             if (LOG.isDebugEnabled())
112                 LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
113         }
114         return null;
115     }
116 
117 
118     /* ------------------------------------------------------------------------------- */
119     @Override
120     public void deconfigure (WebAppContext context) throws Exception
121     {
122         // TODO preserve any configuration that pre-existed.
123 
124         ServletHandler _servletHandler = context.getServletHandler();
125        
126         _servletHandler.setFilters(null);
127         _servletHandler.setFilterMappings(null);
128         _servletHandler.setServlets(null);
129         _servletHandler.setServletMappings(null);
130 
131         context.setEventListeners(null);
132         context.setWelcomeFiles(null);
133 
134         if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
135             ((ErrorPageErrorHandler) 
136                     context.getErrorHandler()).setErrorPages(null);
137 
138 
139         // TODO remove classpaths from classloader
140 
141     }
142 }