1 package org.codehaus.plexus.component.composition;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.List;
20
21 import org.codehaus.plexus.PlexusConstants;
22 import org.codehaus.plexus.component.repository.ComponentDescriptor;
23 import org.codehaus.plexus.component.repository.ComponentRequirement;
24 import org.codehaus.plexus.component.repository.ComponentRequirementList;
25 import org.codehaus.plexus.util.StringUtils;
26 import org.codehaus.plexus.util.dag.CycleDetectedException;
27 import org.codehaus.plexus.util.dag.DAG;
28
29
30
31
32
33 public class DefaultCompositionResolver implements CompositionResolver {
34 private DAG dag = new DAG();
35
36 public void addComponentDescriptor(ComponentDescriptor<?> componentDescriptor)
37 throws CycleDetectedInComponentGraphException {
38 String key = getDAGKey(componentDescriptor.getRole(), componentDescriptor.getRoleHint());
39
40 List<ComponentRequirement> requirements = componentDescriptor.getRequirements();
41
42 for (ComponentRequirement requirement : requirements) {
43 try {
44 if (requirement instanceof ComponentRequirementList) {
45 for (String hint : ((ComponentRequirementList) requirement).getRoleHints()) {
46 dag.addEdge(key, getDAGKey(requirement.getRole(), hint));
47 }
48 } else {
49 dag.addEdge(key, getDAGKey(requirement.getRole(), requirement.getRoleHint()));
50 }
51 } catch (CycleDetectedException e) {
52 throw new CycleDetectedInComponentGraphException("Cyclic requirement detected", e);
53 }
54 }
55 }
56
57
58
59
60 public List getRequirements(String role, String roleHint) {
61 return dag.getChildLabels(getDAGKey(role, roleHint));
62 }
63
64
65
66
67 public List findRequirements(String role, String roleHint) {
68 return dag.getParentLabels(getDAGKey(role, roleHint));
69 }
70
71 private String getDAGKey(String role, String roleHint) {
72 return role
73 + SEPARATOR_CHAR
74 + (StringUtils.isNotEmpty(roleHint) ? roleHint : PlexusConstants.PLEXUS_DEFAULT_HINT);
75 }
76 }