FileBasedBuilderParametersImpl.java

  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.configuration2.builder;

  18. import java.io.File;
  19. import java.net.URL;
  20. import java.util.Map;

  21. import org.apache.commons.configuration2.io.FileHandler;
  22. import org.apache.commons.configuration2.io.FileLocationStrategy;
  23. import org.apache.commons.configuration2.io.FileSystem;
  24. import org.apache.commons.configuration2.io.URLConnectionOptions;

  25. /**
  26.  * <p>
  27.  * An implementation of {@code BuilderParameters} which contains parameters related to {@code Configuration}
  28.  * implementations that are loaded from files.
  29.  * </p>
  30.  * <p>
  31.  * The parameters defined here are interpreted by builder implementations that can deal with file-based configurations.
  32.  * Note that these parameters are typically no initialization properties of configuration objects (i.e. they are not
  33.  * passed to set methods after the creation of the result configuration). Rather, the parameters object is stored as a
  34.  * whole in the builder's map with initialization parameters and can be accessed from there.
  35.  * </p>
  36.  * <p>
  37.  * This class is not thread-safe. It is intended that an instance is constructed and initialized by a single thread
  38.  * during configuration of a {@code ConfigurationBuilder}.
  39.  * </p>
  40.  *
  41.  * @since 2.0
  42.  */
  43. public class FileBasedBuilderParametersImpl extends BasicBuilderParameters implements FileBasedBuilderProperties<FileBasedBuilderParametersImpl> {
  44.     /** Constant for the key in the parameters map used by this class. */
  45.     private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX + "fileBased";

  46.     /** Property name for the reloading refresh delay. */
  47.     private static final String PROP_REFRESH_DELAY = "reloadingRefreshDelay";

  48.     /** Property name of the reloading detector factory. */
  49.     private static final String PROP_DETECTOR_FACTORY = "reloadingDetectorFactory";

  50.     /**
  51.      * Creates a new {@code FileBasedBuilderParametersImpl} object from the content of the given map. While
  52.      * {@code fromParameters()} expects that an object already exists and is stored in the given map, this method creates a
  53.      * new instance based on the content of the map. The map can contain properties of a {@code FileHandler} and some
  54.      * additional settings which are stored directly in the newly created object. If the map is <strong>null</strong>, an
  55.      * uninitialized instance is returned.
  56.      *
  57.      * @param map the map with properties (must not be <strong>null</strong>)
  58.      * @return the newly created instance
  59.      * @throws ClassCastException if the map contains invalid data
  60.      */
  61.     public static FileBasedBuilderParametersImpl fromMap(final Map<String, ?> map) {
  62.         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(FileHandler.fromMap(map));
  63.         if (map != null) {
  64.             params.setReloadingRefreshDelay((Long) map.get(PROP_REFRESH_DELAY));
  65.             params.setReloadingDetectorFactory((ReloadingDetectorFactory) map.get(PROP_DETECTOR_FACTORY));
  66.         }
  67.         return params;
  68.     }

  69.     /**
  70.      * Looks up an instance of this class in the specified parameters map. This is equivalent to
  71.      * {@code fromParameters(params, false};}
  72.      *
  73.      * @param params the map with parameters (must not be <strong>null</strong>
  74.      * @return the instance obtained from the map or <strong>null</strong>
  75.      * @throws IllegalArgumentException if the map is <strong>null</strong>
  76.      */
  77.     public static FileBasedBuilderParametersImpl fromParameters(final Map<String, ?> params) {
  78.         return fromParameters(params, false);
  79.     }

  80.     /**
  81.      * Looks up an instance of this class in the specified parameters map and optionally creates a new one if none is found.
  82.      * This method can be used to obtain an instance of this class which has been stored in a parameters map. It is
  83.      * compatible with the {@code getParameters()} method.
  84.      *
  85.      * @param params the map with parameters (must not be <strong>null</strong>
  86.      * @param createIfMissing determines the behavior if no instance is found in the map; if <strong>true</strong>, a new instance
  87.      *        with default settings is created; if <strong>false</strong>, <strong>null</strong> is returned
  88.      * @return the instance obtained from the map or <strong>null</strong>
  89.      * @throws IllegalArgumentException if the map is <strong>null</strong>
  90.      */
  91.     public static FileBasedBuilderParametersImpl fromParameters(final Map<String, ?> params, final boolean createIfMissing) {
  92.         if (params == null) {
  93.             throw new IllegalArgumentException("Parameters map must not be null!");
  94.         }

  95.         FileBasedBuilderParametersImpl instance = (FileBasedBuilderParametersImpl) params.get(PARAM_KEY);
  96.         if (instance == null && createIfMissing) {
  97.             instance = new FileBasedBuilderParametersImpl();
  98.         }
  99.         return instance;
  100.     }

  101.     /**
  102.      * Stores the associated file handler for the location of the configuration.
  103.      */
  104.     private FileHandler fileHandler;

  105.     /** The factory for reloading detectors. */
  106.     private ReloadingDetectorFactory reloadingDetectorFactory;

  107.     /** The refresh delay for reloading support. */
  108.     private Long reloadingRefreshDelay;

  109.     /**
  110.      * Creates a new instance of {@code FileBasedBuilderParametersImpl} with an uninitialized {@code FileHandler} object.
  111.      */
  112.     public FileBasedBuilderParametersImpl() {
  113.         this(null);
  114.     }

  115.     /**
  116.      * Creates a new instance of {@code FileBasedBuilderParametersImpl} and associates it with the given {@code FileHandler}
  117.      * object. If the handler is <strong>null</strong>, a new handler instance is created.
  118.      *
  119.      * @param handler the associated {@code FileHandler} (can be <strong>null</strong>)
  120.      */
  121.     public FileBasedBuilderParametersImpl(final FileHandler handler) {
  122.         fileHandler = handler != null ? handler : new FileHandler();
  123.     }

  124.     /**
  125.      * {@inheritDoc} This implementation also creates a copy of the {@code FileHandler}.
  126.      */
  127.     @Override
  128.     public FileBasedBuilderParametersImpl clone() {
  129.         final FileBasedBuilderParametersImpl copy = (FileBasedBuilderParametersImpl) super.clone();
  130.         copy.fileHandler = new FileHandler(fileHandler.getContent(), fileHandler);
  131.         return copy;
  132.     }

  133.     /**
  134.      * Gets the {@code FileHandler} managed by this object. This object is updated every time the file location is
  135.      * changed.
  136.      *
  137.      * @return the managed {@code FileHandler}
  138.      */
  139.     public FileHandler getFileHandler() {
  140.         return fileHandler;
  141.     }

  142.     /**
  143.      * {@inheritDoc} This implementation returns a map which contains this object itself under a specific key. The static
  144.      * {@code fromParameters()} method can be used to extract an instance from a parameters map. Of course, the properties
  145.      * inherited from the base class are also added to the result map.
  146.      */
  147.     @Override
  148.     public Map<String, Object> getParameters() {
  149.         final Map<String, Object> params = super.getParameters();
  150.         params.put(PARAM_KEY, this);
  151.         return params;
  152.     }

  153.     /**
  154.      * Gets the {@code ReloadingDetectorFactory}. Result may be <strong>null</strong> which means that the default factory is to be
  155.      * used.
  156.      *
  157.      * @return the {@code ReloadingDetectorFactory}
  158.      */
  159.     public ReloadingDetectorFactory getReloadingDetectorFactory() {
  160.         return reloadingDetectorFactory;
  161.     }

  162.     /**
  163.      * Gets the refresh delay for reload operations. Result may be <strong>null</strong> if this value has not been set.
  164.      *
  165.      * @return the reloading refresh delay
  166.      */
  167.     public Long getReloadingRefreshDelay() {
  168.         return reloadingRefreshDelay;
  169.     }

  170.     /**
  171.      * {@inheritDoc} This implementation takes some properties defined in this class into account.
  172.      */
  173.     @Override
  174.     public void inheritFrom(final Map<String, ?> source) {
  175.         super.inheritFrom(source);

  176.         final FileBasedBuilderParametersImpl srcParams = fromParameters(source);
  177.         if (srcParams != null) {
  178.             setFileSystem(srcParams.getFileHandler().getFileSystem());
  179.             setLocationStrategy(srcParams.getFileHandler().getLocationStrategy());
  180.             if (srcParams.getFileHandler().getEncoding() != null) {
  181.                 setEncoding(srcParams.getFileHandler().getEncoding());
  182.             }
  183.             if (srcParams.getReloadingDetectorFactory() != null) {
  184.                 setReloadingDetectorFactory(srcParams.getReloadingDetectorFactory());
  185.             }
  186.             if (srcParams.getReloadingRefreshDelay() != null) {
  187.                 setReloadingRefreshDelay(srcParams.getReloadingRefreshDelay());
  188.             }
  189.         }
  190.     }

  191.     @Override
  192.     public FileBasedBuilderParametersImpl setBasePath(final String path) {
  193.         getFileHandler().setBasePath(path);
  194.         return this;
  195.     }

  196.     @Override
  197.     public FileBasedBuilderParametersImpl setEncoding(final String enc) {
  198.         getFileHandler().setEncoding(enc);
  199.         return this;
  200.     }

  201.     @Override
  202.     public FileBasedBuilderParametersImpl setFile(final File file) {
  203.         getFileHandler().setFile(file);
  204.         return this;
  205.     }

  206.     @Override
  207.     public FileBasedBuilderParametersImpl setFileName(final String name) {
  208.         getFileHandler().setFileName(name);
  209.         return this;
  210.     }

  211.     @Override
  212.     public FileBasedBuilderParametersImpl setFileSystem(final FileSystem fs) {
  213.         getFileHandler().setFileSystem(fs);
  214.         return this;
  215.     }

  216.     @Override
  217.     public FileBasedBuilderParametersImpl setLocationStrategy(final FileLocationStrategy strategy) {
  218.         getFileHandler().setLocationStrategy(strategy);
  219.         return this;
  220.     }

  221.     @Override
  222.     public FileBasedBuilderParametersImpl setPath(final String path) {
  223.         getFileHandler().setPath(path);
  224.         return this;
  225.     }

  226.     @Override
  227.     public FileBasedBuilderParametersImpl setReloadingDetectorFactory(final ReloadingDetectorFactory reloadingDetectorFactory) {
  228.         this.reloadingDetectorFactory = reloadingDetectorFactory;
  229.         return this;
  230.     }

  231.     @Override
  232.     public FileBasedBuilderParametersImpl setReloadingRefreshDelay(final Long reloadingRefreshDelay) {
  233.         this.reloadingRefreshDelay = reloadingRefreshDelay;
  234.         return this;
  235.     }

  236.     @Override
  237.     public FileBasedBuilderParametersImpl setURL(final URL url) {
  238.         getFileHandler().setURL(url);
  239.         return this;
  240.     }

  241.     @Override
  242.     public FileBasedBuilderParametersImpl setURL(final URL url, final URLConnectionOptions urlConnectionOptions) {
  243.         getFileHandler().setURL(url, urlConnectionOptions);
  244.         return this;
  245.     }
  246. }