Skip to content

Package: CamelKapuaDefaultRouter

CamelKapuaDefaultRouter

nameinstructionbranchcomplexitylinemethod
CamelKapuaDefaultRouter()
M: 75 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
defaultRoute(Exchange, Object, String, Map)
M: 53 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
logLoadedEndPoints(List)
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 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%

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.apache.camel.Exchange;
16: import org.apache.camel.Header;
17: import org.apache.camel.Properties;
18: import org.eclipse.kapua.KapuaErrorCodes;
19: import org.eclipse.kapua.KapuaRuntimeException;
20: import org.eclipse.kapua.broker.core.listener.CamelConstants;
21: import org.eclipse.kapua.broker.core.message.MessageConstants;
22: import org.eclipse.kapua.broker.core.setting.BrokerSetting;
23: import org.eclipse.kapua.broker.core.setting.BrokerSettingKey;
24: import org.eclipse.kapua.commons.setting.KapuaSettingException;
25: import org.eclipse.kapua.commons.util.KapuaFileUtils;
26: import org.eclipse.kapua.commons.util.xml.XmlUtil;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29: import org.xml.sax.SAXException;
30:
31: import javax.xml.bind.JAXBException;
32: import java.io.FileReader;
33: import java.io.IOException;
34: import java.net.URL;
35: import java.util.List;
36: import java.util.Map;
37:
38: /**
39: * Default {@link CamelKapuaDefaultRouter}
40: *
41: * @since 1.0.0
42: */
43: public class CamelKapuaDefaultRouter {
44:
45: private static final Logger LOG = LoggerFactory.getLogger(CamelKapuaDefaultRouter.class);
46:
47: private EndPointContainer endPointContainer;
48:
49: public CamelKapuaDefaultRouter() {
50: String configurationFileName = BrokerSetting.getInstance().getString(BrokerSettingKey.CAMEL_DEFAULT_ROUTE_CONFIGURATION_FILE_NAME);
51:
52: URL url;
53: try {
54: url = KapuaFileUtils.getAsURL(configurationFileName);
55: } catch (KapuaSettingException e) {
56: throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, e, "Cannot find configuration file!");
57: }
58:
59: LOG.info("Default Camel routing | Loading configuration from file {}...", url.getFile());
60:
61: try (FileReader configurationFileReader = new FileReader(url.getFile())) {
62: endPointContainer = XmlUtil.unmarshal(configurationFileReader, EndPointContainer.class);
63: LOG.info("Default Camel routing | Found {} parent endpoints in the route", endPointContainer.getEndPoints().size());
64: logLoadedEndPoints(endPointContainer.getEndPoints());
65: } catch (JAXBException | SAXException | IOException e) {
66: throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, e, "Cannot load configuration!");
67: }
68:
69: LOG.info("Default Camel routing | Loading configuration from file '{}'... DONE!", url.getFile());
70: }
71:
72: public String defaultRoute(Exchange exchange, Object value, @Header(Exchange.SLIP_ENDPOINT) String previous, @Properties Map<String, Object> properties) {
73: LOG.trace("Received message on topic {} - Previous slip endpoint {} - id {}",
74: exchange.getIn().getHeader(MessageConstants.PROPERTY_ORIGINAL_TOPIC, String.class),
75: previous,
76: exchange.getIn().getHeader(CamelConstants.JMS_CORRELATION_ID));
77:
78:• for (EndPoint endPoint : endPointContainer.getEndPoints()) {
79:• if (endPoint.matches(exchange, value, previous, properties)) {
80: return endPoint.getEndPoint(exchange, value, previous, properties);
81: }
82: }
83:
84: return null;
85: }
86:
87: private void logLoadedEndPoints(List<EndPoint> endPoints) {
88: StringBuffer buffer = new StringBuffer();
89: buffer.append("\n");
90:• for (EndPoint endPoint : endPoints) {
91: endPoint.toLog(buffer, "");
92: }
93: LOG.info(buffer.toString());
94: }
95:
96: }