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