View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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   * <p>
29   * Process web-fragments in jars
30   */
31  public class FragmentConfiguration extends AbstractConfiguration
32  {
33      public final static String FRAGMENT_RESOURCES="org.eclipse.jetty.webFragments";
34      
35      @Override
36      public void preConfigure(WebAppContext context) throws Exception
37      {
38          if (!context.isConfigurationDiscovered())
39              return;
40  
41          //find all web-fragment.xmls
42          findWebFragments(context, context.getMetaData());
43          
44      }
45  
46  
47      @Override
48      public void postConfigure(WebAppContext context) throws Exception
49      {
50          context.setAttribute(FRAGMENT_RESOURCES, null);
51      }
52  
53      /* ------------------------------------------------------------------------------- */
54      /**
55       * Look for any web-fragment.xml fragments in META-INF of jars in WEB-INF/lib
56       * @param context the web app context to look in
57       * @param metaData the metadata to populate with fragments
58       * 
59       * @throws Exception if unable to find web fragments
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  }