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