@Nullable @Override protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { String argument = (String) super.parseValue(source, args); Callable<BufferedReader> reader = () -> new BufferedReader(new StringReader(argument)); ConfigurationLoader<? extends ConfigurationNode> loader = HoconConfigurationLoader.builder() .setSource(reader).build(); ConfigurationNode node; try { node = loader.load(); } catch (IOException ex) { throw args.createError(Text.of("Node parsing failed: ", ex.getMessage())); } return DataTranslators.CONFIGURATION_NODE.translate(node); }
.setHeaderMode(HeaderMode.PRESERVE) .setPath(configurationPath) .build();
@GET @Path("/{plugin}/config") @Permission({ "config", "get" }) @ApiOperation(value = "Get plugin configs", notes = "Gets a map containing the plugin config file names as keys, " + "and their config file contents as their values.") public Map<String, Object> getPluginConfig( @PathParam("plugin") @ApiParam("The id of the plugin") String pluginName) throws NotFoundException { Optional<CachedPluginContainer> optPlugin = cacheService.getPlugin(pluginName); if (!optPlugin.isPresent()) { throw new NotFoundException("Plugin with id '" + pluginName + "' could not be found"); } List<java.nio.file.Path> paths = getConfigFiles(optPlugin.get()); Map<String, Object> configs = new HashMap<>(); for (java.nio.file.Path path : paths) { String key = path.getFileName().toString(); try { ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setPath(path).build(); CommentedConfigurationNode config = loader.load(); configs.put(key, parseConfiguration(config)); } catch (IOException e) { configs.put(key, e); } } return configs; }
configPath.toFile().mkdirs(); File configFile = new File(configPath.toFile(), "config.conf"); HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build(); CommentedConfigurationNode node; if (!configFile.exists()) {
configPath.toFile().mkdirs(); File configFile = new File(configPath.toFile(), "config.conf"); HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build(); CommentedConfigurationNode node; if (!configFile.exists()) {
public static void init(Path config) { HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(config).build(); if (!Files.exists(config)) { CommentedConfigurationNode node = loader.createEmptyNode();
permissionsFile = migrateLegacy(permissionsFile, ".yml", YAMLConfigurationLoader.builder().setPath(permissionsFile).build(), "YML"); } else if (file.endsWith(".conf")) { permissionsFile = migrateLegacy(permissionsFile, ".conf", HoconConfigurationLoader.builder().setPath(permissionsFile).build(), "HOCON"); } else { permissionsFileLoader = createLoader(permissionsFile);
configPath.toFile().mkdirs(); File configFile = new File(configPath.toFile(), "config.conf"); HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build(); CommentedConfigurationNode node; if (!configFile.exists()) {
public static <T extends BaseConfig> T load(Path path, T defaultConfig) { ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder() .setPath(path) .build(); CommentedConfigurationNode node; try { node = loader.load(); } catch (IOException e) { e.printStackTrace(); node = loader.createEmptyNode(); } T config = null; try { config = (T)node.getValue(TypeToken.of(defaultConfig.getClass())); } catch (ObjectMappingException e) { e.printStackTrace(); } if (config == null) { config = defaultConfig; } config.setLoader(loader); config.setNode(node); config.save(); return config; } }
/** * Loads the given configuration file. */ public static void load(Path path) { System.out.println("Loading config from " + path.toString()); try { Files.createDirectories(path.getParent()); if (Files.notExists(path)) { Files.createFile(path); } loader = HoconConfigurationLoader.builder().setPath(path).build(); configMapper = ObjectMapper.forClass(ConfigBase.class).bindToNew(); node = loader.load(ConfigurationOptions.defaults().setHeader(HEADER)); config = configMapper.populate(node); configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error loading configuration:"); e.printStackTrace(); } }
private <T> void loadMapper(ObjectMapper<T>.BoundInstance mapper, Path file, ConfigurationOptions options) { ConfigurationNode rootNode; if (mapper != null) { HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(file).build(); try { rootNode = loader.load(options.setShouldCopyDefaults(true)); ConfigurationNode hashNode = rootNode.getNode("hashAlgo"); if ("bcrypt".equalsIgnoreCase(hashNode.getString())) { hashNode.setValue("BCrypt"); } //load the config into the object mapper.populate(rootNode); //add missing default values loader.save(rootNode); } catch (ObjectMappingException objMappingExc) { logger.error("Error loading the configuration", objMappingExc); } catch (IOException ioExc) { logger.error("Error saving the default configuration", ioExc); } } }
/** * Loads the configuration */ private void loadConfiguration() { configurationFile = getConfigFile(); configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build(); try { if (!configurationFile.exists()) { configurationFile.createNewFile(); config = configurationLoader.load(); config.setComment("This contains settings for MCStats: http://mcstats.org"); config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString()); config.getNode("mcstats.opt-out").setValue(false); config.getNode("mcstats.debug").setValue(false); configurationLoader.save(config); } else { config = configurationLoader.load(); } guid = config.getNode("mcstats.guid").getString(); debug = config.getNode("mcstats.debug").getBoolean(); } catch (final IOException e) { e.printStackTrace(); } }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.conf"); final Path saveTest = tempDir.resolve("text1.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); CommentedConfigurationNode node = loader.load(); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); CommentedConfigurationNode testNode = node.getNode("test"); assertEquals(" Test node", testNode.getComment().orElse(null)); assertEquals("dog park", node.getNode("other", "location").getValue()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/roundtrip-test.conf"), UTF_8), Files .readAllLines(saveTest, UTF_8)); }
public ConfigCategory loadConfig() { try { File file = new File(instance.configDir, "MagiBridge.conf"); if (!file.exists()) { file.createNewFile(); } ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setFile(file).build(); CommentedConfigurationNode config = loader.load(ConfigurationOptions.defaults().setObjectMapperFactory(instance.factory).setShouldCopyDefaults(true).setHeader(HEADER)); root = config.getValue(TypeToken.of(ConfigCategory.class), new ConfigCategory()); loader.save(config); return root; } catch (Exception e) { MagiBridge.getLogger().error("Could not load config.", e); return root; } }
@Test public void testBooleansNotShared(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/comments-test.conf"); final Path saveTo = tempDir.resolve("text4.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo).setURL(url).build(); CommentedConfigurationNode node = loader.createEmptyNode(ConfigurationOptions.defaults()); node.getNode("test", "third").setValue(false).setComment("really?"); node.getNode("test", "apple").setComment("fruit").setValue(false); node.getNode("test", "donut").setValue(true).setComment("tasty"); node.getNode("test", "guacamole").setValue(true).setComment("and chips?"); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(saveTo, UTF_8)); }
@Test public void testHeaderSaved(@TempDirectory.TempDir Path tempDir) throws IOException { final Path saveTo = tempDir.resolve("text3.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo) .build(); CommentedConfigurationNode node = loader.createEmptyNode(ConfigurationOptions.defaults().setHeader("Hi! I am a header!\n" + "Look at meeeeeee!!!")); node.getNode("node").setComment("I have a comment").getNode("party").setValue("now"); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/header.conf"), UTF_8), Files.readAllLines(saveTo, UTF_8)); }
@Test public void testSplitLineCommentInput(@TempDirectory.TempDir Path tempDir) throws IOException { final Path saveTo = tempDir.resolve("text2.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo) .setURL(getClass().getResource("/splitline-comment-input.conf")) .build(); CommentedConfigurationNode node = loader.load(); System.out.println(node.getOptions().getHeader()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/splitline-comment-output.conf"), UTF_8), Files.readAllLines(saveTo, UTF_8)); }
public static ConfigurationNode loadDefaultConfiguration() throws IOException { final URL defaultConfig = FilePermissionsExConfiguration.class.getResource("default.conf"); if (defaultConfig == null) { throw new Error(t("Default config file is not present in jar!").translate(Locale.getDefault())); } HoconConfigurationLoader fallbackLoader = HoconConfigurationLoader.builder().setURL(defaultConfig).build(); return fallbackLoader.load(); } }
public SpongeConfig(File folder, String fileName, ServerCaller serverCaller) { super(folder, fileName, serverCaller); try { loader = HoconConfigurationLoader.builder().setFile(new File(folder, fileName)).build(); file = loader.load(); } catch (IOException e) { e.printStackTrace(); } }