1 package org.wcb.autohome.util; 2 /*** 3 * Copyright (C) 1999 Walter Bogaardt 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18 * 19 */ 20 import java.util.Properties; 21 import java.io.BufferedInputStream; 22 import java.io.FileInputStream; 23 import java.io.IOException; 24 import java.io.FileOutputStream; 25 import java.io.PrintWriter; 26 import java.io.BufferedWriter; 27 import java.io.FileWriter; 28 29 /*** 30 * Project: Alice X10 Interface 31 * 32 * Filename: $Id: HAConfigLoader.java,v 1.11 2004/02/02 23:41:31 wbogaardt Exp $ 33 * 34 * Abstract: Server Config file loader 35 * 36 * @author wbogaardt 37 * @version 1.1 38 */ 39 public class HAConfigLoader { 40 41 public static final String sDEFAULT_FILENAME = "HASconfig.ini"; 42 public static final String sFILE_HEADER = "#This is the property file for Alice server.\n"+ 43 "# To set values in this file uncomment the line you wish to set a value\n"+ 44 "# for (by deleting the # at the beginning) and then fill in a value after the =\n"; 45 private String sUserHome = System.getProperty("user.home"); 46 private static Properties pCONFIGURATION = null; 47 private String sFileName; 48 private static final String sSEPARATOR = System.getProperty("file.separator"); 49 private String sConfigurationFileName = null; 50 51 public HAConfigLoader() 52 { 53 this(sDEFAULT_FILENAME); 54 } 55 56 public HAConfigLoader(String fileName) 57 { 58 this(fileName, null); 59 } 60 61 public HAConfigLoader(String fileName, String directory) 62 { 63 if (directory != null) 64 { 65 sUserHome = directory; 66 } 67 this.sConfigurationFileName = fileName; 68 pCONFIGURATION = load(sUserHome, fileName); 69 } 70 71 /*** 72 * Load the specified properties file. If the file does not exist, a 73 * default file will be created. 74 */ 75 public Properties load(String directory, String file) 76 { 77 Properties returnValue = null; 78 try 79 { 80 returnValue = loadProperties(directory, file); 81 } 82 catch (IOException e) 83 { 84 System.err.println("HASConfig.ini not found building default file."); 85 try 86 { 87 buildDefaultFile(directory,file); 88 returnValue = loadProperties(directory, file); 89 } 90 catch(IOException ex) 91 { 92 } 93 } 94 return returnValue; 95 } 96 97 /*** 98 * Gets the properties from the file system and 99 * returns it as a properties hash. 100 * @return Properties hash of saved configuration. 101 */ 102 public Properties getProperties(){ 103 if(pCONFIGURATION == null) 104 { 105 pCONFIGURATION = load(sUserHome, sConfigurationFileName); 106 } 107 return pCONFIGURATION; 108 } 109 110 /*** 111 * Returns true if the specified property is empty. 112 * @param propName property key to check 113 * @return true if property is empty. 114 */ 115 public boolean isEmpty(String propName) { 116 String propValue = getProperty(propName); 117 return (propValue == null || propValue.trim().length() < 1); 118 } 119 120 /*** 121 * Gets the value of a specified key name in the 122 * properties hash. 123 * @param propName key name 124 * @return string value 125 */ 126 public String getProperty(String propName){ 127 if(pCONFIGURATION.getProperty(propName) != null) { 128 return pCONFIGURATION.getProperty(propName); 129 } 130 return ""; 131 } 132 133 /*** 134 * This returns the object, which is usually a string 135 * from the property file 136 * @param propName 137 * @return value Object of the key object. 138 */ 139 public Object getObject(Object propName){ 140 return pCONFIGURATION.get(propName); 141 } 142 143 /*** 144 * This returns an object, which is a string from the 145 * property file based on a string of the key name. 146 * @param prop String representation of the key name 147 * @param value OBject, which is a string. 148 */ 149 public void setProperty(String prop, Object value) { 150 pCONFIGURATION.put(prop,value.toString()); 151 } 152 153 /*** 154 * Sets the properties object to the passed 155 * in value. 156 * @param newProp New properties hash. 157 */ 158 public void setProperties(Properties newProp) { 159 pCONFIGURATION = newProp; 160 } 161 162 /*** 163 * Load the properties file. 164 * @param directory the directory location of the property file 165 * @param file the file name itself. 166 * @throws IOException 167 */ 168 private Properties loadProperties(String directory, String file) 169 throws IOException 170 { 171 sFileName = directory + sSEPARATOR + file; 172 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sFileName)); 173 Properties returnValue = new Properties(); 174 returnValue.load(bis); 175 return returnValue; 176 } 177 178 /*** 179 * build a generic properties file with no values loaded 180 * and have that files properties set to null and commented out 181 * @param directory the directory location of the property file 182 * @param file the file name itself. 183 * @throws IOException 184 */ 185 private void buildDefaultFile(String directory, String file) 186 throws IOException 187 { 188 String filename = directory + sSEPARATOR + file; 189 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename))); 190 out.println(sFILE_HEADER); 191 out.println("serial.port=NO PORTS FOUND"); 192 out.println("baud=4800"); 193 out.println("data.bit=8"); 194 out.println("stop.bit=1"); 195 out.println("parity=none"); 196 out.println("interface.type=CM17A"); 197 out.println("initial.on.start=false"); 198 out.println("rmi.server=127.0.0.1"); 199 out.println("language=en"); 200 out.println("country=US"); 201 out.println("look.and.feel="); 202 out.println("email.to="); 203 out.println("email.from=alice@localhost"); 204 out.println("email.smtp=localhost"); 205 out.println("email.username="); 206 out.println("email.password="); 207 out.println("email.authorized=false"); 208 out.println("email.frequency=never"); 209 out.println("recover.cm11a=false"); 210 out.println("speech.engine=false"); 211 out.flush(); 212 out.close(); 213 } 214 215 /*** 216 * Saves the properties file to the file system. 217 */ 218 public void saveProperties() { 219 FileOutputStream output; 220 try { 221 output = new FileOutputStream(sFileName); 222 pCONFIGURATION.store(output, sFILE_HEADER); 223 } catch(IOException ioe) { 224 System.err.println("Error :" + ioe); 225 } catch(Exception e){ 226 System.err.println("Unable to save file " + e); 227 } 228 } 229 } 230 231 232 233 234 235 236 237 238 239 240