/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.jdbc.config; /** * Represents a Connection configuration property. *
* A property has an associated raw value and a parsed value. * The raw value is any Object that is provided by a user when setting * the property, while the parsed value is the effective value computed * from the raw value. *
* The raw value and parsed value need not have similar instance types * since properties may accept a varied set of input raw values to compute * the parsed value e.g a String "true" or "false" may be accepted to * compute a Boolean property. *
* During Connection initialization, all defined connection properties
* are expected to be initialized with the value provided by the user or
* a null if no value was provided by the user. Each property defines
* its own behavior of how it gets initialized.
*
* @param
* This function is meant to offload any value pre-processing
* like trimming of String input values prior to the value being
* used in the parseValue function.
*
* Currently, the only pre-processing applied is whitespace trimming of
* the input in case the raw input is a String. Subclass properties may
* override this method to modify or extend the default pre-processing
* of their raw input values.
*
* @param value - The raw value provided for the property
*
* @return The value that should be used by the parseValue function
*/
protected Object preProcess(Object value) {
if (value instanceof String) {
return ((String) value).trim();
} else {
return value;
}
}
/**
* Given a raw value for a property, the method returns the actual
* value that the property should be set to.
*
* @param rawValue raw property input value
*
* @return the actual value the property should be set to
*
* @throws ConnectionPropertyException if the raw input value can not
* be parsed or fails validation constraints applicable on the
* property value.
*/
protected abstract T parseValue(Object rawValue) throws ConnectionPropertyException;
/**
* Execute parsing of the raw value
*
* @throws ConnectionPropertyException if the raw input value can not
* be parsed or fails validation constraints applicable on the
* property value.
*/
private void parse() throws ConnectionPropertyException {
if (!parsed) {
this.parsedValue = parseValue(preProcess(rawValue));
parsed = true;
}
}
/**
* Verify if the supplied value for this property was successfully
* parsed.
*
* @throws {@link IllegalStateException} if a valid property value
* has not been provided.
*/
private void verifyParsed() {
if (!isParsed()) {
throw new IllegalStateException(String.format("Property %s is not yet successfully parsed.", getKey()));
}
}
}