/
RefinedResourceSchema.java
504 lines (431 loc) · 20.8 KB
/
RefinedResourceSchema.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* Copyright (c) 2010-2013 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.midpoint.common.refinery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import org.apache.commons.lang.Validate;
import org.w3c.dom.Element;
import com.evolveum.midpoint.common.monitor.InternalMonitor;
import com.evolveum.midpoint.prism.Definition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.processor.ResourceSchema;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LayerType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceObjectTypeDefinitionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SchemaHandlingType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
/**
* @author semancik
*
*/
public class RefinedResourceSchema extends ResourceSchema implements DebugDumpable {
private static final String USER_DATA_KEY_PARSED_RESOURCE_SCHEMA = RefinedResourceSchema.class.getName()+".parsedResourceSchema";
private static final String USER_DATA_KEY_REFINED_SCHEMA = RefinedResourceSchema.class.getName()+".refinedSchema";
private ResourceSchema originalResourceSchema;
protected RefinedResourceSchema(PrismContext prismContext) {
super(prismContext);
}
private RefinedResourceSchema(ResourceType resourceType, ResourceSchema originalResourceSchema, PrismContext prismContext) {
super(ResourceTypeUtil.getResourceNamespace(resourceType), prismContext);
Validate.notNull(originalResourceSchema);
this.originalResourceSchema = originalResourceSchema;
}
public Collection<? extends RefinedObjectClassDefinition> getRefinedDefinitions() {
Collection<RefinedObjectClassDefinition> ocDefs = new ArrayList<RefinedObjectClassDefinition>();
for (Definition def: definitions) {
if (def instanceof RefinedObjectClassDefinition) {
RefinedObjectClassDefinition rOcDef = (RefinedObjectClassDefinition)def;
ocDefs.add(rOcDef);
}
}
return ocDefs;
}
public Collection<? extends RefinedObjectClassDefinition> getRefinedDefinitions(ShadowKindType kind) {
Collection<RefinedObjectClassDefinition> ocDefs = new ArrayList<RefinedObjectClassDefinition>();
for (Definition def: definitions) {
if ((def instanceof RefinedObjectClassDefinition)
&& MiscSchemaUtil.matchesKind(kind, ((RefinedObjectClassDefinition) def).getKind())) {
RefinedObjectClassDefinition rOcDef = (RefinedObjectClassDefinition)def;
ocDefs.add(rOcDef);
}
}
return ocDefs;
}
public ResourceSchema getOriginalResourceSchema() {
return originalResourceSchema;
}
public RefinedObjectClassDefinition getRefinedDefinition(ShadowKindType kind, ShadowType shadow) {
return getRefinedDefinition(kind, ShadowUtil.getIntent(shadow));
}
/**
* if null accountType is provided, default account definition is returned.
*/
public RefinedObjectClassDefinition getRefinedDefinition(ShadowKindType kind, String intent) {
for (RefinedObjectClassDefinition acctDef: getRefinedDefinitions(kind)) {
if (intent == null && acctDef.isDefault()) {
return acctDef;
}
if (acctDef.getIntent() != null && acctDef.getIntent().equals(intent)) {
return acctDef;
}
if (acctDef.getIntent() == null && intent == null) {
return acctDef;
}
}
return null;
}
public CompositeRefinedObjectClassDefinition determineCompositeObjectClassDefinition(ResourceShadowDiscriminator discriminator) {
if (discriminator.getKind() == null && discriminator.getObjectClass() == null) {
return null;
}
RefinedObjectClassDefinition structuralObjectClassDefinition;
if (discriminator.getKind() == null && discriminator.getObjectClass() != null) {
structuralObjectClassDefinition = getRefinedDefinition(discriminator.getObjectClass());
} else {
structuralObjectClassDefinition = getRefinedDefinition(discriminator.getKind(), discriminator.getIntent());
}
if (structuralObjectClassDefinition == null) {
return null;
}
Collection<RefinedObjectClassDefinition> auxiliaryObjectClassDefinitions = structuralObjectClassDefinition.getAuxiliaryObjectClassDefinitions();
return new CompositeRefinedObjectClassDefinition(structuralObjectClassDefinition, auxiliaryObjectClassDefinitions);
}
public CompositeRefinedObjectClassDefinition determineCompositeObjectClassDefinition(PrismObject<ShadowType> shadow) throws SchemaException {
return determineCompositeObjectClassDefinition(shadow, null);
}
public CompositeRefinedObjectClassDefinition determineCompositeObjectClassDefinition(PrismObject<ShadowType> shadow,
Collection<QName> additionalAuxiliaryObjectClassQNames) throws SchemaException {
ShadowType shadowType = shadow.asObjectable();
RefinedObjectClassDefinition structuralObjectClassDefinition = null;
ShadowKindType kind = shadowType.getKind();
String intent = shadowType.getIntent();
QName structuralObjectClassQName = shadowType.getObjectClass();
if (kind != null) {
structuralObjectClassDefinition = getRefinedDefinition(kind, intent);
}
if (structuralObjectClassDefinition == null) {
// Fallback to objectclass only
if (structuralObjectClassQName == null) {
return null;
}
structuralObjectClassDefinition = getRefinedDefinition(structuralObjectClassQName);
}
if (structuralObjectClassDefinition == null) {
return null;
}
List<QName> auxiliaryObjectClassQNames = shadowType.getAuxiliaryObjectClass();
if (additionalAuxiliaryObjectClassQNames != null) {
auxiliaryObjectClassQNames.addAll(additionalAuxiliaryObjectClassQNames);
}
Collection<RefinedObjectClassDefinition> auxiliaryObjectClassDefinitions = new ArrayList<>(auxiliaryObjectClassQNames.size());
for (QName auxiliaryObjectClassQName: auxiliaryObjectClassQNames) {
RefinedObjectClassDefinition auxiliaryObjectClassDef = getRefinedDefinition(auxiliaryObjectClassQName);
if (auxiliaryObjectClassDef == null) {
throw new SchemaException("Auxiliary object class "+auxiliaryObjectClassQName+" specified in "+shadow+" does not exist");
}
auxiliaryObjectClassDefinitions.add(auxiliaryObjectClassDef);
}
return new CompositeRefinedObjectClassDefinition(structuralObjectClassDefinition, auxiliaryObjectClassDefinitions);
}
public CompositeRefinedObjectClassDefinition determineCompositeObjectClassDefinition(QName structuralObjectClassQName, ShadowKindType kind, String intent) {
RefinedObjectClassDefinition structuralObjectClassDefinition = null;
Collection<RefinedObjectClassDefinition> auxiliaryObjectClassDefinitions;
if (kind != null) {
structuralObjectClassDefinition = getRefinedDefinition(kind, intent);
}
if (structuralObjectClassDefinition == null) {
// Fallback to objectclass only
if (structuralObjectClassQName == null) {
throw new IllegalArgumentException("No kind nor objectclass defined");
}
structuralObjectClassDefinition = getRefinedDefinition(structuralObjectClassQName);
}
if (structuralObjectClassDefinition == null) {
return null;
}
auxiliaryObjectClassDefinitions = structuralObjectClassDefinition.getAuxiliaryObjectClassDefinitions();
return new CompositeRefinedObjectClassDefinition(structuralObjectClassDefinition, auxiliaryObjectClassDefinitions);
}
/**
* If no intents are provided, default account definition is returned.
* We check whether there is only one relevant rOCD.
*/
public RefinedObjectClassDefinition getRefinedDefinition(ShadowKindType kind, Collection<String> intents) throws SchemaException {
RefinedObjectClassDefinition found = null;
for (RefinedObjectClassDefinition acctDef: getRefinedDefinitions(kind)) {
RefinedObjectClassDefinition foundCurrent = null;
if (intents == null || intents.isEmpty()) {
if (acctDef.isDefault()) {
foundCurrent = acctDef;
}
} else {
if (intents.contains(acctDef.getIntent())) {
foundCurrent = acctDef;
}
}
if (foundCurrent != null) {
if (found != null) {
if (!QNameUtil.match(found.getTypeName(), foundCurrent.getTypeName())) {
throw new SchemaException("More than one ObjectClass found for kind " + kind + ", intents: " + intents + ": " + found.getTypeName() + ", " + foundCurrent.getTypeName());
}
} else {
found = foundCurrent;
}
}
}
return found;
}
public RefinedObjectClassDefinition getRefinedDefinition(QName objectClassName) {
for (Definition def: definitions) {
if ((def instanceof RefinedObjectClassDefinition)
&& ((RefinedObjectClassDefinition)def).isDefault()
&& (QNameUtil.match(def.getTypeName(), objectClassName))) {
//&& (def.getTypeName().equals(objectClassName))) {
return (RefinedObjectClassDefinition)def;
}
}
// No default for this object class, so just use the first one.
// This is not strictly correct .. but it is a "compatible bug" :-)
// TODO: remove this in next major revision
for (Definition def: definitions) {
if ((def instanceof RefinedObjectClassDefinition)
&& (QNameUtil.match(def.getTypeName(), objectClassName))) {
//&& (def.getTypeName().equals(objectClassName))) {
return (RefinedObjectClassDefinition)def;
}
}
return null;
}
public RefinedObjectClassDefinition getDefaultRefinedDefinition(ShadowKindType kind) {
return getRefinedDefinition(kind, (String)null);
}
public PrismObjectDefinition<ShadowType> getObjectDefinition(ShadowKindType kind, String intent) {
return getRefinedDefinition(kind, intent).getObjectDefinition();
}
public PrismObjectDefinition<ShadowType> getObjectDefinition(ShadowKindType kind, ShadowType shadow) {
return getObjectDefinition(kind, ShadowUtil.getIntent(shadow));
}
private void add(RefinedObjectClassDefinition rOcDef) {
definitions.add(rOcDef);
}
public RefinedObjectClassDefinition findRefinedDefinitionByObjectClassQName(ShadowKindType kind, QName objectClass) {
if (objectClass == null) {
return getDefaultRefinedDefinition(kind);
}
for (RefinedObjectClassDefinition acctDef: getRefinedDefinitions(kind)) {
if (acctDef.isDefault() && QNameUtil.match(acctDef.getObjectClassDefinition().getTypeName(), objectClass)) {
return acctDef;
}
}
// No default for this object class, so just use the first one.
// This is not strictly correct .. but it is a "compatible bug" :-)
// TODO: remove this in next major revision
for (RefinedObjectClassDefinition acctDef: getRefinedDefinitions(kind)) {
if (QNameUtil.match(acctDef.getObjectClassDefinition().getTypeName(), objectClass)) {
return acctDef;
}
}
return null;
}
public ObjectClassComplexTypeDefinition findObjectClassDefinition(QName objectClassQName) {
return originalResourceSchema.findObjectClassDefinition(objectClassQName);
}
public static RefinedResourceSchema getRefinedSchema(ResourceType resourceType) throws SchemaException {
return getRefinedSchema(resourceType, resourceType.asPrismObject().getPrismContext());
}
public static LayerRefinedResourceSchema getRefinedSchema(ResourceType resourceType, LayerType layer) throws SchemaException {
return getRefinedSchema(resourceType, layer, resourceType.asPrismObject().getPrismContext());
}
public static RefinedResourceSchema getRefinedSchema(ResourceType resourceType, PrismContext prismContext) throws SchemaException {
PrismObject<ResourceType> resource = resourceType.asPrismObject();
return getRefinedSchema(resource, prismContext);
}
public static LayerRefinedResourceSchema getRefinedSchema(ResourceType resourceType, LayerType layer, PrismContext prismContext) throws SchemaException {
PrismObject<ResourceType> resource = resourceType.asPrismObject();
return getRefinedSchema(resource, layer, prismContext);
}
public static RefinedResourceSchema getRefinedSchema(PrismObject<ResourceType> resource) throws SchemaException {
return getRefinedSchema(resource, resource.getPrismContext());
}
public static RefinedResourceSchema getRefinedSchema(PrismObject<ResourceType> resource, PrismContext prismContext) throws SchemaException {
if (resource == null){
throw new SchemaException("Could not get refined schema, resource does not exist.");
}
Object userDataEntry = resource.getUserData(USER_DATA_KEY_REFINED_SCHEMA);
if (userDataEntry != null) {
if (userDataEntry instanceof RefinedResourceSchema) {
return (RefinedResourceSchema)userDataEntry;
} else {
throw new IllegalStateException("Expected RefinedResourceSchema under user data key "+USER_DATA_KEY_REFINED_SCHEMA+
"in "+resource+", but got "+userDataEntry.getClass());
}
} else {
RefinedResourceSchema refinedSchema = parse(resource, prismContext);
resource.setUserData(USER_DATA_KEY_REFINED_SCHEMA, refinedSchema);
return refinedSchema;
}
}
public static LayerRefinedResourceSchema getRefinedSchema(PrismObject<ResourceType> resource, LayerType layer, PrismContext prismContext) throws SchemaException {
RefinedResourceSchema refinedSchema = getRefinedSchema(resource, prismContext);
if (refinedSchema == null) {
return null;
}
return refinedSchema.forLayer(layer);
}
public static boolean hasRefinedSchema(ResourceType resourceType) {
PrismObject<ResourceType> resource = resourceType.asPrismObject();
return resource.getUserData(USER_DATA_KEY_REFINED_SCHEMA) != null;
}
public static ResourceSchema getResourceSchema(ResourceType resourceType, PrismContext prismContext) throws SchemaException {
PrismObject<ResourceType> resource = resourceType.asPrismObject();
return getResourceSchema(resource, prismContext);
}
public static ResourceSchema getResourceSchema(PrismObject<ResourceType> resource, PrismContext prismContext) throws SchemaException {
Element resourceXsdSchema = ResourceTypeUtil.getResourceXsdSchema(resource);
if (resourceXsdSchema == null) {
return null;
}
Object userDataEntry = resource.getUserData(USER_DATA_KEY_PARSED_RESOURCE_SCHEMA);
if (userDataEntry != null) {
if (userDataEntry instanceof ResourceSchema) {
return (ResourceSchema)userDataEntry;
} else {
throw new IllegalStateException("Expected ResourceSchema under user data key "+
USER_DATA_KEY_PARSED_RESOURCE_SCHEMA+ "in "+resource+", but got "+userDataEntry.getClass());
}
} else {
InternalMonitor.recordResourceSchemaParse();
ResourceSchema parsedSchema = ResourceSchema.parse(resourceXsdSchema, "resource schema of "+resource, prismContext);
if (parsedSchema == null) {
throw new IllegalStateException("Parsed schema is null: most likely an internall error");
}
resource.setUserData(USER_DATA_KEY_PARSED_RESOURCE_SCHEMA, parsedSchema);
return parsedSchema;
}
}
public static void setParsedResourceSchemaConditional(ResourceType resourceType, ResourceSchema parsedSchema) {
if (hasParsedSchema(resourceType)) {
return;
}
PrismObject<ResourceType> resource = resourceType.asPrismObject();
resource.setUserData(USER_DATA_KEY_PARSED_RESOURCE_SCHEMA, parsedSchema);
}
public static boolean hasParsedSchema(ResourceType resourceType) {
PrismObject<ResourceType> resource = resourceType.asPrismObject();
return resource.getUserData(USER_DATA_KEY_PARSED_RESOURCE_SCHEMA) != null;
}
public static RefinedResourceSchema parse(PrismObject<ResourceType> resource, PrismContext prismContext) throws SchemaException {
return parse(resource.asObjectable(), prismContext);
}
public static RefinedResourceSchema parse(ResourceType resourceType, PrismContext prismContext) throws SchemaException {
ResourceSchema originalResourceSchema = getResourceSchema(resourceType, prismContext);
if (originalResourceSchema == null) {
return null;
}
String contextDescription = "definition of "+resourceType;
RefinedResourceSchema rSchema = new RefinedResourceSchema(resourceType, originalResourceSchema, prismContext);
SchemaHandlingType schemaHandling = resourceType.getSchemaHandling();
if (schemaHandling != null) {
parseObjectTypeDefsFromSchemaHandling(rSchema, resourceType, schemaHandling,
schemaHandling.getObjectType(), null, prismContext, contextDescription);
}
parseObjectTypesFromSchema(rSchema, resourceType, prismContext, contextDescription);
// We need to parse associations and auxiliary object classes in a second pass. We need to have all object classes parsed before correctly setting association
// targets
for (RefinedObjectClassDefinition rOcDef: rSchema.getRefinedDefinitions()) {
rOcDef.parseAssociations(rSchema);
rOcDef.parseAuxiliaryObjectClasses(rSchema);
}
// We can parse attributes only after we have all the object class info parsed (including auxiliary object classes)
for (RefinedObjectClassDefinition rOcDef: rSchema.getRefinedDefinitions()) {
rOcDef.parseAttributes(rSchema, contextDescription);
}
return rSchema;
}
private static boolean hasAnyObjectTypeDef(SchemaHandlingType schemaHandling) {
if (schemaHandling == null) {
return false;
}
if (!schemaHandling.getObjectType().isEmpty()) {
return true;
}
return false;
}
private static void parseObjectTypeDefsFromSchemaHandling(RefinedResourceSchema rSchema, ResourceType resourceType,
SchemaHandlingType schemaHandling, Collection<ResourceObjectTypeDefinitionType> resourceObjectTypeDefs,
ShadowKindType impliedKind, PrismContext prismContext, String contextDescription) throws SchemaException {
if (resourceObjectTypeDefs == null) {
return;
}
Map<ShadowKindType, RefinedObjectClassDefinition> defaults = new HashMap<ShadowKindType, RefinedObjectClassDefinition>();
for (ResourceObjectTypeDefinitionType accountTypeDefType: resourceObjectTypeDefs) {
RefinedObjectClassDefinition rOcDef = RefinedObjectClassDefinition.parse(accountTypeDefType, resourceType, rSchema, impliedKind,
prismContext, contextDescription);
if (rOcDef.isDefault()) {
if (defaults.containsKey(rOcDef.getKind())) {
throw new SchemaException("More than one default "+rOcDef.getKind()+" definitions ("+defaults.get(rOcDef.getKind())+", "+rOcDef+") in " + contextDescription);
} else {
defaults.put(rOcDef.getKind(), rOcDef);
}
}
rSchema.add(rOcDef);
}
}
private static void parseObjectTypesFromSchema(RefinedResourceSchema rSchema, ResourceType resourceType,
PrismContext prismContext, String contextDescription) throws SchemaException {
RefinedObjectClassDefinition rAccountDefDefault = null;
for(ObjectClassComplexTypeDefinition objectClassDef: rSchema.getOriginalResourceSchema().getObjectClassDefinitions()) {
QName objectClassname = objectClassDef.getTypeName();
if (rSchema.getRefinedDefinition(objectClassname) != null) {
continue;
}
RefinedObjectClassDefinition rOcDef = RefinedObjectClassDefinition.parseFromSchema(objectClassDef, resourceType, rSchema, prismContext,
"object class " + objectClassname + ", in " + contextDescription);
if (objectClassDef.getKind() == ShadowKindType.ACCOUNT && rOcDef.isDefault()) {
if (rAccountDefDefault == null) {
rAccountDefDefault = rOcDef;
} else {
throw new SchemaException("More than one default account definitions ("+rAccountDefDefault+", "+rOcDef+") in " + contextDescription);
}
}
rSchema.add(rOcDef);
}
}
public LayerRefinedResourceSchema forLayer(LayerType layer) {
return LayerRefinedResourceSchema.wrap(this, layer);
}
@Override
public String toString() {
return "rSchema(ns=" + namespace + ")";
}
}