View Javadoc

1   // ========================================================================
2   // Copyright (c) 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  
14  
15  package org.eclipse.jetty.webapp;
16  
17  import java.util.List;
18  
19  import org.eclipse.jetty.util.resource.Resource;
20  
21  /**
22   * FragmentConfiguration
23   * 
24   * This configuration supports some Servlet 3.0 features in jetty-7. 
25   * 
26   * Process web-fragments in jars
27   */
28  public class FragmentConfiguration implements Configuration
29  {
30      public final static String FRAGMENT_RESOURCES="org.eclipse.jetty.webFragments";
31      
32      public void preConfigure(WebAppContext context) throws Exception
33      {
34          if (!context.isConfigurationDiscovered())
35              return;
36          
37          WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR); 
38          if (processor == null)
39          {
40              processor = new WebXmlProcessor (context);
41              context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
42          }
43        
44          
45          //parse web-fragment.xmls
46          parseWebFragments(context, processor);
47         
48          //TODO for jetty-8/servletspec 3 we will need to merge the parsed web fragments into the 
49          //effective pom in this preConfigure step
50      }
51      
52      public void configure(WebAppContext context) throws Exception
53      { 
54          if (!context.isConfigurationDiscovered())
55              return;
56          
57          //TODO for jetty-8/servletspec3 the fragments will not be separately processed here, but
58          //will be done by webXmlConfiguration when it processes the effective merged web.xml
59          WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR); 
60          if (processor == null)
61          {
62              processor = new WebXmlProcessor (context);
63              context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
64          }
65         
66          processor.processFragments(); 
67      }
68  
69      public void deconfigure(WebAppContext context) throws Exception
70      {
71         
72      }
73  
74      public void postConfigure(WebAppContext context) throws Exception
75      {
76          context.setAttribute(FRAGMENT_RESOURCES, null);
77      }
78  
79      /* ------------------------------------------------------------------------------- */
80      /**
81       * Look for any web.xml fragments in META-INF of jars in WEB-INF/lib
82       * 
83       * @throws Exception
84       */
85      public void parseWebFragments (final WebAppContext context, final WebXmlProcessor processor) throws Exception
86      {
87          List<Resource> frags = (List<Resource>)context.getAttribute(FRAGMENT_RESOURCES);
88          if (frags!=null)
89          {
90              for (Resource frag : frags)
91              {
92                  processor.parseFragment(Resource.newResource("jar:"+frag.getURL()+"!/META-INF/web-fragment.xml"));
93              }
94          }
95      }
96  
97  }