forked from ModeShape/modeshape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MockConnector.java
346 lines (293 loc) · 14.7 KB
/
MockConnector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.connector.mock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.jcr.NamespaceRegistry;
import org.infinispan.schematic.document.Document;
import org.infinispan.schematic.document.EditableDocument;
import org.modeshape.jcr.JcrNtLexicon;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.modeshape.jcr.federation.spi.DocumentChanges;
import org.modeshape.jcr.federation.spi.DocumentReader;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.PageKey;
import org.modeshape.jcr.federation.spi.Pageable;
import org.modeshape.jcr.federation.spi.WritableConnector;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
/**
* Mock implementation of a {@code Connector} which uses some hard-coded, in-memory external nodes to validate the Connector SPI.
*/
public class MockConnector extends WritableConnector implements Pageable {
public static final String DOC1_LOCATION = "/doc1";
public static final String DOC2_LOCATION = "/doc2";
static final String PAGED_DOC_LOCATION = "/pagedDoc";
static final String NONT_QUERYABLE_DOC_LOCATION = "/nonQueryableDoc";
private static final String DOC3_LOCATION = DOC2_LOCATION + "/doc3";
private final String PAGED_DOCUMENT_ID = newId();
private final String NON_QUERYABLE_DOCUMENT_ID = newId();
private final static Map<String, Document> persistentDocumentsByLocation = new LinkedHashMap<String, Document>();
private final static Map<String, Document> persistentDocumentsById = new HashMap<String, Document>();
private final Map<String, Document> documentsByLocation = new LinkedHashMap<String, Document>();
private final Map<String, Document> documentsById = new HashMap<String, Document>();
/**
* Flag which allow the mock data to be the same across repository restarts (i.e. connector re-initialization). This is
* set via reflection from the configuration file.
*/
private boolean persistentDataAcrossRestarts = false;
private static String newId() {
return UUID.randomUUID().toString();
}
@Override
public void initialize( NamespaceRegistry registry,
NodeTypeManager nodeTypeManager ) {
boolean alreadyInitialized = !persistentDocumentsById.isEmpty() || !persistentDocumentsByLocation.isEmpty();
if (!alreadyInitialized || !persistentDataAcrossRestarts) {
String id1 = newId();
EditableDocument doc1 = newDocument(id1).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.addProperty(nameFrom("federated1_prop1"), "a string")
.addProperty("federated1_prop2", 12)
.document();
documentsByLocation.put(DOC1_LOCATION, doc1);
documentsById.put(id1, doc1);
String id2 = newId();
String id3 = newId();
EditableDocument doc3 = newDocument(id3).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.addProperty("federated3_prop1", "yet another string")
.setParent(id2)
.document();
documentsById.put(id3, doc3);
documentsByLocation.put(DOC3_LOCATION, doc3);
EditableDocument doc2 = newDocument(id2).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.addProperty("federated2_prop1", "another string")
.addProperty("federated2_prop2", Boolean.FALSE)
.addChild(id3, "federated3")
.document();
documentsByLocation.put(DOC2_LOCATION, doc2);
documentsById.put(id2, doc2);
String id4 = newId();
EditableDocument doc4 = newDocument(id4).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.setParent(PAGED_DOCUMENT_ID)
.document();
documentsById.put(id4, doc4);
String id5 = newId();
EditableDocument doc5 = newDocument(id5).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.setParent(PAGED_DOCUMENT_ID)
.document();
documentsById.put(id5, doc5);
String id6 = newId();
EditableDocument doc6 = newDocument(id6).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.setParent(PAGED_DOCUMENT_ID)
.document();
documentsById.put(id6, doc6);
EditableDocument pagedDoc = newDocument(PAGED_DOCUMENT_ID).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.addChild(id4, "federated4")
.addChild(id5, "federated5")
.addChild(id6, "federated6")
.document();
documentsById.put(PAGED_DOCUMENT_ID, pagedDoc);
documentsByLocation.put(PAGED_DOC_LOCATION, pagedDoc);
EditableDocument nonQueryableDoc = newDocument(NON_QUERYABLE_DOCUMENT_ID).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.setNotQueryable().document();
documentsById.put(NON_QUERYABLE_DOCUMENT_ID, nonQueryableDoc);
documentsByLocation.put(NONT_QUERYABLE_DOC_LOCATION, nonQueryableDoc);
}
if (!alreadyInitialized) {
//store the static data only once
persistentDocumentsByLocation.putAll(documentsByLocation);
persistentDocumentsById.putAll(documentsById);
}
if (persistentDataAcrossRestarts) {
//make sure the same data that was created when first initialization was performed is saved
documentsByLocation.clear();
documentsByLocation.putAll(persistentDocumentsByLocation);
documentsById.clear();
documentsById.putAll(persistentDocumentsById);
}
}
@Override
public Document getDocumentById( String id ) {
Document doc = documentsById.get(id);
// this is hard-coded in order to be able to validate the paging mechanism
if (PAGED_DOCUMENT_ID.equals(id)) {
DocumentReader reader = readDocument(doc);
List<? extends Document> children = reader.getChildren();
DocumentWriter writer = newDocument(id).setPrimaryType(JcrNtLexicon.UNSTRUCTURED)
.setChildren(children.subList(0, 1))
.addPage(reader.getDocumentId(), 1, 1, children.size());
return writer.document();
}
return doc;
}
@Override
public String getDocumentId( String path ) {
Document document = documentsByLocation.get(path);
return document != null ? readDocument(document).getDocumentId() : null;
}
@Override
public boolean removeDocument( String id ) {
Document doc = documentsById.remove(id);
if (doc != null) {
for (Iterator<Map.Entry<String, Document>> iterator = documentsByLocation.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, Document> entry = iterator.next();
if (entry.getValue().equals(doc)) {
iterator.remove();
return true;
}
}
}
return false;
}
@Override
public boolean hasDocument( String id ) {
return documentsById.containsKey(id);
}
@Override
public void storeDocument( Document document ) {
DocumentReader reader = readDocument(document);
String documentId = reader.getDocumentId();
assert documentId != null;
documentsById.put(documentId, document);
}
@Override
public String newDocumentId( String parentId,
Name newDocumentName,
Name newDocumentPrimaryType ) {
return null;
}
@Override
public void updateDocument( DocumentChanges documentChanges ) {
String id = documentChanges.getDocumentId();
if (!documentsById.containsKey(id)) {
return;
}
Document existingDocument = documentsById.get(id);
DocumentReader existingDocumentReader = readDocument(existingDocument);
DocumentWriter updatedDocumentWriter = newDocument(id);
updateProperties(existingDocumentReader, updatedDocumentWriter, documentChanges);
updateMixins(existingDocumentReader, updatedDocumentWriter, documentChanges);
updateChildren(existingDocumentReader, updatedDocumentWriter, documentChanges);
updateParents(existingDocumentReader, updatedDocumentWriter, documentChanges);
documentsById.put(id, updatedDocumentWriter.document());
}
private void updateParents( DocumentReader existingDocumentReader,
DocumentWriter updatedDocumentWriter,
DocumentChanges documentChanges ) {
DocumentChanges.ParentChanges parentChanges = documentChanges.getParentChanges();
List<String> parentIds = new ArrayList<String>(existingDocumentReader.getParentIds());
if (parentChanges.hasNewPrimaryParent()) {
parentIds.clear();
parentIds.add(parentChanges.getNewPrimaryParent());
}
parentIds.removeAll(parentChanges.getRemoved());
parentIds.addAll(parentChanges.getAdded());
updatedDocumentWriter.setParents(parentIds);
}
private void updateMixins( DocumentReader existingDocumentReader,
DocumentWriter updatedDocumentWriter,
DocumentChanges documentChanges ) {
DocumentChanges.MixinChanges mixinChanges = documentChanges.getMixinChanges();
Set<Name> mixins = new HashSet<Name>(existingDocumentReader.getMixinTypes());
mixins.removeAll(mixinChanges.getRemoved());
mixins.addAll(mixinChanges.getAdded());
for (Name mixinName : mixins) {
updatedDocumentWriter.addMixinType(mixinName);
}
}
private void updateChildren( DocumentReader existingDocumentReader,
DocumentWriter updatedDocumentWriter,
DocumentChanges documentChanges ) {
DocumentChanges.ChildrenChanges childrenChanges = documentChanges.getChildrenChanges();
LinkedHashMap<String, Name> childrenMap = existingDocumentReader.getChildrenMap();
//process renames and appended
childrenMap.putAll(childrenChanges.getRenamed());
childrenMap.putAll(childrenChanges.getAppended());
//process removals
for (String removedChildId : childrenChanges.getRemoved()) {
childrenMap.remove(removedChildId);
}
List<String> childrenIdsList = new ArrayList<String>(childrenMap.keySet());
if (!childrenChanges.getInsertedBeforeAnotherChild().isEmpty()) {
Map<String, LinkedHashMap<String, Name>> insertedBeforeAnotherChild = childrenChanges.getInsertedBeforeAnotherChild();
for (String insertedBefore : insertedBeforeAnotherChild.keySet()) {
List<String> insertedChildren = new ArrayList<String>(insertedBeforeAnotherChild.get(
insertedBefore).keySet());
for (String insertedChild : insertedChildren) {
Collections.swap(childrenIdsList, childrenIdsList.indexOf(insertedBefore),
childrenIdsList.indexOf(insertedChild));
}
}
LinkedHashMap<String, Name> reorderedChildMap = new LinkedHashMap<String, Name>();
for (String childId : childrenIdsList) {
reorderedChildMap.put(childId, childrenMap.get(childId));
}
childrenMap = reorderedChildMap;
}
updatedDocumentWriter.setChildren(childrenMap);
}
private void updateProperties( DocumentReader existingDocumentReader,
DocumentWriter updatedDocumentWriter,
DocumentChanges documentChanges ) {
DocumentChanges.PropertyChanges propertyChanges = documentChanges.getPropertyChanges();
Map<Name, Property> properties = existingDocumentReader.getProperties();
DocumentReader updatedDocumentReader = readDocument(documentChanges.getDocument());
// additions
for (Name changedPropertyName : propertyChanges.getAdded()) {
properties.put(changedPropertyName, updatedDocumentReader.getProperty(changedPropertyName));
}
// changes
for (Name changedPropertyName : propertyChanges.getChanged()) {
properties.put(changedPropertyName, updatedDocumentReader.getProperty(changedPropertyName));
}
//removals
for (Name removedPropertyName : propertyChanges.getRemoved()) {
properties.remove(removedPropertyName);
}
updatedDocumentWriter.setProperties(properties);
}
@Override
public Document getChildren( PageKey pageKey ) {
String parentId = pageKey.getParentId();
Document doc = documentsById.get(parentId);
assert doc != null;
DocumentReader reader = readDocument(doc);
List<? extends Document> children = reader.getChildren();
int blockSize = (int)pageKey.getBlockSize();
int offset = pageKey.getOffsetInt();
DocumentWriter writer = newDocument(parentId).setChildren(children.subList(offset, offset + blockSize));
if (offset + blockSize == children.size()) {
return writer.document();
}
return writer.addPage(parentId, offset + 1, blockSize, children.size()).document();
}
}