Coverage Report - ubic.basecode.util.ConfigUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigUtils
60 %
35/58
75 %
9/12
3,3
 
 1  
 /*
 2  
  * The baseCode project
 3  
  *
 4  
  * Copyright (c) 2013 University of British Columbia
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (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 distributed under the License is distributed on
 12  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 13  
  * specific language governing permissions and limitations under the License.
 14  
  */
 15  
 package ubic.basecode.util;
 16  
 
 17  
 import java.io.File;
 18  
 import java.io.IOException;
 19  
 import java.net.URISyntaxException;
 20  
 import java.net.URL;
 21  
 
 22  
 import org.apache.commons.configuration2.PropertiesConfiguration;
 23  
 import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
 24  
 import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
 25  
 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
 26  
 import org.apache.commons.configuration2.convert.ListDelimiterHandler;
 27  
 import org.apache.commons.configuration2.ex.ConfigurationException;
 28  
 import org.apache.commons.configuration2.io.FileHandler;
 29  
 import org.apache.commons.configuration2.io.FileLocator;
 30  
 import org.apache.commons.configuration2.io.FileLocatorUtils;
 31  
 import org.apache.commons.io.FileUtils;
 32  
 
 33  
 /**
 34  
  * Convenience methods for loading configurations
 35  
  *
 36  
  * @author Paul
 37  
  *
 38  
  */
 39  0
 public class ConfigUtils {
 40  
 
 41  1
     private static final ListDelimiterHandler LIST_DELIMITER_HANDLER = new DefaultListDelimiterHandler( ',' );
 42  
 
 43  
     /**
 44  
      * @param file
 45  
      * @return
 46  
      */
 47  
     public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( File file )
 48  
             throws ConfigurationException {
 49  6
         if ( !file.exists() ) {
 50  
             try {
 51  3
                 FileTools.touch( file );
 52  0
             } catch ( IOException e ) {
 53  0
                 throw new ConfigurationException( "Couldn't create the file: " + e.getMessage() );
 54  3
             }
 55  
         }
 56  6
         FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(
 57  
                 PropertiesConfiguration.class );
 58  6
         builder.configure( new FileBasedBuilderParametersImpl().setFile( file ).setListDelimiterHandler( LIST_DELIMITER_HANDLER ) );
 59  6
         return builder;
 60  
     }
 61  
 
 62  
     /**
 63  
      * @param name If the file does not exist, attempts to create it in the user's home directory.
 64  
      * @return
 65  
      */
 66  
     public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( String name )
 67  
             throws ConfigurationException {
 68  2
         File f = locateConfig( name );
 69  2
         return getConfigBuilder( f );
 70  
     }
 71  
 
 72  
     /**
 73  
      * @param url
 74  
      * @return
 75  
      * @throws ConfigurationException
 76  
      */
 77  
     public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( URL url )
 78  
             throws ConfigurationException {
 79  
         File file;
 80  
         try {
 81  2
             file = new File( url.toURI() );
 82  2
             return getConfigBuilder( file );
 83  0
         } catch ( URISyntaxException e ) {
 84  0
             throw new ConfigurationException( "Couldn't map url to a uri: " + url );
 85  
         }
 86  
 
 87  
     }
 88  
 
 89  
     /**
 90  
      * @param name the classpath location, such as "project.properties" in the base package, or
 91  
      *        org/foo/project.properties.
 92  
      * @return
 93  
      * @throws ConfigurationException
 94  
      */
 95  
     public static PropertiesConfiguration loadClasspathConfig( String name ) throws ConfigurationException {
 96  0
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 97  
 
 98  0
         URL url = loader.getResource( name );
 99  0
         if ( url == null ) {
 100  0
             throw new ConfigurationException( "Couldn't locate: " + name );
 101  
         }
 102  
 
 103  0
         PropertiesConfiguration pc = createConfiguration();
 104  0
         FileHandler handler = new FileHandler( pc );
 105  0
         handler.setURL( url );
 106  0
         handler.load();
 107  0
         return pc;
 108  
     }
 109  
 
 110  
     /**
 111  
      * @param file
 112  
      * @return
 113  
      * @throws ConfigurationException
 114  
      */
 115  
     public static PropertiesConfiguration loadConfig( File file ) throws ConfigurationException {
 116  7
         if ( !file.exists() ) {
 117  
             try {
 118  4
                 FileTools.touch( file );
 119  0
             } catch ( IOException e ) {
 120  0
                 throw new ConfigurationException( "Couldn't create the file: " + e.getMessage() );
 121  4
             }
 122  
         }
 123  7
         PropertiesConfiguration pc = createConfiguration();
 124  7
         FileHandler handler = new FileHandler( pc );
 125  7
         handler.setFile( file );
 126  7
         handler.load();
 127  7
         return pc;
 128  
     }
 129  
 
 130  
     /**
 131  
      * @param name If the file does not exist, attempts to create it in the user's home directory.
 132  
      * @return
 133  
      * @throws ConfigurationException
 134  
      */
 135  
     public static PropertiesConfiguration loadConfig( String name ) throws ConfigurationException {
 136  
 
 137  
         /*
 138  
          * We have to locate it, if it's not a path.
 139  
          */
 140  3
         File f = locateConfig( name );
 141  
 
 142  3
         if ( f == null ) {
 143  
             /*
 144  
              * Try again from the classpath?
 145  
              */
 146  0
             return loadClasspathConfig( name );
 147  
         }
 148  
 
 149  3
         return loadConfig( f );
 150  
     }
 151  
 
 152  
     /**
 153  
      * @param url
 154  
      * @return
 155  
      * @throws ConfigurationException
 156  
      */
 157  
     public static PropertiesConfiguration loadConfig( URL url ) throws ConfigurationException {
 158  
 
 159  
         try {
 160  2
             File file = new File( url.toURI() );
 161  2
             return loadConfig( file );
 162  0
         } catch ( Exception e ) {
 163  0
             throw new ConfigurationException( "Couldn't map url to a uri: " + url );
 164  
         }
 165  
     }
 166  
 
 167  
     /**
 168  
      * @param name
 169  
      * @return
 170  
      * @throws ConfigurationException
 171  
      */
 172  
     private static File locateConfig( String name ) throws ConfigurationException {
 173  
         File f;
 174  5
         FileLocator fl = FileLocatorUtils.fileLocator().fileName( name ).create();
 175  5
         URL location = FileLocatorUtils.locate( fl );
 176  5
         if ( location == null ) {
 177  3
             f = new File( name );
 178  3
             if ( f.isAbsolute() ) {
 179  2
                 return f;
 180  
             }
 181  1
             return new File( FileUtils.getUserDirectory(), name );
 182  
         }
 183  
         try {
 184  2
             return new File( location.toURI() );
 185  0
         } catch ( Exception e ) {
 186  0
             throw new ConfigurationException( "Couldn't map url to a uri: " + name );
 187  
         }
 188  
     }
 189  
 
 190  
     public static URL locate( String name ) {
 191  0
         FileLocator fl = FileLocatorUtils.fileLocator().fileName( name ).create();
 192  0
         return FileLocatorUtils.locate( fl );
 193  
     }
 194  
 
 195  
     private static PropertiesConfiguration createConfiguration() {
 196  7
         PropertiesConfiguration pc = new PropertiesConfiguration();
 197  7
         pc.setListDelimiterHandler( LIST_DELIMITER_HANDLER );
 198  7
         return pc;
 199  
     }
 200  
 }