Skip to content

Commit

Permalink
Added try-with-resources
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Feb 9, 2021
1 parent 78f836f commit 58b2aa8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import java.util.Map;

/**
* Default message router
* Default {@link CamelKapuaDefaultRouter}
*
* @since 1.0
* @since 1.0.0
*/
public class CamelKapuaDefaultRouter {

Expand All @@ -49,44 +49,39 @@ public class CamelKapuaDefaultRouter {

public CamelKapuaDefaultRouter() {
String configurationFileName = BrokerSetting.getInstance().getString(BrokerSettingKey.CAMEL_DEFAULT_ROUTE_CONFIGURATION_FILE_NAME);
URL url = null;

URL url;
try {
url = KapuaFileUtils.getAsURL(configurationFileName);
} catch (KapuaSettingException e) {
throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, e, "Cannot find configuration file!");
}
LOG.info("Default Camel routing... Loading configuration from file {}", url.getFile());
FileReader configurationFileReader = null;
try {
configurationFileReader = new FileReader(url.getFile());

LOG.info("Default Camel routing | Loading configuration from file {}...", url.getFile());

try (FileReader configurationFileReader = new FileReader(url.getFile())) {
endPointContainer = XmlUtil.unmarshal(configurationFileReader, EndPointContainer.class);
LOG.info("Default Camel routing... Loading configuration from file {} Found {} parent endpoints in the route", configurationFileName,
(endPointContainer.getEndPoints() != null ? endPointContainer.getEndPoints().size() : 0));
LOG.info("Default Camel routing | Found {} parent endpoints in the route", endPointContainer.getEndPoints().size());
logLoadedEndPoints(endPointContainer.getEndPoints());
} catch (XMLStreamException | JAXBException | SAXException | IOException e) {
throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, e, "Cannot load configuration!");
} finally {
if (configurationFileReader != null) {
try {
configurationFileReader.close();
} catch (IOException e) {
LOG.warn("Cannot close configuration file '{}'!", configurationFileName, e);
}
}
}
LOG.info("Default Camel routing... Loading configuration '{}' from file '{}' DONE", configurationFileName, url.getFile());

LOG.info("Default Camel routing | Loading configuration from file '{}'... DONE!", url.getFile());
}

public String defaultRoute(Exchange exchange, Object value, @Header(Exchange.SLIP_ENDPOINT) String previous, @Properties Map<String, Object> properties) {
LOG.trace("Received message on topic {} - Previous slip endpoint {} - id {}",
exchange.getIn().getHeader(MessageConstants.PROPERTY_ORIGINAL_TOPIC, String.class),
previous,
exchange.getIn().getHeader(CamelConstants.JMS_CORRELATION_ID));

for (EndPoint endPoint : endPointContainer.getEndPoints()) {
if (endPoint.matches(exchange, value, previous, properties)) {
return endPoint.getEndPoint(exchange, value, previous, properties);
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
*******************************************************************************/
package org.eclipse.kapua.broker.core.router;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "endPoints")
@XmlAccessorType(XmlAccessType.PROPERTY)
Expand All @@ -31,11 +30,14 @@ public class EndPointContainer {
private List<EndPoint> endPoints;

public EndPointContainer() {
endPoints = new ArrayList<>();
}

@XmlAnyElement
public List<EndPoint> getEndPoints() {
if (endPoints == null) {
endPoints = new ArrayList<>();
}

return endPoints;
}

Expand Down

0 comments on commit 58b2aa8

Please sign in to comment.