/** * 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() })); } }
public Map<String, Object> loadFromYaml(){ InputStream stream = getClass().getClassLoader().getResourceAsStream(YML_CONFIG_LOCATION); if(stream == null) { logger.warn("Configuration file not found. Empty config loaded."); return new HashMap<>(); } else { try { String yamlContent = IOUtils.toString(stream, Charset.defaultCharset()); CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor( Thread.currentThread().getContextClassLoader()); Yaml yaml = new Yaml(constr); Map<String, Object> map = (Map<String, Object>) yaml.load(yamlContent); logger.info("Loaded config file"); return map; } catch (IOException e) { throw new ServiceConfigurationException(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: " + 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; }
@SuppressWarnings("unchecked") protected void configureFromInputStream(InputStream is) { Yaml yaml = new Yaml(new CustomClassLoaderConstructor(getClass().getClassLoader())); Map<String, Object> document = (Map<String, Object>) yaml.load(is); if (document == null || !document.containsKey(HANDLER_MAPPINGS_KEY)) { throw new IllegalArgumentException(String.format("Expected yaml document with key: %s", HANDLER_MAPPINGS_KEY)); } else { Map<String, SaltReturnHandler> handlers = (Map<String, SaltReturnHandler>) document .get(HANDLER_MAPPINGS_KEY); for (Map.Entry<String, SaltReturnHandler> entry : handlers.entrySet()) { if (handlerMap.containsKey(entry.getKey())) { throw new IllegalStateException(String.format( "Already received a salt return handler configuration entry for %s", entry.getKey())); } handlerMap.put(entry.getKey(), entry.getValue()); } } }
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); } }
public static Info load(URL directory) { CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor(Info.class.getClassLoader()); Yaml yaml = new Yaml(constr); Object obj; URL infoFile = newURL(directory,FILENAME_INFO); try (InputStream is = infoFile.openStream()) { obj = yaml.loadAs(new InputStreamReader(is,"UTF-8"),Info.class); } catch (Exception ex) { throw new GateRuntimeException("Could not load info file "+infoFile,ex); } Info info = (Info)obj; return info; }
protected Yaml initialValue() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(getClass().getClassLoader()); PropertyUtils propertyUtils = constructor.getPropertyUtils(); propertyUtils.setSkipMissingProperties(true); constructor.setPropertyUtils(propertyUtils); return new Yaml(constructor, new Representer(), options); }
/** * 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 * @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); }
/** * Parses the configuration file and produces an object of the specified * class. * * @param <T> * Class of the object specified by the {@code clazz} parameter * @param clazz * class of the object to be created * @return the object created from the configuration file * @throws IOException * if an I/O error occurs opening the configuration file */ public <T> T loadAs(Class<T> clazz) throws IOException { CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor( clazz.getClassLoader()); Yaml yaml = new Yaml(constr); try (BufferedReader r = Files.newBufferedReader(path)) { return yaml.loadAs(r, clazz); } }