1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.objectweb.fractal.cecilia.adl.components;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.SortedMap;
30 import java.util.TreeMap;
31
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.AbstractLoader;
34 import org.objectweb.fractal.adl.Definition;
35 import org.objectweb.fractal.adl.components.Component;
36 import org.objectweb.fractal.adl.components.ComponentContainer;
37
38
39
40
41
42
43
44 public class StartOrderLoader extends AbstractLoader {
45
46 public Definition load(final String name, final Map<Object, Object> context)
47 throws ADLException {
48 final Definition d = clientLoader.load(name, context);
49 if (d instanceof ComponentContainer) checkOrder((ComponentContainer) d);
50
51 return d;
52 }
53
54 protected void checkOrder(final ComponentContainer container)
55 throws ADLException {
56
57
58
59 SortedMap<Integer, List<Component>> sortedComps = null;
60 final Component[] components = container.getComponents();
61 for (final Component component : components) {
62 checkOrder(component);
63 final int startOrder;
64 try {
65 startOrder = ComponentHelper.getStartOrder(component);
66 } catch (final NumberFormatException e) {
67 throw new ADLException(ComponentErrors.INVALID_START_ORDER, component);
68 }
69 if (startOrder < 0) {
70 throw new ADLException(ComponentErrors.INVALID_NEGATIVE_START_ORDER,
71 component);
72 } else if (startOrder != Integer.MAX_VALUE) {
73 if (sortedComps == null)
74 sortedComps = new TreeMap<Integer, List<Component>>();
75 List<Component> comps = sortedComps.get(startOrder);
76 if (comps == null) {
77 comps = new ArrayList<Component>();
78 sortedComps.put(startOrder, comps);
79 }
80 comps.add(component);
81 }
82 }
83
84 if (sortedComps != null) {
85
86
87
88 final List<Component> unsortedComps = new ArrayList<Component>(
89 components.length - sortedComps.size());
90 for (final Component component : components) {
91 if (ComponentHelper.getStartOrder(component) == Integer.MAX_VALUE)
92 unsortedComps.add(component);
93
94 container.removeComponent(component);
95 }
96 for (final List<Component> comps : sortedComps.values()) {
97 for (final Component component : comps) {
98 container.addComponent(component);
99 }
100 }
101 for (final Component component : unsortedComps) {
102 container.addComponent(component);
103 }
104 }
105 }
106 }