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.modeshape.graph.connector.federation;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import net.jcip.annotations.Immutable;
34 import org.modeshape.graph.ExecutionContext;
35 import org.modeshape.graph.Location;
36 import org.modeshape.graph.property.Name;
37 import org.modeshape.graph.property.Path;
38 import org.modeshape.graph.property.PathFactory;
39 import org.modeshape.graph.property.Property;
40
41
42
43
44
45
46 @Immutable
47 abstract class ProjectorWithPlaceholders implements Projector {
48
49 private Map<Path, PlaceholderNode> placeholderNodesByPath;
50 private Map<UUID, PlaceholderNode> placeholderNodesByUuid;
51
52 ProjectorWithPlaceholders( ExecutionContext context,
53 List<Projection> projections ) {
54 placeholderNodesByPath = new HashMap<Path, PlaceholderNode>();
55 placeholderNodesByUuid = new HashMap<UUID, PlaceholderNode>();
56
57 Collection<PlaceholderNode> placeholders = new ArrayList<PlaceholderNode>();
58 loadPlaceholderNodes(context, projections, placeholders);
59 for (PlaceholderNode placeholder : placeholders) {
60 placeholderNodesByPath.put(placeholder.location().getPath(), placeholder);
61 placeholderNodesByUuid.put(placeholder.location().getUuid(), placeholder);
62 }
63 }
64
65
66
67
68
69
70
71 public PlaceholderNode isPlaceholder( Location location ) {
72 Path path = location.getPath();
73 if (path != null) {
74 return placeholderNodesByPath.get(path);
75 }
76 UUID uuid = location.getUuid();
77 if (uuid != null) {
78 return placeholderNodesByUuid.get(uuid);
79 }
80 return null;
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 protected static void loadPlaceholderNodes( ExecutionContext context,
97 Iterable<Projection> projections,
98 Collection<PlaceholderNode> placeholderNodes ) {
99 final PathFactory pathFactory = context.getValueFactories().getPathFactory();
100 Map<Path, ProxyNode> proxyNodesByPath = new HashMap<Path, ProxyNode>();
101 Map<Path, PlaceholderNode> placeholdersByPath = new HashMap<Path, PlaceholderNode>();
102 for (Projection projection : projections) {
103
104 for (Path path : projection.getTopLevelPathsInRepository(pathFactory)) {
105 if (path.isRoot()) continue;
106
107 Location inRepository = Location.create(path);
108 ProxyNode previous = null;
109 for (Path pathInSource : projection.getPathsInSource(path, pathFactory)) {
110 Location inSource = Location.create(pathInSource);
111 ProxyNode proxy = new ProxyNode(projection, inSource, inRepository);
112 if (previous == null) {
113 previous = proxy;
114 proxyNodesByPath.put(path, proxy);
115 } else {
116 previous.add(proxy);
117 }
118 }
119
120 ProjectedNode child = previous;
121 while (!path.isRoot()) {
122
123 Path parent = path.getParent();
124 PlaceholderNode parentPlaceholder = placeholdersByPath.get(parent);
125 if (parentPlaceholder == null) {
126
127 Map<Name, Property> properties = Collections.emptyMap();
128 Location location = Location.create(parent, UUID.randomUUID());
129 parentPlaceholder = new PlaceholderNode(location, properties, new ArrayList<ProjectedNode>());
130 placeholdersByPath.put(parent, parentPlaceholder);
131 placeholderNodes.add(parentPlaceholder);
132 parentPlaceholder.children().add(child);
133 } else {
134
135 List<ProjectedNode> children = parentPlaceholder.children();
136 if (!children.contains(child)) children.add(child);
137 }
138 child = parentPlaceholder;
139 path = parent;
140 }
141 }
142 }
143 }
144 }