001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.builder.combined; 018 019import java.util.Set; 020 021import org.apache.commons.configuration2.HierarchicalConfiguration; 022import org.apache.commons.configuration2.beanutils.XMLBeanDeclaration; 023 024/** 025 * <p> 026 * A specialized {@code BeanDeclaration} implementation that represents the 027 * declaration of a configuration source. 028 * </p> 029 * <p> 030 * Instances of this class are able to extract all information about a 031 * configuration source from the configuration definition file. The declaration 032 * of a configuration source is very similar to a bean declaration processed by 033 * {@code XMLBeanDeclaration}. There are very few differences, e.g. some 034 * reserved attributes like {@code optional} and {@code at}, and the fact that a 035 * bean factory is never needed. 036 * </p> 037 * 038 * @version $Id: ConfigurationDeclaration.java 1624601 2014-09-12 18:04:36Z oheger $ 039 * @since 2.0 040 */ 041public class ConfigurationDeclaration extends XMLBeanDeclaration 042{ 043 /** Stores a reference to the associated configuration builder. */ 044 private final CombinedConfigurationBuilder configurationBuilder; 045 046 /** 047 * Creates a new instance of {@code ConfigurationDeclaration} and 048 * initializes it. 049 * 050 * @param builder the associated configuration builder 051 * @param config the configuration this declaration is based onto 052 */ 053 public ConfigurationDeclaration(CombinedConfigurationBuilder builder, 054 HierarchicalConfiguration<?> config) 055 { 056 super(config); 057 configurationBuilder = builder; 058 } 059 060 /** 061 * Returns the associated configuration builder. 062 * 063 * @return the configuration builder 064 */ 065 public CombinedConfigurationBuilder getConfigurationBuilder() 066 { 067 return configurationBuilder; 068 } 069 070 /** 071 * Returns the value of the {@code at} attribute. 072 * 073 * @return the value of the {@code at} attribute (can be <b>null</b>) 074 */ 075 public String getAt() 076 { 077 String result = 078 this.getConfiguration().getString( 079 CombinedConfigurationBuilder.ATTR_AT_RES); 080 return (result == null) ? this.getConfiguration().getString( 081 CombinedConfigurationBuilder.ATTR_AT) : result; 082 } 083 084 /** 085 * Returns a flag whether this is an optional configuration. 086 * 087 * @return a flag if this declaration points to an optional configuration 088 */ 089 public boolean isOptional() 090 { 091 Boolean value = 092 this.getConfiguration().getBoolean( 093 CombinedConfigurationBuilder.ATTR_OPTIONAL_RES, null); 094 if (value == null) 095 { 096 value = 097 this.getConfiguration().getBoolean( 098 CombinedConfigurationBuilder.ATTR_OPTIONAL, 099 Boolean.FALSE); 100 } 101 return value.booleanValue(); 102 } 103 104 /** 105 * Returns a flag whether this configuration should always be created and 106 * added to the resulting combined configuration. This flag is evaluated 107 * only for optional configurations whose normal creation has caused an 108 * error. If for such a configuration the {@code forceCreate} attribute is 109 * set and the corresponding configuration provider supports this mode, an 110 * empty configuration will be created and added to the resulting combined 111 * configuration. 112 * 113 * @return the value of the {@code forceCreate} attribute 114 */ 115 public boolean isForceCreate() 116 { 117 return this.getConfiguration().getBoolean( 118 CombinedConfigurationBuilder.ATTR_FORCECREATE, false); 119 } 120 121 /** 122 * Returns a flag whether a builder with reloading support should be 123 * created. This may not be supported by all configuration builder 124 * providers. 125 * 126 * @return a flag whether a reloading builder should be created 127 */ 128 public boolean isReload() 129 { 130 return getConfiguration().getBoolean( 131 CombinedConfigurationBuilder.ATTR_RELOAD, false); 132 } 133 134 /** 135 * Returns the name for the represented configuration source. The name is 136 * optional, so this method can return <b>null</b>. 137 * 138 * @return the name of the associated configuration source or <b>null</b> 139 */ 140 public String getName() 141 { 142 return getConfiguration().getString( 143 CombinedConfigurationBuilder.ATTR_NAME); 144 } 145 146 /** 147 * Returns the name of the bean factory. For configuration source 148 * declarations always a reserved factory is used. This factory's name is 149 * returned by this implementation. 150 * 151 * @return the name of the bean factory 152 */ 153 @Override 154 public String getBeanFactoryName() 155 { 156 return CombinedConfigurationBuilder.CONFIG_BEAN_FACTORY_NAME; 157 } 158 159 /** 160 * Returns the bean's class name. This implementation will always return 161 * <b>null</b>. 162 * 163 * @return the name of the bean's class 164 */ 165 @Override 166 public String getBeanClassName() 167 { 168 return null; 169 } 170 171 /** 172 * {@inheritDoc} This implementation checks for additional reserved 173 * attribute names. Note that in some cases the presence of other attribute 174 * names determine whether a name is reserved or not. For instance, per 175 * default the attribute {@code config-at} is reserved. However, if this 176 * attribute is not present, the attribute {@code at} is also considered as 177 * a reserved attribute. (This is mainly done for dealing with legacy 178 * configuration files supported by earlier versions of this library.) 179 */ 180 @Override 181 protected boolean isReservedAttributeName(String name) 182 { 183 if (super.isReservedAttributeName(name)) 184 { 185 return true; 186 } 187 188 Set<String> attributes = getAttributeNames(); 189 return (CombinedConfigurationBuilder.ATTR_ATNAME.equals(name) && !attributes 190 .contains(RESERVED_PREFIX 191 + CombinedConfigurationBuilder.ATTR_ATNAME)) 192 || (CombinedConfigurationBuilder.ATTR_OPTIONALNAME.equals(name) && !attributes 193 .contains(RESERVED_PREFIX 194 + CombinedConfigurationBuilder.ATTR_OPTIONALNAME)); 195 } 196}