Skip to content

Enhance error handling for invalid config files #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package de.csdev.ebus.cfg;

import java.text.MessageFormat;

/**
* @author Christian Sowada - Initial contribution
*
Expand All @@ -18,20 +16,19 @@ public class EBusConfigurationReaderException extends Exception {

private static final long serialVersionUID = 1L;

public EBusConfigurationReaderException(final String message, final Throwable cause, final Object... args) {
super(String.format(message, args), cause);
public EBusConfigurationReaderException(final String message) {
super(message);
}

public EBusConfigurationReaderException(final String message, final Object... args) {
super(MessageFormat.format(message, args));
}

public EBusConfigurationReaderException(final String message) {
super(message);
super(String.format(message, args));
}

public EBusConfigurationReaderException(final String message, final Throwable cause) {
super(message, cause);
}

public EBusConfigurationReaderException(final String message, final Throwable cause, final Object... args) {
super(String.format(message, args), cause);
}
}
72 changes: 37 additions & 35 deletions src/main/java/de/csdev/ebus/cfg/IEBusConfigurationReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,41 @@
@NonNullByDefault
public interface IEBusConfigurationReader {

/**
* Loads all build-in command collections implemented by the reader
*
* @return
*/
public List<IEBusCommandCollection> loadBuildInConfigurationCollections();

/**
* Loads the configuration from an InputStream and returns a command collection
*
* @param url
* @return
* @throws EBusConfigurationReaderException
* @throws IOException
*/
public @Nullable IEBusCommandCollection loadConfigurationCollection(final URL url)
throws EBusConfigurationReaderException, IOException;

/**
* @param url
* @return
*/
public List<IEBusCommandCollection> loadConfigurationCollectionBundle(final URL url);

/**
* Sets the eBUS type registry to use
*
* @param ebusTypes
*/
public void setEBusTypes(final EBusTypeRegistry ebusTypes);

/**
* Clears all internal states
*/
public void clear();
/**
* Loads all build-in command collections implemented by the reader
*
* @return
*/
public List<IEBusCommandCollection> loadBuildInConfigurationCollections()
throws EBusConfigurationReaderException, IOException;

/**
* Loads the configuration from an InputStream and returns a command collection
*
* @param url
* @return
* @throws EBusConfigurationReaderException
* @throws IOException
*/
public @Nullable IEBusCommandCollection loadConfigurationCollection(final URL url)
throws EBusConfigurationReaderException, IOException;

/**
* @param url
* @return
*/
public List<IEBusCommandCollection> loadConfigurationCollectionBundle(final URL url)
throws EBusConfigurationReaderException, IOException;

/**
* Sets the eBUS type registry to use
*
* @param ebusTypes
*/
public void setEBusTypes(final EBusTypeRegistry ebusTypes);

