Missing factoryutils

This commit is contained in:
renaudboutet 2017-05-29 17:33:06 +02:00
parent 29d2ab8aca
commit d20fd7788c
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
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> A loadConfigFromResource(String resourceName, Class<A> targetClass){
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
A config = null;
try {
Enumeration<URL> 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;
}
}