/
TraceParser.java
92 lines (77 loc) · 3.54 KB
/
TraceParser.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
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.schema.traces;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.evolveum.midpoint.util.annotation.Experimental;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TracingOutputType;
@Experimental
public class TraceParser {
private static final Trace LOGGER = TraceManager.getTrace(TraceParser.class);
@NotNull private final PrismContext prismContext;
public TraceParser(@NotNull PrismContext prismContext) {
this.prismContext = prismContext;
}
public TracingOutputType parse(File file) throws IOException, SchemaException {
return parse(file, false);
}
public TracingOutputType parse(File file, boolean raw) throws IOException, SchemaException {
boolean isZip = file.getName().toLowerCase().endsWith(".zip");
return parse(new FileInputStream(file), isZip, raw, file.getPath());
}
public TracingOutputType parse(InputStream inputStream, boolean isZip, String description) throws SchemaException, IOException {
return parse(inputStream, isZip, false, description);
}
public TracingOutputType parse(InputStream inputStream, boolean isZip, boolean raw, String description) throws SchemaException, IOException {
TracingOutputType wholeTracingOutput = getObject(inputStream, isZip, description);
if (!raw && wholeTracingOutput != null) {
new DictionaryExpander(wholeTracingOutput).expand();
new OperationCategorizer(wholeTracingOutput).categorize();
}
return wholeTracingOutput;
}
public TracingOutputType getObject(InputStream stream, boolean isZip, String description) throws IOException, SchemaException {
long start = System.currentTimeMillis();
Object object;
if (isZip) {
try (ZipInputStream zis = new ZipInputStream(stream)) {
ZipEntry zipEntry = zis.getNextEntry();
if (zipEntry != null) {
object = prismContext.parserFor(zis).xml().compat().parseRealValue();
} else {
LOGGER.error("No zip entry in input file '{}'", description);
object = null;
}
}
} else {
object = prismContext.parserFor(stream).xml().compat().parseRealValue();
}
stream.close();
long read = System.currentTimeMillis();
LOGGER.info("Read the content of {} in {} milliseconds", description, read - start);
if (object instanceof TracingOutputType) {
return (TracingOutputType) object;
} else if (object instanceof OperationResultType) {
TracingOutputType rv = new TracingOutputType(prismContext);
rv.setResult((OperationResultType) object);
return rv;
} else {
LOGGER.error("Wrong object type in input file '{}': {}", description, object);
return null;
}
}
}