/** * Fetches a list of words from the specified settings file. The list should either be available * at the key specified by settingsPrefix or in a file specified by settingsPrefix + _path. * * @throws IllegalArgumentException If the word list cannot be found at either key. */ public static List<String> getWordList(Environment env, Settings settings, String settingPrefix) { String wordListPath = settings.get(settingPrefix + "_path", null); if (wordListPath == null) { String[] explicitWordList = settings.getAsArray(settingPrefix, null); if (explicitWordList == null) { return null; } else { return Arrays.asList(explicitWordList); } } final Path wordListFile = env.configFile().resolve(wordListPath); try (BufferedReader reader = FileSystemUtils.newBufferedReader(wordListFile.toUri().toURL(), StandardCharsets.UTF_8)) { return loadWordList(reader, "#"); } catch (IOException ioe) { String message = String.format( Locale.ROOT, "IOException while reading %s_path: %s", settingPrefix, ioe.getMessage()); throw new IllegalArgumentException(message); } }
@Test public void testResolveFileResource() throws IOException { Environment environment = newEnvironment(); URL url = environment.resolveConfig("org/elasticsearch/common/cli/tool.help"); assertNotNull(url); try (BufferedReader reader = FileSystemUtils.newBufferedReader(url, Charsets.UTF_8)) { String string = Streams.copyToString(reader); assertEquals(string, "tool help"); } }
public static String randomNodeName(URL nodeNames) { try { int numberOfNames = 0; try (BufferedReader reader = FileSystemUtils.newBufferedReader(nodeNames, Charsets.UTF_8)) { while (reader.readLine() != null) { numberOfNames++; } } try (BufferedReader reader = FileSystemUtils.newBufferedReader(nodeNames, Charsets.UTF_8)) { int number = ((ThreadLocalRandom.current().nextInt(numberOfNames)) % numberOfNames); for (int i = 0; i < number; i++) { reader.readLine(); } return reader.readLine(); } } catch (IOException e) { return null; } }
@Test public void testResolveJaredResource() throws IOException { Environment environment = newEnvironment(); URL url = environment.resolveConfig( "META-INF/MANIFEST.MF"); // this works because there is one jar having this file in the // classpath assertNotNull(url); try (BufferedReader reader = FileSystemUtils.newBufferedReader(url, Charsets.UTF_8)) { String string = Streams.copyToString(reader); assertTrue(string, string.contains("Manifest-Version")); } }
/** * @return null If no settings set for "settingsPrefix" then return <code>null</code>. * @throws IllegalArgumentException If the Reader can not be instantiated. */ public static Reader getReaderFromFile(Environment env, Settings settings, String settingPrefix) { String filePath = settings.get(settingPrefix, null); if (filePath == null) { return null; } final Path path = env.configFile().resolve(filePath); try { return FileSystemUtils.newBufferedReader(path.toUri().toURL(), StandardCharsets.UTF_8); } catch (IOException ioe) { String message = String.format( Locale.ROOT, "IOException while reading %s_path: %s", settingPrefix, ioe.getMessage()); throw new IllegalArgumentException(message); } }