/**
* Clears all internal states
*/
public void clear();
}
94 changes: 52 additions & 42 deletions src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@
import java.util.Map;
import java.util.Objects;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import de.csdev.ebus.cfg.EBusConfigurationReaderException;
import de.csdev.ebus.cfg.IEBusConfigurationReader;
import de.csdev.ebus.cfg.std.dto.EBusCollectionDTO;
Expand Down Expand Up @@ -86,7 +84,8 @@ public EBusConfigurationReader() {
* @see de.csdev.ebus.cfg.IEBusConfigurationReader#loadBuildInConfigurations()
*/
@Override
public @NonNull List<@NonNull IEBusCommandCollection> loadBuildInConfigurationCollections() {
public @NonNull List<@NonNull IEBusCommandCollection> loadBuildInConfigurationCollections()
throws EBusConfigurationReaderException, IOException {

URL url = EBusConfigurationReader.class.getResource("/index-configuration.json");
Objects.requireNonNull(url);
Expand All @@ -97,7 +96,9 @@ public EBusConfigurationReader() {
/*
* (non-Javadoc)
*
* @see de.csdev.ebus.cfg.IEBusConfigurationReader#loadConfigurationCollection(java.io.InputStream)
* @see
* de.csdev.ebus.cfg.IEBusConfigurationReader#loadConfigurationCollection(java.
* io.InputStream)
*/
@Override
public @NonNull IEBusCommandCollection loadConfigurationCollection(@NonNull URL url)
Expand All @@ -124,19 +125,39 @@ public EBusConfigurationReader() {
EBusCollectionDTO collection = Objects
.requireNonNull(gson.fromJson(new InputStreamReader(dis), EBusCollectionDTO.class));

EBusCommandCollection commandCollection = (EBusCommandCollection) loadConfigurationCollection(collection);
try {
EBusCommandCollection commandCollection = (EBusCommandCollection) loadConfigurationCollection(collection);
// add md5 hash
commandCollection.setSourceHash(md.digest());

// add md5 hash
commandCollection.setSourceHash(md.digest());
return commandCollection;

return commandCollection;
} catch (EBusConfigurationReaderException e) {
throw new EBusConfigurationReaderException("%s [ URL: %s ]", e.getMessage(), url);
}
}

public @NonNull IEBusCommandCollection loadConfigurationCollection(@NonNull EBusCollectionDTO collection)
throws EBusConfigurationReaderException {

Objects.requireNonNull(collection, "collection");

if (StringUtils.isEmpty(collection.getId())) {
throw new EBusConfigurationReaderException("The property 'id' is missing for the configuration!");
}

if (StringUtils.isEmpty(collection.getLabel())) {
throw new EBusConfigurationReaderException("The property 'label' is missing for the configuration!");
}

if (StringUtils.isEmpty(collection.getDescription())) {
throw new EBusConfigurationReaderException("The property 'description' is missing for the configuration!");
}

if (collection.getProperties() == null) {
throw new EBusConfigurationReaderException("The property 'properties' is missing for the configuration!");
}

EBusCommandCollection commandCollection = new EBusCommandCollection(collection.getId(), collection.getLabel(),
collection.getDescription(), collection.getProperties());

Expand Down Expand Up @@ -250,7 +271,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection
}

if (id == null) {
throw new EBusConfigurationReaderException("Property 'id' is missing for command ! {0}",
throw new EBusConfigurationReaderException("Property 'id' is missing for command ! %s",
commandElement != null ? commandElement.toString() : "<NULL>");
}

Expand Down Expand Up @@ -367,7 +388,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection
}

if (StringUtils.isEmpty(typeStr)) {
throw new EBusConfigurationReaderException("Property 'type' is missing for command ! {0}",
throw new EBusConfigurationReaderException("Property 'type' is missing for command ! %s",
commandMethod != null ? commandMethod.getParent() : "<NULL>");
}

Expand Down Expand Up @@ -395,7 +416,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection
templateCollection = templateBlockRegistry.get(globalId);

if (templateCollection == null) {
throw new EBusConfigurationReaderException("Unable to find a template-block with id {0}!", id);
throw new EBusConfigurationReaderException("Unable to find a template-block with id %s!", id);
}
}

Expand Down Expand Up @@ -445,7 +466,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection
templateCollection.add(templateMap.get(id));

} else {
throw new EBusConfigurationReaderException("Unable to find a template for id {0}!", id);
throw new EBusConfigurationReaderException("Unable to find a template for id %s!", id);

}

Expand Down Expand Up @@ -556,7 +577,9 @@ private void overwritePropertiesFromTemplate(@NonNull EBusCommandValue clone, @N
/*
* (non-Javadoc)
*
* @see de.csdev.ebus.cfg.IEBusConfigurationReader#setEBusTypes(de.csdev.ebus.command.datatypes.EBusTypeRegistry)
* @see
* de.csdev.ebus.cfg.IEBusConfigurationReader#setEBusTypes(de.csdev.ebus.command
* .datatypes.EBusTypeRegistry)
*/
@Override
public void setEBusTypes(@NonNull EBusTypeRegistry ebusTypes) {
Expand All @@ -565,7 +588,8 @@ public void setEBusTypes(@NonNull EBusTypeRegistry ebusTypes) {
}

@Override
public @NonNull List<@NonNull IEBusCommandCollection> loadConfigurationCollectionBundle(@NonNull URL url) {
public @NonNull List<@NonNull IEBusCommandCollection> loadConfigurationCollectionBundle(@NonNull URL url)
throws EBusConfigurationReaderException, IOException {

Objects.requireNonNull(url, "url");

Expand All @@ -575,36 +599,22 @@ public void setEBusTypes(@NonNull EBusTypeRegistry ebusTypes) {
Type type = new TypeToken<Map<String, ?>>() {
}.getType();

try {
Map<String, ?> mapping = gson.fromJson(new InputStreamReader(url.openStream()), type);

Map<String, ?> mapping = gson.fromJson(new InputStreamReader(url.openStream()), type);
if (mapping.containsKey("files")) {

if (mapping.containsKey("files")) {
@SuppressWarnings("unchecked")
List<Map<String, String>> files = (List<Map<String, String>>) mapping.get("files");

@SuppressWarnings("unchecked")
List<Map<String, String>> files = (List<Map<String, String>>) mapping.get("files");
if (files != null && !files.isEmpty()) {
for (Map<String, String> file : files) {
URL fileUrl = new URL(url, file.get("url"));

if (files != null && !files.isEmpty()) {
for (Map<String, String> file : files) {
URL fileUrl = new URL(url, file.get("url"));

try {
logger.debug("Load configuration from url {} ...", fileUrl);
IEBusCommandCollection collection = loadConfigurationCollection(fileUrl);
result.add(collection);

} catch (EBusConfigurationReaderException e) {
logger.error("{} (url: {})", e.getMessage(), fileUrl);
} catch (IOException e) {
logger.error("error!", e);
}

}
logger.debug("Load configuration from url {} ...", fileUrl);
IEBusCommandCollection collection = loadConfigurationCollection(fileUrl);
result.add(collection);
}
}

} catch (JsonSyntaxException | JsonIOException | IOException e) {
logger.error("error!", e);
}

return result;
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/de/csdev/ebus/command/EBusCommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ public EBusCommandRegistry(Class<? extends IEBusConfigurationReader> readerClass
loadBuildInCommandCollections();
}

} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | EBusTypeException e) {
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | EBusTypeException
| EBusConfigurationReaderException | IOException e) {
throw new IllegalStateException(e);
}
}

/**
* Loads all build-in command collections
*
* @throws IOException
* @throws EBusConfigurationReaderException
*/
public void loadBuildInCommandCollections() {
public void loadBuildInCommandCollections() throws EBusConfigurationReaderException, IOException {
List<@NonNull IEBusCommandCollection> loadBuildInConfigurations = reader.loadBuildInConfigurationCollections();

if (!loadBuildInConfigurations.isEmpty()) {
Expand All @@ -97,23 +101,15 @@ public void loadBuildInCommandCollections() {
*
* @param url
*/
public void loadCommandCollection(@NonNull URL url) {

public void loadCommandCollection(@NonNull URL url) throws EBusConfigurationReaderException, IOException {
Objects.requireNonNull(url);

try {
addCommandCollection(reader.loadConfigurationCollection(url));

} catch (EBusConfigurationReaderException | IOException e) {
logger.error("error!", e);
}

addCommandCollection(reader.loadConfigurationCollection(url));
}

/**
* @param url
*/
public void loadCommandCollectionBundle(@NonNull URL url) {
public void loadCommandCollectionBundle(@NonNull URL url) throws EBusConfigurationReaderException, IOException {

Objects.requireNonNull(url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URL;

import org.junit.Test;
Expand All @@ -24,7 +25,7 @@
public class EBusConfigurationBundleTest {

@Test
public void test_BuildMasterTelegram() {
public void test_BuildMasterTelegram() throws EBusConfigurationReaderException, IOException {

URL url = EBusCommandRegistry.class.getResource("/index-configuration.json");
assertNotNull(url);
Expand Down
Loading