Search in sources :

Example 1 with OkaeriConfig

use of eu.okaeri.configs.OkaeriConfig in project okaeri-platform by OkaeriPoland.

the class ConfigurationComponentResolver method make.

@Override
@SuppressWarnings("unchecked")
public Object make(@NonNull ComponentCreator creator, @NonNull BeanManifest manifest, @NonNull Injector injector) {
    if (!OkaeriConfig.class.isAssignableFrom(manifest.getType())) {
        throw new IllegalArgumentException("Component of @Configuration on type requires class to be a OkaeriConfig: " + manifest);
    }
    long start = System.currentTimeMillis();
    Class<? extends OkaeriConfig> configType = (Class<? extends OkaeriConfig>) manifest.getType();
    Messages messages = configType.getAnnotation(Messages.class);
    if (messages != null) {
        throw new IllegalArgumentException("Cannot register @Messages with raw OkaeriConfig type, use LocaleConfig: " + configType);
    }
    Configuration configuration = configType.getAnnotation(Configuration.class);
    String path = configuration.path();
    boolean defaultNotNull = configuration.defaultNotNull();
    Class<? extends Configurer> provider = configuration.provider();
    try {
        Configurer configurer = (provider == Configuration.DEFAULT.class) ? this.defaultConfigurerProvider.get() : injector.createInstance(provider);
        OkaeriSerdesPack[] serdesPacks = Stream.concat(Stream.of(this.defaultConfigurerSerdes), Arrays.stream(configuration.serdes())).map(injector::createInstance).distinct().toArray(OkaeriSerdesPack[]::new);
        String extension = configurer.getExtensions().isEmpty() ? "bin" : configurer.getExtensions().get(0);
        String resolvedPath = path.replace("{ext}", extension);
        OkaeriConfig config = ConfigManager.create(configType, (it) -> {
            it.withBindFile(new File(this.dataFolder, resolvedPath));
            it.withConfigurer(new OkaeriValidator(configurer, defaultNotNull), serdesPacks);
            it.saveDefaults();
            it.load(true);
        });
        long took = System.currentTimeMillis() - start;
        creator.log(ComponentHelper.buildComponentMessage().type("Loaded configuration").name(configType.getSimpleName()).took(took).meta("path", path).meta("provider", provider.getSimpleName()).build());
        creator.increaseStatistics("configs", 1);
        return config;
    } catch (Exception exception) {
        throw new RuntimeException("Configuration load failure", exception);
    }
}
Also used : Messages(eu.okaeri.platform.core.annotation.Messages) Configuration(eu.okaeri.platform.core.annotation.Configuration) OkaeriConfig(eu.okaeri.configs.OkaeriConfig) OkaeriSerdesPack(eu.okaeri.configs.serdes.OkaeriSerdesPack) Configurer(eu.okaeri.configs.configurer.Configurer) OkaeriValidator(eu.okaeri.configs.validator.okaeri.OkaeriValidator) File(java.io.File)

Aggregations

OkaeriConfig (eu.okaeri.configs.OkaeriConfig)1 Configurer (eu.okaeri.configs.configurer.Configurer)1 OkaeriSerdesPack (eu.okaeri.configs.serdes.OkaeriSerdesPack)1 OkaeriValidator (eu.okaeri.configs.validator.okaeri.OkaeriValidator)1 Configuration (eu.okaeri.platform.core.annotation.Configuration)1 Messages (eu.okaeri.platform.core.annotation.Messages)1 File (java.io.File)1