-
Notifications
You must be signed in to change notification settings - Fork 188
/
DummyObject.java
473 lines (409 loc) · 15.1 KB
/
DummyObject.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright (c) 2010-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.icf.dummy.resource;
import java.io.FileNotFoundException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
/**
* @author Radovan Semancik
*
*/
public abstract class DummyObject implements DebugDumpable {
private String id;
// private int internalId = -1;
private String name;
private Map<String,Set<Object>> attributes = new HashMap<String, Set<Object>>();
private Boolean enabled = true;
private Date validFrom = null;
private Date validTo = null;
protected DummyResource resource;
private final Set<String> auxiliaryObjectClassNames = new HashSet<>();
private BreakMode modifyBreakMode = null;
public DummyObject() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public DummyObject(String name) {
this.name = name;
}
public DummyResource getResource() {
return resource;
}
public void setResource(DummyResource resource) {
this.resource = resource;
}
public String getName() {
return name;
}
public void setName(String username) {
this.name = username;
}
public Boolean isEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) throws ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
this.enabled = enabled;
}
public Date getValidFrom() {
return validFrom;
}
public void setValidFrom(Date validFrom) throws ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
this.validFrom = validFrom;
}
public Date getValidTo() {
return validTo;
}
public void setValidTo(Date validTo) throws ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
this.validTo = validTo;
}
public BreakMode getModifyBreakMode() {
return modifyBreakMode;
}
public void setModifyBreakMode(BreakMode modifyBreakMode) {
this.modifyBreakMode = modifyBreakMode;
}
public Set<String> getAttributeNames() {
return attributes.keySet();
}
public <T> Set<T> getAttributeValues(String attrName, Class<T> type) {
Set<Object> values = attributes.get(attrName);
return (Set)values;
}
public <T> T getAttributeValue(String attrName, Class<T> type) {
Set<T> values = getAttributeValues(attrName, type);
if (values == null || values.isEmpty()) {
return null;
}
if (values.size()>1) {
throw new IllegalArgumentException("Attempt to fetch single value from a multi-valued attribute "+attrName);
}
return values.iterator().next();
}
public String getAttributeValue(String attrName) {
return getAttributeValue(attrName,String.class);
}
public void replaceAttributeValue(String name, Object value) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
Collection<Object> values = new ArrayList<Object>(1);
values.add(value);
replaceAttributeValues(name, values);
}
public void replaceAttributeValues(String name, Collection<Object> values) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
Set<Object> currentValues = attributes.get(name);
if (currentValues == null) {
currentValues = new HashSet<Object>();
attributes.put(name, currentValues);
} else {
currentValues.clear();
}
currentValues.addAll(values);
checkSchema(name, values, "replace");
recordModify();
}
public void replaceAttributeValues(String name, Object... values) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
Set<Object> currentValues = attributes.get(name);
if (currentValues == null) {
currentValues = new HashSet<Object>();
attributes.put(name, currentValues);
} else {
currentValues.clear();
}
List<Object> valuesList = Arrays.asList(values);
currentValues.addAll(valuesList);
checkSchema(name, valuesList, "replace");
if (valuesList.isEmpty()) {
attributes.remove(name);
}
recordModify();
}
public void addAttributeValue(String name, Object value) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
Collection<Object> values = new ArrayList<Object>(1);
values.add(value);
addAttributeValues(name, values);
}
public void addAttributeValues(String name, Collection<Object> valuesToAdd) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
Set<Object> currentValues = attributes.get(name);
if (currentValues == null) {
currentValues = new HashSet<Object>();
attributes.put(name, currentValues);
}
for(Object valueToAdd: valuesToAdd) {
addAttributeValue(name, currentValues, valueToAdd);
}
recordModify();
}
public void addAttributeValues(String name, String... valuesToAdd) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
Set<Object> currentValues = attributes.get(name);
if (currentValues == null) {
currentValues = new HashSet<Object>();
attributes.put(name, currentValues);
}
for (Object valueToAdd: valuesToAdd) {
addAttributeValue(name, currentValues, valueToAdd);
}
recordModify();
}
private void addAttributeValue(String attrName, Set<Object> currentValues, Object valueToAdd) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
if (resource != null && !resource.isTolerateDuplicateValues()) {
for (Object currentValue: currentValues) {
if (currentValue.equals(valueToAdd)) {
throw new IllegalArgumentException("The value '"+valueToAdd+"' of attribute '"+attrName+"' conflicts with existing value: Attempt to add value that already exists");
}
if (resource.isCaseIgnoreValues() && (valueToAdd instanceof String)) {
if (StringUtils.equalsIgnoreCase((String)currentValue, (String)valueToAdd)) {
throw new IllegalArgumentException("The value '"+valueToAdd+"' of attribute '"+attrName+"' conflicts with existing value: Attempt to add value that already exists");
}
}
}
}
Set<Object> valuesToCheck = new HashSet<Object>();
valuesToCheck.addAll(currentValues);
valuesToCheck.add(valueToAdd);
checkSchema(attrName, valuesToCheck, "add");
currentValues.add(valueToAdd);
}
public void removeAttributeValue(String name, Object value) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
Collection<Object> values = new ArrayList<Object>();
values.add(value);
removeAttributeValues(name, values);
}
public void removeAttributeValues(String name, Collection<Object> values) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException {
checkModifyBreak();
Set<Object> currentValues = attributes.get(name);
if (currentValues == null) {
currentValues = new HashSet<Object>();
attributes.put(name, currentValues);
}
Set<Object> valuesToCheck = new HashSet<Object>();
valuesToCheck.addAll(currentValues);
valuesToCheck.removeAll(values);
checkSchema(name, valuesToCheck, "remove");
Iterator<Object> iterator = currentValues.iterator();
boolean foundMember = false;
if (name.equals(DummyGroup.ATTR_MEMBERS_NAME) && !resource.isTolerateDuplicateValues()){
checkIfExist(values, currentValues);
}
while(iterator.hasNext()) {
Object currentValue = iterator.next();
boolean found = false;
for (Object value: values) {
if (resource.isCaseIgnoreValues() && currentValue instanceof String && value instanceof String) {
if (StringUtils.equalsIgnoreCase((String)currentValue, (String)value)) {
found = true;
break;
}
} else {
if (currentValue.equals(value)) {
found = true;
break;
}
}
}
if (found) {
iterator.remove();
}
}
recordModify();
}
public Set<String> getAuxiliaryObjectClassNames() {
return auxiliaryObjectClassNames;
}
public void addAuxiliaryObjectClassName(String name) {
auxiliaryObjectClassNames.add(name);
}
public void replaceAuxiliaryObjectClassNames(List<?> values) {
auxiliaryObjectClassNames.clear();
addAuxiliaryObjectClassNames(values);
}
public void deleteAuxiliaryObjectClassNames(List<?> values) {
for (Object value : values) {
auxiliaryObjectClassNames.remove(String.valueOf(value));
}
}
public void addAuxiliaryObjectClassNames(List<?> values) {
for (Object value : values) {
auxiliaryObjectClassNames.add(String.valueOf(value));
}
}
private void checkIfExist(Collection<Object> valuesToDelete, Set<Object> currentValues) throws SchemaViolationException{
for (Object valueToDelete : valuesToDelete) {
boolean found = false;
for (Object currentValue : currentValues) {
if (resource.isCaseIgnoreValues() && currentValue instanceof String && valueToDelete instanceof String) {
if (StringUtils.equalsIgnoreCase((String)currentValue, (String)valueToDelete)) {
found = true;
break;
}
} else {
if (currentValue.equals(valueToDelete)) {
found = true;
break;
}
}
}
if (!found){
throw new SchemaViolationException("no such member: " + valueToDelete + " in " + currentValues);
}
}
}
protected void checkModifyBreak() throws ConnectException, FileNotFoundException, SchemaViolationException {
if (resource == null) {
return;
}
BreakMode modifyBreakMode = this.modifyBreakMode;
if (modifyBreakMode == null) {
modifyBreakMode = resource.getModifyBreakMode();
}
if (modifyBreakMode == BreakMode.NONE) {
// go on
} else if (modifyBreakMode == BreakMode.NETWORK) {
throw new ConnectException("Network error (simulated error)");
} else if (modifyBreakMode == BreakMode.IO) {
throw new FileNotFoundException("IO error (simulated error)");
} else if (modifyBreakMode == BreakMode.SCHEMA) {
throw new SchemaViolationException("Schema violation (simulated error)");
} else if (modifyBreakMode == BreakMode.GENERIC) {
// The connector will react with generic exception
throw new IllegalArgumentException("Generic error (simulated error)");
} else if (modifyBreakMode == BreakMode.RUNTIME) {
// The connector will just pass this up
throw new IllegalStateException("Generic error (simulated error)");
} else if (modifyBreakMode == BreakMode.UNSUPPORTED) {
throw new UnsupportedOperationException("Not supported (simulated error)");
} else {
// This is a real error. Use this strange thing to make sure it passes up
throw new RuntimeException("Unknown schema break mode "+modifyBreakMode);
}
}
private void recordModify() {
if (resource != null) {
resource.recordModify(this);
}
}
protected void checkSchema(String attrName, Collection<Object> values, String operationName) throws SchemaViolationException {
if (resource == null || !resource.isEnforceSchema()) {
return;
}
DummyObjectClass accountObjectClass;
try {
accountObjectClass = getObjectClass();
} catch (Exception e) {
// No not enforce schema if the schema is broken (simulated)
return;
}
if (accountObjectClass == null) {
// Nothing to check
return;
}
DummyAttributeDefinition attributeDefinition = getAttributeDefinition(attrName);
if (attributeDefinition == null) {
throw new SchemaViolationException("Attribute "+attrName+" is not defined in resource schema");
}
if (attributeDefinition.isRequired() && (values == null || values.isEmpty())) {
throw new SchemaViolationException(operationName + " of required attribute "+attrName+" results in no values");
}
if (!attributeDefinition.isMulti() && values != null && values.size() > 1) {
throw new SchemaViolationException(operationName + " of single-valued attribute "+attrName+" results in "+values.size()+" values");
}
}
public DummyAttributeDefinition getAttributeDefinition(String attrName) {
DummyAttributeDefinition def = getObjectClassNoExceptions().getAttributeDefinition(attrName);
if (def != null) {
return def;
}
for (String auxClassName : getAuxiliaryObjectClassNames()) {
DummyObjectClass auxObjectClass = resource.getAuxiliaryObjectClassMap().get(auxClassName);
if (auxObjectClass == null) {
throw new IllegalStateException("Auxiliary object class " + auxClassName + " couldn't be found");
}
def = auxObjectClass.getAttributeDefinition(attrName);
if (def != null) {
return def;
}
}
return null;
}
abstract protected DummyObjectClass getObjectClass() throws ConnectException, FileNotFoundException, SchemaViolationException;
abstract protected DummyObjectClass getObjectClassNoExceptions();
public abstract String getShortTypeName();
@Override
public String toString() {
return getClass().getSimpleName()+"(" + toStringContent() + ")";
}
protected String toStringContent() {
return "name=" + name + ", attributes=" + attributes + ", enabled=" + enabled;
}
@Override
public String debugDump() {
return debugDump(0);
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("DummyAccount: ").append(name).append("\n");
if (!auxiliaryObjectClassNames.isEmpty()) {
DebugUtil.debugDumpWithLabelToString(sb, "Auxiliary object classes", auxiliaryObjectClassNames, indent + 1);
}
sb.append("\n");
DebugUtil.debugDumpWithLabelToString(sb, "Enabled", enabled, indent + 1);
if (validFrom != null || validTo != null) {
sb.append("\n");
DebugUtil.debugDumpLabel(sb, "Validity", indent + 1);
sb.append(" ").append(PrettyPrinter.prettyPrint(validFrom)).append(" - ").append(PrettyPrinter.prettyPrint(validTo));
}
sb.append("\n");
DebugUtil.debugDumpWithLabel(sb, "Attributes", attributes, indent + 1);
extendDebugDump(sb, indent);
return sb.toString();
}
protected void extendDebugDump(StringBuilder sb, int indent) {
// Nothing to do
}
public boolean isReturnedByDefault(String attrName) {
final DummyAttributeDefinition attributeDefinition = getAttributeDefinition(attrName);
if (attributeDefinition != null) {
return attributeDefinition.isReturnedByDefault();
} else {
System.out.println("Warning: attribute " + attrName + " is not defined in " + this);
return false;
}
}
}