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 implements Configuration
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      public static final String CONTAINER_JAR_RESOURCES = WebInfConfiguration.CONTAINER_JAR_RESOURCES;
41      public static final String WEB_INF_JAR_RESOURCES = WebInfConfiguration.WEB_INF_JAR_RESOURCES;
42  
43      public void preConfigure(final WebAppContext context) throws Exception
44      {
45         //Merge all container and webinf lib jars to look for META-INF resources
46        
47          ArrayList<Resource> jars = new ArrayList<Resource>();
48          jars.addAll((List<Resource>)context.getAttribute(CONTAINER_JAR_RESOURCES));
49          jars.addAll((List<Resource>)context.getAttribute(WEB_INF_JAR_RESOURCES));
50          
51          JarScanner scanner = new JarScanner()
52          {
53              public void processEntry(URI jarUri, JarEntry entry)
54              {
55                  try
56                  {
57                      MetaInfConfiguration.this.processEntry(context,jarUri,entry);
58                  }
59                  catch (Exception e)
60                  {
61                      Log.warn("Problem processing jar entry " + entry, e);
62                  }
63              }
64          };
65          
66          
67          //Scan jars for META-INF information
68          if (jars != null)
69          {
70              URI[] uris = new URI[jars.size()];
71              int i=0;
72              for (Resource r : jars)
73              {
74                  uris[i++] = r.getURI();
75              }
76              scanner.scan(null, uris, true);
77          }
78      }
79      
80      
81      public void configure(WebAppContext context) throws Exception
82      {
83          
84      }
85  
86      public void deconfigure(WebAppContext context) throws Exception
87      {
88   
89      }
90  
91      public void postConfigure(WebAppContext context) throws Exception
92      {
93          context.setAttribute(METAINF_FRAGMENTS, null); 
94          context.setAttribute(METAINF_RESOURCES, null);
95          context.setAttribute(METAINF_TLDS, null);
96      }
97  
98      public void addResource (WebAppContext context, String attribute, Resource jar)
99      {
100         List<Resource> list = (List<Resource>)context.getAttribute(attribute);
101         if (list==null)
102         {
103             list=new ArrayList<Resource>();
104             context.setAttribute(attribute,list);
105         }
106         if (!list.contains(jar))
107             list.add(jar);
108     }
109     
110     
111     protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
112     {
113         String name = entry.getName();
114 
115         if (!name.startsWith("META-INF/"))
116             return;
117         
118         try
119         {
120             if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
121             {
122                 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));     
123             }
124             else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
125             {
126                 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
127             }
128             else
129             {
130                 String lcname = name.toLowerCase();
131                 if (lcname.endsWith(".tld"))
132                 {
133                     addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
134                 }
135             }
136         }
137         catch(Exception e)
138         {
139             context.getServletContext().log(jarUri+"!/"+name,e);
140         }
141     }
142 }