Coverage Report - org.apache.commons.configuration.FileSystem
 
Classes in this File Line Coverage Branch Coverage Complexity
FileSystem
50%
17/34
50%
2/4
1,176
 
 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: org.apache.commons.configuration.FileSystem.html 910708 2014-05-31 19:13:45Z 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  119
     {
 52  119
         setLogger(null);
 53  119
     }
 54  
 
 55  
     /**
 56  
      * Returns the logger used by this FileSystem.
 57  
      *
 58  
      * @return the logger
 59  
      */
 60  
     public Log getLogger()
 61  
     {
 62  0
         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  119
         this.log = (log != null) ? log : new NoOpLog();
 77  119
     }
 78  
 
 79  
     static
 80  
     {
 81  1
         String fsClassName = System.getProperty(FILE_SYSTEM);
 82  1
         if (fsClassName != null)
 83  
         {
 84  0
             Log log = LogFactory.getLog(FileSystem.class);
 85  
 
 86  
             try
 87  
             {
 88  0
                 Class<?> clazz = Class.forName(fsClassName);
 89  0
                 if (FileSystem.class.isAssignableFrom(clazz))
 90  
                 {
 91  0
                     fileSystem = (FileSystem) clazz.newInstance();
 92  0
                     if (log.isDebugEnabled())
 93  
                     {
 94  0
                         log.debug("Using " + fsClassName);
 95  
                     }
 96  
                 }
 97  
             }
 98  0
             catch (InstantiationException ex)
 99  
             {
 100  0
                 log.error("Unable to create " + fsClassName, ex);
 101  
             }
 102  0
             catch (IllegalAccessException ex)
 103  
             {
 104  0
                 log.error("Unable to create " + fsClassName, ex);
 105  
             }
 106  0
             catch (ClassNotFoundException ex)
 107  
             {
 108  0
                 log.error("Unable to create " + fsClassName, ex);
 109  0
             }
 110  
         }
 111  
 
 112  1
         if (fileSystem == null)
 113  
         {
 114  1
             fileSystem = new DefaultFileSystem();
 115  
         }
 116  1
     }
 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  53
         if (fs == null)
 126  
         {
 127  0
             throw new NullPointerException("A FileSystem implementation is required");
 128  
         }
 129  53
         fileSystem = fs;
 130  53
     }
 131  
 
 132  
     /**
 133  
      * Reset the FileSystem to the default.
 134  
      */
 135  
     public static void resetDefaultFileSystem()
 136  
     {
 137  58
         fileSystem = new DefaultFileSystem();
 138  58
     }
 139  
 
 140  
     /**
 141  
      * Retrieve the FileSystem being used.
 142  
      * @return The FileSystem.
 143  
      */
 144  
     public static FileSystem getDefaultFileSystem()
 145  
     {
 146  1809
         return fileSystem;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Set the FileOptionsProvider
 151  
      * @param provider The FileOptionsProvider
 152  
      */
 153  
     public void setFileOptionsProvider(FileOptionsProvider provider)
 154  
     {
 155  0
         this.optionsProvider = provider;
 156  0
     }
 157  
 
 158  
     public FileOptionsProvider getFileOptionsProvider()
 159  
     {
 160  221
         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  
 }