View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.webapp;
15  
16  
17  import java.net.URI;
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.jar.JarEntry;
21  
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.resource.Resource;
24  
25  /**
26   * MetaInfConfiguration
27   *
28   * Scan META-INF of all jars in WEB-INF/lib to find:
29   * <ul>
30   * <li>tlds
31   * <li>web-fragment.xml
32   * <li>resources
33   * </ul>
34   */
35  public class MetaInfConfiguration extends AbstractConfiguration
36  {
37      public static final String METAINF_TLDS = TagLibConfiguration.TLD_RESOURCES;
38      public static final String METAINF_FRAGMENTS = FragmentConfiguration.FRAGMENT_RESOURCES;
39      public static final String METAINF_RESOURCES = WebInfConfiguration.RESOURCE_URLS;
40    
41      @Override
42      public void preConfigure(final WebAppContext context) throws Exception
43      {
44         //Merge all container and webinf lib jars to look for META-INF resources
45        
46          ArrayList<Resource> jars = new ArrayList<Resource>();
47          jars.addAll(context.getMetaData().getOrderedContainerJars());
48          jars.addAll(context.getMetaData().getWebInfJars());
49          
50          JarScanner scanner = new JarScanner()
51          {
52              public void processEntry(URI jarUri, JarEntry entry)
53              {
54                  try
55                  {
56                      MetaInfConfiguration.this.processEntry(context,jarUri,entry);
57                  }
58                  catch (Exception e)
59                  {
60                      Log.warn("Problem processing jar entry " + entry, e);
61                  }
62              }
63          };
64          
65          
66          //Scan jars for META-INF information
67          if (jars != null)
68          {
69              URI[] uris = new URI[jars.size()];
70              int i=0;
71              for (Resource r : jars)
72              {
73                  uris[i++] = r.getURI();
74              }
75              scanner.scan(null, uris, true);
76          }
77      }
78  
79      @Override
80      public void postConfigure(WebAppContext context) throws Exception
81      {
82          context.setAttribute(METAINF_FRAGMENTS, null); 
83          context.setAttribute(METAINF_RESOURCES, null);
84          context.setAttribute(METAINF_TLDS, null);
85      }
86  
87      public void addResource (WebAppContext context, String attribute, Resource jar)
88      {
89          List<Resource> list = (List<Resource>)context.getAttribute(attribute);
90          if (list==null)
91          {
92              list=new ArrayList<Resource>();
93              context.setAttribute(attribute,list);
94          }
95          if (!list.contains(jar))
96              list.add(jar);
97      }
98      
99      
100     protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
101     {
102         String name = entry.getName();
103 
104         if (!name.startsWith("META-INF/"))
105             return;
106         
107         try
108         {
109             if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
110             {
111                 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));     
112             }
113             else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
114             {
115                 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
116             }
117             else
118             {
119                 String lcname = name.toLowerCase();
120                 if (lcname.endsWith(".tld"))
121                 {
122                     addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
123                 }
124             }
125         }
126         catch(Exception e)
127         {
128             context.getServletContext().log(jarUri+"!/"+name,e);
129         }
130     }
131 }