use of com.peterphi.std.io.PropertyFile in project stdlib by petergeneric.
the class GuiceFactory method load.
private static PropertyFile load(String propertyFile) {
if (propertyFile == null)
return null;
try {
final File file = new File(propertyFile);
if (file.exists())
return new PropertyFile(file);
else {
PropertyFile props = new PropertyFile();
props.setFile(file);
return props;
}
} catch (IOException e) {
throw new IllegalArgumentException("Failed to load property file: " + propertyFile, e);
}
}
use of com.peterphi.std.io.PropertyFile in project stdlib by petergeneric.
the class GuiceFactory method loadConfig.
static List<PropertyFile> loadConfig(final ClassLoader loader, String name) throws IOException {
log.trace("Search for config files with name: " + name);
final List<PropertyFile> configs = new ArrayList<>();
final Enumeration<URL> urls = loader.getResources(name);
while (urls.hasMoreElements()) {
final URL url = urls.nextElement();
log.debug("Loading property file: " + url);
configs.add(new PropertyFile(url));
}
return configs;
}
use of com.peterphi.std.io.PropertyFile in project stdlib by petergeneric.
the class Log4JModule method reconfigure.
private static void reconfigure(final GuiceConfig guice) {
final PropertyFile config = getProperties(guice);
if (config != null) {
// reset any existing log config
LogManager.resetConfiguration();
// apply the specified properties
PropertyConfigurator.configure(config.toProperties());
} else {
log.debug("Leaving logging subsystem to initialise itself");
}
}
use of com.peterphi.std.io.PropertyFile in project stdlib by petergeneric.
the class HibernateModule method extractHibernateProperties.
private Properties extractHibernateProperties(final GuiceConfig guiceConfig, @Named(GuiceProperties.HIBERNATE_PROPERTIES) final String propertyFileName) {
final Properties properties;
if (PROPFILE_VAL_EMBEDDED.equals(propertyFileName)) {
// Extract all properties starting with "hibernate." and "liquibase."
properties = guiceConfig.toProperties(k -> k.startsWith("hibernate.") || k.startsWith("liquibase."));
} else {
if (StringUtils.contains(propertyFileName, '\n')) {
log.debug("Assuming hibernate.properties contains literal hibernate.properties file, not a resource/file reference");
properties = PropertyFile.fromString(propertyFileName).toProperties();
} else {
PropertyFile[] files = PropertyFile.findAll(propertyFileName);
if (files == null || files.length == 0) {
throw new IllegalArgumentException("Cannot find any property files called: " + propertyFileName);
} else {
// Merge all hibernate property files into a single file
PropertyFile file = PropertyFile.readOnlyUnion(files);
// Now Merge all the values and interpret them via the guice config to allow for interpolation of variables
GuiceConfig temp = new GuiceConfig();
temp.setAll(guiceConfig);
temp.setAll(file);
// Now extract the hibernate properties again with any variables
properties = temp.toProperties(key -> file.containsKey(key));
}
}
}
return properties;
}
use of com.peterphi.std.io.PropertyFile in project stdlib by petergeneric.
the class TimeoutConverterTest method test.
@Test
public void test() {
PropertyFile props = new PropertyFile();
props.set("timeout1msNoUnit", "1");
props.set("timeout1ms", "1ms");
props.set("timeout1s", "1s");
props.set("timeout1h", "1h");
props.set("timeout60m", "60m");
props.set("timeout50h", "50h");
final Injector injector = Guice.createInjector(new ServicePropertiesModule(props));
injector.injectMembers(this);
// extract values
assertEquals(new Timeout(1, TimeUnit.MILLISECONDS), timeout1msNoUnit);
assertEquals(new Timeout(1, TimeUnit.MILLISECONDS), timeout1ms);
assertEquals(new Timeout(1, TimeUnit.SECONDS), timeout1s);
assertEquals(new Timeout(60, TimeUnit.MINUTES), timeout60m);
assertEquals(new Timeout(1, TimeUnit.HOURS), timeout1h);
// compare against one another
assertEquals(timeout60m, timeout1h);
assertEquals(timeout1msNoUnit, timeout1ms);
assertFalse(timeout50h.equals(timeout1h));
}
Aggregations