@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); }
/** * 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(); } }
public static void init(Path config) { HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(config).build(); if (!Files.exists(config)) { CommentedConfigurationNode node = loader.createEmptyNode(); createDefaults(node); try { Files.createDirectories(config.toAbsolutePath().getParent()); loader.save(node); } catch (IOException e) { VoxelSniper.getLogger().error("Failed to save default configuration"); CommentedConfigurationNode node = loader.load(); boolean modified = false; for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) { loader.save(node); } catch (IOException e) { VoxelSniper.getLogger().error("Failed to save updated configuration");
@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)); }
/** * Saves the config back to disk to persist and changes made. */ public static void update() { try { configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error saving configuration:"); e.printStackTrace(); } }
private static ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
@Test public void testNewConfigObject() { Map<String, ConfigValue> entries = ImmutableMap.of("a", ConfigValueFactory.fromAnyRef("hi"), "b", ConfigValueFactory.fromAnyRef("bye")); HoconConfigurationLoader.newConfigObject(entries); }
@Test public void testNewConfigList() { List<ConfigValue> entries = ImmutableList.of(ConfigValueFactory.fromAnyRef("hello"), ConfigValueFactory.fromAnyRef("goodbye")); HoconConfigurationLoader.newConfigList(entries); } }
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); } } }
@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)); }
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(); } }
@Override public void setValue(String path, Object value) { file.getNode(path).setValue(value); try { loader.save(file); } catch (IOException e) { e.printStackTrace(); } }
private ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
configPath.toFile().mkdirs(); File configFile = new File(configPath.toFile(), "config.conf"); HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build(); CommentedConfigurationNode node; if (!configFile.exists()) { configFile.createNewFile(); node = configurationLoader.load(); ); configurationLoader.save(node); } else { node = configurationLoader.load();
loader = HoconConfigurationLoader.builder() .setDefaultOptions(getDefaultOptions()) .setHeaderMode(HeaderMode.PRESERVE)
@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)); }
@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()) { configFile.createNewFile(); node = configurationLoader.load(); node.getNode("configVersion").setValue(2); configurationLoader.save(node); } else { node = configurationLoader.load(); ); configurationLoader.save(node);
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; } }