/
StateReporter.java
170 lines (149 loc) · 7 KB
/
StateReporter.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
/*
* Copyright (c) 2010-2015 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;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.statistics.ProvisioningOperation;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import org.identityconnectors.framework.common.objects.Uid;
import javax.xml.namespace.QName;
import java.util.Date;
/**
* TODO better name (ProgressReporter ? StatisticsReporter ? ...)
*
* Used to report state, progress and performance statistics to upper layers.
* Generally a Task is the place where such information are reported and collected.
*
* However, because of a complex nature of some operations (namely, search) it tries
* to remember the state of an operation.
*
* TODO maybe this could be simplified in the future.
*
* @author Pavol Mederly
*/
public class StateReporter {
private static final Trace LOGGER = TraceManager.getTrace(StateReporter.class);
private Task task;
private String resourceOid;
private String resourceName; // lazily set when available
public StateReporter() {
}
public StateReporter(String resourceOid, Task task) {
this.resourceOid = resourceOid;
this.task = task;
}
private String getResourceName() {
if (resourceName != null) {
return resourceName;
} else {
return resourceOid;
}
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
// Operational information
private ProvisioningOperation lastOperation = null;
private ObjectClassComplexTypeDefinition lastObjectClass = null;
private Date lastStarted = null;
public void recordIcfOperationStart(ProvisioningOperation operation, ObjectClassComplexTypeDefinition objectClassDef, Uid uid) {
if (lastOperation != null) {
LOGGER.warn("Unfinished operation: {}, resource: {}, OC: {}, started: {}", lastOperation, getResourceName(), lastObjectClass, lastStarted);
}
lastOperation = operation;
lastObjectClass = objectClassDef;
lastStarted = new Date();
String object = "";
if (uid != null) {
object = " " + uid.getUidValue();
}
recordState("Starting " + operation + " of " + getObjectClassName(objectClassDef) + object + " on " + getResourceName());
}
// we just add duration, not count (we'll do this on end)
public void recordIcfOperationSuspend(ProvisioningOperation operation, ObjectClassComplexTypeDefinition objectClassDef) {
if (lastOperation != operation) {
LOGGER.warn("Suspending operation other than current: finishing {}, last recorded {}",
operation, lastOperation);
} else if (lastObjectClass == null || !lastObjectClass.getTypeName().equals(objectClassDef.getTypeName())) {
LOGGER.warn("Suspending operation on object class other than current: finishing on {}, last recorded {}",
objectClassDef.getTypeName(), lastObjectClass != null ? lastObjectClass.getTypeName() : "(null)");
} else {
long duration = System.currentTimeMillis() - lastStarted.getTime();
if (task != null) {
task.recordProvisioningOperation(resourceOid, getResourceName(), objectClassDef.getTypeName(), lastOperation, true, 0, duration);
}
}
lastOperation = null;
recordState("Returned from " + operation + " of " + objectClassDef.getTypeName().getLocalPart() + " on " + getResourceName());
}
public void recordIcfOperationResume(ProvisioningOperation operation, ObjectClassComplexTypeDefinition objectClassDef) {
if (lastOperation != null) {
LOGGER.warn("Unfinished operation: {}, resource: {}, OC: {}, started: {}", lastOperation, getResourceName(), lastObjectClass, lastStarted);
}
lastOperation = operation;
lastObjectClass = objectClassDef;
lastStarted = new Date();
recordState("Continuing " + operation + " of " + objectClassDef.getTypeName().getLocalPart() + " on " + getResourceName());
}
private String getObjectClassName(ObjectClassComplexTypeDefinition objectClassDef) {
return objectClassDef != null && objectClassDef.getTypeName() != null ? objectClassDef.getTypeName().getLocalPart() : "(null)";
}
private QName getObjectClassQName(ObjectClassComplexTypeDefinition objectClassDef) {
return objectClassDef != null ? objectClassDef.getTypeName() : null;
}
public void recordIcfOperationEnd(ProvisioningOperation operation, ObjectClassComplexTypeDefinition objectClassDef, Throwable ex, Uid uid) {
long duration = -1L;
if (lastOperation != operation) {
LOGGER.warn("Finishing operation other than current: finishing {}, last recorded {}",
operation, lastOperation);
} else if (objectClassDef != null && (lastObjectClass == null || !lastObjectClass.getTypeName().equals(objectClassDef.getTypeName()))) {
LOGGER.warn("Finishing operation on object class other than current: finishing on {}, last recorded {}",
getObjectClassName(objectClassDef), getObjectClassName(lastObjectClass));
} else {
duration = System.currentTimeMillis() - lastStarted.getTime();
}
String finished;
if (ex == null) {
finished = "Successfully finished";
} else {
finished = "Finished (unsuccessfully)";
}
String durationString;
if (duration >= 0) {
durationString = " in " + duration + " ms";
} else {
durationString = "";
}
String object = "";
if (uid != null) {
object = " " + uid.getUidValue();
}
recordState(finished + " " + operation + " of " + getObjectClassName(objectClassDef) + object + " on " + getResourceName() + durationString);
if (task != null && duration >= 0) {
task.recordProvisioningOperation(resourceOid, getResourceName(), getObjectClassQName(objectClassDef), lastOperation, ex == null, 1, duration);
}
lastOperation = null;
}
private void recordState(String message) {
if (task != null) {
task.recordState(message);
}
}
public void setTask(Task task) {
this.task = task;
}
}