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