001 /*
002 * Copyright 2005 [ini4j] Development Team
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
017 package org.ini4j;
018
019 import java.util.prefs.PreferencesFactory;
020 import java.util.prefs.Preferences;
021 import java.util.Properties;
022 import java.io.InputStream;
023
024 import java.net.URI;
025 import java.net.URL;
026
027 public class IniPreferencesFactory implements PreferencesFactory
028 {
029 public static final String PROPERTIES = "ini4j.properties";
030
031 public static final String KEY_USER = "org.ini4j.prefs.user";
032 public static final String KEY_SYSTEM = "org.ini4j.prefs.system";
033
034 private Preferences _system;
035 private Preferences _user;
036
037 public synchronized Preferences systemRoot()
038 {
039 if ( _system == null )
040 {
041 _system = newIniPreferences(KEY_SYSTEM);
042 }
043
044 return _system;
045 }
046
047 public synchronized Preferences userRoot()
048 {
049 if ( _user == null )
050 {
051 _user = newIniPreferences(KEY_USER);
052 }
053
054 return _user;
055 }
056
057 protected Preferences newIniPreferences(String key)
058 {
059 Ini ini = new Ini();
060
061 String location = getIniLocation(key);
062
063 if ( location != null )
064 {
065 try
066 {
067 ini.load(getResourceAsStream(location));
068 }
069 catch (Exception x)
070 {
071 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
072 }
073 }
074
075 return new IniPreferences(ini);
076 }
077
078 protected String getIniLocation(String key)
079 {
080 String location = System.getProperty(key);
081
082 if ( location == null )
083 {
084 try
085 {
086 Properties props = new Properties();
087 props.load(getClass().getClassLoader().getResourceAsStream(PROPERTIES));
088 location = props.getProperty(key);
089 }
090 catch (Exception x)
091 {
092 ;
093 }
094 }
095
096 return location;
097 }
098
099 protected URL getResource(String location) throws IllegalArgumentException
100 {
101 try
102 {
103 URI uri = new URI(location);
104 URL url;
105
106 if ( uri.getScheme() == null )
107 {
108 url = getClass().getClassLoader().getResource(location);
109 }
110 else
111 {
112 url = uri.toURL();
113 }
114
115 return url;
116 }
117 catch (Exception x)
118 {
119 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
120 }
121 }
122
123 protected InputStream getResourceAsStream(String location) throws IllegalArgumentException
124 {
125 try
126 {
127 return getResource(location).openStream();
128 }
129 catch (Exception x)
130 {
131 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
132 }
133 }
134 }