Skip to content

Package: EndPointAdapter

EndPointAdapter

nameinstructionbranchcomplexitylinemethod
EndPointAdapter()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createDocument()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
marshal(List)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
unmarshal(Element)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.broker.core.router;
14:
15: import org.eclipse.kapua.KapuaErrorCodes;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.KapuaRuntimeException;
18: import org.eclipse.kapua.broker.core.BrokerJAXBContextProvider;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21: import org.w3c.dom.Document;
22: import org.w3c.dom.Element;
23: import org.w3c.dom.Node;
24: import org.w3c.dom.NodeList;
25: import org.w3c.dom.Text;
26:
27: import javax.xml.XMLConstants;
28: import javax.xml.bind.JAXBContext;
29: import javax.xml.bind.JAXBException;
30: import javax.xml.bind.Marshaller;
31: import javax.xml.bind.Unmarshaller;
32: import javax.xml.bind.annotation.adapters.XmlAdapter;
33: import javax.xml.parsers.DocumentBuilder;
34: import javax.xml.parsers.DocumentBuilderFactory;
35: import javax.xml.parsers.ParserConfigurationException;
36: import javax.xml.transform.dom.DOMSource;
37: import java.util.ArrayList;
38: import java.util.List;
39:
40: /**
41: * {@link CamelKapuaDefaultRouter} {@link EndPoint} adapter
42: *
43: * @since 1.0
44: */
45: public class EndPointAdapter extends XmlAdapter<Element, List<EndPoint>> {
46:
47: private static final Logger logger = LoggerFactory.getLogger(EndPointAdapter.class);
48:
49: @Override
50: public Element marshal(List<EndPoint> value) throws Exception {
51: Document document = createDocument();
52:• for (EndPoint endPoint : value) {
53: JaxbContextHandler.getMarshaller().marshal(endPoint, document);
54: }
55: DOMSource source = new DOMSource(document);
56: return (Element) source.getNode();
57: }
58:
59: @Override
60: public List<EndPoint> unmarshal(Element element) throws Exception {
61: List<EndPoint> endPointList = new ArrayList<>();
62: NodeList nodeList = element.getChildNodes();
63:• for (int i = 0; i < nodeList.getLength(); i++) {
64: Node node = nodeList.item(i);
65:• if (!(node instanceof Text)) {
66: logger.debug("Node name: {}", node.getNodeName());
67: endPointList.add((EndPoint) JaxbContextHandler.getUnmarshaller().unmarshal(node));
68: }
69: }
70: return endPointList;
71: }
72:
73: private Document createDocument() throws ParserConfigurationException {
74: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
75: dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // Sonar java:S2755
76: dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); // Sonar java:S2755
77: DocumentBuilder db = dbf.newDocumentBuilder();
78: return db.newDocument();
79: }
80:
81: }
82:
83: class JaxbContextHandler {
84:
85: private static Marshaller marshaller;
86: private static Unmarshaller unmarshaller;
87:
88: static {
89: JAXBContext jaxbContext;
90: try {
91: jaxbContext = new BrokerJAXBContextProvider().getJAXBContext();
92: marshaller = jaxbContext.createMarshaller();
93: unmarshaller = jaxbContext.createUnmarshaller();
94: } catch (KapuaException | JAXBException e) {
95: throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, e, "Cannot initialize JaxbContext!");
96: }
97: }
98:
99: private JaxbContextHandler() {
100: }
101:
102: public static Marshaller getMarshaller() {
103: return marshaller;
104: }
105:
106: public static Unmarshaller getUnmarshaller() {
107: return unmarshaller;
108: }
109:
110: }