View Javadoc
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    *     https://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  
19  import java.util.Set;
20  
21  import org.apache.commons.configuration2.HierarchicalConfiguration;
22  import org.apache.commons.configuration2.beanutils.XMLBeanDeclaration;
23  
24  /**
25   * <p>
26   * A specialized {@code BeanDeclaration} implementation that represents the declaration of a configuration source.
27   * </p>
28   * <p>
29   * Instances of this class are able to extract all information about a configuration source from the configuration
30   * definition file. The declaration of a configuration source is very similar to a bean declaration processed by
31   * {@code XMLBeanDeclaration}. There are very few differences, for example some reserved attributes like {@code optional} and
32   * {@code at}, and the fact that a bean factory is never needed.
33   * </p>
34   *
35   * @since 2.0
36   */
37  public class ConfigurationDeclaration extends XMLBeanDeclaration {
38      /** Stores a reference to the associated configuration builder. */
39      private final CombinedConfigurationBuilder configurationBuilder;
40  
41      /**
42       * Creates a new instance of {@code ConfigurationDeclaration} and initializes it.
43       *
44       * @param builder the associated configuration builder
45       * @param config the configuration this declaration is based onto
46       */
47      public ConfigurationDeclaration(final CombinedConfigurationBuilder builder, final HierarchicalConfiguration<?> config) {
48          super(config);
49          configurationBuilder = builder;
50      }
51  
52      /**
53       * Gets the value of the {@code at} attribute.
54       *
55       * @return the value of the {@code at} attribute (can be <strong>null</strong>)
56       */
57      public String getAt() {
58          final String result = getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT_RES);
59          return result == null ? getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT) : result;
60      }
61  
62      /**
63       * Gets the bean's class name. This implementation will always return <strong>null</strong>.
64       *
65       * @return the name of the bean's class
66       */
67      @Override
68      public String getBeanClassName() {
69          return null;
70      }
71  
72      /**
73       * Gets the name of the bean factory. For configuration source declarations always a reserved factory is used. This
74       * factory's name is returned by this implementation.
75       *
76       * @return the name of the bean factory
77       */
78      @Override
79      public String getBeanFactoryName() {
80          return CombinedConfigurationBuilder.CONFIG_BEAN_FACTORY_NAME;
81      }
82  
83      /**
84       * Gets the associated configuration builder.
85       *
86       * @return the configuration builder
87       */
88      public CombinedConfigurationBuilder getConfigurationBuilder() {
89          return configurationBuilder;
90      }
91  
92      /**
93       * Gets the name for the represented configuration source. The name is optional, so this method can return
94       * <strong>null</strong>.
95       *
96       * @return the name of the associated configuration source or <strong>null</strong>
97       */
98      public String getName() {
99          return getConfiguration().getString(CombinedConfigurationBuilder.ATTR_NAME);
100     }
101 
102     /**
103      * Gets a flag whether this configuration should always be created and added to the resulting combined configuration.
104      * This flag is evaluated only for optional configurations whose normal creation has caused an error. If for such a
105      * configuration the {@code forceCreate} attribute is set and the corresponding configuration provider supports this
106      * mode, an empty configuration will be created and added to the resulting combined configuration.
107      *
108      * @return the value of the {@code forceCreate} attribute
109      */
110     public boolean isForceCreate() {
111         return getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_FORCECREATE, false);
112     }
113 
114     /**
115      * Gets a flag whether this is an optional configuration.
116      *
117      * @return a flag if this declaration points to an optional configuration
118      */
119     public boolean isOptional() {
120         Boolean value = getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL_RES, null);
121         if (value == null) {
122             value = getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL, Boolean.FALSE);
123         }
124         return value.booleanValue();
125     }
126 
127     /**
128      * Returns a flag whether a builder with reloading support should be created. This may not be supported by all
129      * configuration builder providers.
130      *
131      * @return a flag whether a reloading builder should be created
132      */
133     public boolean isReload() {
134         return getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_RELOAD, false);
135     }
136 
137     /**
138      * {@inheritDoc} This implementation checks for additional reserved attribute names. Note that in some cases the
139      * presence of other attribute names determine whether a name is reserved or not. For instance, per default the
140      * attribute {@code config-at} is reserved. However, if this attribute is not present, the attribute {@code at} is also
141      * considered as a reserved attribute. (This is mainly done for dealing with legacy configuration files supported by
142      * earlier versions of this library.)
143      */
144     @Override
145     protected boolean isReservedAttributeName(final String name) {
146         if (super.isReservedAttributeName(name)) {
147             return true;
148         }
149 
150         final Set<String> attributes = getAttributeNames();
151         return CombinedConfigurationBuilder.ATTR_ATNAME.equals(name) && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_ATNAME)
152             || CombinedConfigurationBuilder.ATTR_OPTIONALNAME.equals(name)
153                 && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_OPTIONALNAME);
154     }
155 }