1 17 18 package org.apache.avalon.util.defaults; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.util.Properties ; 25 26 import org.apache.avalon.util.env.Env; 27 28 29 36 public class DefaultsBuilder 37 { 38 42 55 public static File getHomeDirectory( String key ) throws IOException 56 { 57 final String homeKey = key + ".home"; 58 final String symbol = key.toUpperCase() + "_HOME"; 59 final String home = 60 System.getProperty( 61 homeKey, 62 Env.getEnvVariable( symbol ) ); 63 64 if( null != home ) 65 { 66 return new File ( home ).getCanonicalFile(); 67 } 68 else 69 { 70 final File user = 71 new File ( System.getProperty( "user.home" ) ); 72 final String path = "." + key; 73 return new File ( user, path ).getCanonicalFile(); 74 } 75 } 76 77 90 public static Properties getHomeProperties( 91 File home, String key, boolean flag ) throws IOException 92 { 93 Properties properties = getProperties( home, key ); 94 if( flag ) 95 { 96 final String name = key + ".home"; 97 final String path = home.getCanonicalPath(); 98 properties.setProperty( name, path ); 99 } 100 return properties; 101 } 102 103 111 public static Properties getUserProperties( 112 String key ) throws IOException 113 { 114 final File user = new File ( System.getProperty( "user.home" ) ); 115 return getProperties( user, key ); 116 } 117 118 127 public static Properties getProperties( 128 File dir, String key ) throws IOException 129 { 130 final String filename = key + ".properties"; 131 final File file = new File ( dir, filename ); 132 return getProperties( file ); 133 } 134 135 143 public static Properties getProperties( File file ) throws IOException 144 { 145 if( null == file ) 146 { 147 throw new NullPointerException ( "file" ); 148 } 149 150 Properties properties = new Properties (); 151 if( file.exists() ) 152 { 153 properties.load( 154 new FileInputStream ( file ) ); 155 } 156 return properties; 157 } 158 159 public static Properties getProperties( 160 ClassLoader classloader, String path ) throws IOException 161 { 162 Properties properties = new Properties (); 163 InputStream input = 164 classloader.getResourceAsStream( path ); 165 if( input != null ) 166 { 167 properties.load( input ); 168 } 169 return properties; 170 } 171 172 176 private final String m_key; 177 178 private final File m_work; 179 180 private final File m_root; 181 182 private final Properties m_home; 183 184 private final Properties m_user; 185 186 private final Properties m_dir; 187 188 192 public DefaultsBuilder( final String key, File work ) throws IOException 193 { 194 m_key = key; 195 m_work = work; 196 m_root = getHomeDirectory( m_key ); 197 m_home = getHomeProperties( m_root, m_key, true ); 198 m_user = getUserProperties( m_key ); 199 m_dir = getProperties( m_work, m_key ); 200 } 201 202 206 210 public File getHomeDirectory() 211 { 212 return m_root; 213 } 214 215 222 public Properties getHomeProperties() 223 { 224 return m_home; 225 } 226 227 231 public Properties getUserProperties() 232 { 233 return m_user; 234 } 235 236 240 public Properties getDirProperties() 241 { 242 return m_dir; 243 } 244 245 251 public Properties getConsolidatedProperties( 252 final Properties defaults, final String [] keys ) throws IOException 253 { 254 return getConsolidatedProperties( defaults, keys, new String [0] ); 255 } 256 257 264 public Properties getConsolidatedProperties( 265 final Properties defaults, final String [] keys, String [] sequence ) throws IOException 266 { 267 final Properties [] parameters = 268 new Properties [] { 269 defaults, 270 m_home, 271 m_user, 272 m_dir }; 273 final DefaultsFinder[] finders = 274 new DefaultsFinder[]{ 275 new SimpleDefaultsFinder( 276 parameters, 277 false ), 278 new SystemDefaultsFinder() 279 }; 280 return new Defaults( keys, sequence, finders ); 281 } 282 } 283 | Popular Tags |