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      /**
43       * Gets the base path of the given path, for example a directory for a file.
44       *
45       * @param path the source path.
46       * @return the base path.
47       */
48      public abstract String getBasePath(String path);
49  
50      /**
51       * Gets the file name of the given path.
52       *
53       * @param path the source path.
54       * @return the file name.
55       */
56      public abstract String getFileName(String path);
57  
58      /**
59       * Gets the FileSystem options provider.
60       *
61       * @return the FileSystem options provider.
62       */
63      public FileOptionsProvider getFileOptionsProvider() {
64          return this.optionsProvider;
65      }
66  
67      /**
68       * Gets an input stream for a URL.
69       *
70       * @param url the source URL.
71       * @return an input stream.
72       * @throws ConfigurationException if an problem occurs getting the input stream.
73       */
74      public abstract InputStream getInputStream(URL url) throws ConfigurationException;
75  
76      /**
77       * Not abstract for binary compatibility.
78       *
79       * @param url TODO
80       * @param urlConnectionOptions Ignored.
81       * @return TODO
82       * @throws ConfigurationException TODO
83       * @since 2.8.0
84       */
85      public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException {
86          return getInputStream(url);
87      }
88  
89      /**
90       * Gets the logger used by this FileSystem.
91       *
92       * @return the logger
93       */
94      public ConfigurationLogger getLogger() {
95          final ConfigurationLogger result = log;
96          return result != null ? result : DEFAULT_LOG;
97      }
98  
99      /**
100      * Gets an output stream for a File.
101      *
102      * @param file the source File.
103      * @return an output stream.
104      * @throws ConfigurationException if an problem occurs getting the output stream.
105      */
106     public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
107 
108     /**
109      * Gets an output stream for a URL.
110      *
111      * @param url the source URL.
112      * @return an output stream.
113      * @throws ConfigurationException if an problem occurs getting the output stream.
114      */
115     public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
116 
117     /**
118      * Gets a path string for the given input where some values may be null.
119      * <p>
120      * The implementation decides on which argument take precedence.
121      * </p>
122      *
123      * @param file A file.
124      * @param url A URL.
125      * @param basePath A base path string.
126      * @param fileName A file name.
127      * @return A path string.
128      */
129     public abstract String getPath(File file, URL url, String basePath, String fileName);
130 
131     /**
132      * Gets a URL for a base path and file name.
133      *
134      * @param basePath The base path.
135      * @param fileName The file name.
136      * @return a URL.
137      * @throws MalformedURLException if a problem occurs creating the URL.
138      */
139     public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
140 
141     /**
142      * Locates a URL for a base path and file name.
143      *
144      * @param basePath The base path.
145      * @param fileName The file name.
146      * @return a URL.
147      */
148     public abstract URL locateFromURL(String basePath, String fileName);
149 
150     /**
151      * Sets the FileOptionsProvider
152      *
153      * @param provider The FileOptionsProvider
154      */
155     public void setFileOptionsProvider(final FileOptionsProvider provider) {
156         this.optionsProvider = provider;
157     }
158 
159     /**
160      * Sets the logger to be used by this FileSystem. This method makes it possible for clients to exactly control
161      * logging behavior. Per default a logger is set that will ignore all log messages. Derived classes that want to enable
162      * logging should call this method during their initialization with the logger to be used. Passing in a <strong>null</strong>
163      * argument disables logging.
164      *
165      * @param log the new logger
166      */
167     public void setLogger(final ConfigurationLogger log) {
168         this.log = log;
169     }
170 }