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().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
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 }