1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.quickstart;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25
26 import javax.servlet.ServletContext;
27
28 import org.eclipse.jetty.annotations.AnnotationConfiguration;
29 import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
30 import org.eclipse.jetty.plus.annotation.ContainerInitializer;
31 import org.eclipse.jetty.util.QuotedStringTokenizer;
32 import org.eclipse.jetty.util.resource.Resource;
33 import org.eclipse.jetty.util.resource.ResourceCollection;
34 import org.eclipse.jetty.webapp.Descriptor;
35 import org.eclipse.jetty.webapp.IterativeDescriptorProcessor;
36 import org.eclipse.jetty.webapp.MetaInfConfiguration;
37 import org.eclipse.jetty.webapp.WebAppContext;
38 import org.eclipse.jetty.xml.XmlParser;
39
40
41
42
43
44
45 public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
46 {
47
48
49
50 public QuickStartDescriptorProcessor()
51 {
52 try
53 {
54 registerVisitor("context-param", this.getClass().getMethod("visitContextParam", __signature));
55 }
56 catch (Exception e)
57 {
58 throw new IllegalStateException(e);
59 }
60 }
61
62
63
64
65 @Override
66 public void start(WebAppContext context, Descriptor descriptor)
67 {
68 }
69
70
71
72
73 @Override
74 public void end(WebAppContext context, Descriptor descriptor)
75 {
76 }
77
78
79
80
81
82
83
84 public void visitContextParam (WebAppContext context, Descriptor descriptor, XmlParser.Node node)
85 throws Exception
86 {
87 String name = node.getString("param-name", false, true);
88 String value = node.getString("param-value", false, true);
89 List<String> values = new ArrayList<>();
90
91
92 switch(name)
93 {
94 case ServletContext.ORDERED_LIBS:
95 case AnnotationConfiguration.CONTAINER_INITIALIZERS:
96 case MetaInfConfiguration.METAINF_TLDS:
97 case MetaInfConfiguration.METAINF_RESOURCES:
98
99 context.removeAttribute(name);
100
101 QuotedStringTokenizer tok = new QuotedStringTokenizer(value,",");
102 while(tok.hasMoreElements())
103 values.add(tok.nextToken().trim());
104
105 break;
106
107 default:
108 values.add(value);
109 }
110
111
112 switch(name)
113 {
114 case ServletContext.ORDERED_LIBS:
115 {
116 List<Object> libs = new ArrayList<>();
117 Object o=context.getAttribute(ServletContext.ORDERED_LIBS);
118 if (o instanceof Collection<?>)
119 libs.addAll((Collection<?>)o);
120 libs.addAll(values);
121 if (libs.size()>0)
122 context.setAttribute(ServletContext.ORDERED_LIBS,libs);
123
124 break;
125 }
126
127 case AnnotationConfiguration.CONTAINER_INITIALIZERS:
128 {
129 for (String i : values)
130 visitContainerInitializer(context, new ContainerInitializer(Thread.currentThread().getContextClassLoader(), i));
131 break;
132 }
133
134 case MetaInfConfiguration.METAINF_TLDS:
135 {
136 List<Object> tlds = new ArrayList<>();
137 String war=context.getBaseResource().getURI().toString();
138 Object o=context.getAttribute(MetaInfConfiguration.METAINF_TLDS);
139 if (o instanceof Collection<?>)
140 tlds.addAll((Collection<?>)o);
141 for (String i : values)
142 {
143 Resource r = Resource.newResource(i.replace("${WAR}/",war));
144 if (r.exists())
145 tlds.add(r.getURL());
146 else
147 throw new IllegalArgumentException("TLD not found: "+r);
148 }
149
150 if (tlds.size()>0)
151 context.setAttribute(MetaInfConfiguration.METAINF_TLDS,tlds);
152 break;
153 }
154
155 case MetaInfConfiguration.METAINF_RESOURCES:
156 {
157 String war=context.getBaseResource().getURI().toString();
158 for (String i : values)
159 {
160 Resource r = Resource.newResource(i.replace("${WAR}/",war));
161 if (r.exists())
162 visitMetaInfResource(context,r);
163 else
164 throw new IllegalArgumentException("Resource not found: "+r);
165 }
166 break;
167 }
168
169 default:
170
171 }
172 }
173
174
175 public void visitContainerInitializer (WebAppContext context, ContainerInitializer containerInitializer)
176 {
177 if (containerInitializer == null)
178 return;
179
180
181 List<ContainerInitializer> containerInitializers = (List<ContainerInitializer>)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
182 if (containerInitializers == null)
183 {
184 containerInitializers = new ArrayList<ContainerInitializer>();
185 context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS, containerInitializers);
186 }
187
188 containerInitializers.add(containerInitializer);
189
190
191 ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
192 if (starter == null)
193 {
194 starter = new ServletContainerInitializersStarter(context);
195 context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
196 context.addBean(starter, true);
197 }
198 }
199
200
201 public void visitMetaInfResource (WebAppContext context, Resource dir)
202 {
203 Collection<Resource> metaInfResources = (Collection<Resource>)context.getAttribute(MetaInfConfiguration.METAINF_RESOURCES);
204 if (metaInfResources == null)
205 {
206 metaInfResources = new HashSet<Resource>();
207 context.setAttribute(MetaInfConfiguration.METAINF_RESOURCES, metaInfResources);
208 }
209 metaInfResources.add(dir);
210
211 Resource[] collection=new Resource[metaInfResources.size()+1];
212 int i=0;
213 collection[i++]=context.getBaseResource();
214 for (Resource resource : metaInfResources)
215 collection[i++]=resource;
216 context.setBaseResource(new ResourceCollection(collection));
217 }
218 }