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.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.config.encode.*;
021import org.apache.juneau.config.store.*;
022import org.apache.juneau.json.*;
023import org.apache.juneau.parser.*;
024import org.apache.juneau.serializer.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Builder for creating instances of {@link Config Configs}.
029 *
030 * <h5 class='section'>Example:</h5>
031 * <p class='bcode w800'>
032 *    Config cf = Config.<jsm>create</jsm>().name(<js>"MyConfig.cfg"</js>).build();
033 *    String setting = cf.getString(<js>"MySection/mysetting"</js>);
034 * </p>
035 *
036 * <ul class='seealso'>
037 *    <li class='link'>{@doc juneau-config}
038 * </ul>
039 */
040public class ConfigBuilder extends ContextBuilder {
041
042   /**
043    * Constructor, default settings.
044    */
045   public ConfigBuilder() {
046      super();
047   }
048
049   /**
050    * Constructor.
051    *
052    * @param ps The initial configuration settings for this builder.
053    */
054   public ConfigBuilder(PropertyStore ps) {
055      super(ps);
056   }
057
058   @Override /* ContextBuilder */
059   public Config build() {
060      return build(Config.class);
061   }
062
063
064   //-----------------------------------------------------------------------------------------------------------------
065   // Properties
066   //-----------------------------------------------------------------------------------------------------------------
067
068   /**
069    * Configuration property:  Configuration name.
070    *
071    * <p>
072    * Specifies the configuration name.
073    * <br>This is typically the configuration file name, although
074    * the name can be anything identifiable by the {@link ConfigStore} used for retrieving and storing the configuration.
075    *
076    * @param value
077    *    The new value for this property.
078    *    <br>The default is <js>"Configuration"</js>.
079    * @return This object (for method chaining).
080    */
081   public ConfigBuilder name(String value) {
082      return set(CONFIG_name, value);
083   }
084
085   /**
086    * Configuration property:  Configuration store.
087    *
088    * <p>
089    * The configuration store used for retrieving and storing configurations.
090    *
091    * @param value
092    *    The new value for this property.
093    *    <br>The default is {@link ConfigFileStore#DEFAULT}.
094    * @return This object (for method chaining).
095    */
096   public ConfigBuilder store(ConfigStore value) {
097      return set(CONFIG_store, value);
098   }
099
100   /**
101    * Configuration property:  Configuration store.
102    *
103    * <p>
104    * Convenience method for calling <code>store(ConfigMemoryStore.<jsf>DEFAULT</jsf>)</code>.
105    *
106    * @return This object (for method chaining).
107    */
108   public ConfigBuilder memStore() {
109      return set(CONFIG_store, ConfigMemoryStore.DEFAULT);
110   }
111
112   /**
113    * Configuration property:  POJO serializer.
114    *
115    * <p>
116    * The serializer to use for serializing POJO values.
117    *
118    * @param value
119    *    The new value for this property.
120    *    <br>The default is {@link SimpleJsonSerializer#DEFAULT}.
121    * @return This object (for method chaining).
122    */
123   public ConfigBuilder serializer(WriterSerializer value) {
124      return set(CONFIG_serializer, value);
125   }
126
127   /**
128    * Configuration property:  POJO serializer.
129    *
130    * <p>
131    * The serializer to use for serializing POJO values.
132    *
133    * @param value
134    *    The new value for this property.
135    *    <br>The default is {@link SimpleJsonSerializer#DEFAULT}.
136    * @return This object (for method chaining).
137    */
138   public ConfigBuilder serializer(Class<? extends WriterSerializer> value) {
139      return set(CONFIG_serializer, value);
140   }
141
142   /**
143    * Configuration property:  POJO parser.
144    *
145    * <p>
146    * The parser to use for parsing values to POJOs.
147    *
148    * @param value
149    *    The new value for this property.
150    *    <br>The default is {@link JsonParser#DEFAULT}.
151    * @return This object (for method chaining).
152    */
153   public ConfigBuilder parser(ReaderParser value) {
154      return set(CONFIG_parser, value);
155   }
156
157   /**
158    * Configuration property:  POJO parser.
159    *
160    * <p>
161    * The parser to use for parsing values to POJOs.
162    *
163    * @param value
164    *    The new value for this property.
165    *    <br>The default is {@link JsonParser#DEFAULT}.
166    * @return This object (for method chaining).
167    */
168   public ConfigBuilder parser(Class<? extends ReaderParser> value) {
169      return set(CONFIG_parser, value);
170   }
171
172   /**
173    * Configuration property:  Value encoder.
174    *
175    * <p>
176    * The encoder to use for encoding encoded configuration values.
177    *
178    * @param value
179    *    The new value for this property.
180    *    <br>The default is {@link ConfigXorEncoder#INSTANCE}.
181    * @return This object (for method chaining).
182    */
183   public ConfigBuilder encoder(ConfigEncoder value) {
184      return set(CONFIG_encoder, value);
185   }
186
187   /**
188    * Configuration property:  Value encoder.
189    *
190    * <p>
191    * The encoder to use for encoding encoded configuration values.
192    *
193    * @param value
194    *    The new value for this property.
195    *    <br>The default is {@link ConfigXorEncoder#INSTANCE}.
196    * @return This object (for method chaining).
197    */
198   public ConfigBuilder encoder(Class<? extends ConfigEncoder> value) {
199      return set(CONFIG_encoder, value);
200   }
201
202   /**
203    * Configuration property:  SVL variable resolver.
204    *
205    * <p>
206    * The resolver to use for resolving SVL variables.
207    *
208    * @param value
209    *    The new value for this property.
210    *    <br>The default is {@link VarResolver#DEFAULT}.
211    * @return This object (for method chaining).
212    */
213   public ConfigBuilder varResolver(VarResolver value) {
214      return set(CONFIG_varResolver, value);
215   }
216
217   /**
218    * Configuration property:  SVL variable resolver.
219    *
220    * <p>
221    * The resolver to use for resolving SVL variables.
222    *
223    * @param value
224    *    The new value for this property.
225    *    <br>The default is {@link VarResolver#DEFAULT}.
226    * @return This object (for method chaining).
227    */
228   public ConfigBuilder varResolver(Class<? extends VarResolver> value) {
229      return set(CONFIG_varResolver, value);
230   }
231
232   /**
233    * Configuration property:  Binary value line length.
234    *
235    * <p>
236    * When serializing binary values, lines will be split after this many characters.
237    * <br>Use <c>-1</c> to represent no line splitting.
238    *
239    * @param value
240    *    The new value for this property.
241    *    <br>The default is <c>-1</c>.
242    * @return This object (for method chaining).
243    */
244   public ConfigBuilder binaryLineLength(int value) {
245      return set(CONFIG_binaryLineLength, value);
246   }
247
248   /**
249    * Configuration property:  Binary value format.
250    *
251    * <p>
252    * The format to use when persisting byte arrays.
253    *
254    * <p>
255    * Possible values:
256    * <ul>
257    *    <li>{@link BinaryFormat#BASE64} - BASE64-encoded string.
258    *    <li>{@link BinaryFormat#HEX} - Hexadecimal.
259    *    <li>{@link BinaryFormat#SPACED_HEX} - Hexadecimal with spaces between bytes.
260    * </ul>
261    *
262    * @param value
263    *    The new value for this property.
264    *    <br>The default is {@link BinaryFormat#BASE64}.
265    * @return This object (for method chaining).
266    */
267   public ConfigBuilder binaryFormat(BinaryFormat value) {
268      return set(CONFIG_binaryFormat, value);
269   }
270
271   /**
272    * Configuration property:  Multi-line values on separate lines.
273    *
274    * <p>
275    * When enabled, multi-line values will always be placed on a separate line from the key.
276    *
277    * @return This object (for method chaining).
278    */
279   public ConfigBuilder multiLineValuesOnSeparateLines() {
280      return set(CONFIG_multiLineValuesOnSeparateLines, true);
281   }
282
283   /**
284    * Configuration property:  Beans on separate lines.
285    *
286    * <p>
287    * When enabled, attempts to call any setters on this object will throw an {@link UnsupportedOperationException}.
288    *
289    * @return This object (for method chaining).
290    */
291   public ConfigBuilder readOnly() {
292      return set(CONFIG_readOnly, true);
293   }
294
295   @Override /* ContextBuilder */
296   public ConfigBuilder set(String name, Object value) {
297      super.set(name, value);
298      return this;
299   }
300
301   @Override /* ContextBuilder */
302   public ConfigBuilder set(Map<String,Object> properties) {
303      super.set(properties);
304      return this;
305   }
306
307   @Override /* ContextBuilder */
308   public ConfigBuilder add(Map<String,Object> properties) {
309      super.add(properties);
310      return this;
311   }
312
313   @Override /* ContextBuilder */
314   public ConfigBuilder addTo(String name, Object value) {
315      super.addTo(name, value);
316      return this;
317   }
318
319   @Override /* ContextBuilder */
320   public ConfigBuilder addTo(String name, String key, Object value) {
321      super.addTo(name, key, value);
322      return this;
323   }
324
325   @Override /* ContextBuilder */
326   public ConfigBuilder removeFrom(String name, Object value) {
327      super.removeFrom(name, value);
328      return this;
329   }
330
331   @Override /* ContextBuilder */
332   public ConfigBuilder apply(PropertyStore copyFrom) {
333      super.apply(copyFrom);
334      return this;
335   }
336}