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