View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.Map;
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  
49      @Override
50      public void postConfigure(WebAppContext context) throws Exception
51      {
52          context.setAttribute(FRAGMENT_RESOURCES, null);
53      }
54  
55      /* ------------------------------------------------------------------------------- */
56      /**
57       * Look for any web-fragment.xml fragments in META-INF of jars in WEB-INF/lib
58       * 
59       * @throws Exception
60       */
61      public void findWebFragments (final WebAppContext context, final MetaData metaData) throws Exception
62      {
63          @SuppressWarnings("unchecked")
64          Map<Resource, Resource> frags = (Map<Resource,Resource>)context.getAttribute(FRAGMENT_RESOURCES);
65          if (frags!=null)
66          {
67              for (Resource key : frags.keySet())
68              {
69                  if (key.isDirectory()) //tolerate the case where the library is a directory, not a jar. useful for OSGi for example
70                  {
71                      metaData.addFragment(key, frags.get(key));                          
72                  }
73                  else //the standard case: a jar most likely inside WEB-INF/lib
74                  {
75                      metaData.addFragment(key, frags.get(key));
76                  }
77              }
78          }
79      }
80  }