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);
}
}
Aggregations