@OnEnabled public void onEnabled(final ConfigurationContext context) throws InitializationException { final String config = context.getProperty(CONFIGURATION_FILE).evaluateAttributeExpressions().getValue(); final FileBasedBuilderParameters params = new Parameters().fileBased().setFile(new File(config)); this.builder = new ReloadingFileBasedConfigurationBuilder<>(resultClass).configure(params); builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, new EventListener<ConfigurationBuilderEvent>() { @Override public void onEvent(ConfigurationBuilderEvent event) { if (builder.getReloadingController().checkForReloading(null)) { getLogger().debug("Reloading " + config); } } }); try { // Try getting configuration to see if there is any issue, for example wrong file format. // Then throw InitializationException to keep this service in 'Enabling' state. builder.getConfiguration(); } catch (ConfigurationException e) { throw new InitializationException(e); } }
@Override public void onEvent(ConfigurationBuilderEvent event) { if (builder.getReloadingController().checkForReloading(null)) { getLogger().debug("Reloading " + config); } } });
/** * The singleton map os not entirely awesome, but this allows our legacy code to * benefit from live reloads for configuration while the application is running * * @param configFile the path to the config file which should be loaded * @return configuration file */ public static INIConfiguration parseConfig(String configFile) { if (!MAP.containsKey(configFile)) { ReloadingFileBasedConfigurationBuilder<INIConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>( INIConfiguration.class).configure(new Parameters().properties().setFileName(configFile)); PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, 1, TimeUnit.MINUTES); trigger.start(); MAP.put(configFile, builder); } try { return MAP.get(configFile).getConfiguration(); } catch (ConfigurationException ex) { throw new RuntimeException("Could not read " + configFile); } }
/** * {@inheritDoc} This implementation creates a builder for XML * configurations with reloading support. */ @Override protected ConfigurationBuilder<? extends HierarchicalConfiguration<?>> createXMLDefinitionBuilder( final BuilderParameters builderParams) { return new ReloadingFileBasedConfigurationBuilder<>( XMLConfiguration.class).configure(builderParams); }
/** * {@inheritDoc} This implementation returns a file-based configuration * builder with reloading support. */ @Override protected FileBasedConfigurationBuilder<T> createManagedBuilder( final String fileName, final Map<String, Object> params) throws ConfigurationException { return new ReloadingFileBasedConfigurationBuilder<>(getResultClass(), params, isAllowFailOnInit()); }
/** * Creates the {@code ReloadingController} associated with this object. The * controller is assigned a specialized reloading detector which delegates * to the detector for the current result object. ( * {@code FileHandlerReloadingDetector} does not support changing the file * handler, and {@code ReloadingController} does not support changing the * reloading detector; therefore, this level of indirection is needed to * change the monitored file dynamically.) * * @return the new {@code ReloadingController} */ private ReloadingController createReloadingController() { final ReloadingDetector ctrlDetector = createReloadingDetectorForController(); final ReloadingController ctrl = new ReloadingController(ctrlDetector); connectToReloadingController(ctrl); return ctrl; }
/** * {@inheritDoc} This implementation also takes care that a new * {@code ReloadingDetector} for the new current {@code FileHandler} is * created. Also, the reloading controller's reloading state has to be * reset; after the creation of a new result object changes in the * underlying configuration source have to be monitored again. */ @Override protected void initFileHandler(final FileHandler handler) throws ConfigurationException { super.initFileHandler(handler); resultReloadingDetector = createReloadingDetector(handler, FileBasedBuilderParametersImpl.fromParameters( getParameters(), true)); }
/** * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} * which produces result objects of the specified class. * * @param resCls the result class (must not be <b>null</b> * @throws IllegalArgumentException if the result class is <b>null</b> */ public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls) { super(resCls); reloadingController = createReloadingController(); }
ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class); builder.configure(params); cfgReloadTrigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, refDelay, TimeUnit.MILLISECONDS); config = builder; } else {
/** * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} * which produces result objects of the specified class and sets * initialization parameters. * * @param resCls the result class (must not be <b>null</b> * @param params a map with initialization parameters * @throws IllegalArgumentException if the result class is <b>null</b> */ public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params) { super(resCls, params); reloadingController = createReloadingController(); }
/** * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} * which produces result objects of the specified class and sets * initialization parameters and the <em>allowFailOnInit</em> flag. * * @param resCls the result class (must not be <b>null</b> * @param params a map with initialization parameters * @param allowFailOnInit the <em>allowFailOnInit</em> flag * @throws IllegalArgumentException if the result class is <b>null</b> */ public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params, final boolean allowFailOnInit) { super(resCls, params, allowFailOnInit); reloadingController = createReloadingController(); }