@Override protected Set<Property> createPropertySet(Class<?> type, BeanAccess bAccess) { try { Set<Property> properties = super.createPropertySet(type, bAccess); if (Tape.class.isAssignableFrom(type)) { return sort(properties, "name", "interactions"); } else if (YamlRecordedInteraction.class.isAssignableFrom(type)) { return sort(properties, "recorded", "request", "response"); } else if (YamlRecordedRequest.class.isAssignableFrom(type)) { return sort(properties, "method", "uri", "headers", "body"); } else if (YamlRecordedResponse.class.isAssignableFrom(type)) { return sort(properties, "status", "headers", "body"); } else { return properties; } } catch (IntrospectionException e) { throw new RuntimeException(e); } }
public void testArrayAsMapValue() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); A data = createA(); String dump = yaml2dump.dump(data); // System.out.println(dump); Yaml yaml2load = new Yaml(); yaml2load.setBeanAccess(BeanAccess.FIELD); A loaded = (A) yaml2load.load(dump); assertEquals(data.meta.size(), loaded.meta.size()); Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); for (Entry<String, String[]> entry : loadedMeta) { assertTrue(data.meta.containsKey(entry.getKey())); Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); } }
public void testArrayAsMapValueWithTypeDespriptor() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); A data = createA(); String dump = yaml2dump.dump(data); // System.out.println(dump); TypeDescription aTypeDescr = new TypeDescription(A.class); aTypeDescr.putMapPropertyType("meta", String.class, String[].class); Constructor c = new Constructor(); c.addTypeDescription(aTypeDescr); Yaml yaml2load = new Yaml(c); yaml2load.setBeanAccess(BeanAccess.FIELD); A loaded = (A) yaml2load.load(dump); assertEquals(data.meta.size(), loaded.meta.size()); Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); for (Entry<String, String[]> entry : loadedMeta) { assertTrue(data.meta.containsKey(entry.getKey())); Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); } }
public void testArrayAsListValueWithTypeDespriptor() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); B data = createB(); String dump = yaml2dump.dump(data); // System.out.println(dump); TypeDescription aTypeDescr = new TypeDescription(B.class); aTypeDescr.putListPropertyType("meta", String[].class); Constructor c = new Constructor(); c.addTypeDescription(aTypeDescr); Yaml yaml2load = new Yaml(c); yaml2load.setBeanAccess(BeanAccess.FIELD); B loaded = (B) yaml2load.load(dump); Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); }
public void testErrorMessage() throws Exception { BeanA1 b = new BeanA1(); b.setId(2l); b.setName("name1"); Constructor c = new Constructor(); Representer r = new Representer(); PropertyUtils pu = new PropertyUtils(); c.setPropertyUtils(pu); r.setPropertyUtils(pu); pu.getProperties(BeanA1.class, BeanAccess.FIELD); Yaml yaml = new Yaml(c, r); // yaml.setBeanAccess(BeanAccess.FIELD); String dump = yaml.dump(b); BeanA1 b2 = (BeanA1) yaml.load(dump); assertEquals(b.getId(), b2.getId()); assertEquals(b.getName(), b2.getName()); }
/** * Read a.yaml file according to a class type. * * @param file File path of the configuration file * @param classType Class type of the.yaml bean * @param <T> Class T * @return Config file bean * @throws CarbonIdentityMgtConfigException Error in reading configuration file */ public static <T> T readConfigFile(Path file, Class<T> classType) throws CarbonIdentityMgtConfigException { if (Files.exists(file)) { try { Reader in = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8); CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(classType.getClassLoader()); Yaml yaml = new Yaml(constructor); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(in, classType); } catch (IOException e) { throw new CarbonIdentityMgtConfigException(String.format("Error in reading file %s", file.toString()) , e); } } else { throw new CarbonIdentityMgtConfigException(String .format("Configuration file %s is not available.", file.toString())); } }
/** * Read a.yaml file according to a class type. * * @param path folder which contain the config files * @param classType Class type of the.yaml bean * @param fileNameRegex file name regex * @param <T> Class T * @return Config file bean * @throws CarbonIdentityMgtConfigException Error in reading configuration file */ public static <T> List<T> readConfigFiles(Path path, Class<T> classType, String fileNameRegex) throws CarbonIdentityMgtConfigException { List<T> configEntries = new ArrayList<>(); if (Files.exists(path)) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, fileNameRegex)) { for (Path file : stream) { Reader in = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8); CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(classType.getClassLoader()); Yaml yaml = new Yaml(constructor); yaml.setBeanAccess(BeanAccess.FIELD); configEntries.add(yaml.loadAs(in, classType)); } } catch (DirectoryIteratorException | IOException e) { throw new CarbonIdentityMgtConfigException(String.format("Failed to read identity connector files " + "from path: %s", path.toString()), e); } } return configEntries; }
public static <T> void writeConfigFiles(Path file, Object data) throws IdentityRecoveryException { if (Files.exists(file, new LinkOption[0])) { try { CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(FileUtil.class.getClassLoader()); Yaml yaml = new Yaml(constructor); yaml.setBeanAccess(BeanAccess.FIELD); try (Writer writer = new OutputStreamWriter(new FileOutputStream(file.toFile()), StandardCharsets.UTF_8)) { yaml.dump(data, writer); } } catch (IOException e) { throw new IdentityRecoveryException( String.format("Error in reading file %s", new Object[] { file.toString() }), e); } } else { throw new IdentityRecoveryException( String.format("Configuration file %s is not available.", new Object[] { file.toString() })); } }
@SuppressWarnings("unchecked") private static Map<String, Object> readProperties(final InputStream in) throws IOException { String inputStreamString = IOUtils.toString(new InputStreamReader(in, "UTF-8")); Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); final Iterable<Object> iter = yaml.loadAll(substituteVariables(inputStreamString)); final Map<String, Object> newProperties = new LinkedHashMap<>(); iter.forEach(item -> { final Map<String, Object> section = (Map<String, Object>) item; newProperties.putAll(section); }); return newProperties; }
@Override public List<MailReceiver> readMailReceiver() { File dir = git.getRepo().directory(); File file = new File(dir, "/mailinglists.yml"); if (!file.exists()) { return null; } try { Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); return (List<MailReceiver>) yaml.load(FileUtils.readFileToString(file)); } catch (IOException e) { log.error(e.getMessage(), e); } return null; }
public ChronixSparkLoader() { Representer representer = new Representer(); representer.getPropertyUtils().setSkipMissingProperties(true); Constructor constructor = new Constructor(YamlConfiguration.class); TypeDescription typeDescription = new TypeDescription(ChronixYAMLConfiguration.class); typeDescription.putMapPropertyType("configurations", Object.class, ChronixYAMLConfiguration.IndividualConfiguration.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor, representer); yaml.setBeanAccess(BeanAccess.FIELD); InputStream in = this.getClass().getClassLoader().getResourceAsStream("test_config.yml"); chronixYAMLConfiguration = yaml.loadAs(in, ChronixYAMLConfiguration.class); }
public static TransportsConfiguration build(String nettyTransportsConfigFile) { TransportsConfiguration transportsConfiguration; File file = new File(nettyTransportsConfigFile); if (file.exists()) { try (Reader in = new InputStreamReader(new FileInputStream(file), StandardCharsets.ISO_8859_1)) { Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); transportsConfiguration = yaml.loadAs(in, TransportsConfiguration.class); } catch (IOException e) { String msg = "Error while loading " + nettyTransportsConfigFile + " configuration file"; throw new RuntimeException(msg, e); } } else { // return a default config log.warn("Netty transport configuration file not found in: " + nettyTransportsConfigFile); transportsConfiguration = TransportsConfiguration.getDefault(); } return transportsConfiguration; }
/** * Get the {@code TransportsConfiguration} represented by a particular configuration file * * @param configFileLocation configuration file location * @return TransportsConfiguration represented by a particular configuration file */ public TransportsConfiguration getConfiguration(String configFileLocation) { TransportsConfiguration transportsConfiguration; File file = new File(configFileLocation); if (file.exists()) { try (Reader in = new InputStreamReader(new FileInputStream(file), StandardCharsets.ISO_8859_1)) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor (TransportsConfiguration.class, TransportsConfiguration.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); transportsConfiguration = yaml.loadAs(in, TransportsConfiguration.class); } catch (IOException e) { throw new RuntimeException( "Error while loading " + configFileLocation + " configuration file", e); } } else { // return a default config log.warn("Netty transport configuration file not found in: " + configFileLocation + " ,hence using default configuration"); transportsConfiguration = TransportsConfiguration.getDefault(); } return transportsConfiguration; }
public static TransportsConfiguration getConfiguration(String configFileLocation) { TransportsConfiguration transportsConfiguration; File file = new File(TestUtil.class.getResource(configFileLocation).getFile()); if (file.exists()) { try (Reader in = new InputStreamReader(new FileInputStream(file), StandardCharsets.ISO_8859_1)) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor (TransportsConfiguration.class, TransportsConfiguration.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); transportsConfiguration = yaml.loadAs(in, TransportsConfiguration.class); } catch (IOException e) { throw new RuntimeException( "Error while loading " + configFileLocation + " configuration file", e); } } else { // return a default config log.warn("Netty transport configuration file not found in: " + configFileLocation + " ,hence using default configuration"); transportsConfiguration = TransportsConfiguration.getDefault(); } return transportsConfiguration; }
@Override protected Map<String, Property> getPropertiesMap(Class<?> type, BeanAccess bAccess) throws IntrospectionException { Map<String, Property> newPropertyMap = new LinkedHashMap<>(); Map<String, Property> propertiesMap = super.getPropertiesMap(type, bAccess); for (Iterator<Map.Entry<String, Property>> iterator = propertiesMap.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<String, Property> entry = iterator.next(); boolean updated = false; if (entry.getValue() instanceof FieldProperty) { try { Field field = type.getDeclaredField(entry.getValue().getName()); Path path = field.getAnnotation(Path.class); if (path != null) { newPropertyMap.put(path.value(), new CustomNameFieldProperty(field, path.value())); updated = true; } } catch (NoSuchFieldException ignored) { } } if (!updated) { newPropertyMap.put(entry.getKey(), entry.getValue()); } } return newPropertyMap; }
@Override public Property getProperty(Class<?> type, String name, BeanAccess bAccess) throws IntrospectionException { if (bAccess != BeanAccess.FIELD) return super.getProperty(type, name, bAccess); Map<String, Property> properties = getPropertiesMap(type, bAccess); Property property = properties.get(Helper.toLowerCase(name)); if (property == null) { // Check if property was missing and notify user if necessary if (type != UnknownConf.class) core.getLogger().log(WARN, "Unknown configuration property: %s @ %s", name, type.getSimpleName()); return new OutdatedMissingProperty(name); } if (!property.isWritable()) // Throw exception from super method throw new YAMLException("Unable to find writable property '" + name + "' on class: " + type.getName()); return property; }
public void save() throws IOException { FileWriter writer = new FileWriter(getConfigFile()); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); options.setIndent(4); Representer representer = new Representer(); representer.getPropertyUtils().setBeanAccess(BeanAccess.DEFAULT); new Yaml(options).dump(this, writer); }
public static Yaml newYaml() { Representer representer = new Representer(); PropertyUtils propertyUtils = representer.getPropertyUtils(); propertyUtils.setSkipMissingProperties(true); propertyUtils.setBeanAccess(BeanAccess.FIELD); propertyUtils.setAllowReadOnlyProperties(true); return new Yaml(representer); }
@Override protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) throws IntrospectionException { if (Federations.class.equals(type)) { // Serializing on Field order for Federations. Set<Property> fieldOrderedProperties = new LinkedHashSet<>(getPropertiesMap(type, BeanAccess.FIELD).values()); Set<Property> defaultOrderPropertySet = super.createPropertySet(type, bAccess); Set<Property> filtered = Sets.difference(fieldOrderedProperties, defaultOrderPropertySet); fieldOrderedProperties.removeAll(filtered); return fieldOrderedProperties; } else { return super.createPropertySet(type, bAccess); } }
@Override protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess beanAccess) throws IntrospectionException { Set<Property> properties = new TreeSet<>(); Collection<Property> props = getPropertiesMap(type, beanAccess).values(); for (Property property : props) { if (include(property)) { properties.add(property); } } return properties; }
@Test public void createPropertySetWithFieldBeanAccess() throws Exception { Set<Property> properties = properyUtils.createPropertySet(TestBean.class, BeanAccess.FIELD); assertThat(properties.size(), is(2)); Iterator<Property> iterator = properties.iterator(); assertThat(iterator.next().getName(), is("longPropertyName")); assertThat(iterator.next().getName(), is("transientProperty")); }
private static Yaml getYaml() { Representer representer = new TapeRepresenter(); representer.addClassTag(YamlTape.class, YamlTape.TAPE_TAG); Constructor constructor = new TapeConstructor(); constructor.addTypeDescription(new TypeDescription(YamlTape.class, YamlTape.TAPE_TAG)); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(BLOCK); dumperOptions.setWidth(256); Yaml yaml = new Yaml(constructor, representer, dumperOptions); yaml.setBeanAccess(BeanAccess.FIELD); return yaml; }
public void testYaml() { Yaml beanLoader = new Yaml(); beanLoader.setBeanAccess(BeanAccess.FIELD); BlogBean rehydrated = (BlogBean) beanLoader.loadAs( Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class); assertEquals(4, rehydrated.getPosts().size()); }
public void testYaml() { Blog original = createTestBlog(); Yaml yamlDumper = constructYamlDumper(); String serialized = yamlDumper.dumpAsMap(original); // System.out.println(serialized); assertEquals(Util.getLocalResource("issues/issue55_1.txt"), serialized); Yaml blogLoader = new Yaml(); blogLoader.setBeanAccess(BeanAccess.FIELD); Blog rehydrated = blogLoader.loadAs(serialized, Blog.class); checkTestBlog(rehydrated); }
public void testYamlDefaultWithFeildAccess() { Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); Blog original = createTestBlog(); String serialized = yaml.dump(original); assertEquals(Util.getLocalResource("issues/issue55_1_rootTag.txt"), serialized); Blog rehydrated = (Blog) yaml.load(serialized); checkTestBlog(rehydrated); }
public void testYaml() { Yaml beanLoader = new Yaml(); beanLoader.setBeanAccess(BeanAccess.FIELD); BlogField rehydrated = beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogField.class); assertEquals(4, rehydrated.getPosts().size()); }
public void testLoad() { Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); String doc = Util.getLocalResource("issues/issue73-1.txt"); Blog blog = (Blog) yaml.load(doc); // System.out.println(blog); assertEquals("Test Me!", blog.getName()); assertEquals(2, blog.numbers.size()); assertEquals(2, blog.getPosts().size()); for (Post post : blog.getPosts()) { assertEquals(Post.class, post.getClass()); } }
public void testYaml() { String serialized = Util.getLocalResource("issues/issue73-2.txt"); // System.out.println(serialized); Yaml beanLoader = new Yaml(); beanLoader.setBeanAccess(BeanAccess.FIELD); Blog rehydrated = beanLoader.loadAs(serialized, Blog.class); checkTestBlog(rehydrated); }
private void check(String doc) { Yaml yamlLoader = new Yaml(); yamlLoader.setBeanAccess(BeanAccess.FIELD); Blog blog = (Blog) yamlLoader.load(doc); assertEquals("Test Me!", blog.getName()); assertEquals(2, blog.numbers.size()); assertEquals(2, blog.getPosts().size()); for (Post post : blog.getPosts()) { assertEquals(Post.class, post.getClass()); } }
public void testArrayAsListValue() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); B data = createB(); String dump = yaml2dump.dump(data); // System.out.println(dump); Yaml yaml2load = new Yaml(); yaml2load.setBeanAccess(BeanAccess.FIELD); B loaded = (B) yaml2load.load(dump); Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); }
public void testNoTags() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); B data = createB(); String dump = yaml2dump.dumpAs(data, Tag.MAP, FlowStyle.AUTO); // System.out.println(dump); assertEquals("meta:\n- [whatever]\n- [something, something else]\n", dump); // Constructor constr = new Constructor(B.class); Yaml yaml2load = new Yaml(constr); yaml2load.setBeanAccess(BeanAccess.FIELD); B loaded = (B) yaml2load.load(dump); Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); }
public static <T> T readConfigFile(Path file, Class<T> classType) throws IdentityRecoveryException { try (InputStreamReader inputStreamReader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) { CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(FileUtil.class.getClassLoader()); Yaml yaml = new Yaml(constructor); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(inputStreamReader, classType); } catch (IOException e) { throw new IdentityRecoveryException( String.format("Error in reading file %s", file.toString()), e); } }
private void writeTreeToPlan(ArtifactDescriptor artifact, File artifactRoot) { try (FileWriter writer = new FileWriter(artifactRoot)) { Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); yaml.dump(artifact, writer); writer.flush(); } catch (IOException e) { // Something went wrong, just delete the plan file artifactRoot.delete(); } }
@Override public String format(ServiceBrokerProperties serviceBrokerProperties, String... strings) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); final SkipNullEmptyRepresenter representer = new SkipNullEmptyRepresenter(); representer.addClassTag(ServiceBrokerProperties.class, Tag.MAP); representer.addClassTag(SharedVolumeDevice.class, Tag.MAP); Yaml yaml = new Yaml(representer, options); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.dump(serviceBrokerProperties); }
@PostConstruct public void init() { String configUrl = System.getProperty("config.url"); String configBranch = System.getProperty("config.branch"); String configUser = System.getProperty("config.user"); String configPassword = System.getProperty("config.password"); userhome = System.getProperty("user.home"); if (!StringUtils.isEmpty(configUrl) && !StringUtils.isEmpty(configBranch) && !StringUtils.isEmpty(configUser) && !StringUtils.isEmpty(configPassword)) { model = new GitModel("", configUrl, configBranch, configUser, configPassword); } else { File file = new File(userhome + "/.releasetrain/gitConfig.yaml"); String str = ""; if (file.exists()) { try { str = FileUtils.readFileToString(file); } catch (IOException e) { log.error(e.getMessage(), e); } Yaml yaml = new Yaml(); yaml.setBeanAccess(BeanAccess.FIELD); model = (GitModel) yaml.load(str); } else { model = new GitModel("", "", "", "", ""); } } writeModel(); reset(); }
@NotNull private static Yaml createYaml() { final DumperOptions options = new DumperOptions(); options.setLineBreak(DumperOptions.LineBreak.UNIX); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); final Yaml yaml = new Yaml(new YamlConstructor(), new YamlRepresenter(), options); yaml.setBeanAccess(BeanAccess.FIELD); return yaml; }