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.configuration2.io;
018
019import java.io.File;
020import java.io.InputStream;
021import java.io.OutputStream;
022import java.net.MalformedURLException;
023import java.net.URL;
024
025import org.apache.commons.configuration2.ex.ConfigurationException;
026
027/**
028 * Abstract layer to allow various types of file systems.
029 *
030 * @since 1.7
031 */
032public abstract class FileSystem {
033    /** Constant for the default logger. */
034    private static final ConfigurationLogger DEFAULT_LOG = ConfigurationLogger.newDummyLogger();
035
036    /** The Logger */
037    private volatile ConfigurationLogger log;
038
039    /** FileSystem options provider */
040    private volatile FileOptionsProvider optionsProvider;
041
042    public abstract String getBasePath(String path);
043
044    public abstract String getFileName(String path);
045
046    public FileOptionsProvider getFileOptionsProvider() {
047        return this.optionsProvider;
048    }
049
050    public abstract InputStream getInputStream(URL url) throws ConfigurationException;
051
052    /**
053     * Not abstract for binary compatibility.
054     *
055     * @param url TODO
056     * @param urlConnectionOptions Ignored.
057     * @return TODO
058     * @throws ConfigurationException TODO
059     *
060     * @since 2.8.0
061     */
062    public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException {
063        return getInputStream(url);
064    }
065
066    /**
067     * Gets the logger used by this FileSystem.
068     *
069     * @return the logger
070     */
071    public ConfigurationLogger getLogger() {
072        final ConfigurationLogger result = log;
073        return result != null ? result : DEFAULT_LOG;
074    }
075
076    public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
077
078    public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
079
080    public abstract String getPath(File file, URL url, String basePath, String fileName);
081
082    public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
083
084    public abstract URL locateFromURL(String basePath, String fileName);
085
086    /**
087     * Sets the FileOptionsProvider
088     *
089     * @param provider The FileOptionsProvider
090     */
091    public void setFileOptionsProvider(final FileOptionsProvider provider) {
092        this.optionsProvider = provider;
093    }
094
095    /**
096     * Allows setting the logger to be used by this FileSystem. This method makes it possible for clients to exactly control
097     * logging behavior. Per default a logger is set that will ignore all log messages. Derived classes that want to enable
098     * logging should call this method during their initialization with the logger to be used. Passing in a <b>null</b>
099     * argument disables logging.
100     *
101     * @param log the new logger
102     */
103    public void setLogger(final ConfigurationLogger log) {
104        this.log = log;
105    }
106}