/** * Load the specified InputStream as Yaml. * * @param classloader The classloader to use to instantiate Java objects. */ public static Object load(InputStream is, ClassLoader classloader) { org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new CustomClassLoaderConstructor(classloader)); return yaml.load(is); }
/** * Load the specified InputStream as Yaml. * * @param classloader The classloader to use to instantiate Java objects. */ public static Object load(InputStream is, ClassLoader classloader) { org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new CustomClassLoaderConstructor(classloader)); return yaml.load(is); }
private static IdPClientConfiguration readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(IdPClientConfiguration.class, IdPClientConfiguration.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, IdPClientConfiguration.class); }
private RDBMSDataProviderConfBean readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(RDBMSDataProviderConfBean.class, RDBMSDataProviderConfBean.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, RDBMSDataProviderConfBean.class); } }
private BusinessRulesQueryConfigs readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(BusinessRulesQueryConfigs.class, BusinessRulesQueryConfigs.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, BusinessRulesQueryConfigs.class); } }
private PersistenceStoreConfigs readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(PersistenceStoreConfigs.class, PersistenceStoreConfigs.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, PersistenceStoreConfigs.class); } }
private StrategyConfig readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(StrategyConfig.class, StrategyConfig.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, StrategyConfig.class); } }
private DeploymentConfig readYamlContent(InputStream yamlContent) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(DeploymentConfig.class, DeploymentConfig.class.getClassLoader())); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlContent, DeploymentConfig.class); } }
public static <T> T loadYamlFromResource(String resourcePath, Class<T> yamlClazz) { Yaml yaml = null; yaml = new Yaml(new CustomClassLoaderConstructor(yamlClazz.getClassLoader())); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath); return yaml.loadAs(in, yamlClazz); }
/** * Returns the configuration object for the given YAML string. * * @param configClass returning configuration object type * @param classLoader class loader of the configuration object * @param yamlString YAML string * @param <T> type of the returning configuration object * @return configuration object for the given YAML string */ private <T> T getConfigurationObject(Class<T> configClass, ClassLoader classLoader, String yamlString) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(configClass, classLoader)); yaml.setBeanAccess(BeanAccess.FIELD); return yaml.loadAs(yamlString, configClass); }
/** * Load and parse a plain YAML file and returns the corresponding Java Map. The YAML parser used is SnakeYAML * (http://code.google.com/p/snakeyaml/) * * @param name * Name of a YAML file somewhere in the classpath (or conf/)me * @param clazz * the expected class * @param <T> * The entity load * @return Object representing the YAML data */ @SuppressWarnings("unchecked") public static <T> T loadYaml(String name, Class<T> clazz) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(clazz, Play.classloader)); yaml.setBeanAccess(BeanAccess.FIELD); return (T) loadYaml(name, yaml); }
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); } }
/** * 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: {} ,hence using default configuration", configFileLocation); transportsConfiguration = new TransportsConfiguration(); } return transportsConfiguration; } }
protected BaseConfigMapper() { DumperOptions yamlOptions = new DumperOptions(); yamlOptions.setIndent( 2 ); yamlOptions.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK ); this.yamlRepresenter.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK ); this.yaml = new Yaml( new CustomClassLoaderConstructor( BaseConfigMapper.class.getClassLoader() ), this.yamlRepresenter, yamlOptions ); /* Configure the settings for serializing via the annotations present. */ configureFromSerializeOptionsAnnotation(); }
/** * 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; } }
CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(MyClass.class, MyController.class.getClassLoader()); constructor.addTypeDescription(new TypeDescription(MyClass.class));
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() })); } } }
/** * 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())); } }
private static MetaData loadMetaData(InputStream in) throws IOException { Constructor constructor = new CustomClassLoaderConstructor(Thread.currentThread().getContextClassLoader()); MetaData metaData = (MetaData) new Yaml(constructor).load(in); if (metaData == null) { throw new IOException("Can't load metadata from input stream"); } // Make sure that the loaded metadata contains default values: assignDefaults(metaData); // Remove leading slashes from all the action names: for (Action action : metaData.getActions()) { String name = action.getName(); name = name.replaceAll("^/?", ""); action.setName(name); } return metaData; }
/** * 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 transportsConfiguration = new TransportsConfiguration(); } return transportsConfiguration; }