-
Notifications
You must be signed in to change notification settings - Fork 188
/
ObjectNotFoundHandler.java
197 lines (170 loc) · 10.3 KB
/
ObjectNotFoundHandler.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
/*
* Copyright (c) 2010-2018 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.provisioning.impl.errorhandling;
import java.util.Collection;
import java.util.List;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.common.Clock;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.provisioning.api.GenericConnectorException;
import com.evolveum.midpoint.provisioning.api.ProvisioningOperationOptions;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.provisioning.api.ResourceObjectShadowChangeDescription;
import com.evolveum.midpoint.provisioning.api.ResourceOperationDescription;
import com.evolveum.midpoint.provisioning.impl.ConstraintsChecker;
import com.evolveum.midpoint.provisioning.impl.ProvisioningContext;
import com.evolveum.midpoint.provisioning.impl.ProvisioningOperationState;
import com.evolveum.midpoint.provisioning.impl.ShadowCaretaker;
import com.evolveum.midpoint.provisioning.impl.ShadowManager;
import com.evolveum.midpoint.provisioning.impl.ShadowState;
import com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException;
import com.evolveum.midpoint.provisioning.util.ProvisioningUtil;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.AsynchronousOperationResult;
import com.evolveum.midpoint.schema.result.AsynchronousOperationReturnValue;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.PendingOperationExecutionStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
@Component
public class ObjectNotFoundHandler extends HardErrorHandler {
private static final String OP_DISCOVERY = ObjectNotFoundHandler.class + ".discovery";
private static final Trace LOGGER = TraceManager.getTrace(ObjectNotFoundHandler.class);
@Autowired private ShadowManager shadowManager;
@Autowired private ShadowCaretaker shadowCaretaker;
@Autowired private Clock clock;
@Override
public PrismObject<ShadowType> handleGetError(ProvisioningContext ctx,
PrismObject<ShadowType> repositoryShadow, GetOperationOptions rootOptions, Exception cause,
Task task, OperationResult parentResult) throws SchemaException, GenericFrameworkException,
CommunicationException, ObjectNotFoundException, ObjectAlreadyExistsException,
ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
if (ProvisioningUtil.isDoDiscovery(ctx.getResource(), rootOptions)) {
discoverDeletedShadow(ctx, repositoryShadow, cause, task, parentResult);
}
if (repositoryShadow != null) {
if (ShadowUtil.isExists(repositoryShadow.asObjectable())) {
// This is some kind of reality mismatch. We obviously have shadow that is supposed
// to be alive (exists=true). But it does not exist on resource.
// This is NOT gestation quantum state, as that is handled directly in ShadowCache.
// This may be "lost shadow" - shadow which exists but the resource object has disappeared without trace.
// Or this may be a corpse - quantum state that has just collapsed to to tombstone.
// Either way, it should be safe to set exists=false.
LOGGER.trace("Setting {} as tombstone. This may be a quntum state collapse. Or maybe a lost shadow.", repositoryShadow);
repositoryShadow = shadowManager.markShadowDead(repositoryShadow, parentResult);
} else {
// We always want to return repository shadow it such shadow is available.
// The shadow may be dead, or it may be marked as "not exists", but we want
// to return something if shadow exists in the repo. Otherwise model may
// unlink the shadow or otherwise "forget" about it.
LOGGER.debug("Shadow {} not found on the resource. However still have it in the repository. Therefore returning repository version.", repositoryShadow);
}
parentResult.setStatus(OperationResultStatus.HANDLED_ERROR);
return repositoryShadow;
}
return super.handleGetError(ctx, repositoryShadow, rootOptions, cause, task, parentResult);
}
@Override
public OperationResultStatus handleModifyError(ProvisioningContext ctx, PrismObject<ShadowType> repoShadow,
Collection<? extends ItemDelta> modifications, ProvisioningOperationOptions options,
ProvisioningOperationState<AsynchronousOperationReturnValue<Collection<PropertyDelta<PrismPropertyValue>>>> opState,
Exception cause, OperationResult failedOperationResult, Task task, OperationResult parentResult)
throws SchemaException, GenericFrameworkException, CommunicationException,
ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException,
SecurityViolationException, ExpressionEvaluationException {
if (ProvisioningUtil.isDoDiscovery(ctx.getResource(), options)) {
discoverDeletedShadow(ctx, repoShadow, cause, task, parentResult);
}
return super.handleModifyError(ctx, repoShadow, modifications, options, opState, cause, failedOperationResult, task, parentResult);
}
@Override
public OperationResultStatus handleDeleteError(ProvisioningContext ctx,
PrismObject<ShadowType> repoShadow, ProvisioningOperationOptions options,
ProvisioningOperationState<AsynchronousOperationResult> opState, Exception cause,
OperationResult failedOperationResult, Task task, OperationResult parentResult)
throws SchemaException, GenericFrameworkException, CommunicationException,
ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException,
SecurityViolationException, ExpressionEvaluationException {
if (ProvisioningUtil.isDoDiscovery(ctx.getResource(), options)) {
discoverDeletedShadow(ctx, repoShadow, cause, task, parentResult);
}
// Error deleting shadow because the shadow is already deleted. This means someone has done our job already.
failedOperationResult.setStatus(OperationResultStatus.HANDLED_ERROR);
opState.setExecutionStatus(PendingOperationExecutionStatusType.COMPLETED);
return OperationResultStatus.HANDLED_ERROR;
}
private void discoverDeletedShadow(ProvisioningContext ctx, PrismObject<ShadowType> repositoryShadow,
Exception cause, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
ShadowState shadowState = shadowCaretaker.determineShadowState(ctx, repositoryShadow, clock.currentTimeXMLGregorianCalendar());
if (shadowState != ShadowState.LIFE) {
// Do NOT do discovery of shadow that can legally not exist. This is no discovery.
// We already know that the object are supposed not to exist yet or to dead already.
LOGGER.trace("Skipping discovery of shadow {} becasue it is {}, we expect that it might not exist", repositoryShadow, shadowState);
return;
}
// TODO: this probably should NOT be a subresult of parentResult. We probably want new result (and maybe also task) here.
OperationResult result = parentResult.createSubresult(OP_DISCOVERY);
LOGGER.debug("DISCOVERY: discovered deleted shadow {}", repositoryShadow);
repositoryShadow = shadowManager.markShadowDead(repositoryShadow, result);
ResourceObjectShadowChangeDescription change = new ResourceObjectShadowChangeDescription();
change.setResource(ctx.getResource().asPrismObject());
change.setSourceChannel(QNameUtil.qNameToUri(SchemaConstants.CHANGE_CHANNEL_DISCOVERY));
change.setObjectDelta(repositoryShadow.createDeleteDelta());
change.setOldShadow(repositoryShadow);
change.setCurrentShadow(null);
// TODO: task initialization
// Task task = taskManager.createTaskInstance();
changeNotificationDispatcher.notifyChange(change, task, result);
result.computeStatus();
}
@Override
protected void throwException(Exception cause, ProvisioningOperationState<? extends AsynchronousOperationResult> opState, OperationResult result)
throws ObjectNotFoundException {
recordCompletionError(cause, opState, result);
if (cause instanceof ObjectNotFoundException) {
throw (ObjectNotFoundException)cause;
} else {
throw new ObjectNotFoundException(cause.getMessage(), cause);
}
}
}