1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.webapp;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.jetty.util.resource.Resource;
23
24
25
26
27
28
29
30 public interface Ordering
31 {
32
33 public List<Resource> order(List<Resource> fragments);
34 public boolean isAbsolute ();
35 public boolean hasOther();
36
37
38
39
40
41
42
43 public static class AbsoluteOrdering implements Ordering
44 {
45 public static final String OTHER = "@@-OTHER-@@";
46 protected List<String> _order = new ArrayList<String>();
47 protected boolean _hasOther = false;
48 protected MetaData _metaData;
49
50 public AbsoluteOrdering (MetaData metaData)
51 {
52 _metaData = metaData;
53 }
54
55
56
57
58
59 public List<Resource> order(List<Resource> jars)
60 {
61 List<Resource> orderedList = new ArrayList<Resource>();
62 List<Resource> tmp = new ArrayList<Resource>(jars);
63
64
65
66 Map<String,FragmentDescriptor> others = new HashMap<String,FragmentDescriptor>(_metaData.getNamedFragments());
67
68
69 int index = -1;
70 for (String item:_order)
71 {
72 if (!item.equals(OTHER))
73 {
74 FragmentDescriptor f = others.remove(item);
75 if (f != null)
76 {
77 Resource jar = _metaData.getJarForFragment(item);
78 orderedList.add(jar);
79
80 tmp.remove(jar);
81 }
82 }
83 else
84 index = orderedList.size();
85 }
86
87
88 if (_hasOther)
89 {
90 orderedList.addAll((index < 0? 0: index), tmp);
91 }
92
93 return orderedList;
94 }
95
96
97 public boolean isAbsolute()
98 {
99 return true;
100 }
101
102 public void add (String name)
103 {
104 _order.add(name);
105 }
106
107 public void addOthers ()
108 {
109 if (_hasOther)
110 throw new IllegalStateException ("Duplicate <other> element in absolute ordering");
111
112 _hasOther = true;
113 _order.add(OTHER);
114 }
115
116
117 public boolean hasOther ()
118 {
119 return _hasOther;
120 }
121 }
122
123
124
125
126
127 public static class RelativeOrdering implements Ordering
128 {
129 protected MetaData _metaData;
130 protected LinkedList<Resource> _beforeOthers = new LinkedList<Resource>();
131 protected LinkedList<Resource> _afterOthers = new LinkedList<Resource>();
132 protected LinkedList<Resource> _noOthers = new LinkedList<Resource>();
133
134 public RelativeOrdering (MetaData metaData)
135 {
136 _metaData = metaData;
137 }
138
139
140
141
142
143
144 public List<Resource> order(List<Resource> jars)
145 {
146
147 for (Resource jar:jars)
148 {
149
150 FragmentDescriptor descriptor = _metaData.getFragment(jar);
151 if (descriptor != null)
152 {
153 switch (descriptor.getOtherType())
154 {
155 case None:
156 {
157 ((RelativeOrdering)_metaData.getOrdering()).addNoOthers(jar);
158 break;
159 }
160 case Before:
161 {
162 ((RelativeOrdering)_metaData.getOrdering()).addBeforeOthers(jar);
163 break;
164 }
165 case After:
166 {
167 ((RelativeOrdering)_metaData.getOrdering()).addAfterOthers(jar);
168 break;
169 }
170 }
171 }
172 else
173 {
174
175 ((RelativeOrdering)_metaData.getOrdering()).addNoOthers(jar);
176 }
177 }
178
179
180 List<Resource> orderedList = new ArrayList<Resource>();
181 int maxIterations = 2;
182 boolean done = false;
183 do
184 {
185
186 boolean changesBefore = orderList(_beforeOthers);
187
188
189 boolean changesAfter = orderList(_afterOthers);
190
191
192 boolean changesNone = orderList(_noOthers);
193
194
195 done = (!changesBefore && !changesAfter && !changesNone);
196 }
197 while (!done && (--maxIterations >0));
198
199
200 if (!done)
201 throw new IllegalStateException("Circular references for fragments");
202
203 for (Resource r: _beforeOthers)
204 orderedList.add(r);
205 for (Resource r: _noOthers)
206 orderedList.add(r);
207 for(Resource r: _afterOthers)
208 orderedList.add(r);
209
210 return orderedList;
211 }
212
213
214 public boolean isAbsolute ()
215 {
216 return false;
217 }
218
219
220 public boolean hasOther ()
221 {
222 return !_beforeOthers.isEmpty() || !_afterOthers.isEmpty();
223 }
224
225 public void addBeforeOthers (Resource r)
226 {
227 _beforeOthers.addLast(r);
228 }
229
230 public void addAfterOthers (Resource r)
231 {
232 _afterOthers.addLast(r);
233 }
234
235 public void addNoOthers (Resource r)
236 {
237 _noOthers.addLast(r);
238 }
239
240 protected boolean orderList (LinkedList<Resource> list)
241 {
242
243 boolean changes = false;
244 List<Resource> iterable = new ArrayList<Resource>(list);
245 Iterator<Resource> itor = iterable.iterator();
246
247 while (itor.hasNext())
248 {
249 Resource r = itor.next();
250 FragmentDescriptor f = _metaData.getFragment(r);
251 if (f == null)
252 {
253
254 continue;
255 }
256
257
258 List<String> befores = f.getBefores();
259 if (befores != null && !befores.isEmpty())
260 {
261 for (String b: befores)
262 {
263
264
265
266 if (!isBefore(list, f.getName(), b))
267 {
268
269 int idx1 = getIndexOf(list, f.getName());
270 int idx2 = getIndexOf(list, b);
271
272
273 if (idx2 < 0)
274 {
275 changes = true;
276
277 Resource bResource = _metaData.getJarForFragment(b);
278 if (bResource != null)
279 {
280
281 if (_noOthers.remove(bResource))
282 {
283 insert(list, idx1+1, b);
284
285 }
286 }
287 }
288 else
289 {
290
291 list.remove(idx1);
292 insert(list, idx2, f.getName());
293 changes = true;
294 }
295 }
296 }
297 }
298
299
300 List<String> afters = f.getAfters();
301 if (afters != null && !afters.isEmpty())
302 {
303 for (String a: afters)
304 {
305
306 if (!isAfter(list, f.getName(), a))
307 {
308
309 int idx1 = getIndexOf(list, f.getName());
310 int idx2 = getIndexOf(list, a);
311
312
313 if (idx2 < 0)
314 {
315 changes = true;
316
317 Resource aResource = _metaData.getJarForFragment(a);
318 if (aResource != null)
319 {
320 if (_noOthers.remove(aResource))
321 {
322 insert(list,idx1, aResource);
323 }
324 }
325 }
326 else
327 {
328
329 list.remove(idx2);
330 insert(list,idx1, a);
331 changes = true;
332 }
333 }
334
335
336
337 }
338 }
339 }
340
341 return changes;
342 }
343
344
345
346
347
348
349
350
351 protected boolean isBefore (List<Resource> list, String fragNameA, String fragNameB)
352 {
353
354
355 int idxa = getIndexOf(list, fragNameA);
356 int idxb = getIndexOf(list, fragNameB);
357
358
359 if (idxb >=0 && idxb < idxa)
360 {
361
362 return false;
363 }
364
365 if (idxb < 0)
366 {
367
368
369 if (list == _beforeOthers)
370 {
371
372
373 return true;
374 }
375 else if (list == _afterOthers)
376 {
377
378
379 if (_beforeOthers.contains(fragNameB))
380 throw new IllegalStateException("Incorrect relationship: "+fragNameA+" before "+fragNameB);
381 else
382 return false;
383 }
384 }
385
386
387 return true;
388 }
389
390
391
392
393
394
395
396
397
398 protected boolean isAfter(List<Resource> list, String fragNameA, String fragNameB)
399 {
400 int idxa = getIndexOf(list, fragNameA);
401 int idxb = getIndexOf(list, fragNameB);
402
403 if (idxb >=0 && idxa < idxb)
404 {
405
406 return false;
407 }
408
409 if (idxb < 0)
410 {
411
412
413 if (list == _afterOthers)
414 {
415
416
417 return true;
418 }
419 else if (list == _beforeOthers)
420 {
421
422
423 if (_afterOthers.contains(fragNameB))
424 throw new IllegalStateException("Incorrect relationship: "+fragNameB+" after "+fragNameA);
425 else
426 return false;
427 }
428 }
429
430 return true;
431 }
432
433
434
435
436
437
438
439
440
441 protected void insert(List<Resource> list, int index, String fragName)
442 {
443 Resource jar = _metaData.getJarForFragment(fragName);
444 if (jar == null)
445 throw new IllegalStateException("No jar for insertion");
446
447 insert(list, index, jar);
448 }
449
450 protected void insert(List<Resource> list, int index, Resource resource)
451 {
452 if (list == null)
453 throw new IllegalStateException("List is null for insertion");
454
455
456 if (index > list.size())
457 list.add(resource);
458 else
459 list.add(index, resource);
460 }
461
462 protected void remove (List<Resource> resources, Resource r)
463 {
464 if (resources == null)
465 return;
466 resources.remove(r);
467 }
468
469 protected int getIndexOf(List<Resource> resources, String fragmentName)
470 {
471 FragmentDescriptor fd = _metaData.getFragment(fragmentName);
472 if (fd == null)
473 return -1;
474
475
476 Resource r = _metaData.getJarForFragment(fragmentName);
477 if (r == null)
478 return -1;
479
480 return resources.indexOf(r);
481 }
482 }
483
484 }