View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.List;
25  import java.util.jar.JarEntry;
26  
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.util.resource.Resource;
30  
31  /**
32   * MetaInfConfiguration
33   *
34   * Scan META-INF of all jars in WEB-INF/lib to find:
35   * <ul>
36   * <li>tlds
37   * <li>web-fragment.xml
38   * <li>resources
39   * </ul>
40   */
41  public class MetaInfConfiguration extends AbstractConfiguration
42  {
43      private static final Logger LOG = Log.getLogger(MetaInfConfiguration.class);
44  
45      public static final String METAINF_TLDS = TagLibConfiguration.TLD_RESOURCES;
46      public static final String METAINF_FRAGMENTS = FragmentConfiguration.FRAGMENT_RESOURCES;
47      public static final String METAINF_RESOURCES = WebInfConfiguration.RESOURCE_URLS;
48    
49      @Override
50      public void preConfigure(final WebAppContext context) throws Exception
51      {
52         //Merge all container and webinf lib jars to look for META-INF resources
53        
54          ArrayList<Resource> jars = new ArrayList<Resource>();
55          jars.addAll(context.getMetaData().getOrderedContainerJars());
56          jars.addAll(context.getMetaData().getWebInfJars());
57          
58          JarScanner scanner = new JarScanner()
59          {
60              public void processEntry(URI jarUri, JarEntry entry)
61              {
62                  try
63                  {
64                      MetaInfConfiguration.this.processEntry(context,jarUri,entry);
65                  }
66                  catch (Exception e)
67                  {
68                      LOG.warn("Problem processing jar entry " + entry, e);
69                  }
70              }
71          };
72          
73          
74          //Scan jars for META-INF information
75          if (jars != null)
76          {
77              URI[] uris = new URI[jars.size()];
78              int i=0;
79              for (Resource r : jars)
80              {
81                  uris[i++] = r.getURI();
82              }
83              scanner.scan(null, uris, true);
84          }
85      }
86  
87      @Override
88      public void postConfigure(WebAppContext context) throws Exception
89      {
90          context.setAttribute(METAINF_FRAGMENTS, null); 
91          context.setAttribute(METAINF_RESOURCES, null);
92          context.setAttribute(METAINF_TLDS, null);
93      }
94  
95      public void addResource (WebAppContext context, String attribute, Resource jar)
96      {
97          @SuppressWarnings("unchecked")
98          List<Resource> list = (List<Resource>)context.getAttribute(attribute);
99          if (list==null)
100         {
101             list=new ArrayList<Resource>();
102             context.setAttribute(attribute,list);
103         }
104         if (!list.contains(jar))
105             list.add(jar);
106     }
107     
108     
109     protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
110     {
111         String name = entry.getName();
112 
113         if (!name.startsWith("META-INF/"))
114             return;
115         
116         try
117         {
118             if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
119             {
120                 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));     
121             }
122             else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
123             {
124                 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
125             }
126             else
127             {
128                 String lcname = name.toLowerCase();
129                 if (lcname.endsWith(".tld"))
130                 {
131                     addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
132                 }
133             }
134         }
135         catch(Exception e)
136         {
137             context.getServletContext().log(jarUri+"!/"+name,e);
138         }
139     }
140 }