1 15 16 30 package ixenon.free.util; 31 32 import java.io.*; 33 import java.util.*; 34 35 45 public class Environment 46 { 47 115 public static void readEnvironment( String propertyFile ) 116 { 117 String osname = System.getProperty( "os.name" ); 118 String appresPath = System.getProperty( "appres.path" ); 119 String fileSep = System.getProperty( "file.separator" ); 120 String pathSep = System.getProperty( "path.separator" ); 121 boolean foundPropFile = false; 122 123 if ( appresPath == null ) { 124 appresPath = System.getProperty( "user.home" ); 125 if ( osname.startsWith("Windows 95") || 126 osname.startsWith("Windows 98") || 127 osname.startsWith("Windows NT") || 128 osname.startsWith("Windows 2000") || 129 osname.startsWith("OS/2") ) 130 appresPath += pathSep + "C:\\Program Files"; 131 else 132 appresPath += pathSep + fileSep + "usr"+fileSep+"local"; 133 } 134 135 StringTokenizer st = new StringTokenizer( appresPath, pathSep ); 140 while ( st.hasMoreTokens() ) { 141 String dirpath = st.nextToken(); 142 File propFile = new File( dirpath, propertyFile ); 143 if ( propFile.exists() ) { 145 foundPropFile = readDefaultPropertyFile( propFile, false ); 147 break; 148 } 149 } 150 151 boolean foundEnv = Environment.readEnvironmentVariables(); 152 if ( !foundPropFile && !foundEnv ) { 153 System.out.println( "*WARNING: cannot find or retrieve application default resources" ); 154 System.out.println( "property file: `"+propertyFile+"'" ); 155 System.out.println( "HINT: % java -Denv=\"`env`\" Foobar is used." ); 156 } 157 } 158 159 163 public static boolean readDefaultPropertyFile( File propFile, boolean override ) 164 { 165 if ( !propFile.exists() ) 166 return (false); 167 168 Properties propJVM = System.getProperties (); 169 170 System.out.println("Loading default application properties file: `"+propFile.getPath()+"'" ); 174 if ( !propFile.canRead() ) { 175 System.err.println("Cannot read default properties file:`"+propFile.getPath()+"'" ); 176 return (false); 177 } 178 179 boolean retval=false; 183 Properties propTemp = new Properties(); 184 try { 185 FileInputStream fis = new FileInputStream( propFile ); 186 propTemp.load(fis); 187 Enumeration keys = propTemp.propertyNames(); 188 while( keys.hasMoreElements() ) { 189 String name = (String )keys.nextElement(); 190 String value1 = propTemp.getProperty( name ); 191 String value2 = propJVM.getProperty( name ); 192 if (value2 == null || override) { 193 propJVM.put( name, value1 ); 195 } 196 } 197 } 198 catch (IOException ioe) { 199 retval = false; 200 System.err.println( 201 "I/O Exception occurred reading file:" + 202 propFile.getPath() + " message:`" + ioe.getMessage()+"'" ); 203 } 204 205 return (retval); 206 } 207 208 212 public static boolean readEnvironmentVariables() 213 { 214 Properties propJVM = System.getProperties(); 215 216 String env = propJVM.getProperty("env"); 217 if ( env == null ) 218 return (false); 219 220 try { 224 propJVM.remove("env"); 226 227 ReadbackStream readback = new ReadbackStream(); 232 OutputStreamWriter osw = new OutputStreamWriter(readback); 233 PrintWriter wholeenv = new PrintWriter(osw); 234 235 propJVM.save( readback, null); 238 byte [] envarray = env.getBytes( osw.getEncoding() ); 239 240 BufferedReader envin = 241 new BufferedReader( 242 new InputStreamReader( 243 new ByteArrayInputStream(envarray) ) ); 244 String line; 245 while ( (line = envin.readLine() ) != null ) 246 { 247 line = "env." + line; 252 wholeenv.println(line); 253 wholeenv.flush(); } 256 propJVM.load( readback.getInputStream() ); 260 } 261 catch ( UnsupportedEncodingException e ) { 262 System.err.println( "Environment character encoding is not supported."); 263 System.err.println( "Exception Msg:"+e.getMessage()); 264 System.exit(1); 265 } 266 catch ( IOException e ) {} 267 269 return (true); 270 } 271 } 272 273 | Popular Tags |