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  
20  package org.eclipse.jetty.webapp;
21  
22  import java.util.List;
23  
24  import org.eclipse.jetty.util.resource.Resource;
25  
26  /**
27   * FragmentConfiguration
28   * 
29   * 
30   * 
31   * Process web-fragments in jars
32   */
33  public class FragmentConfiguration extends AbstractConfiguration
34  {
35      public final static String FRAGMENT_RESOURCES="org.eclipse.jetty.webFragments";
36      
37      @Override
38      public void preConfigure(WebAppContext context) throws Exception
39      {
40          if (!context.isConfigurationDiscovered())
41              return;
42  
43          //find all web-fragment.xmls
44          findWebFragments(context, context.getMetaData());
45          
46      }
47  
48      @Override
49      public void configure(WebAppContext context) throws Exception
50      { 
51          if (!context.isConfigurationDiscovered())
52              return;
53          
54          //order the fragments
55          context.getMetaData().orderFragments(); 
56      }
57  
58      @Override
59      public void postConfigure(WebAppContext context) throws Exception
60      {
61          context.setAttribute(FRAGMENT_RESOURCES, null);
62      }
63  
64      /* ------------------------------------------------------------------------------- */
65      /**
66       * Look for any web-fragment.xml fragments in META-INF of jars in WEB-INF/lib
67       * 
68       * @throws Exception
69       */
70      public void findWebFragments (final WebAppContext context, final MetaData metaData) throws Exception
71      {
72          @SuppressWarnings("unchecked")
73          List<Resource> frags = (List<Resource>)context.getAttribute(FRAGMENT_RESOURCES);
74          if (frags!=null)
75          {
76              for (Resource frag : frags)
77              {
78              	if (frag.isDirectory()) //tolerate the case where the library is a directory, not a jar. useful for OSGi for example
79              	{
80                      metaData.addFragment(frag, Resource.newResource(frag.getURL()+"/META-INF/web-fragment.xml"));            		
81              	}
82                  else //the standard case: a jar most likely inside WEB-INF/lib
83                  {
84                      metaData.addFragment(frag, Resource.newResource("jar:"+frag.getURL()+"!/META-INF/web-fragment.xml"));
85                  }
86              }
87          }
88      }
89  }