-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
JaxbContextProducer.java
93 lines (82 loc) · 3.42 KB
/
JaxbContextProducer.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
package io.quarkus.jaxb.runtime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import io.quarkus.arc.DefaultBean;
@ApplicationScoped
public class JaxbContextProducer {
@DefaultBean
@Singleton
@Produces
public JAXBContext jaxbContext(Instance<JaxbContextCustomizer> customizers) {
try {
Map<String, Object> properties = new HashMap<>();
List<JaxbContextCustomizer> sortedCustomizers = sortCustomizersInDescendingPriorityOrder(customizers);
for (JaxbContextCustomizer customizer : sortedCustomizers) {
customizer.customizeContextProperties(properties);
}
String[] classNamesToBeBounded = JaxbContextConfigRecorder.getClassesToBeBound();
List<Class<?>> classes = new ArrayList<>();
for (int i = 0; i < classNamesToBeBounded.length; i++) {
Class<?> clazz = getClassByName(classNamesToBeBounded[i]);
if (!clazz.isPrimitive()) {
classes.add(clazz);
}
}
return JAXBContext.newInstance(classes.toArray(new Class[0]), properties);
} catch (JAXBException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@DefaultBean
@Singleton
@Produces
public Marshaller marshaller(JAXBContext jaxbContext, Instance<JaxbContextCustomizer> customizers) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
List<JaxbContextCustomizer> sortedCustomizers = sortCustomizersInDescendingPriorityOrder(customizers);
for (JaxbContextCustomizer customizer : sortedCustomizers) {
customizer.customizeMarshaller(marshaller);
}
return marshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@DefaultBean
@Singleton
@Produces
public Unmarshaller unmarshaller(JAXBContext jaxbContext, Instance<JaxbContextCustomizer> customizers) {
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
List<JaxbContextCustomizer> sortedCustomizers = sortCustomizersInDescendingPriorityOrder(customizers);
for (JaxbContextCustomizer customizer : sortedCustomizers) {
customizer.customizeUnmarshaller(unmarshaller);
}
return unmarshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
private List<JaxbContextCustomizer> sortCustomizersInDescendingPriorityOrder(Instance<JaxbContextCustomizer> customizers) {
List<JaxbContextCustomizer> sortedCustomizers = new ArrayList<>();
for (JaxbContextCustomizer customizer : customizers) {
sortedCustomizers.add(customizer);
}
Collections.sort(sortedCustomizers);
return sortedCustomizers;
}
private Class<?> getClassByName(String name) throws ClassNotFoundException {
return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
}
}