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.configuration2.io;
18  
19  import java.io.File;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import org.apache.commons.configuration2.ex.ConfigurationException;
26  
27  /**
28   * Abstract layer to allow various types of file systems.
29   *
30   * @since 1.7
31   */
32  public abstract class FileSystem {
33      /** Constant for the default logger. */
34      private static final ConfigurationLogger DEFAULT_LOG = ConfigurationLogger.newDummyLogger();
35  
36      /** The Logger */
37      private volatile ConfigurationLogger log;
38  
39      /** FileSystem options provider */
40      private volatile FileOptionsProvider optionsProvider;
41  
42      public abstract String getBasePath(String path);
43  
44      public abstract String getFileName(String path);
45  
46      public FileOptionsProvider getFileOptionsProvider() {
47          return this.optionsProvider;
48      }
49  
50      public abstract InputStream getInputStream(URL url) throws ConfigurationException;
51  
52      /**
53       * Not abstract for binary compatibility.
54       *
55       * @param url TODO
56       * @param urlConnectionOptions Ignored.
57       * @return TODO
58       * @throws ConfigurationException TODO
59       *
60       * @since 2.8.0
61       */
62      public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException {
63          return getInputStream(url);
64      }
65  
66      /**
67       * Gets the logger used by this FileSystem.
68       *
69       * @return the logger
70       */
71      public ConfigurationLogger getLogger() {
72          final ConfigurationLogger result = log;
73          return result != null ? result : DEFAULT_LOG;
74      }
75  
76      public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
77  
78      public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
79  
80      public abstract String getPath(File file, URL url, String basePath, String fileName);
81  
82      public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
83  
84      public abstract URL locateFromURL(String basePath, String fileName);
85  
86      /**
87       * Sets the FileOptionsProvider
88       *
89       * @param provider The FileOptionsProvider
90       */
91      public void setFileOptionsProvider(final FileOptionsProvider provider) {
92          this.optionsProvider = provider;
93      }
94  
95      /**
96       * Allows setting the logger to be used by this FileSystem. This method makes it possible for clients to exactly control
97       * logging behavior. Per default a logger is set that will ignore all log messages. Derived classes that want to enable
98       * logging should call this method during their initialization with the logger to be used. Passing in a <b>null</b>
99       * argument disables logging.
100      *
101      * @param log the new logger
102      */
103     public void setLogger(final ConfigurationLogger log) {
104         this.log = log;
105     }
106 }