001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j;
017    
018    import java.io.InputStream;
019    
020    import java.net.URI;
021    import java.net.URL;
022    
023    import java.util.Properties;
024    import java.util.prefs.Preferences;
025    import java.util.prefs.PreferencesFactory;
026    
027    public class IniPreferencesFactory implements PreferencesFactory
028    {
029        public static final String PROPERTIES = "ini4j.properties";
030        public static final String KEY_USER = "org.ini4j.prefs.user";
031        public static final String KEY_SYSTEM = "org.ini4j.prefs.system";
032        private Preferences _system;
033        private Preferences _user;
034    
035        @Override public synchronized Preferences systemRoot()
036        {
037            if (_system == null)
038            {
039                _system = newIniPreferences(KEY_SYSTEM);
040            }
041    
042            return _system;
043        }
044    
045        @Override public synchronized Preferences userRoot()
046        {
047            if (_user == null)
048            {
049                _user = newIniPreferences(KEY_USER);
050            }
051    
052            return _user;
053        }
054    
055        String getIniLocation(String key)
056        {
057            String location = Config.getSystemProperty(key);
058    
059            if (location == null)
060            {
061                try
062                {
063                    Properties props = new Properties();
064    
065                    props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES));
066                    location = props.getProperty(key);
067                }
068                catch (Exception x)
069                {
070                    assert true;
071                }
072            }
073    
074            return location;
075        }
076    
077        URL getResource(String location) throws IllegalArgumentException
078        {
079            try
080            {
081                URI uri = new URI(location);
082                URL url;
083    
084                if (uri.getScheme() == null)
085                {
086                    url = Thread.currentThread().getContextClassLoader().getResource(location);
087                }
088                else
089                {
090                    url = uri.toURL();
091                }
092    
093                return url;
094            }
095            catch (Exception x)
096            {
097                throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
098            }
099        }
100    
101        InputStream getResourceAsStream(String location) throws IllegalArgumentException
102        {
103            try
104            {
105                return getResource(location).openStream();
106            }
107            catch (Exception x)
108            {
109                throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
110            }
111        }
112    
113        Preferences newIniPreferences(String key)
114        {
115            Ini ini = new Ini();
116            String location = getIniLocation(key);
117    
118            if (location != null)
119            {
120                try
121                {
122                    ini.load(getResourceAsStream(location));
123                }
124                catch (Exception x)
125                {
126                    throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
127                }
128            }
129    
130            return new IniPreferences(ini);
131        }
132    }