1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.agent; |
12 | |
|
13 | |
import org.mule.api.agent.Agent; |
14 | |
|
15 | |
import java.util.ArrayList; |
16 | |
import java.util.Collection; |
17 | |
import java.util.Iterator; |
18 | |
import java.util.List; |
19 | |
import java.util.ListIterator; |
20 | |
|
21 | |
import org.apache.commons.collections.CollectionUtils; |
22 | |
import org.apache.commons.collections.Predicate; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class AgentSorter extends Object |
29 | |
{ |
30 | |
|
31 | |
public static List sortAgents(Collection agents) |
32 | |
{ |
33 | 12 | List sortedAgents = new ArrayList(); |
34 | |
|
35 | |
|
36 | 12 | Collection agentsWithoutDependencies = CollectionUtils.select(agents, new Predicate() |
37 | |
{ |
38 | 12 | public boolean evaluate(Object object) |
39 | |
{ |
40 | 34 | return ((Agent) object).getDependentAgents().size() == 0; |
41 | |
} |
42 | |
}); |
43 | 12 | sortedAgents.addAll(agentsWithoutDependencies); |
44 | |
|
45 | |
|
46 | 12 | List remainingAgents = new ArrayList(agents); |
47 | 12 | remainingAgents.removeAll(agentsWithoutDependencies); |
48 | 24 | while (!remainingAgents.isEmpty()) |
49 | |
{ |
50 | 14 | int processedAgents = 0; |
51 | 14 | ListIterator iter = remainingAgents.listIterator(); |
52 | 36 | while (iter.hasNext()) |
53 | |
{ |
54 | 22 | Agent agent = (Agent) iter.next(); |
55 | 22 | if (dependentAgentsPresent(agent.getDependentAgents(), agents, sortedAgents)) |
56 | |
{ |
57 | 12 | sortedAgents.add(agent); |
58 | 12 | iter.remove(); |
59 | 12 | processedAgents++; |
60 | |
} |
61 | 22 | } |
62 | |
|
63 | |
|
64 | |
|
65 | 14 | if (processedAgents == 0) |
66 | |
{ |
67 | 2 | throw new IllegalArgumentException("Dependency cycle: " + remainingAgents); |
68 | |
} |
69 | 12 | } |
70 | |
|
71 | 10 | return sortedAgents; |
72 | |
} |
73 | |
|
74 | |
private static boolean dependentAgentsPresent(List dependentClasses, Collection allRegisteredAgents, |
75 | |
List sortedAgents) |
76 | |
{ |
77 | 22 | Iterator dependencyIterator = dependentClasses.iterator(); |
78 | 40 | while (dependencyIterator.hasNext()) |
79 | |
{ |
80 | 28 | Class dependentClass = (Class) dependencyIterator.next(); |
81 | |
|
82 | 28 | if (!classExistsInCollection(dependentClass, allRegisteredAgents)) |
83 | |
{ |
84 | |
|
85 | 2 | continue; |
86 | |
} |
87 | |
|
88 | 26 | if (!classExistsInCollection(dependentClass, sortedAgents)) |
89 | |
{ |
90 | 10 | return false; |
91 | |
} |
92 | 16 | } |
93 | 12 | return true; |
94 | |
} |
95 | |
|
96 | |
private static boolean classExistsInCollection(Class clazz, Collection collection) |
97 | |
{ |
98 | 54 | return CollectionUtils.exists(collection, new ClassEqualityPredicate(clazz)); |
99 | |
} |
100 | |
|
101 | 0 | private static class ClassEqualityPredicate extends Object implements Predicate |
102 | |
{ |
103 | |
private Class requiredClass; |
104 | |
|
105 | |
public ClassEqualityPredicate(Class requiredClass) |
106 | |
{ |
107 | 54 | super(); |
108 | 54 | this.requiredClass = requiredClass; |
109 | 54 | } |
110 | |
|
111 | |
public boolean evaluate(Object object) |
112 | |
{ |
113 | 92 | return object.getClass().equals(requiredClass); |
114 | |
} |
115 | |
} |
116 | |
|
117 | |
} |
118 | |
|
119 | |
|