Java 类org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor 实例源码

项目:carbon-identity-mgt    文件:FileUtil.java   
/**
 * 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()));
    }
}
项目:carbon-identity-mgt    文件:FileUtil.java   
/**
 * 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;
}
项目:carbon-identity-mgt    文件:FileUtil.java   
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() }));
    }
}
项目:warp    文件:ConfigLoader.java   
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);
        }

    }
}
项目:carbon-transports    文件:ConfigurationBuilder.java   
/**
 * 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;
}
项目:carbon-transports    文件:TestUtil.java   
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;
}
项目:salt-step    文件:SaltReturnHandlerRegistry.java   
@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());
        }
    }
}
项目:carbon-identity-mgt    文件:FileUtil.java   
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);
        }
    }
项目:gateplugin-LearningFramework    文件:Info.java   
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;
}
项目:FreeBungeeChat    文件:CustomClassLoaderYamlConfiguration.java   
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);
}
项目:restcommander    文件:Fixtures.java   
/**
 * 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);
}
项目:sfera    文件:Configuration.java   
/**
 * 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);
    }
}