public void save() { try { node.setValue(TypeToken.of(BaseConfig.class), this); loader.save(node); } catch (ObjectMappingException | IOException e) { e.printStackTrace(); } }
@Override public void setValue(String path, Object value) { file.getNode(path).setValue(value); try { loader.save(file); } catch (IOException e) { e.printStackTrace(); } }
public static void createDefaults(CommentedConfigurationNode config) { for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigValue.class)) { ConfigValue anno = field.getAnnotation(ConfigValue.class); CommentedConfigurationNode node = config.getNode((Object[]) anno.name().split("\\.")); node.setComment(anno.comment()); try { node.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + node.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Failed to extract default value for configuration field " + field.toGenericString()); e.printStackTrace(); } } } }
private void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
private static void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
/** * 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(); } }
/** * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * * @throws IOException */ public void enable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (isOptOut()) { config.getNode("mcstats.opt-out").setValue(false); configurationLoader.save(config); } // Enable Task, if it is not running if (task == null) { start(); } } }
/** * Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task. * * @throws IOException */ public void disable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (!isOptOut()) { config.getNode("mcstats.opt-out").setValue(true); configurationLoader.save(config); } // Disable Task, if it is running if (task != null) { task.cancel(); task = null; } } }
node.getNode("enabled").setValue(false); node.getNode("serverUuid").setValue(UUID.randomUUID().toString()); node.getNode("logFailedRequests").setValue(false); node.getNode("logSentData").setValue(false); node.getNode("logResponseStatusText").setValue(false); "Check out https://bStats.org/ to learn more :)" ); node.getNode("configVersion").setValue(2); node.getNode("configVersion").setValue(2);
node.getNode("enabled").setValue(false); node.getNode("serverUuid").setValue(UUID.randomUUID().toString()); node.getNode("logFailedRequests").setValue(false); node.getNode("logSentData").setValue(false); node.getNode("logResponseStatusText").setValue(false); "Check out https://bStats.org/ to learn more :)" ); node.getNode("configVersion").setValue(2); node.getNode("configVersion").setValue(2);
n.setComment(anno.comment()); try { n.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + n.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) {
@Test public void testWalker() { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); node.getNode("l1-1").setValue(1); node.getNode("l1-1", "l2-1").setValue(1); node.getNode("l1-1", "l2-2").setValue(1); node.getNode("l1-1", "l2-3", "l3-1").setValue(1); node.getNode("l1-1", "l2-3", "l3-2").setValue(1); node.getNode("l1-1", "l2-4").setValue(1); node.getNode("l1-2").setValue(1); node.getNode("l1-2", "l2-1").setValue(ImmutableList.of(1, 2, 3)); List<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> breadthFirst = new ArrayList<>(); Iterators.addAll(breadthFirst, ConfigurationNodeWalker.BREADTH_FIRST.walkWithPath(node)); List<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> depthFirstPre = new ArrayList<>(); Iterators.addAll(depthFirstPre, ConfigurationNodeWalker.DEPTH_FIRST_PRE_ORDER.walkWithPath(node)); List<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> depthFirstPost = new ArrayList<>(); Iterators.addAll(depthFirstPost, ConfigurationNodeWalker.DEPTH_FIRST_POST_ORDER.walkWithPath(node)); assertEquals(13, breadthFirst.size()); assertEquals(13, depthFirstPre.size()); assertEquals(13, depthFirstPost.size()); List<String> breadthFirstOrder = breadthFirst.stream().map(PATH_TO_STRING).collect(Collectors.toList()); assertEquals(EXPECTED_BREADTH_FIRST_ORDER, breadthFirstOrder); List<String> depthFirstPreOrder = depthFirstPre.stream().map(PATH_TO_STRING).collect(Collectors.toList()); assertEquals(EXPECTED_DEPTH_FIRST_PRE_ORDER, depthFirstPreOrder); List<String> depthFirstPostOrder = depthFirstPost.stream().map(PATH_TO_STRING).collect(Collectors.toList()); assertEquals(EXPECTED_DEPTH_FIRST_POST_ORDER, depthFirstPostOrder); }
@Test public void testCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); firstChild.setValue("test value"); firstChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getValue()); assertEquals("Such comment. Very wow.", secondChild.getComment().orElse(null)); }
@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 testNestedCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); CommentedConfigurationNode firstChildChild = firstChild.getNode("child"); firstChildChild.setValue("test value"); firstChildChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getNode("child").getValue()); assertEquals("Such comment. Very wow.", secondChild.getNode("child").getComment().orElse(null)); }
@Test public void testCommentsMerged() { CommentedConfigurationNode source = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode target = SimpleCommentedConfigurationNode.root(); source.getNode("no-value").setValue("a").setComment("yeah"); source.getNode("existing-value-no-comment").setValue("orig").setComment("maybe"); source.getNode("existing-value").setValue("a").setComment("yeah"); source.getNode("no-parent", "child").setValue("x").setComment("always"); target.getNode("existing-value-no-comment").setValue("new"); target.getNode("existing-value").setValue("b").setComment("nope"); target.mergeValuesFrom(source); assertEquals("yeah", target.getNode("no-value").getComment().orElse(null)); assertEquals("maybe", target.getNode("existing-value-no-comment").getComment().orElse(null)); assertEquals("new", target.getNode("existing-value-no-comment").getString()); assertEquals("nope", target.getNode("existing-value").getComment().orElse(null)); assertEquals("always", target.getNode("no-parent", "child").getComment().orElse(null)); } }
@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)); }