From 3a80c78e801cb6227f058db22e87dc58fd0fd7f2 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 30 Mar 2016 09:34:34 -0400 Subject: Added error-checking and input validation --- .../utils/components/FileComponentRepository.java | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index 1e67a72..f2d2cf8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -5,6 +5,8 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import org.slf4j.LoggerFactory; + import bjc.utils.funcdata.FunctionalList; /** @@ -33,6 +35,10 @@ public class FileComponentRepository * Create a new component repository sourcing components from files in * a directory * + * An exception thrown during the loading of a component will only + * cause the loading of that component to fail, but a warning will be + * logged. + * * @param dir * The directory to read component files from * @param reader @@ -47,9 +53,17 @@ public class FileComponentRepository // Do nothing with directories. They probably contain // support files for components } else { - E comp = reader.apply(fle); + try { + E comp = reader.apply(fle); + + comps.put(comp.getName(), comp); + } catch (Exception ex) { + LoggerFactory.getLogger(getClass()) + .warn("Error found reading component from file " + + fle.toString() + + ". This component will not be loaded"); + } - comps.put(comp.getName(), comp); } } } @@ -58,9 +72,7 @@ public class FileComponentRepository public FunctionalList getComponentList() { FunctionalList ret = new FunctionalList<>(); - comps.forEach((name, comp) -> { - ret.add(comp); - }); + comps.forEach((name, comp) -> ret.add(comp)); return ret; } @@ -72,6 +84,6 @@ public class FileComponentRepository @Override public String getSource() { - return "Read from directory " + sourcePath + "."; + return "Components read from directory " + sourcePath + "."; } } -- cgit v1.2.3