001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.config; 014 015import static org.apache.juneau.config.Config.*; 016 017import java.lang.reflect.*; 018import java.util.*; 019 020import org.apache.juneau.*; 021import org.apache.juneau.config.encode.*; 022import org.apache.juneau.config.store.*; 023import org.apache.juneau.http.*; 024import org.apache.juneau.json.*; 025import org.apache.juneau.parser.*; 026import org.apache.juneau.reflect.*; 027import org.apache.juneau.serializer.*; 028import org.apache.juneau.svl.*; 029 030/** 031 * Builder for creating instances of {@link Config Configs}. 032 * 033 * <h5 class='section'>Example:</h5> 034 * <p class='bcode w800'> 035 * Config cf = Config.<jsm>create</jsm>().name(<js>"MyConfig.cfg"</js>).build(); 036 * String setting = cf.getString(<js>"MySection/mysetting"</js>); 037 * </p> 038 * 039 * <ul class='seealso'> 040 * <li class='link'>{@doc juneau-config} 041 * </ul> 042 */ 043public class ConfigBuilder extends ContextBuilder { 044 045 /** 046 * Constructor, default settings. 047 */ 048 public ConfigBuilder() { 049 super(); 050 } 051 052 /** 053 * Constructor. 054 * 055 * @param ps The initial configuration settings for this builder. 056 */ 057 public ConfigBuilder(PropertyStore ps) { 058 super(ps); 059 } 060 061 @Override /* ContextBuilder */ 062 public Config build() { 063 return build(Config.class); 064 } 065 066 067 //----------------------------------------------------------------------------------------------------------------- 068 // Properties 069 //----------------------------------------------------------------------------------------------------------------- 070 071 /** 072 * <i><l>Config</l> configuration property: </i> Configuration name. 073 * 074 * <p> 075 * Specifies the configuration name. 076 * <br>This is typically the configuration file name, although 077 * the name can be anything identifiable by the {@link ConfigStore} used for retrieving and storing the configuration. 078 * 079 * @param value 080 * The new value for this property. 081 * <br>The default is <js>"Configuration"</js>. 082 * @return This object (for method chaining). 083 */ 084 public ConfigBuilder name(String value) { 085 return set(CONFIG_name, value); 086 } 087 088 /** 089 * <i><l>Config</l> configuration property: </i> Configuration store. 090 * 091 * <p> 092 * The configuration store used for retrieving and storing configurations. 093 * 094 * @param value 095 * The new value for this property. 096 * <br>The default is {@link ConfigFileStore#DEFAULT}. 097 * @return This object (for method chaining). 098 */ 099 public ConfigBuilder store(ConfigStore value) { 100 return set(CONFIG_store, value); 101 } 102 103 /** 104 * <i><l>Config</l> configuration property: </i> Configuration store. 105 * 106 * <p> 107 * Convenience method for calling <code>store(ConfigMemoryStore.<jsf>DEFAULT</jsf>)</code>. 108 * 109 * @return This object (for method chaining). 110 */ 111 public ConfigBuilder memStore() { 112 return set(CONFIG_store, ConfigMemoryStore.DEFAULT); 113 } 114 115 /** 116 * <i><l>Config</l> configuration property: </i> POJO serializer. 117 * 118 * <p> 119 * The serializer to use for serializing POJO values. 120 * 121 * @param value 122 * The new value for this property. 123 * <br>The default is {@link SimpleJsonSerializer#DEFAULT}. 124 * @return This object (for method chaining). 125 */ 126 public ConfigBuilder serializer(WriterSerializer value) { 127 return set(CONFIG_serializer, value); 128 } 129 130 /** 131 * <i><l>Config</l> configuration property: </i> POJO serializer. 132 * 133 * <p> 134 * The serializer to use for serializing POJO values. 135 * 136 * @param value 137 * The new value for this property. 138 * <br>The default is {@link SimpleJsonSerializer#DEFAULT}. 139 * @return This object (for method chaining). 140 */ 141 public ConfigBuilder serializer(Class<? extends WriterSerializer> value) { 142 return set(CONFIG_serializer, value); 143 } 144 145 /** 146 * <i><l>Config</l> configuration property: </i> POJO parser. 147 * 148 * <p> 149 * The parser to use for parsing values to POJOs. 150 * 151 * @param value 152 * The new value for this property. 153 * <br>The default is {@link JsonParser#DEFAULT}. 154 * @return This object (for method chaining). 155 */ 156 public ConfigBuilder parser(ReaderParser value) { 157 return set(CONFIG_parser, value); 158 } 159 160 /** 161 * <i><l>Config</l> configuration property: </i> POJO parser. 162 * 163 * <p> 164 * The parser to use for parsing values to POJOs. 165 * 166 * @param value 167 * The new value for this property. 168 * <br>The default is {@link JsonParser#DEFAULT}. 169 * @return This object (for method chaining). 170 */ 171 public ConfigBuilder parser(Class<? extends ReaderParser> value) { 172 return set(CONFIG_parser, value); 173 } 174 175 /** 176 * <i><l>Config</l> configuration property: </i> Value encoder. 177 * 178 * <p> 179 * The encoder to use for encoding encoded configuration values. 180 * 181 * @param value 182 * The new value for this property. 183 * <br>The default is {@link ConfigXorEncoder#INSTANCE}. 184 * @return This object (for method chaining). 185 */ 186 public ConfigBuilder encoder(ConfigEncoder value) { 187 return set(CONFIG_encoder, value); 188 } 189 190 /** 191 * <i><l>Config</l> configuration property: </i> Value encoder. 192 * 193 * <p> 194 * The encoder to use for encoding encoded configuration values. 195 * 196 * @param value 197 * The new value for this property. 198 * <br>The default is {@link ConfigXorEncoder#INSTANCE}. 199 * @return This object (for method chaining). 200 */ 201 public ConfigBuilder encoder(Class<? extends ConfigEncoder> value) { 202 return set(CONFIG_encoder, value); 203 } 204 205 /** 206 * <i><l>Config</l> configuration property: </i> SVL variable resolver. 207 * 208 * <p> 209 * The resolver to use for resolving SVL variables. 210 * 211 * @param value 212 * The new value for this property. 213 * <br>The default is {@link VarResolver#DEFAULT}. 214 * @return This object (for method chaining). 215 */ 216 public ConfigBuilder varResolver(VarResolver value) { 217 return set(CONFIG_varResolver, value); 218 } 219 220 /** 221 * <i><l>Config</l> configuration property: </i> SVL variable resolver. 222 * 223 * <p> 224 * The resolver to use for resolving SVL variables. 225 * 226 * @param value 227 * The new value for this property. 228 * <br>The default is {@link VarResolver#DEFAULT}. 229 * @return This object (for method chaining). 230 */ 231 public ConfigBuilder varResolver(Class<? extends VarResolver> value) { 232 return set(CONFIG_varResolver, value); 233 } 234 235 /** 236 * <i><l>Config</l> configuration property: </i> Binary value line length. 237 * 238 * <p> 239 * When serializing binary values, lines will be split after this many characters. 240 * <br>Use <c>-1</c> to represent no line splitting. 241 * 242 * @param value 243 * The new value for this property. 244 * <br>The default is <c>-1</c>. 245 * @return This object (for method chaining). 246 */ 247 public ConfigBuilder binaryLineLength(int value) { 248 return set(CONFIG_binaryLineLength, value); 249 } 250 251 /** 252 * <i><l>Config</l> configuration property: </i> Binary value format. 253 * 254 * <p> 255 * The format to use when persisting byte arrays. 256 * 257 * <p> 258 * Possible values: 259 * <ul> 260 * <li>{@link BinaryFormat#BASE64} - BASE64-encoded string. 261 * <li>{@link BinaryFormat#HEX} - Hexadecimal. 262 * <li>{@link BinaryFormat#SPACED_HEX} - Hexadecimal with spaces between bytes. 263 * </ul> 264 * 265 * @param value 266 * The new value for this property. 267 * <br>The default is {@link BinaryFormat#BASE64}. 268 * @return This object (for method chaining). 269 */ 270 public ConfigBuilder binaryFormat(BinaryFormat value) { 271 return set(CONFIG_binaryFormat, value); 272 } 273 274 /** 275 * <i><l>Config</l> configuration property: </i> Multi-line values on separate lines. 276 * 277 * <p> 278 * When enabled, multi-line values will always be placed on a separate line from the key. 279 * 280 * @return This object (for method chaining). 281 */ 282 public ConfigBuilder multiLineValuesOnSeparateLines() { 283 return set(CONFIG_multiLineValuesOnSeparateLines, true); 284 } 285 286 /** 287 * <i><l>Config</l> configuration property: </i> Beans on separate lines. 288 * 289 * <p> 290 * When enabled, attempts to call any setters on this object will throw an {@link UnsupportedOperationException}. 291 * 292 * @return This object (for method chaining). 293 */ 294 public ConfigBuilder readOnly() { 295 return set(CONFIG_readOnly, true); 296 } 297 298 // <FluentSetters> 299 300 @Override /* GENERATED - ContextBuilder */ 301 public ConfigBuilder add(Map<String,Object> properties) { 302 super.add(properties); 303 return this; 304 } 305 306 @Override /* GENERATED - ContextBuilder */ 307 public ConfigBuilder addTo(String name, Object value) { 308 super.addTo(name, value); 309 return this; 310 } 311 312 @Override /* GENERATED - ContextBuilder */ 313 public ConfigBuilder appendTo(String name, Object value) { 314 super.appendTo(name, value); 315 return this; 316 } 317 318 @Override /* GENERATED - ContextBuilder */ 319 public ConfigBuilder apply(PropertyStore copyFrom) { 320 super.apply(copyFrom); 321 return this; 322 } 323 324 @Override /* GENERATED - ContextBuilder */ 325 public ConfigBuilder applyAnnotations(java.lang.Class<?>...fromClasses) { 326 super.applyAnnotations(fromClasses); 327 return this; 328 } 329 330 @Override /* GENERATED - ContextBuilder */ 331 public ConfigBuilder applyAnnotations(Method...fromMethods) { 332 super.applyAnnotations(fromMethods); 333 return this; 334 } 335 336 @Override /* GENERATED - ContextBuilder */ 337 public ConfigBuilder applyAnnotations(AnnotationList al, VarResolverSession r) { 338 super.applyAnnotations(al, r); 339 return this; 340 } 341 342 @Override /* GENERATED - ContextBuilder */ 343 public ConfigBuilder debug() { 344 super.debug(); 345 return this; 346 } 347 348 @Override /* GENERATED - ContextBuilder */ 349 public ConfigBuilder locale(Locale value) { 350 super.locale(value); 351 return this; 352 } 353 354 @Override /* GENERATED - ContextBuilder */ 355 public ConfigBuilder mediaType(MediaType value) { 356 super.mediaType(value); 357 return this; 358 } 359 360 @Override /* GENERATED - ContextBuilder */ 361 public ConfigBuilder prependTo(String name, Object value) { 362 super.prependTo(name, value); 363 return this; 364 } 365 366 @Override /* GENERATED - ContextBuilder */ 367 public ConfigBuilder putAllTo(String name, Object value) { 368 super.putAllTo(name, value); 369 return this; 370 } 371 372 @Override /* GENERATED - ContextBuilder */ 373 public ConfigBuilder putTo(String name, String key, Object value) { 374 super.putTo(name, key, value); 375 return this; 376 } 377 378 @Override /* GENERATED - ContextBuilder */ 379 public ConfigBuilder removeFrom(String name, Object value) { 380 super.removeFrom(name, value); 381 return this; 382 } 383 384 @Override /* GENERATED - ContextBuilder */ 385 public ConfigBuilder set(Map<String,Object> properties) { 386 super.set(properties); 387 return this; 388 } 389 390 @Override /* GENERATED - ContextBuilder */ 391 public ConfigBuilder set(String name, Object value) { 392 super.set(name, value); 393 return this; 394 } 395 396 @Override /* GENERATED - ContextBuilder */ 397 public ConfigBuilder timeZone(TimeZone value) { 398 super.timeZone(value); 399 return this; 400 } 401 402 // </FluentSetters> 403}