private static YamlConfiguration createConfigurtion(Optional<File> configurationFile) throws BaleenException { if (configurationFile.isPresent()) { try (InputStream is = new FileInputStream(configurationFile.get())) { LOGGER.info("Configuration file provided {}", configurationFile.get().getAbsolutePath()); return new YamlConfiguration(configurationFile.get()); } catch (Exception ioe) { throw new BaleenException("Unable to read configuration file", ioe); } } else { LOGGER.info("No configuration file provided - default configuration will be used"); } try { return new YamlConfiguration(); } catch (Exception e) { throw new BaleenException("Unable to read configuration file", e); } }
@Override public <T> Optional<T> get(Class<T> clazz, String path) { return Optional.ofNullable(get(clazz, path, null)); }
@Override public List<Map<String, Object>> getAsListOfMaps(String path) { return getAsList(path); }
@Test public void testReadString() throws IOException { YamlConfiguration yc = new YamlConfiguration("example:\n color: red\n count: 7\n list:\n - a\n - b\n - c"); assertTrue(yc.getAsList("example.list").containsAll(Arrays.asList("a", "b", "c"))); assertEquals("red", yc.get(String.class, "example.color").get()); }
@Test public void testCleanTabs() throws IOException { assertEquals( "test: \"Hello\\tWorld!\"\n" + "testing:\n" + "- 1.. 2.. 3.. 4..\n", new YamlConfiguration("\t\ttest: Hello\tWorld!\n\t\ttesting:\n\t\t- 1.. 2.. 3.. 4.. ") .toString()); } }
@Override @SuppressWarnings("unchecked") public <T> T get(String path, T defaultValue) { Optional<Object> o = internalGet(path); T ret = defaultValue; if (o.isPresent()) { try { ret = (T) o.get(); } catch (ClassCastException cce) { LOGGER.warn("Requested path cannot be cast to requested type", cce); } } return ret; }
@Test public void testGetFirst() { assertEquals( "uk.gov.dstl.baleen.testing.DummyContentExtractor", config.getFirst(String.class, "contentextractor.class", "contentextractor").get()); assertEquals( "uk.gov.dstl.baleen.testing.DummyCollectionReader", config.getFirst(String.class, "collectionreader.class", "collectionreader").get()); Optional<String> found = config.getFirst(String.class, MISSING_KEY, "example", "missing"); assertFalse(found.isPresent()); }
@Test public void testGetAsListOfMaps() { List<Map<String, Object>> listMap = config.getAsListOfMaps("annotators"); assertFalse(listMap.isEmpty()); }
@Override public <T> T get(Class<T> clazz, String path, T defaultValue) { Optional<Object> o = internalGet(path); T ret = defaultValue; if (o.isPresent()) { try { ret = clazz.cast(o.get()); } catch (ClassCastException cce) { LOGGER.debug("Requested path cannot be cast to requested type", cce); } } return ret; }
private static YamlConfiguration createConfigurtion(Optional<File> configurationFile) throws BaleenException { if (configurationFile.isPresent()) { try (InputStream is = new FileInputStream(configurationFile.get())) { LOGGER.info("Configuration file provided {}", configurationFile.get().getAbsolutePath()); return new YamlConfiguration(configurationFile.get()); } catch (Exception ioe) { throw new BaleenException("Unable to read configuration file", ioe); } } else { LOGGER.info("No configuration file provided - default configuration will be used"); } try { return new YamlConfiguration(); } catch (Exception e) { throw new BaleenException("Unable to read configuration file", e); } }
@Override public <T> Optional<T> get(String path) { return Optional.ofNullable(get(path, null)); }
@Override public List<Map<String, Object>> getAsListOfMaps(String path) { return getAsList(path); }
@Override @SuppressWarnings("unchecked") public <T> List<T> getAsList(String path) { Optional<Object> o = internalGet(path); List<T> ret = Collections.emptyList(); if (o.isPresent() && o.get() instanceof List) { try { ret = (List<T>) o.get(); } catch (ClassCastException cce) { LOGGER.warn("Requested path is not a list of the correct type", cce); } } else { LOGGER.debug( "Path [{}] not found, or isn't an instanceof List - an empty list will be returned", path); } return ret; }
BaleenManager baleen = new BaleenManager(new YamlConfiguration(yaml)); if (dryRun) { baleen.run(manager -> LOGGER.info("Dry run mode: closing immediately"));
@Override public <T> Optional<T> getFirst(Class<T> clazz, String... paths) { for (String path : Arrays.asList(paths)) { Optional<T> optional = get(clazz, path); if (optional.isPresent()) { return optional; } } return Optional.empty(); }
@Override @SuppressWarnings("unchecked") public <T> List<T> getAsList(String path) { Optional<Object> o = internalGet(path); List<T> ret = Collections.emptyList(); if (o.isPresent() && o.get() instanceof List) { try { ret = (List<T>) o.get(); } catch (ClassCastException cce) { LOGGER.warn("Requested path is not a list of the correct type", cce); } } else { LOGGER.debug( "Path [{}] not found, or isn't an instanceof List - an empty list will be returned", path); } return ret; }
@Before public void setup() throws IOException { config = new YamlConfiguration(YamlConfigurationTest.class, YAMLCONFIGURATION_YAML); }
@Test public void testNonExistentFile() { try { new YamlConfiguration(new File("missing.yaml")); } catch (IOException ioe) { return; } fail("Didn't throw expected IOException"); }