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