1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
36
37
38
39
40
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
54
55 ArrayList<Resource> jars = new ArrayList<Resource>();
56 jars.addAll(context.getMetaData().getOrderedContainerJars());
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
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
88 @Override
89 public void postConfigure(WebAppContext context) throws Exception
90 {
91 context.setAttribute(METAINF_FRAGMENTS, null);
92 context.setAttribute(METAINF_RESOURCES, null);
93 context.setAttribute(METAINF_TLDS, null);
94 }
95
96 public void addResource (WebAppContext context, String attribute, Resource jar)
97 {
98 @SuppressWarnings("unchecked")
99 List<Resource> list = (List<Resource>)context.getAttribute(attribute);
100 if (list==null)
101 {
102 list=new ArrayList<Resource>();
103 context.setAttribute(attribute,list);
104 }
105 if (!list.contains(jar))
106 list.add(jar);
107 }
108
109
110 protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
111 {
112 String name = entry.getName();
113
114 if (!name.startsWith("META-INF/"))
115 return;
116
117 try
118 {
119 if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
120 {
121 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));
122 }
123 else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
124 {
125 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
126 }
127 else
128 {
129 String lcname = name.toLowerCase(Locale.ENGLISH);
130 if (lcname.endsWith(".tld"))
131 {
132 addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
133 }
134 }
135 }
136 catch(Exception e)
137 {
138 context.getServletContext().log(jarUri+"!/"+name,e);
139 }
140 }
141 }