View Javadoc

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