FileSystem.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.io;

  18. import java.io.File;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;

  23. import org.apache.commons.configuration2.ex.ConfigurationException;

  24. /**
  25.  * Abstract layer to allow various types of file systems.
  26.  *
  27.  * @since 1.7
  28.  */
  29. public abstract class FileSystem {
  30.     /** Constant for the default logger. */
  31.     private static final ConfigurationLogger DEFAULT_LOG = ConfigurationLogger.newDummyLogger();

  32.     /** The Logger. */
  33.     private volatile ConfigurationLogger log;

  34.     /** FileSystem options provider. */
  35.     private volatile FileOptionsProvider optionsProvider;

  36.     /**
  37.      * Gets the base path of the given path, for example a directory for a file.
  38.      *
  39.      * @param path the source path.
  40.      * @return the base path.
  41.      */
  42.     public abstract String getBasePath(String path);

  43.     /**
  44.      * Gets the file name of the given path.
  45.      *
  46.      * @param path the source path.
  47.      * @return the file name.
  48.      */
  49.     public abstract String getFileName(String path);

  50.     /**
  51.      * Gets the FileSystem options provider.
  52.      *
  53.      * @return the FileSystem options provider.
  54.      */
  55.     public FileOptionsProvider getFileOptionsProvider() {
  56.         return this.optionsProvider;
  57.     }

  58.     /**
  59.      * Gets an input stream for a URL.
  60.      *
  61.      * @param url the source URL.
  62.      * @return an input stream.
  63.      * @throws ConfigurationException if an problem occurs getting the input stream.
  64.      */
  65.     public abstract InputStream getInputStream(URL url) throws ConfigurationException;

  66.     /**
  67.      * Not abstract for binary compatibility.
  68.      *
  69.      * @param url TODO
  70.      * @param urlConnectionOptions Ignored.
  71.      * @return TODO
  72.      * @throws ConfigurationException TODO
  73.      * @since 2.8.0
  74.      */
  75.     public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException {
  76.         return getInputStream(url);
  77.     }

  78.     /**
  79.      * Gets the logger used by this FileSystem.
  80.      *
  81.      * @return the logger
  82.      */
  83.     public ConfigurationLogger getLogger() {
  84.         final ConfigurationLogger result = log;
  85.         return result != null ? result : DEFAULT_LOG;
  86.     }

  87.     /**
  88.      * Gets an output stream for a File.
  89.      *
  90.      * @param file the source File.
  91.      * @return an output stream.
  92.      * @throws ConfigurationException if an problem occurs getting the output stream.
  93.      */
  94.     public abstract OutputStream getOutputStream(File file) throws ConfigurationException;

  95.     /**
  96.      * Gets an output stream for a URL.
  97.      *
  98.      * @param url the source URL.
  99.      * @return an output stream.
  100.      * @throws ConfigurationException if an problem occurs getting the output stream.
  101.      */
  102.     public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;

  103.     /**
  104.      * Gets a path string for the given input where some values may be null.
  105.      * <p>
  106.      * The implementation decides on which argument take precedence.
  107.      * </p>
  108.      *
  109.      * @param file A file.
  110.      * @param url A URL.
  111.      * @param basePath A base path string.
  112.      * @param fileName A file name.
  113.      * @return A path string.
  114.      */
  115.     public abstract String getPath(File file, URL url, String basePath, String fileName);

  116.     /**
  117.      * Gets a URL for a base path and file name.
  118.      *
  119.      * @param basePath The base path.
  120.      * @param fileName The file name.
  121.      * @return a URL.
  122.      * @throws MalformedURLException if a problem occurs creating the URL.
  123.      */
  124.     public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;

  125.     /**
  126.      * Locates a URL for a base path and file name.
  127.      *
  128.      * @param basePath The base path.
  129.      * @param fileName The file name.
  130.      * @return a URL.
  131.      */
  132.     public abstract URL locateFromURL(String basePath, String fileName);

  133.     /**
  134.      * Sets the FileOptionsProvider
  135.      *
  136.      * @param provider The FileOptionsProvider
  137.      */
  138.     public void setFileOptionsProvider(final FileOptionsProvider provider) {
  139.         this.optionsProvider = provider;
  140.     }

  141.     /**
  142.      * Sets the logger to be used by this FileSystem. This method makes it possible for clients to exactly control
  143.      * logging behavior. Per default a logger is set that will ignore all log messages. Derived classes that want to enable
  144.      * logging should call this method during their initialization with the logger to be used. Passing in a <strong>null</strong>
  145.      * argument disables logging.
  146.      *
  147.      * @param log the new logger
  148.      */
  149.     public void setLogger(final ConfigurationLogger log) {
  150.         this.log = log;
  151.     }
  152. }