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  package org.eclipse.jetty.webapp;
20  
21  
22  import java.net.URI;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.Set;
30  import java.util.concurrent.TimeUnit;
31  import java.util.jar.JarEntry;
32  
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
35  import org.eclipse.jetty.util.resource.Resource;
36  
37  /**
38   * MetaInfConfiguration
39   *
40   * Scan META-INF of all jars in WEB-INF/lib to find:
41   * <ul>
42   * <li>tlds
43   * <li>web-fragment.xml
44   * <li>resources
45   * </ul>
46   */
47  public class MetaInfConfiguration extends AbstractConfiguration
48  {
49      private static final Logger LOG = Log.getLogger(MetaInfConfiguration.class);
50  
51      public static final String METAINF_TLDS = TagLibConfiguration.TLD_RESOURCES;
52      public static final String METAINF_FRAGMENTS = FragmentConfiguration.FRAGMENT_RESOURCES;
53      public static final String METAINF_RESOURCES = WebInfConfiguration.RESOURCE_DIRS;
54    
55      @Override
56      public void preConfigure(final WebAppContext context) throws Exception
57      {
58         //Merge all container and webinf lib jars to look for META-INF resources     
59          ArrayList<Resource> jars = new ArrayList<Resource>();
60          jars.addAll(context.getMetaData().getContainerResources());
61          jars.addAll(context.getMetaData().getWebInfJars());
62          
63          //Scan jars for META-INF information
64          if (jars != null)
65          {
66              for (Resource r : jars)
67              {
68                  URI uri = r.getURI();
69                  Resource fragXml = Resource.newResource("jar:"+uri+"!/META-INF/web-fragment.xml");
70                  if (fragXml.exists())
71                  {
72                      //add mapping for resource->fragment
73                      Map<Resource, Resource> fragments = (Map<Resource,Resource>)context.getAttribute(METAINF_FRAGMENTS);
74                      if (fragments == null)
75                      {
76                          fragments = new HashMap<Resource, Resource>();
77                          context.setAttribute(METAINF_FRAGMENTS, fragments);
78                      }
79                      fragments.put(r, fragXml);    
80                  }
81                  
82                  Resource resourcesDir = Resource.newResource("jar:"+uri+"!/META-INF/resources");
83                  if (resourcesDir.exists())
84                  {
85                      //add resources dir
86                      Set<Resource> dirs = (Set<Resource>)context.getAttribute(METAINF_RESOURCES);
87                      if (dirs == null)
88                      {
89                          dirs = new HashSet<Resource>();
90                          context.setAttribute(METAINF_RESOURCES, dirs);
91                      }
92                      dirs.add(resourcesDir);
93                  }
94              }
95          }
96      }
97     
98      @Override
99      public void postConfigure(WebAppContext context) throws Exception
100     {
101         context.setAttribute(METAINF_FRAGMENTS, null); 
102         context.setAttribute(METAINF_RESOURCES, null);
103         context.setAttribute(METAINF_TLDS, null);
104     }
105 }