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      @Override
82      public void configure(WebAppContext context) throws Exception
83      {
84          
85      }
86  
87      @Override
88      public void deconfigure(WebAppContext context) throws Exception
89      {
90   
91      }
92  
93      @Override
94      public void postConfigure(WebAppContext context) throws Exception
95      {
96          context.setAttribute(METAINF_FRAGMENTS, null); 
97          context.setAttribute(METAINF_RESOURCES, null);
98          context.setAttribute(METAINF_TLDS, null);
99      }
100 
101     public void addResource (WebAppContext context, String attribute, Resource jar)
102     {
103         @SuppressWarnings("unchecked")
104         List<Resource> list = (List<Resource>)context.getAttribute(attribute);
105         if (list==null)
106         {
107             list=new ArrayList<Resource>();
108             context.setAttribute(attribute,list);
109         }
110         if (!list.contains(jar))
111             list.add(jar);
112     }
113     
114     
115     protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
116     {
117         String name = entry.getName();
118 
119         if (!name.startsWith("META-INF/"))
120             return;
121         
122         try
123         {
124             if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
125             {
126                 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));     
127             }
128             else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
129             {
130                 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
131             }
132             else
133             {
134                 String lcname = name.toLowerCase();
135                 if (lcname.endsWith(".tld"))
136                 {
137                     addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
138                 }
139             }
140         }
141         catch(Exception e)
142         {
143             context.getServletContext().log(jarUri+"!/"+name,e);
144         }
145     }
146 }