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.resource.Resource;
24
25
26
27
28
29
30
31
32
33
34
35 public class MetaInfConfiguration extends AbstractConfiguration
36 {
37 public static final String METAINF_TLDS = TagLibConfiguration.TLD_RESOURCES;
38 public static final String METAINF_FRAGMENTS = FragmentConfiguration.FRAGMENT_RESOURCES;
39 public static final String METAINF_RESOURCES = WebInfConfiguration.RESOURCE_URLS;
40
41 @Override
42 public void preConfigure(final WebAppContext context) throws Exception
43 {
44
45
46 ArrayList<Resource> jars = new ArrayList<Resource>();
47 jars.addAll(context.getMetaData().getOrderedContainerJars());
48 jars.addAll(context.getMetaData().getWebInfJars());
49
50 JarScanner scanner = new JarScanner()
51 {
52 public void processEntry(URI jarUri, JarEntry entry)
53 {
54 try
55 {
56 MetaInfConfiguration.this.processEntry(context,jarUri,entry);
57 }
58 catch (Exception e)
59 {
60 Log.warn("Problem processing jar entry " + entry, e);
61 }
62 }
63 };
64
65
66
67 if (jars != null)
68 {
69 URI[] uris = new URI[jars.size()];
70 int i=0;
71 for (Resource r : jars)
72 {
73 uris[i++] = r.getURI();
74 }
75 scanner.scan(null, uris, true);
76 }
77 }
78
79 @Override
80 public void postConfigure(WebAppContext context) throws Exception
81 {
82 context.setAttribute(METAINF_FRAGMENTS, null);
83 context.setAttribute(METAINF_RESOURCES, null);
84 context.setAttribute(METAINF_TLDS, null);
85 }
86
87 public void addResource (WebAppContext context, String attribute, Resource jar)
88 {
89 List<Resource> list = (List<Resource>)context.getAttribute(attribute);
90 if (list==null)
91 {
92 list=new ArrayList<Resource>();
93 context.setAttribute(attribute,list);
94 }
95 if (!list.contains(jar))
96 list.add(jar);
97 }
98
99
100 protected void processEntry(WebAppContext context, URI jarUri, JarEntry entry)
101 {
102 String name = entry.getName();
103
104 if (!name.startsWith("META-INF/"))
105 return;
106
107 try
108 {
109 if (name.equals("META-INF/web-fragment.xml") && context.isConfigurationDiscovered())
110 {
111 addResource(context,METAINF_FRAGMENTS,Resource.newResource(jarUri));
112 }
113 else if (name.equals("META-INF/resources/") && context.isConfigurationDiscovered())
114 {
115 addResource(context,METAINF_RESOURCES,Resource.newResource("jar:"+jarUri+"!/META-INF/resources"));
116 }
117 else
118 {
119 String lcname = name.toLowerCase();
120 if (lcname.endsWith(".tld"))
121 {
122 addResource(context,METAINF_TLDS,Resource.newResource("jar:"+jarUri+"!/"+name));
123 }
124 }
125 }
126 catch(Exception e)
127 {
128 context.getServletContext().log(jarUri+"!/"+name,e);
129 }
130 }
131 }