All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datadoghq.trace.resolver.FactoryUtils Maven / Gradle / Ivy

There is a newer version: 0.2.12
Show newest version
package com.datadoghq.trace.resolver;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class FactoryUtils {
	private final static Logger logger = LoggerFactory.getLogger(FactoryUtils.class);

	private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
	
	public static  A loadConfigFromResource(String resourceName, Class targetClass){
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		A config = null;
		try {
			Enumeration iter = classLoader.getResources(resourceName);
			while (iter.hasMoreElements()) {
				config = objectMapper.readValue(iter.nextElement().openStream(), targetClass);

				break; // ONLY the closest resource file is taken into account
			}
		} catch (IOException e) {
			logger.warn("Could not load configuration file {}.", resourceName);
			logger.error("Error when loading config file", e);
		}
		return config;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy