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   * 
25   * 
26   * Process web-fragments in jars
27   */
28  public class FragmentConfiguration extends AbstractConfiguration
29  {
30      public final static String FRAGMENT_RESOURCES="org.eclipse.jetty.webFragments";
31      
32      @Override
33      public void preConfigure(WebAppContext context) throws Exception
34      {
35          if (!context.isConfigurationDiscovered())
36              return;
37  
38          //find all web-fragment.xmls
39          findWebFragments(context, context.getMetaData());
40          
41      }
42  
43      @Override
44      public void configure(WebAppContext context) throws Exception
45      { 
46          if (!context.isConfigurationDiscovered())
47              return;
48          
49          //order the fragments
50          context.getMetaData().orderFragments(); 
51      }
52  
53      @Override
54      public void postConfigure(WebAppContext context) throws Exception
55      {
56          context.setAttribute(FRAGMENT_RESOURCES, null);
57      }
58  
59      /* ------------------------------------------------------------------------------- */
60      /**
61       * Look for any web-fragment.xml fragments in META-INF of jars in WEB-INF/lib
62       * 
63       * @throws Exception
64       */
65      public void findWebFragments (final WebAppContext context, final MetaData metaData) throws Exception
66      {
67          List<Resource> frags = (List<Resource>)context.getAttribute(FRAGMENT_RESOURCES);
68          if (frags!=null)
69          {
70              for (Resource frag : frags)
71              {
72                  metaData.addFragment(frag, Resource.newResource("jar:"+frag.getURL()+"!/META-INF/web-fragment.xml"));
73              }
74          }
75      }
76  }