ConfigurationDeclaration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.builder.combined;

  18. import java.util.Set;

  19. import org.apache.commons.configuration2.HierarchicalConfiguration;
  20. import org.apache.commons.configuration2.beanutils.XMLBeanDeclaration;

  21. /**
  22.  * <p>
  23.  * A specialized {@code BeanDeclaration} implementation that represents the declaration of a configuration source.
  24.  * </p>
  25.  * <p>
  26.  * Instances of this class are able to extract all information about a configuration source from the configuration
  27.  * definition file. The declaration of a configuration source is very similar to a bean declaration processed by
  28.  * {@code XMLBeanDeclaration}. There are very few differences, for example some reserved attributes like {@code optional} and
  29.  * {@code at}, and the fact that a bean factory is never needed.
  30.  * </p>
  31.  *
  32.  * @since 2.0
  33.  */
  34. public class ConfigurationDeclaration extends XMLBeanDeclaration {
  35.     /** Stores a reference to the associated configuration builder. */
  36.     private final CombinedConfigurationBuilder configurationBuilder;

  37.     /**
  38.      * Creates a new instance of {@code ConfigurationDeclaration} and initializes it.
  39.      *
  40.      * @param builder the associated configuration builder
  41.      * @param config the configuration this declaration is based onto
  42.      */
  43.     public ConfigurationDeclaration(final CombinedConfigurationBuilder builder, final HierarchicalConfiguration<?> config) {
  44.         super(config);
  45.         configurationBuilder = builder;
  46.     }

  47.     /**
  48.      * Gets the value of the {@code at} attribute.
  49.      *
  50.      * @return the value of the {@code at} attribute (can be <strong>null</strong>)
  51.      */
  52.     public String getAt() {
  53.         final String result = getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT_RES);
  54.         return result == null ? getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT) : result;
  55.     }

  56.     /**
  57.      * Gets the bean's class name. This implementation will always return <strong>null</strong>.
  58.      *
  59.      * @return the name of the bean's class
  60.      */
  61.     @Override
  62.     public String getBeanClassName() {
  63.         return null;
  64.     }

  65.     /**
  66.      * Gets the name of the bean factory. For configuration source declarations always a reserved factory is used. This
  67.      * factory's name is returned by this implementation.
  68.      *
  69.      * @return the name of the bean factory
  70.      */
  71.     @Override
  72.     public String getBeanFactoryName() {
  73.         return CombinedConfigurationBuilder.CONFIG_BEAN_FACTORY_NAME;
  74.     }

  75.     /**
  76.      * Gets the associated configuration builder.
  77.      *
  78.      * @return the configuration builder
  79.      */
  80.     public CombinedConfigurationBuilder getConfigurationBuilder() {
  81.         return configurationBuilder;
  82.     }

  83.     /**
  84.      * Gets the name for the represented configuration source. The name is optional, so this method can return
  85.      * <strong>null</strong>.
  86.      *
  87.      * @return the name of the associated configuration source or <strong>null</strong>
  88.      */
  89.     public String getName() {
  90.         return getConfiguration().getString(CombinedConfigurationBuilder.ATTR_NAME);
  91.     }

  92.     /**
  93.      * Gets a flag whether this configuration should always be created and added to the resulting combined configuration.
  94.      * This flag is evaluated only for optional configurations whose normal creation has caused an error. If for such a
  95.      * configuration the {@code forceCreate} attribute is set and the corresponding configuration provider supports this
  96.      * mode, an empty configuration will be created and added to the resulting combined configuration.
  97.      *
  98.      * @return the value of the {@code forceCreate} attribute
  99.      */
  100.     public boolean isForceCreate() {
  101.         return getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_FORCECREATE, false);
  102.     }

  103.     /**
  104.      * Gets a flag whether this is an optional configuration.
  105.      *
  106.      * @return a flag if this declaration points to an optional configuration
  107.      */
  108.     public boolean isOptional() {
  109.         Boolean value = getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL_RES, null);
  110.         if (value == null) {
  111.             value = getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL, Boolean.FALSE);
  112.         }
  113.         return value.booleanValue();
  114.     }

  115.     /**
  116.      * Returns a flag whether a builder with reloading support should be created. This may not be supported by all
  117.      * configuration builder providers.
  118.      *
  119.      * @return a flag whether a reloading builder should be created
  120.      */
  121.     public boolean isReload() {
  122.         return getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_RELOAD, false);
  123.     }

  124.     /**
  125.      * {@inheritDoc} This implementation checks for additional reserved attribute names. Note that in some cases the
  126.      * presence of other attribute names determine whether a name is reserved or not. For instance, per default the
  127.      * attribute {@code config-at} is reserved. However, if this attribute is not present, the attribute {@code at} is also
  128.      * considered as a reserved attribute. (This is mainly done for dealing with legacy configuration files supported by
  129.      * earlier versions of this library.)
  130.      */
  131.     @Override
  132.     protected boolean isReservedAttributeName(final String name) {
  133.         if (super.isReservedAttributeName(name)) {
  134.             return true;
  135.         }

  136.         final Set<String> attributes = getAttributeNames();
  137.         return CombinedConfigurationBuilder.ATTR_ATNAME.equals(name) && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_ATNAME)
  138.             || CombinedConfigurationBuilder.ATTR_OPTIONALNAME.equals(name)
  139.                 && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_OPTIONALNAME);
  140.     }
  141. }