1 19 package org.openharmonise.rm.config; 20 21 import java.sql.*; 22 import java.util.*; 23 import java.util.logging.*; 24 25 import org.openharmonise.commons.dsi.*; 26 import org.openharmonise.rm.dsi.*; 27 28 29 37 public class ConfigSettings { 38 39 42 private static ConfigSettings m_instance = null; 43 44 47 private static String m_value_separator = ";"; 48 49 52 private static String TBL_OH_PROP = "oh_prop"; 53 54 57 private static Map m_props = null; 58 59 62 private static AbstractDataStoreInterface m_dsi = null; 63 64 67 private static final Logger m_logger = Logger.getLogger(ConfigSettings.class.getName()); 68 69 private static boolean m_bIsInitialised = false; 70 71 76 private ConfigSettings() throws ConfigException { 77 initialise(); 78 } 79 80 86 private synchronized static void initialise() throws ConfigException { 87 ResultSet rs = null; 88 m_props = new HashMap(); 89 try { 90 91 m_dsi = DataStoreInterfaceFactory.getDataStoreInterface(); 92 93 rs = m_dsi.executeQuery("select * from " + TBL_OH_PROP); 94 while (rs.next()) { 95 String sKey = rs.getString(2); 96 String sValue = rs.getString(3); 97 98 m_props.put(sKey, sValue); 99 100 if(m_logger.isLoggable(Level.CONFIG)) { 101 m_logger.log(Level.CONFIG, "Config value pair: " + sKey + " = " + sValue); 102 } 103 } 104 105 m_bIsInitialised = true; 106 107 } catch (SQLException e) { 108 m_logger.log(Level.WARNING, "Error initialising CongigSettings", e); 109 throw new ConfigException(e); 110 } catch (DataStoreException e) { 111 m_logger.log(Level.WARNING, "Error initialising CongigSettings", e); 112 throw new ConfigException(e); 113 } finally { 114 try { 115 if (rs != null) { 116 rs.close(); 117 } 118 } catch (Exception e) { 119 if(m_logger.isLoggable(Level.WARNING)) { 120 m_logger.log(Level.WARNING, "Exception thrown when closing result set", e); 121 } 122 } 123 } 124 } 125 126 132 synchronized public static ConfigSettings getInstance() 133 throws ConfigException { 134 if (m_instance == null) { 135 m_instance = new ConfigSettings(); 136 } 137 138 return m_instance; 139 } 140 141 149 public static final String getProperty(String property_name) 150 throws ConfigException { 151 return getProperty(property_name, null); 152 } 153 154 163 public static final String getProperty( 164 String property_name, 165 String default_value) 166 throws ConfigException { 167 if (isInitialised() == false) { 168 initialise(); 169 } 170 String sVal = default_value; 171 172 if (m_props.containsKey(property_name) == true) { 173 sVal = (String ) m_props.get(property_name); 174 } 175 176 return sVal; 177 } 178 179 186 private static boolean isInitialised() { 187 188 return m_bIsInitialised; 189 } 190 191 200 public static void insertProperty( 201 String property_name, 202 String property_value) 203 throws ConfigException { 204 if (property_name == null || property_value == null) { 205 throw new ConfigException("Must have values for both property name and value"); 206 } 207 if (isInitialised() == false || m_dsi == null) { 208 initialise(); 209 } 210 if (m_props.containsKey(property_name)) { 211 throw new ConfigException("Property name already exists"); 212 } 213 try { 214 int nId = m_dsi.getSequenceNextValue("seq_oh_prop"); 215 String sSql = 216 "insert into oh_prop values(" 217 + nId 218 + ",'" 219 + property_name 220 + "','" 221 + property_value 222 + "')"; 223 if (m_logger.isLoggable(Level.CONFIG)) { 224 m_logger.logp(Level.CONFIG, ConfigSettings.class.getName(), "insertProperty", sSql); 225 } 226 227 m_dsi.execute(sSql); 228 initialise(); 230 } catch (DataStoreException e) { 231 throw new ConfigException(e); 232 } catch (SQLException e) { 233 throw new ConfigException(e); 234 } 235 236 } 237 238 246 public static void setProperty(String property_name, String property_value) 247 throws ConfigException { 248 String sPropVal = property_value; 249 if (isInitialised() == false || m_dsi == null) { 250 initialise(); 251 } 252 if (m_props.containsKey(property_name) == false) { 253 throw new ConfigException( 254 "Property " + property_name + " does not exist"); 255 } 256 257 String sSql = 258 "update oh_prop set prop_value='" 259 + property_value 260 + "' where prop_name ='" 261 + property_name 262 + "'"; 263 try { 264 m_dsi.execute(sSql); 265 } catch (DataStoreException e) { 266 throw new ConfigException(e); 267 } 268 initialise(); 270 271 } 272 273 279 public static void removeProperty(String prop_name) 280 throws ConfigException { 281 if (m_dsi == null) { 282 initialise(); 283 } 284 String sSql = 285 "delete from oh_prop where prop_name ='" + prop_name + "'"; 286 if (m_logger.isLoggable(Level.CONFIG)) { 287 m_logger.logp(Level.CONFIG, ConfigSettings.class.getName(), "removeProperty", sSql); 288 } 289 290 try { 291 m_dsi.execute(sSql); 293 } catch (DataStoreException e) { 294 throw new ConfigException(e); 295 } 296 297 initialise(); 298 299 } 300 301 307 public static String getPropertyNames() throws ConfigException { 308 String props = ""; 309 if (m_props == null || m_dsi == null) { 310 initialise(); 311 } 312 Set keyset = m_props.keySet(); 313 Object [] keys = keyset.toArray(); 314 for (int i = 0; i < keys.length; i++) { 315 if (i != 0) { 316 props += ","; 317 } 318 props += keys[i]; 319 } 320 return props; 321 } 322 323 329 public static List getPropertyNameArray() throws ConfigException { 330 String props = ""; 331 if (m_props == null || m_dsi == null) { 332 initialise(); 333 } 334 Set keyset = m_props.keySet(); 335 336 ArrayList list = new ArrayList(); 337 338 Iterator iter = keyset.iterator(); 339 340 while (iter.hasNext()) { 341 String sName = (String ) iter.next(); 342 list.add(sName); 343 } 344 345 346 return list; 347 } 348 349 356 public static final List getMultiValuedProperty(String property_name) 357 throws ConfigException { 358 Vector vec = new Vector(); 359 360 String property_value = getProperty(property_name, ""); 361 362 StringTokenizer tokenizer = 363 new StringTokenizer(property_value, m_value_separator); 364 365 while (tokenizer.hasMoreTokens() == true) { 366 vec.add(tokenizer.nextToken()); 367 } 368 369 return vec; 370 } 371 372 381 public static final int getIntProperty(String property_name, String default_value) 382 throws ConfigException { 383 String sVal = null; 384 int property_value = -1; 385 386 try { 387 sVal = getProperty(property_name, default_value); 388 property_value = Integer.parseInt(sVal); 389 390 } catch (NumberFormatException ne) { 391 throw new ConfigException( 392 "Cannot cast property with name:" 393 + property_name 394 + ", and value:" 395 + sVal 396 + " to an integer"); 397 } 398 399 return property_value; 400 } 401 402 411 public static final boolean getBoolProperty( 412 String property_name, 413 String default_value) 414 throws ConfigException { 415 boolean property_value; 416 417 String property_value_str = 418 (String ) getProperty(property_name, default_value); 419 420 if (property_value_str.equalsIgnoreCase("" + true)) { 421 property_value = true; 422 } else if (property_value_str.equalsIgnoreCase("" + false)) { 423 property_value = false; 424 } else { 425 throw new ConfigException( 426 "Cannot cast property with name:" 427 + property_name 428 + ", and value:" 429 + property_value_str 430 + " to a boolean"); 431 } 432 433 return property_value; 434 } 435 } | Popular Tags |