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    *      http://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.vfs2;
18  
19  /**
20   * Abstract class which has the right to fill FileSystemOptions.
21   */
22  public abstract class FileSystemConfigBuilder {
23  
24      /** Default prefix to use when resolving system properties */
25      private static final String PREFIX = "vfs.";
26  
27      /** The root URI of the file system */
28      private static final String ROOTURI = "rootURI";
29  
30      /** The prefix to use when resolving system properties */
31      private final String prefix;
32  
33      /**
34       * Constructs builder with default prefix.
35       *
36       * @since 1.0
37       */
38      protected FileSystemConfigBuilder() {
39          this.prefix = PREFIX;
40      }
41  
42      /**
43       * Constructs builder with specified component name.
44       *
45       * @param component component name to be used in prefix
46       *
47       * @since 2.0
48       */
49      protected FileSystemConfigBuilder(final String component) {
50          this.prefix = PREFIX + component;
51      }
52  
53      /**
54       * Sets the root URI of the file system.
55       *
56       * @param opts the file system options to modify
57       * @param rootURI The creator name to be associated with the file.
58       *
59       * @since 2.0
60       */
61      public void setRootURI(final FileSystemOptions opts, final String rootURI) {
62          setParam(opts, ROOTURI, rootURI);
63      }
64  
65      /**
66       * Gets the root URI of the file system.
67       *
68       * @param opts file system options to work with
69       * @return The root URI
70       *
71       * @since 2.0
72       */
73      public String getRootURI(final FileSystemOptions opts) {
74          return getString(opts, ROOTURI);
75      }
76  
77      /**
78       * Sets named parameter.
79       *
80       * @param opts the file system options to modify
81       * @param name set option with this name
82       * @param value boolean value to set
83       *
84       * @since 2.1
85       */
86      protected void setParam(final FileSystemOptions opts, final String name, final boolean value) {
87          setParam(opts, name, Boolean.valueOf(value));
88      }
89  
90      /**
91       * Sets named parameter.
92       *
93       * @param opts the file system options to modify
94       * @param name set option with this name
95       * @param value object value to set
96       *
97       * @since 1.0
98       */
99      protected void setParam(final FileSystemOptions opts, final String name, final Object value) {
100         opts.setOption(getConfigClass(), name, value);
101     }
102 
103     /**
104      * Gets named parameter.
105      *
106      * @param opts file system options to work with
107      * @param name get option with this name
108      * @return the named option or null
109      *
110      * @since 1.0
111      */
112     protected Object getParam(final FileSystemOptions opts, final String name) {
113         if (opts == null) {
114             return null;
115         }
116 
117         return opts.getOption(getConfigClass(), name);
118     }
119 
120     /**
121      * Checks if option exists.
122      *
123      * @param opts file system options to work with
124      * @param name the name to look up in {@code opts}
125      * @return true if opts have the named parameter
126      *
127      * @since 1.0
128      */
129     protected boolean hasParam(final FileSystemOptions opts, final String name) {
130         return opts != null && opts.hasOption(getConfigClass(), name);
131     }
132 
133     /**
134      * Checks the named setting specified.
135      *
136      * @param opts file system options to work with
137      * @param name the option to check in {@code opts} or system properties
138      * @return true if option exists
139      *
140      * @since 2.0
141      */
142     protected boolean hasObject(final FileSystemOptions opts, final String name) {
143         return hasParam(opts, name) || System.getProperties().containsKey(toPropertyKey(name));
144     }
145 
146     /**
147      * Gets named option as boolean.
148      *
149      * @param opts file system options to work with
150      * @param name the option name
151      * @return the option in {@code opts} or system properties, otherwise null
152      * @see #getBoolean(FileSystemOptions, String, Boolean)
153      *
154      * @since 2.0
155      */
156     protected Boolean getBoolean(final FileSystemOptions opts, final String name) {
157         return getBoolean(opts, name, null);
158     }
159 
160     /**
161      * Gets named option as boolean.
162      *
163      * @param opts file system options to work with
164      * @param name the option name
165      * @param defaultValue value to return if option is not present
166      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
167      * @see #getBoolean(FileSystemOptions, String, Boolean)
168      *
169      * @since 2.0
170      */
171     protected boolean getBoolean(final FileSystemOptions opts, final String name, final boolean defaultValue) {
172         return getBoolean(opts, name, Boolean.valueOf(defaultValue)).booleanValue();
173     }
174 
175     /**
176      * Gets named option as boolean.
177      *
178      * @param opts file system options to work with
179      * @param name the option name
180      * @param defaultValue value to return if option is not present
181      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
182      * @see #getBoolean(FileSystemOptions, String, Boolean)
183      *
184      * @since 2.0
185      */
186     protected Boolean getBoolean(final FileSystemOptions opts, final String name, final Boolean defaultValue) {
187         Boolean value = (Boolean) getParam(opts, name);
188         if (value == null) {
189             final String str = getProperty(name);
190             if (str == null) {
191                 return defaultValue;
192             }
193             value = Boolean.valueOf(str);
194         }
195         return value;
196     }
197 
198     /**
199      * Gets named option as byte.
200      *
201      * @param opts file system options to work with
202      * @param name the option name
203      * @return the option in {@code opts} or system properties, otherwise null
204      * @see #getByte(FileSystemOptions, String, Byte)
205      *
206      * @since 2.0
207      */
208     protected Byte getByte(final FileSystemOptions opts, final String name) {
209         return getByte(opts, name, null);
210     }
211 
212     /**
213      * Gets named option as byte.
214      *
215      * @param opts file system options to work with
216      * @param name the option name
217      * @param defaultValue value to return if option is not present
218      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
219      * @see #getByte(FileSystemOptions, String, Byte)
220      *
221      * @since 2.0
222      */
223     protected byte getByte(final FileSystemOptions opts, final String name, final byte defaultValue) {
224         return getByte(opts, name, Byte.valueOf(defaultValue)).byteValue();
225     }
226 
227     /**
228      * Gets named option as byte.
229      *
230      * @param opts file system options to work with
231      * @param name the option name
232      * @param defaultValue value to return if option is not present
233      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
234      *
235      * @since 2.0
236      */
237     protected Byte getByte(final FileSystemOptions opts, final String name, final Byte defaultValue) {
238         Byte value = (Byte) getParam(opts, name);
239         if (value == null) {
240             final String str = getProperty(name);
241             if (str == null) {
242                 return defaultValue;
243             }
244             value = Byte.valueOf(str);
245         }
246         return value;
247     }
248 
249     /**
250      * Gets named option as character.
251      *
252      * @param opts file system options to work with
253      * @param name the option name
254      * @return the option in {@code opts} or system properties, otherwise null
255      * @see #getCharacter(FileSystemOptions, String, Character)
256      *
257      * @since 2.0
258      */
259     protected Character getCharacter(final FileSystemOptions opts, final String name) {
260         return getCharacter(opts, name, null);
261     }
262 
263     /**
264      * Gets named option as character.
265      *
266      * @param opts file system options to work with
267      * @param name the option name
268      * @param defaultValue value to return if option is not present
269      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
270      * @see #getCharacter(FileSystemOptions, String, Character)
271      *
272      * @since 2.0
273      */
274     protected char getCharacter(final FileSystemOptions opts, final String name, final char defaultValue) {
275         return getCharacter(opts, name, new Character(defaultValue)).charValue();
276     }
277 
278     /**
279      * Gets named option as character.
280      *
281      * @param opts file system options to work with
282      * @param name the option name
283      * @param defaultValue value to return if option is not present
284      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
285      *
286      * @since 2.0
287      */
288     protected Character getCharacter(final FileSystemOptions opts, final String name, final Character defaultValue) {
289         Character value = (Character) getParam(opts, name);
290         if (value == null) {
291             final String str = getProperty(name);
292             if (str == null || str.length() <= 0) {
293                 return defaultValue;
294             }
295             value = new Character(str.charAt(0));
296         }
297         return value;
298     }
299 
300     /**
301      * Gets named option as double.
302      *
303      * @param opts file system options to work with
304      * @param name the option name
305      * @return the option in {@code opts} or system properties, otherwise null
306      * @see #getDouble(FileSystemOptions, String, Double)
307      *
308      * @since 2.0
309      */
310     protected Double getDouble(final FileSystemOptions opts, final String name) {
311         return getDouble(opts, name, null);
312     }
313 
314     /**
315      * Gets named option as double.
316      *
317      * @param opts file system options to work with
318      * @param name the option name
319      * @param defaultValue value to return if option is not present
320      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
321      * @see #getDouble(FileSystemOptions, String, Double)
322      *
323      * @since 2.0
324      */
325     protected double getDouble(final FileSystemOptions opts, final String name, final double defaultValue) {
326         return getDouble(opts, name, new Double(defaultValue)).doubleValue();
327     }
328 
329     /**
330      * Gets named option as double.
331      *
332      * @param opts file system options to work with
333      * @param name the option name
334      * @param defaultValue value to return if option is not present
335      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
336      *
337      * @since 2.0
338      */
339     protected Double getDouble(final FileSystemOptions opts, final String name, final Double defaultValue) {
340         Double value = (Double) getParam(opts, name);
341         if (value == null) {
342             final String str = getProperty(name);
343             if (str == null || str.length() <= 0) {
344                 return defaultValue;
345             }
346             value = Double.valueOf(str);
347         }
348         return value;
349     }
350 
351     /**
352      * Gets named option as enumeration.
353      *
354      * @param <E> enumeration type
355      * @param enumClass class of enumeration type
356      * @param opts file system options to work with
357      * @param name the option name *
358      * @return the option in {@code opts} or system properties, otherwise null
359      * @see #getEnum(Class, FileSystemOptions, String, Enum)
360      * @throws IllegalArgumentException if option value is not a known enumeration.
361      *
362      * @since 2.1
363      */
364     protected <E extends Enum<E>> E getEnum(final Class<E> enumClass, final FileSystemOptions opts, final String name) {
365         return this.<E>getEnum(enumClass, opts, name, null);
366     }
367 
368     /**
369      * Gets named option as enumeration.
370      *
371      * @param <E> enumeration type
372      * @param enumClass class of enumeration type
373      * @param opts file system options to work with
374      * @param name the option name
375      * @param defaultValue value to return if option is not present
376      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
377      * @see #getEnum(Class, FileSystemOptions, String, Enum)
378      * @throws IllegalArgumentException if option value is not a known enumeration.
379      *
380      * @since 2.1
381      */
382     protected <E extends Enum<E>> E getEnum(final Class<E> enumClass, final FileSystemOptions opts, final String name,
383             final E defaultValue) {
384         @SuppressWarnings("unchecked")
385         E value = (E) getParam(opts, name);
386         if (value == null) {
387             final String str = getProperty(name);
388             if (str == null) {
389                 return defaultValue;
390             }
391             value = Enum.valueOf(enumClass, str);
392         }
393         return value;
394     }
395 
396     /**
397      * Gets named option as float.
398      *
399      * @param opts file system options to work with
400      * @param name the option name
401      * @return the option in {@code opts} or system properties, otherwise null
402      * @see #getFloat(FileSystemOptions, String, Float)
403      * @throws NumberFormatException if option value is not a valid float.
404      *
405      * @since 2.0
406      */
407     protected Float getFloat(final FileSystemOptions opts, final String name) {
408         return getFloat(opts, name, null);
409     }
410 
411     /**
412      * Gets named option as float.
413      *
414      * @param opts file system options to work with
415      * @param name the option name
416      * @param defaultValue value to return if option is not present
417      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
418      * @see #getFloat(FileSystemOptions, String, Float)
419      * @throws NumberFormatException if option value is not a valid float.
420      *
421      * @since 2.0
422      */
423     protected float getFloat(final FileSystemOptions opts, final String name, final float defaultValue) {
424         return getFloat(opts, name, new Float(defaultValue)).floatValue();
425     }
426 
427     /**
428      * Gets named option as float.
429      *
430      * @param opts file system options to work with
431      * @param name the option name
432      * @param defaultValue value to return if option is not present
433      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
434      * @throws NumberFormatException if option value is not a valid float.
435      *
436      * @since 2.0
437      */
438     protected Float getFloat(final FileSystemOptions opts, final String name, final Float defaultValue) {
439         Float value = (Float) getParam(opts, name);
440         if (value == null) {
441             final String str = getProperty(name);
442             if (str == null || str.length() <= 0) {
443                 return defaultValue;
444             }
445             value = Float.valueOf(str);
446         }
447         return value;
448     }
449 
450     /**
451      * Gets named option as integer.
452      *
453      * @param opts file system options to work with
454      * @param name the option name
455      * @return the option in {@code opts} or system properties, otherwise null
456      * @see #getInteger(FileSystemOptions, String, Integer)
457      * @throws NumberFormatException if option value is not a valid integer.
458      *
459      * @since 2.0
460      */
461     protected Integer getInteger(final FileSystemOptions opts, final String name) {
462         return getInteger(opts, name, null);
463     }
464 
465     /**
466      * Gets named option as integer.
467      *
468      * @param opts file system options to work with
469      * @param name the option name
470      * @param defaultValue value to return if option is not present
471      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
472      * @see #getInteger(FileSystemOptions, String, Integer)
473      * @throws NumberFormatException if option value is not a valid integer.
474      *
475      * @since 2.0
476      */
477     protected int getInteger(final FileSystemOptions opts, final String name, final int defaultValue) {
478         return getInteger(opts, name, Integer.valueOf(defaultValue)).intValue();
479     }
480 
481     /**
482      * Gets named option as integer.
483      *
484      * @param opts file system options to work with
485      * @param name the option name
486      * @param defaultValue value to return if option is not present
487      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
488      * @throws NumberFormatException if option value is not a valid integer.
489      *
490      * @since 2.0
491      */
492     protected Integer getInteger(final FileSystemOptions opts, final String name, final Integer defaultValue) {
493         Integer value = (Integer) getParam(opts, name);
494         if (value == null) {
495             final String str = getProperty(name);
496             if (str == null) {
497                 return defaultValue;
498             }
499             value = Integer.valueOf(str);
500         }
501         return value;
502     }
503 
504     /**
505      * Gets named option as long.
506      *
507      * @param opts file system options to work with
508      * @param name the option name
509      * @return the option in {@code opts} or system properties, otherwise null
510      * @see #getLong(FileSystemOptions, String, Long)
511      * @throws NumberFormatException if option value is not a valid long.
512      *
513      * @since 2.0
514      */
515     protected Long getLong(final FileSystemOptions opts, final String name) {
516         return getLong(opts, name, null);
517     }
518 
519     /**
520      * Gets named option as long.
521      *
522      * @param opts file system options to work with
523      * @param name the option name
524      * @param defaultValue value to return if option is not present
525      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
526      * @see #getLong(FileSystemOptions, String, Long)
527      * @throws NumberFormatException if option value is not a valid long.
528      *
529      * @since 2.0
530      */
531     protected long getLong(final FileSystemOptions opts, final String name, final long defaultValue) {
532         return getLong(opts, name, Long.valueOf(defaultValue)).longValue();
533     }
534 
535     /**
536      * Gets named option as long.
537      *
538      * @param opts file system options to work with
539      * @param name the option name
540      * @param defaultValue value to return if option is not present
541      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
542      * @throws NumberFormatException if option value is not a valid long.
543      *
544      * @since 2.0
545      */
546     protected Long getLong(final FileSystemOptions opts, final String name, final Long defaultValue) {
547         Long value = (Long) getParam(opts, name);
548         if (value == null) {
549             final String str = getProperty(name);
550             if (str == null) {
551                 return defaultValue;
552             }
553             value = Long.valueOf(str);
554         }
555         return value;
556     }
557 
558     /**
559      * Gets named option as short.
560      *
561      * @param opts file system options to work with
562      * @param name the option name
563      * @return the option in {@code opts} or system properties, otherwise null
564      * @see #getShort(FileSystemOptions, String, Short)
565      * @throws NumberFormatException if option value is not a valid short.
566      *
567      * @since 2.0
568      */
569     protected Short getShort(final FileSystemOptions opts, final String name) {
570         return getShort(opts, name, null);
571     }
572 
573     /**
574      * Gets named option as short.
575      *
576      * @param opts file system options to work with
577      * @param name the option name
578      * @param defaultValue value to return if option is not present
579      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
580      * @see #getShort(FileSystemOptions, String, Short)
581      * @throws NumberFormatException if option value is not a valid short
582      *
583      * @since 2.0
584      */
585     protected short getShort(final FileSystemOptions opts, final String name, final short defaultValue) {
586         return getShort(opts, name, Short.valueOf(defaultValue)).shortValue();
587     }
588 
589     /**
590      * Gets named option as short.
591      *
592      * @param opts file system options to work with
593      * @param name the option name
594      * @param defaultValue value to return if option is not present
595      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
596      * @throws NumberFormatException if option value is not a valid short
597      *
598      * @since 2.0
599      */
600     protected Short getShort(final FileSystemOptions opts, final String name, final Short defaultValue) {
601         Short value = (Short) getParam(opts, name);
602         if (value == null) {
603             final String str = getProperty(name);
604             if (str == null) {
605                 return defaultValue;
606             }
607             value = Short.valueOf(str);
608         }
609         return value;
610     }
611 
612     /**
613      * Gets named option as String.
614      *
615      * @param opts file system options to work with
616      * @param name the option name
617      * @return the option in {@code opts} or system properties, otherwise null
618      * @see #getString(FileSystemOptions, String, String)
619      *
620      * @since 2.0
621      */
622     protected String getString(final FileSystemOptions opts, final String name) {
623         return getString(opts, name, null);
624     }
625 
626     /**
627      * Gets named option as String.
628      *
629      * @param opts file system options to work with
630      * @param name the option name
631      * @param defaultValue value to return if option is not present
632      * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
633      *
634      * @since 2.0
635      */
636     protected String getString(final FileSystemOptions opts, final String name, final String defaultValue) {
637         String value = (String) getParam(opts, name);
638         if (value == null) {
639             value = getProperty(name);
640             if (value == null) {
641                 return defaultValue;
642             }
643         }
644         return value;
645     }
646 
647     /**
648      * Gets the target of this configuration.
649      *
650      * @return the specific file system class
651      *
652      * @since 1.0
653      */
654     protected abstract Class<? extends FileSystem> getConfigClass();
655 
656     /**
657      * Converts the given name into a System property key.
658      *
659      * @param name a name to combine with the builder prefix
660      * @return name of system property
661      *
662      * @since 2.1
663      */
664     private String toPropertyKey(final String name) {
665         return this.prefix + name;
666     }
667 
668     /**
669      * Gets the system property for the given name.
670      *
671      * @param name The name to lookup combined with the prefix.
672      * @return a system property or null
673      *
674      * @since 2.1
675      */
676     private String getProperty(final String name) {
677         return System.getProperty(toPropertyKey(name));
678     }
679 
680 }