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.Collections;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Map;
32 import net.jcip.annotations.Immutable;
33 import org.modeshape.common.collection.Problems;
34 import org.modeshape.common.collection.ThreadSafeProblems;
35 import org.modeshape.common.util.CheckArg;
36 import org.modeshape.graph.ExecutionContext;
37 import org.modeshape.graph.Location;
38 import org.modeshape.graph.cache.CachePolicy;
39 import org.modeshape.graph.connector.RepositoryContext;
40
41
42
43
44 @Immutable
45 class FederatedWorkspace {
46
47 private final RepositoryContext repositoryContext;
48 private final String sourceName;
49 private final String workspaceName;
50 private final List<Projection> projections;
51 private final Map<String, List<Projection>> projectionsBySourceName;
52 private final CachePolicy cachePolicy;
53 private final Problems problems;
54 private final Projector projector;
55
56
57
58
59
60
61
62
63
64
65
66 public FederatedWorkspace( RepositoryContext repositoryContext,
67 String sourceName,
68 String workspaceName,
69 Iterable<Projection> projections,
70 CachePolicy cachePolicy ) {
71 CheckArg.isNotNull(repositoryContext, "repositoryContext");
72 CheckArg.isNotNull(sourceName, "sourceName");
73 CheckArg.isNotNull(workspaceName, "workspaceName");
74 CheckArg.isNotNull(projections, "projections");
75 this.repositoryContext = repositoryContext;
76 this.workspaceName = workspaceName;
77 this.sourceName = sourceName;
78 this.cachePolicy = cachePolicy;
79 this.problems = new ThreadSafeProblems();
80 List<Projection> projectionList = new ArrayList<Projection>();
81 for (Projection projection : projections) {
82 if (projection == null) continue;
83 if (!projectionList.contains(projection)) {
84 projectionList.add(projection);
85 }
86 }
87 this.projections = Collections.unmodifiableList(projectionList);
88 CheckArg.isNotEmpty(this.projections, "sourceProjections");
89 this.projectionsBySourceName = new HashMap<String, List<Projection>>();
90 for (Projection projection : this.projections) {
91 String nameOfSource = projection.getSourceName();
92 List<Projection> projectionsForSource = projectionsBySourceName.get(nameOfSource);
93 if (projectionsForSource == null) {
94 projectionsForSource = new LinkedList<Projection>();
95 projectionsBySourceName.put(nameOfSource, projectionsForSource);
96 }
97 projectionsForSource.add(projection);
98 }
99
100
101 ExecutionContext context = this.repositoryContext.getExecutionContext();
102 Projector projector = MirrorProjector.with(context, projectionList);
103 if (projector == null) projector = BranchedMirrorProjector.with(context, projectionList);
104 if (projector == null) projector = OffsetMirrorProjector.with(context, projectionList);
105 if (projector == null) projector = GeneralProjector.with(context, projectionList);
106 assert projector != null;
107 this.projector = projector;
108 }
109
110
111
112
113
114
115 public RepositoryContext getRepositoryContext() {
116 return repositoryContext;
117 }
118
119
120
121
122
123
124 public String getSourceName() {
125 return sourceName;
126 }
127
128
129
130
131
132
133 public String getName() {
134 return this.workspaceName;
135 }
136
137
138
139
140
141
142 public CachePolicy getCachePolicy() {
143 return cachePolicy;
144 }
145
146
147
148
149
150
151
152 public Problems getProblems() {
153 return problems;
154 }
155
156
157
158
159
160
161 public List<Projection> getProjections() {
162 return projections;
163 }
164
165
166
167
168
169
170
171
172 public boolean contains( String sourceName,
173 String workspaceName ) {
174 List<Projection> projections = this.projectionsBySourceName.get(sourceName);
175 if (projections != null) {
176 for (Projection projection : projections) {
177 if (projection.getWorkspaceName().equals(workspaceName)) return true;
178 }
179 }
180 return false;
181 }
182
183
184
185
186
187
188
189 Map<String, List<Projection>> getProjectionsBySourceName() {
190 return projectionsBySourceName;
191 }
192
193
194
195
196
197
198
199
200
201
202 public ProjectedNode project( ExecutionContext context,
203 Location location,
204 boolean requiresUpdate ) {
205 return projector.project(context, location, requiresUpdate);
206 }
207
208
209
210
211
212
213 @Override
214 public int hashCode() {
215 return this.workspaceName.hashCode();
216 }
217
218
219
220
221
222
223 @Override
224 public boolean equals( Object obj ) {
225 if (obj == this) return true;
226 if (obj instanceof FederatedWorkspace) {
227 FederatedWorkspace that = (FederatedWorkspace)obj;
228 if (!this.getSourceName().equals(that.getSourceName())) return false;
229 if (!this.getName().equals(that.getName())) return false;
230 if (!this.getProjections().equals(that.getProjections())) return false;
231 return true;
232 }
233 return false;
234 }
235
236 }