Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bookkeeper.common.conf;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import lombok.Builder.Default;
import org.apache.bookkeeper.common.annotation.InterfaceAudience.Public;
import org.apache.bookkeeper.common.conf.validators.NullValidator;
import org.apache.bookkeeper.common.util.ReflectionUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
/**
* A configuration key in a configuration.
*/
@Public
public class ConfigKey {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigKey.class);
public static final Comparator ORDERING = (o1, o2) -> {
int ret = Integer.compare(o1.orderInGroup, o2.orderInGroup);
if (ret == 0) {
return o1.name().compareTo(o2.name());
} else {
return ret;
}
};
/**
* Build a config key of name.
*
* @param name config key name
* @return config key builder
*/
public static ConfigKeyBuilder builder(String name) {
return internalBuilder().name(name);
}
/**
* Flag indicates whether the setting is required.
*/
private boolean required;
/**
* Name of the configuration setting.
*/
private String name;
/**
* Type of the configuration setting.
*/
private Type type;
/**
* Description of the configuration setting.
*/
private String description;
/**
* Documentation of the configuration setting.
*/
private String documentation;
/**
* Default value as a string representation.
*/
private Object defaultValue;
private String defaultValueAsString() {
if (null == defaultValue) {
return null;
} else if (defaultValue instanceof String) {
return (String) defaultValue;
} else if (defaultValue instanceof Class) {
return ((Class) defaultValue).getName();
} else {
return defaultValue.toString();
}
}
/**
* The list of options for this setting.
*/
private List optionValues;
/**
* The validator used for validating configuration value.
*/
private Validator validator;
/**
* The key-group to group settings together.
*/
private ConfigKeyGroup group;
/**
* The order of the setting in the key-group.
*/
private int orderInGroup;
/**
* The list of settings dependents on this setting.
*/
private List dependents;
/**
* Whether this setting is deprecated or not.
*/
private boolean deprecated;
/**
* The config key that deprecates this key.
*/
private String deprecatedByConfigKey;
/**
* The version when this settings was deprecated.
*/
private String deprecatedSince;
/**
* The version when this setting was introduced.
*/
private String since;
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof ConfigKey)) {
return false;
}
ConfigKey other = (ConfigKey) o;
return Objects.equals(name, other.name);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return name.hashCode();
}
/**
* Validate the setting is valid in the provided config conf.
*
* @param conf configuration to test
*/
public void validate(Configuration conf) throws ConfigException {
if (conf.containsKey(name()) && validator() != null) {
Object value = get(conf);
if (!validator().validate(name(), value)) {
throw new ConfigException("Invalid setting of \'" + name() + "\' found the configuration: value = \'" + value + "\', requirement = \'" + validator + "\'");
}
} else if (required()) {
// missing config on a required field
throw new ConfigException("Setting \'" + name() + "\' is required but missing in the configuration");
}
}
/**
* Update the setting name in the configuration conf with the provided value.
*
* @param conf configuration to set
* @param value value of the setting
*/
public void set(Configuration conf, Object value) {
if (!type().validator().validate(name(), value)) {
throw new IllegalArgumentException("Invalid value \'" + value + "\' to set on setting \'" + name() + "\': expected type = " + type);
}
if (null != validator() && !validator().validate(name(), value)) {
throw new IllegalArgumentException("Invalid value \'" + value + "\' to set on setting \'" + name() + "\': required \'" + validator() + "\'");
}
if (value instanceof Class) {
conf.setProperty(name(), ((Class) value).getName());
} else {
conf.setProperty(name(), value);
}
}
/**
* Retrieve the setting from the configuration conf as a {@link Long} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a long number
*/
public long getLong(Configuration conf) {
checkArgument(type() == Type.LONG, "\'" + name() + "\' is NOT a LONG numeric setting");
return conf.getLong(name(), (Long) defaultValue());
}
/**
* Retrieve the setting from the configuration conf as a {@link Integer} value.
*
* @param conf configuration to retrieve the setting
* @return the value as an integer number
*/
public int getInt(Configuration conf) {
checkArgument(type() == Type.INT, "\'" + name() + "\' is NOT a INT numeric setting");
return conf.getInt(name(), (Integer) defaultValue());
}
/**
* Retrieve the setting from the configuration conf as a {@link Short} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a short number
*/
public short getShort(Configuration conf) {
checkArgument(type() == Type.SHORT, "\'" + name() + "\' is NOT a SHORT numeric setting");
return conf.getShort(name(), (Short) defaultValue());
}
/**
* Retrieve the setting from the configuration conf as a {@link Boolean} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a boolean flag
*/
public boolean getBoolean(Configuration conf) {
checkArgument(type() == Type.BOOLEAN, "\'" + name() + "\' is NOT a BOOL numeric setting");
return conf.getBoolean(name(), (Boolean) defaultValue());
}
/**
* Retrieve the setting from the configuration conf as a {@link Double} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a double number
*/
public double getDouble(Configuration conf) {
checkArgument(type() == Type.DOUBLE, "\'" + name() + "\' is NOT a DOUBLE numeric setting");
return conf.getDouble(name(), (Double) defaultValue());
}
/**
* Retrieve the setting from the configuration conf as a {@link String} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a string.
*/
public String getString(Configuration conf) {
return conf.getString(name(), defaultValueAsString());
}
/**
* Retrieve the setting from the configuration conf as a {@link Class} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a class
*/
@SuppressWarnings("unchecked")
public Class extends T> getClass(Configuration conf, Class interfaceCls) {
checkArgument(type() == Type.CLASS, "\'" + name() + "\' is NOT a CLASS setting");
try {
Class extends T> defaultClass = (Class extends T>) defaultValue();
return ReflectionUtils.getClass(conf, name(), defaultClass, interfaceCls, getClass().getClassLoader());
} catch (ConfigurationException e) {
throw new IllegalArgumentException("Invalid class is set to setting \'" + name() + "\': ", e);
}
}
/**
* Retrieve the setting from the configuration conf as a {@link Class} value.
*
* @param conf configuration to retrieve the setting
* @return the value as a class
*/
@SuppressWarnings("unchecked")
public Class> getClass(Configuration conf) {
checkArgument(type() == Type.CLASS, "\'" + name() + "\' is NOT a CLASS setting");
try {
Class> defaultClass = (Class>) defaultValue();
return ReflectionUtils.getClass(conf, name(), defaultClass, getClass().getClassLoader());
} catch (ConfigurationException e) {
throw new IllegalArgumentException("Invalid class is set to setting \'" + name() + "\': ", e);
}
}
/**
* Retrieve the setting from the configuration conf as a {@link Class} value.
*
* @param conf configuration to retrieve the setting
* @return the value as list of values
*/
@SuppressWarnings("unchecked")
public List