The Basics of Configiration System -- ConfigNamespace and ConfigOption

来源:互联网 发布:软件著作权 翻译 编辑:程序博客网 时间:2024/06/05 18:25

The configuration system is divided to two type, ConfigNamespace and ConfigOption. When the class GraphDatabaseConfiguration is loaded, the class static member consists a configure tree rooted by ROOT_NS.
The Confignamespace does not contain the configure name and value, it only contains the part of the name before the last dot. Take STORAGE_NS for example, it is a sub namespace of ROOT_NS.

public static ConfigNamespace ROOT_NS = new ConfigNamespace(null,"root","Root Configuration Namespace for the HugeGraph Graph Database");public static final ConfigNamespace STORAGE_NS = new ConfigNamespace(ROOT_NS,"storage","Configuration options for the storage backend.  Some options are applicable only for certain backends.");
public ConfigNamespace(ConfigNamespace parent, String name, String description) {        this(parent,name,description,false);    }
 public ConfigNamespace(ConfigNamespace parent, String name, String description, boolean isUmbrella) {        super(parent,name,description);        this.isUmbrella=isUmbrella;    }

The definition of isUmbrella.

 /**     * Wether this namespace is an umbrella namespace, that is, is expects immediate sub-namespaces which are user defined.     * @return     */    public boolean isUmbrella() {        return isUmbrella;    }

That means, if a namespace is umbrella, then user can define subnamespace. You can define a subnamespace in REGISTRATION_NS, but you can not define a namespace in ROOT_NS.

public static final ConfigNamespace REGISTRATION_NS = new ConfigNamespace(ROOT_NS,"system-registration",            "This is used internally to keep track of open instances.",true);

super(parent,name,description) calls the constructor method of ConfigElement.

 public ConfigElement(ConfigNamespace namespace, String name, String description) {        this.namespace = namespace;        this.name = name;        this.description = description;        if (namespace!=null) namespace.registerChild(this);    }

Method isRoot in class ConfigElement.

   public boolean isRoot() {        return namespace ==null;    }

ConfigOption

public static final ConfigOption<Integer> GRAPHITE_PORT = new ConfigOption<Integer>(METRICS_GRAPHITE_NS,"port",            "The port to which Graphite data are sent",            ConfigOption.Type.MASKABLE, 2003);
public static final<O> Predicate<O> disallowEmpty(Class<O> clazz) {        return new Predicate<O>() {            @Override            public boolean apply(@Nullable O o) {                if (o==null) return false;                if (o instanceof String) return StringUtils.isNotBlank((String)o);                if (o.getClass().isArray() && (Array.getLength(o)==0 || Array.get(o,0)==null)) return false;                if (o instanceof Collection && (((Collection)o).isEmpty() || ((Collection)o).iterator().next()==null)) return false;                return true;            }        };    }
public ConfigOption(ConfigNamespace parent, String name, String description, Type type, Class<O> datatype, O defaultValue, Predicate<O> verificationFct, ConfigOption<?> supersededBy) {        super(parent, name, description);        Preconditions.checkNotNull(type);        Preconditions.checkNotNull(datatype);        Preconditions.checkNotNull(verificationFct);        this.type = type;        this.datatype = datatype;        this.defaultValue = defaultValue;        this.verificationFct = verificationFct;        this.supersededBy = supersededBy;        // This constructor tends to get called by static initializers, so log before throwing the IAE        if (!ACCEPTED_DATATYPES.contains(datatype)) {            String msg = String.format("Datatype %s is not one of %s", datatype, ACCEPTED_DATATYPES_STRING);            log.error(msg);            throw new IllegalArgumentException(msg);        }    }

Enumeration Type in ConfigOption.

public enum Type {        /**         * Once the database has been opened, these configuration options cannot         * be changed for the entire life of the database         */        FIXED,        /**         * These options can only be changed for the entire database cluster at         * once when all instances are shut down         */        GLOBAL_OFFLINE,        /**         * These options can only be changed globally across the entire database         * cluster         */        GLOBAL,        /**         * These options are global but can be overwritten by a local         * configuration file         */        MASKABLE,        /**         * These options can ONLY be provided through a local configuration file         */        LOCAL;    }

The definition of managedTypes.

private static final EnumSet<Type> managedTypes = EnumSet.of(Type.FIXED, Type.GLOBAL_OFFLINE, Type.GLOBAL);
ACCEPTED_DATATYPES = ImmutableSet.of(                ConflictAvoidanceMode.class,                Duration.class,                TimestampProviders.class,                Instant.class,                Boolean.class,                Short.class,                Integer.class,                Byte.class,                Long.class,                Float.class,                Double.class,                String.class,                String[].class        );
1 0