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.vfs2;
018
019/**
020 * Abstract class which has the right to fill FileSystemOptions.
021 */
022public abstract class FileSystemConfigBuilder
023{
024    /** Default prefix to use when resolving system properties */
025    private static final String PREFIX = "vfs.";
026
027    /** The root uri of the file system */
028    private static final String ROOTURI = "rootURI";
029
030    /** The prefix to use when resolving system properties */
031    private final String prefix;
032
033    protected FileSystemConfigBuilder()
034    {
035        this.prefix = PREFIX;
036    }
037
038    /** @since 2.0 */
039    protected FileSystemConfigBuilder(final String component)
040    {
041        this.prefix = PREFIX + component;
042    }
043
044    /**
045     * The root URI of the file system.
046     * @param opts The FileSystem options
047     * @param rootURI The creator name to be associated with the file.
048     * @since 2.0
049     */
050    public void setRootURI(final FileSystemOptions opts, final String rootURI)
051    {
052        setParam(opts, ROOTURI, rootURI);
053    }
054
055    /**
056     * Return the root URI of the file system.
057     * @param opts The FileSystem options
058     * @return The root URI.
059     * @since 2.0
060     */
061    public String getRootURI(final FileSystemOptions opts)
062    {
063        return getString(opts, ROOTURI);
064    }
065
066    /**
067     * @since 2.1
068     */
069    protected void setParam(final FileSystemOptions opts, final String name, final boolean value)
070    {
071        setParam(opts, name, Boolean.valueOf(value));
072    }
073
074    protected void setParam(final FileSystemOptions opts, final String name, final Object value)
075    {
076        opts.setOption(getConfigClass(), name, value);
077    }
078
079    protected Object getParam(final FileSystemOptions opts, final String name)
080    {
081        if (opts == null)
082        {
083            return null;
084        }
085
086        return opts.getOption(getConfigClass(), name);
087    }
088
089    /**
090     * Gets the system property for the given name.
091     *
092     * @param name The name to lookup combined with the prefix.
093     * @return a system property.
094     */
095    private String getProperty(final String name)
096    {
097        return System.getProperty(toPropertyKey(name));
098    }
099
100    protected boolean hasParam(final FileSystemOptions opts, final String name)
101    {
102        return opts != null && opts.hasOption(getConfigClass(), name);
103    }
104
105    /** @since 2.0 */
106    protected boolean hasObject(final FileSystemOptions opts, final String name)
107    {
108        return hasParam(opts, name) || System.getProperties().containsKey(toPropertyKey(name));
109    }
110
111    /** @since 2.0 */
112    protected Boolean getBoolean(final FileSystemOptions opts, final String name)
113    {
114        return getBoolean(opts, name, null);
115    }
116
117    /** @since 2.0 */
118    protected boolean getBoolean(final FileSystemOptions opts, final String name, final boolean defaultValue)
119    {
120        return getBoolean(opts, name, Boolean.valueOf(defaultValue)).booleanValue();
121    }
122
123    /** @since 2.0 */
124    protected Boolean getBoolean(final FileSystemOptions opts, final String name, final Boolean defaultValue)
125    {
126        Boolean value = (Boolean) getParam(opts, name);
127        if (value == null)
128        {
129            final String str = getProperty(name);
130            if (str == null)
131            {
132                return defaultValue;
133            }
134            value = Boolean.valueOf(str);
135        }
136        return value;
137    }
138
139    /** @since 2.0 */
140    protected Byte getByte(final FileSystemOptions opts, final String name)
141    {
142        return getByte(opts, name, null);
143    }
144
145    /** @since 2.0 */
146    protected byte getByte(final FileSystemOptions opts, final String name, final byte defaultValue)
147    {
148        return getByte(opts, name, Byte.valueOf(defaultValue)).byteValue();
149    }
150
151    /** @since 2.0 */
152    protected Byte getByte(final FileSystemOptions opts, final String name, final Byte defaultValue)
153    {
154        Byte value = (Byte) getParam(opts, name);
155        if (value == null)
156        {
157            final String str = getProperty(name);
158            if (str == null)
159            {
160                return defaultValue;
161            }
162            value = Byte.valueOf(str);
163        }
164        return value;
165    }
166
167    /** @since 2.0 */
168    protected Character getCharacter(final FileSystemOptions opts, final String name)
169    {
170        return getCharacter(opts, name, null);
171    }
172
173    /** @since 2.0 */
174    protected char getCharacter(final FileSystemOptions opts, final String name, final char defaultValue)
175    {
176        return getCharacter(opts, name, new Character(defaultValue)).charValue();
177    }
178
179    /** @since 2.0 */
180    protected Character getCharacter(final FileSystemOptions opts, final String name, final Character defaultValue)
181    {
182        Character value = (Character) getParam(opts, name);
183        if (value == null)
184        {
185            final String str = getProperty(name);
186            if (str == null || str.length() <= 0)
187            {
188                return defaultValue;
189            }
190            value = new Character(str.charAt(0));
191        }
192        return value;
193    }
194
195    /** @since 2.0 */
196    protected Double getDouble(final FileSystemOptions opts, final String name)
197    {
198        return getDouble(opts, name, null);
199    }
200
201    /** @since 2.0 */
202    protected double getDouble(final FileSystemOptions opts, final String name, final double defaultValue)
203    {
204        return getDouble(opts, name, new Double(defaultValue)).doubleValue();
205    }
206
207    /** @since 2.0 */
208    protected Double getDouble(final FileSystemOptions opts, final String name, final Double defaultValue)
209    {
210        Double value = (Double) getParam(opts, name);
211        if (value == null)
212        {
213            final String str = getProperty(name);
214            if (str == null || str.length() <= 0)
215            {
216                return defaultValue;
217            }
218            value = Double.valueOf(str);
219        }
220        return value;
221    }
222
223    /** @since 2.1 */
224    protected <E extends Enum<E>> E getEnum(final Class<E> enumClass, final FileSystemOptions opts, final String name)
225    {
226        return this.<E>getEnum(enumClass, opts, name, null);
227    }
228
229    /** @since 2.1 */
230    protected <E extends Enum<E>> E getEnum(final Class<E> enumClass, final FileSystemOptions opts, final String name, final E defaultValue)
231    {
232        @SuppressWarnings("unchecked")
233        E value = (E) getParam(opts, name);
234        if (value == null)
235        {
236            final String str = getProperty(name);
237            if (str == null)
238            {
239                return defaultValue;
240            }
241            value = Enum.valueOf(enumClass, str);
242        }
243        return value;
244    }
245
246    /** @since 2.0 */
247    protected Float getFloat(final FileSystemOptions opts, final String name)
248    {
249        return getFloat(opts, name, null);
250    }
251
252    /** @since 2.0 */
253    protected float getFloat(final FileSystemOptions opts, final String name, final float defaultValue)
254    {
255        return getFloat(opts, name, new Float(defaultValue)).floatValue();
256    }
257
258    /** @since 2.0 */
259    protected Float getFloat(final FileSystemOptions opts, final String name, final Float defaultValue)
260    {
261        Float value = (Float) getParam(opts, name);
262        if (value == null)
263        {
264            final String str = getProperty(name);
265            if (str == null || str.length() <= 0)
266            {
267                return defaultValue;
268            }
269            value = Float.valueOf(str);
270        }
271        return value;
272    }
273
274    /** @since 2.0 */
275    protected Integer getInteger(final FileSystemOptions opts, final String name)
276    {
277        return getInteger(opts, name, null);
278    }
279
280    /** @since 2.0 */
281    protected int getInteger(final FileSystemOptions opts, final String name, final int defaultValue)
282    {
283        return getInteger(opts, name, Integer.valueOf(defaultValue)).intValue();
284    }
285
286    /** @since 2.0 */
287    protected Integer getInteger(final FileSystemOptions opts, final String name, final Integer defaultValue)
288    {
289        Integer value = (Integer) getParam(opts, name);
290        if (value == null)
291        {
292            final String str = getProperty(name);
293            if (str == null)
294            {
295                return defaultValue;
296            }
297            value = Integer.valueOf(str);
298        }
299        return value;
300    }
301
302    /** @since 2.0 */
303    protected Long getLong(final FileSystemOptions opts, final String name)
304    {
305        return getLong(opts, name, null);
306    }
307
308    /** @since 2.0 */
309    protected long getLong(final FileSystemOptions opts, final String name, final long defaultValue)
310    {
311        return getLong(opts, name, Long.valueOf(defaultValue)).longValue();
312    }
313
314    /** @since 2.0 */
315    protected Long getLong(final FileSystemOptions opts, final String name, final Long defaultValue)
316    {
317        Long value = (Long) getParam(opts, name);
318        if (value == null)
319        {
320            final String str = getProperty(name);
321            if (str == null)
322            {
323                return defaultValue;
324            }
325            value = Long.valueOf(str);
326        }
327        return value;
328    }
329
330    /** @since 2.0 */
331    protected Short getShort(final FileSystemOptions opts, final String name)
332    {
333        return getShort(opts, name, null);
334    }
335
336    /** @since 2.0 */
337    protected short getShort(final FileSystemOptions opts, final String name, final short defaultValue)
338    {
339        return getShort(opts, name, Short.valueOf(defaultValue)).shortValue();
340    }
341
342    /** @since 2.0 */
343    protected Short getShort(final FileSystemOptions opts, final String name, final Short defaultValue)
344    {
345        Short value = (Short) getParam(opts, name);
346        if (value == null)
347        {
348            final String str = getProperty(name);
349            if (str == null)
350            {
351                return defaultValue;
352            }
353            value = Short.valueOf(str);
354        }
355        return value;
356    }
357
358    /** @since 2.0 */
359    protected String getString(final FileSystemOptions opts, final String name)
360    {
361        return getString(opts, name, null);
362    }
363
364    /** @since 2.0 */
365    protected String getString(final FileSystemOptions opts, final String name, final String defaultValue)
366    {
367        String value = (String) getParam(opts, name);
368        if (value == null)
369        {
370            value = getProperty(name);
371            if (value == null)
372            {
373                return defaultValue;
374            }
375        }
376        return value;
377    }
378
379    protected abstract Class<? extends FileSystem> getConfigClass();
380
381    /**
382     * Converts the given name into a System property key for this builder.
383     *
384     * @param name a name to combine with the builder prefix.
385     * @return a System property key for this builder.
386     */
387    private String toPropertyKey(final String name)
388    {
389        return this.prefix + name;
390    }
391
392}