Coverage Report - org.ini4j.IniPreferencesFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
IniPreferencesFactory
93%
31/33
90%
9/10
3.333
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.ini4j;
 17  
 
 18  
 import java.io.InputStream;
 19  
 
 20  
 import java.net.URI;
 21  
 import java.net.URL;
 22  
 
 23  
 import java.util.Properties;
 24  
 import java.util.prefs.Preferences;
 25  
 import java.util.prefs.PreferencesFactory;
 26  
 
 27  4
 public class IniPreferencesFactory implements PreferencesFactory
 28  
 {
 29  
     public static final String PROPERTIES = "ini4j.properties";
 30  
     public static final String KEY_USER = "org.ini4j.prefs.user";
 31  
     public static final String KEY_SYSTEM = "org.ini4j.prefs.system";
 32  
     private Preferences _system;
 33  
     private Preferences _user;
 34  
 
 35  
     @Override public synchronized Preferences systemRoot()
 36  
     {
 37  2
         if (_system == null)
 38  
         {
 39  1
             _system = newIniPreferences(KEY_SYSTEM);
 40  
         }
 41  
 
 42  2
         return _system;
 43  
     }
 44  
 
 45  
     @Override public synchronized Preferences userRoot()
 46  
     {
 47  3
         if (_user == null)
 48  
         {
 49  1
             _user = newIniPreferences(KEY_USER);
 50  
         }
 51  
 
 52  3
         return _user;
 53  
     }
 54  
 
 55  
     String getIniLocation(String key)
 56  
     {
 57  5
         String location = Config.getSystemProperty(key);
 58  
 
 59  5
         if (location == null)
 60  
         {
 61  
             try
 62  
             {
 63  1
                 Properties props = new Properties();
 64  
 
 65  1
                 props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES));
 66  0
                 location = props.getProperty(key);
 67  
             }
 68  1
             catch (Exception x)
 69  
             {
 70  
                 assert true;
 71  0
             }
 72  
         }
 73  
 
 74  5
         return location;
 75  
     }
 76  
 
 77  
     URL getResource(String location) throws IllegalArgumentException
 78  
     {
 79  
         try
 80  
         {
 81  6
             URI uri = new URI(location);
 82  
             URL url;
 83  
 
 84  5
             if (uri.getScheme() == null)
 85  
             {
 86  4
                 url = Thread.currentThread().getContextClassLoader().getResource(location);
 87  
             }
 88  
             else
 89  
             {
 90  1
                 url = uri.toURL();
 91  
             }
 92  
 
 93  5
             return url;
 94  
         }
 95  1
         catch (Exception x)
 96  
         {
 97  1
             throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 98  
         }
 99  
     }
 100  
 
 101  
     InputStream getResourceAsStream(String location) throws IllegalArgumentException
 102  
     {
 103  
         try
 104  
         {
 105  6
             return getResource(location).openStream();
 106  
         }
 107  2
         catch (Exception x)
 108  
         {
 109  2
             throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 110  
         }
 111  
     }
 112  
 
 113  
     Preferences newIniPreferences(String key)
 114  
     {
 115  3
         Ini ini = new Ini();
 116  3
         String location = getIniLocation(key);
 117  
 
 118  3
         if (location != null)
 119  
         {
 120  
             try
 121  
             {
 122  3
                 ini.load(getResourceAsStream(location));
 123  
             }
 124  1
             catch (Exception x)
 125  
             {
 126  1
                 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 127  2
             }
 128  
         }
 129  
 
 130  2
         return new IniPreferences(ini);
 131  
     }
 132  
 }