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