Search in sources :

Example 6 with PropertyFile

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);
    }
}
Also used : IOException(java.io.IOException) PropertyFile(com.peterphi.std.io.PropertyFile) PropertyFile(com.peterphi.std.io.PropertyFile) File(java.io.File)

Example 7 with PropertyFile

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;
}
Also used : ArrayList(java.util.ArrayList) PropertyFile(com.peterphi.std.io.PropertyFile) URL(java.net.URL)

Example 8 with PropertyFile

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");
    }
}
Also used : PropertyFile(com.peterphi.std.io.PropertyFile)

Example 9 with PropertyFile

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;
}
Also used : Matchers(com.google.inject.matcher.Matchers) StringUtils(org.apache.commons.lang.StringUtils) Session(org.hibernate.Session) PropertyFile(com.peterphi.std.io.PropertyFile) HibernateObservingInterceptor(com.peterphi.std.guice.hibernate.module.logging.HibernateObservingInterceptor) Transaction(org.hibernate.Transaction) GuiceConfig(com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig) Logger(org.apache.log4j.Logger) SampleCountUserType(com.peterphi.std.guice.hibernate.usertype.SampleCountUserType) DateUserType(com.peterphi.std.guice.hibernate.usertype.DateUserType) Configuration(org.hibernate.cfg.Configuration) JodaLocalDateUserType(com.peterphi.std.guice.hibernate.usertype.JodaLocalDateUserType) HibernateConfigurationValidator(com.peterphi.std.guice.hibernate.module.ext.HibernateConfigurationValidator) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Iterator(java.util.Iterator) SessionFactory(org.hibernate.SessionFactory) Transactional(com.peterphi.std.guice.database.annotation.Transactional) ServiceLoader(java.util.ServiceLoader) ServiceRegistry(org.hibernate.service.ServiceRegistry) JodaDateTimeUserType(com.peterphi.std.guice.hibernate.usertype.JodaDateTimeUserType) UserType(org.hibernate.usertype.UserType) Provides(com.google.inject.Provides) GuiceProperties(com.peterphi.std.guice.apploader.GuiceProperties) Named(com.google.inject.name.Named) TimecodeUserType(com.peterphi.std.guice.hibernate.usertype.TimecodeUserType) AbstractModule(com.google.inject.AbstractModule) Singleton(com.google.inject.Singleton) GuiceConfig(com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig) Properties(java.util.Properties) GuiceProperties(com.peterphi.std.guice.apploader.GuiceProperties) PropertyFile(com.peterphi.std.io.PropertyFile)

Example 10 with PropertyFile

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));
}
Also used : ServicePropertiesModule(com.peterphi.std.guice.common.serviceprops.ServicePropertiesModule) Injector(com.google.inject.Injector) Timeout(com.peterphi.std.threading.Timeout) PropertyFile(com.peterphi.std.io.PropertyFile) Test(org.junit.Test)

Aggregations

PropertyFile (com.peterphi.std.io.PropertyFile)11 File (java.io.File)3 GuiceProperties (com.peterphi.std.guice.apploader.GuiceProperties)2 GuiceRole (com.peterphi.std.guice.apploader.GuiceRole)2 ClassScannerFactory (com.peterphi.std.guice.common.ClassScannerFactory)2 GuiceConfig (com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 Matchers (com.google.inject.matcher.Matchers)1 Named (com.google.inject.name.Named)1 BasicSetup (com.peterphi.std.guice.apploader.BasicSetup)1 GuiceSetup (com.peterphi.std.guice.apploader.GuiceSetup)1 GuiceBuilder (com.peterphi.std.guice.apploader.impl.GuiceBuilder)1