org.ow2.petals.binding.rest.utils.BooleanWithPlaceholder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of petals-bc-rest Show documentation
Show all versions of petals-bc-rest Show documentation
petals-bc-rest Binding Component description
/**
* Copyright (c) 2017-2018 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.binding.rest.utils;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.ebmwebsourcing.easycommons.properties.PropertiesException;
import com.ebmwebsourcing.easycommons.properties.PropertiesHelper;
/**
* A {@link String} defined with placeholders
*
* @author Christophe DENEUX - Linagora
*
*/
// TODO Should be moved into Petals CDK
public class BooleanWithPlaceholder {
/**
* The initial definition of the boolean value using placeholders
*/
private final String initialDefinition;
/**
* The custom place holders read from the component property file to be able to configure service units.
*/
private final Properties componentPlaceholders;
private final Object lock = new Object();
private final Logger logger;
/**
* The current boolean value with placeholders replaced by their values
*/
private boolean currentValue;
/**
* @param initialString
* The boolean definition using placeholders. Not {@code null} and not empty.
* @param componentPlaceholders
* The custom place holders read from the component property file. Not {@code null}.
* @param logger
*
*/
public BooleanWithPlaceholder(final String initialString, final Properties componentPlaceholders,
final Logger logger) {
assert initialString != null;
assert !initialString.trim().isEmpty();
assert componentPlaceholders != null;
assert logger != null;
this.initialDefinition = initialString;
this.componentPlaceholders = componentPlaceholders;
this.logger = logger;
this.resolveValue();
}
/**
* Replace the place holders by their values.
*/
private void resolveValue() {
synchronized (this.lock) {
try {
this.currentValue = Boolean.parseBoolean(
PropertiesHelper.resolveString(this.initialDefinition, this.componentPlaceholders));
} catch (final PropertiesException e) {
this.logger.log(Level.WARNING,
String.format(
"An error occurs reloading placeholder for '%s'. The boolean value returned can be not the expected one. Try to reload placeholders again.",
this.initialDefinition),
e);
}
}
}
/**
* @return The initial definition of the boolean value using placeholders.
*/
public String getInitialDefinition() {
return this.initialDefinition;
}
/**
* @return The current boolean value with placeholders replaced by their values.
*/
public boolean getValue() {
synchronized (this.lock) {
return this.currentValue;
}
}
/**
* Update the current boolean value with the current placeholders values.
*/
public void onPlaceHolderValuesReloaded() {
this.resolveValue();
}
}