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