View Javadoc

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