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); } } }
void serialize(ConfigurationNode node) throws ObjectMappingException { if (!node.hasListChildren()) { node.setValue(null); } Map<Set<Entry<String, String>>, ConfigurationNode> existingSections = new HashMap<>(); for (ConfigurationNode child : node.getChildrenList()) { existingSections.put(contextsFrom(child), child); } for (Map.Entry<Set<Entry<String, String>>, DataEntry> ent : contexts.entrySet()) { ConfigurationNode contextSection = existingSections.remove(ent.getKey()); if (contextSection == null) { contextSection = node.getAppendedNode(); ConfigurationNode contextsNode = contextSection.getNode(KEY_CONTEXTS); for (Entry<String, String> context : ent.getKey()) { contextsNode.getNode(context.getKey()).setValue(context.getValue()); } } MAPPER.bind(ent.getValue()).serialize(contextSection); } for (ConfigurationNode unused : existingSections.values()) { unused.setValue(null); } }
/** * 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 General getGeneral() { return configMapper.getInstance(); }
static FileSubjectData fromNode(ConfigurationNode node) throws ObjectMappingException, PermissionsLoadingException { ImmutableMap.Builder<Set<Entry<String, String>>, DataEntry> map = ImmutableMap.builder(); if (node.hasListChildren()) { for (ConfigurationNode child : node.getChildrenList()) { if (!child.hasMapChildren()) { throw new PermissionsLoadingException(t("Each context section must be of map type! Check that no duplicate nesting has occurred.")); } Set<Entry<String, String>> contexts = contextsFrom(child); DataEntry value = MAPPER.bindToNew().populate(child); map.put(contexts, value); } } return new FileSubjectData(map.build()); }
public TextConfig getText() { return textMapper.getInstance(); }
@Override @SuppressWarnings("unchecked") public void serialize(@NonNull TypeToken<?> type, @Nullable Object obj, @NonNull ConfigurationNode value) throws ObjectMappingException { if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { // serialize obj's concrete type rather than the interface/abstract class value.getNode("__class__").setValue(obj.getClass().getName()); } ((ObjectMapper<Object>) value.getOptions().getObjectMapperFactory().getMapper(obj.getClass())).bind(obj).serialize(value); } }
@Override public Object deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { Class<?> clazz = getInstantiableType(type, value.getNode("__class__").getString()); return value.getOptions().getObjectMapperFactory().getMapper(clazz).bindToNew().populate(value); }
@Test public void testCreateGuiceObjectMapper() throws ObjectMappingException { Injector injector = Guice.createInjector(new TestModule()); GuiceObjectMapperFactory factory = injector.getInstance(GuiceObjectMapperFactory.class); ObjectMapper<ConfigClass> mapper = factory.getMapper(ConfigClass.class); assertTrue(mapper.canCreateInstances()); assertNotNull(mapper.bindToNew().getInstance()); } }
/** * Loads configuration */ public void load() { try { baseNode = loader.load(); configuration = mapper.populate(Objects.requireNonNull(baseNode).getNode(baseNodeName)); } catch (ObjectMappingException | IOException e) { throw new RuntimeException(e); } }
@Override @SuppressWarnings("unchecked") public void serialize(@NonNull TypeToken<?> type, @Nullable Object obj, @NonNull ConfigurationNode value) throws ObjectMappingException { if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { // serialize obj's concrete type rather than the interface/abstract class value.getNode("__class__").setValue(obj.getClass().getName()); } ((ObjectMapper<Object>) value.getOptions().getObjectMapperFactory().getMapper(obj.getClass())).bind(obj).serialize(value); } }
/** * Return a view on this mapper that is bound to a single object instance * * @param instance The instance to bind to * @return A view referencing this mapper and the bound instance */ public BoundInstance bind(T instance) { return new BoundInstance(instance); }
/** * Return a view on this mapper that is bound to a single object instance * * @param instance The instance to bind to * @return A view referencing this mapper and the bound instance */ public BoundInstance bind(T instance) { return new BoundInstance(instance); }
@Test public void testKeyFromFieldName() throws ObjectMappingException { final ObjectMapper<FieldNameObject> mapper = ObjectMapper.forClass(FieldNameObject.class); final ConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("loads").setValue(true); FieldNameObject obj = mapper.bindToNew().populate(node); assertTrue(obj.loads); }
@Test public void testDefaultsApplied() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); instance.stringVal = "hi"; mapper.bind(instance).populate(source); assertEquals("hi", source.getNode("test-key").getString()); }
@Test public void testNullsPreserved() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final TestObject obj = mapper.bindToNew().populate(SimpleConfigurationNode.root()); assertNull(obj.stringVal); }
@Override public Object deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { Class<?> clazz = getInstantiableType(type, value.getNode("__class__").getString()); return value.getOptions().getObjectMapperFactory().getMapper(clazz).bindToNew().populate(value); }
@Override @SuppressWarnings("unchecked") public void serialize(TypeToken<?> type, Object obj, ConfigurationNode value) throws ObjectMappingException { if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { value.getNode("__class__").setValue(type.getRawType().getCanonicalName()); } ((ObjectMapper<Object>) value.getOptions().getObjectMapperFactory().getMapper(obj.getClass())) .bind(obj).serialize(value); } }
@Test public void testLoadExistingObject() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); source.getNode("test-key").setValue("boom"); mapper.bind(instance).populate(source); assertEquals("boom", instance.stringVal); }