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.configuration;
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.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.logging.impl.NoOpLog;
28  
29  /**
30   * Abstract layer to allow various types of file systems.
31   * @since 1.7
32   * @author <a
33   * href="http://commons.apache.org/configuration/team-list.html">Commons Configuration team</a>
34   * @version $Id: FileSystem.java 1209996 2011-12-03 20:24:21Z oheger $
35   */
36  public abstract class FileSystem
37  {
38      /** The name of the system property that can be used to set the file system class name */
39      private static final String FILE_SYSTEM = "org.apache.commons.configuration.filesystem";
40  
41      /** The default file system */
42      private static FileSystem fileSystem;
43  
44      /** The Logger */
45      private Log log;
46  
47      /** FileSystem options provider */
48      private FileOptionsProvider optionsProvider;
49  
50      public FileSystem()
51      {
52          setLogger(null);
53      }
54  
55      /**
56       * Returns the logger used by this FileSystem.
57       *
58       * @return the logger
59       */
60      public Log getLogger()
61      {
62          return log;
63      }
64  
65      /**
66       * Allows to set the logger to be used by this FileSystem. This
67       * method makes it possible for clients to exactly control logging behavior.
68       * Per default a logger is set that will ignore all log messages. Derived
69       * classes that want to enable logging should call this method during their
70       * initialization with the logger to be used.
71       *
72       * @param log the new logger
73       */
74      public void setLogger(Log log)
75      {
76          this.log = (log != null) ? log : new NoOpLog();
77      }
78  
79      static
80      {
81          String fsClassName = System.getProperty(FILE_SYSTEM);
82          if (fsClassName != null)
83          {
84              Log log = LogFactory.getLog(FileSystem.class);
85  
86              try
87              {
88                  Class<?> clazz = Class.forName(fsClassName);
89                  if (FileSystem.class.isAssignableFrom(clazz))
90                  {
91                      fileSystem = (FileSystem) clazz.newInstance();
92                      if (log.isDebugEnabled())
93                      {
94                          log.debug("Using " + fsClassName);
95                      }
96                  }
97              }
98              catch (InstantiationException ex)
99              {
100                 log.error("Unable to create " + fsClassName, ex);
101             }
102             catch (IllegalAccessException ex)
103             {
104                 log.error("Unable to create " + fsClassName, ex);
105             }
106             catch (ClassNotFoundException ex)
107             {
108                 log.error("Unable to create " + fsClassName, ex);
109             }
110         }
111 
112         if (fileSystem == null)
113         {
114             fileSystem = new DefaultFileSystem();
115         }
116     }
117 
118     /**
119      * Set the FileSystem to use.
120      * @param fs The FileSystem
121      * @throws NullPointerException if the FileSystem parameter is null.
122      */
123     public static void setDefaultFileSystem(FileSystem fs) throws NullPointerException
124     {
125         if (fs == null)
126         {
127             throw new NullPointerException("A FileSystem implementation is required");
128         }
129         fileSystem = fs;
130     }
131 
132     /**
133      * Reset the FileSystem to the default.
134      */
135     public static void resetDefaultFileSystem()
136     {
137         fileSystem = new DefaultFileSystem();
138     }
139 
140     /**
141      * Retrieve the FileSystem being used.
142      * @return The FileSystem.
143      */
144     public static FileSystem getDefaultFileSystem()
145     {
146         return fileSystem;
147     }
148 
149     /**
150      * Set the FileOptionsProvider
151      * @param provider The FileOptionsProvider
152      */
153     public void setFileOptionsProvider(FileOptionsProvider provider)
154     {
155         this.optionsProvider = provider;
156     }
157 
158     public FileOptionsProvider getFileOptionsProvider()
159     {
160         return this.optionsProvider;
161     }
162 
163     public abstract InputStream getInputStream(String basePath, String fileName)
164             throws ConfigurationException;
165 
166     public abstract InputStream getInputStream(URL url) throws ConfigurationException;
167 
168     public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
169 
170     public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
171 
172     public abstract String getPath(File file, URL url, String basePath, String fileName);
173 
174     public abstract String getBasePath(String path);
175 
176     public abstract String getFileName(String path);
177 
178     public abstract URL locateFromURL(String basePath, String fileName);
179 
180     public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
181 }