1 46 51 package org.mr.core.configuration; 52 53 import java.io.File ; 54 import java.io.IOException ; 55 import java.util.ArrayList ; 56 import org.mr.core.log.StartupLogger; 57 58 import org.apache.commons.logging.Log; 59 import org.apache.commons.logging.LogFactory; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.Element ; 62 import org.w3c.dom.NodeList ; 63 64 import javax.management.*; 65 import javax.xml.parsers.DocumentBuilder ; 66 import javax.xml.parsers.DocumentBuilderFactory ; 67 68 73 public class ConfigManager extends StandardMBean implements ConfigManagerMBean { 74 String configFileName ; 75 Element configurationDOMElement; 77 ConfigurationElement root; 78 Log log ; 79 ArrayList configurationChangeListeners = new ArrayList (); 81 82 private boolean haslog4JConfig; 83 84 public ConfigManager(String propertyFile)throws NotCompliantMBeanException { 85 super(ConfigManagerMBean.class); 86 File f = new File (propertyFile); 87 if(!f.exists()){ 88 StartupLogger.log.fatal("Did not find configuration file-"+propertyFile, "ConfigManager"); 90 return; 91 } 92 loadConfiguration(f); 93 configFileName = propertyFile; 94 } 96 97 103 public ConfigManager(Element element) throws NotCompliantMBeanException { 104 super(ConfigManagerMBean.class); 105 configurationDOMElement = element; 108 loadConfiguration((Element ) element.cloneNode(true)); 111 } 112 113 private void loadConfiguration(File f) { 114 Element rootElement; 115 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 116 factory.setIgnoringElementContentWhitespace(true); 117 118 try{ 119 DocumentBuilder xmlBuilder= factory.newDocumentBuilder(); 120 Document xmlDoc = xmlBuilder.parse(f); 121 122 rootElement = xmlDoc.getDocumentElement(); 123 loadConfiguration(rootElement); 124 } catch(Exception e){ 125 StartupLogger.log.fatal("Can not load configuration file.", "ConfigManager"); 127 e.printStackTrace(); 128 } 129 } 130 131 private void loadConfiguration(Element element){ 132 try { 133 NodeList logConfig = element.getElementsByTagName("log4j:configuration"); 135 Element logConfigElement = (Element ) logConfig.item(0); 136 137 if(logConfigElement != null){ 138 haslog4JConfig =true; 139 element.removeChild(logConfigElement); 141 } 142 root = new ConfigurationElement(element); 144 } 145 catch (Exception e){ 146 StartupLogger.log.fatal("Can not load configuration DOM element.", "ConfigManager"); 147 e.printStackTrace(); 148 } 149 } 150 151 152 155 public String getStringProperty(String key) { 156 String result = null; 157 ArrayList element = getConfigurationElements(key); 158 if(element != null && element.size() ==1){ 159 ConfigurationElement ce = (ConfigurationElement) element.get(0); 160 if(ce.isLeaf()){ 161 result = ce.getValue(); 162 } 163 } 164 if(result!= null){ 165 result = result.trim(); 166 } 167 return result; 168 } 169 170 public ArrayList getConfigurationElements(String key) { 171 ConfigurationElement element =root; 172 ArrayList result = null; 173 String [] subKeys = key.split("\\."); 174 for (int i = 0; i < subKeys.length; i++) { 175 String subKey = subKeys[i]; 176 if(element != null) 177 result = element.getSubConfigurationElementsByName(subKey); 178 if(result == null){ 179 return null; 180 }else{ 181 element = (ConfigurationElement) result.get(0); 182 } 183 } 184 return result; 185 } 186 187 public ConfigurationElement getConfigurationElement(String key) { 188 ConfigurationElement element =root; 189 ArrayList result = null; 190 String [] subKeys = key.split("\\."); 191 for (int i = 0; i < subKeys.length; i++) { 192 String subKey = subKeys[i]; 193 result = element.getSubConfigurationElementsByName(subKey); 194 if(result == null){ 195 return null; 196 }else{ 197 element = (ConfigurationElement) result.get(0); 198 } 199 } 200 return (ConfigurationElement) result.get(0); 201 } 202 203 207 public boolean setStringProperty(String key ,String value , boolean commitToFile ) { 208 if(key==null||value==null||key.length()==0||value.length()==0){ 209 throw new IllegalArgumentException ("Can't create a new property without values for both 'key' and 'value'."); 210 } 211 try { 212 changeConfigProperty( key , value , commitToFile); 213 } catch (IOException e) { 214 return false; 215 } 216 return true; 217 } 218 219 222 public String getStringProperty(String key, String defaultValue) { 223 String value = getStringProperty(key) ; 224 if(value == null){ 225 if(log != null && log.isDebugEnabled()){ 226 log.debug("Configuration value not found for key '"+key+"'. Returning default value "+defaultValue+"."); 227 } 228 return defaultValue; 229 }else{ 230 return value; 231 } 232 } 233 234 237 public int getIntProperty(String key) { 238 239 String value = getStringProperty(key) ; 240 241 int i = Integer.parseInt(value) ; 242 243 return i ; 244 } 245 246 250 public boolean setIntProperty(String key ,int value , boolean commitToFile ) { 251 try { 252 changeConfigProperty( key ,String.valueOf(value) , commitToFile); 253 } catch (IOException e) { 254 return false; 255 } 256 return true; 257 } 258 259 262 public int getIntProperty(String key, int defaultValue) { 263 String value = getStringProperty(key) ; 264 if (value == null) 265 return defaultValue ; 266 int i ; 267 try { 268 i = Integer.parseInt(value) ; 269 } catch (Throwable t) { 270 if(log != null && log.isWarnEnabled()){ 271 log.warn("Problem while trying to return int for property key '"+key+"' and value '"+value+"'. Returning default value "+defaultValue+"."); 272 } 273 return defaultValue; 274 } 275 return i ; 276 } 277 278 281 public long getLongProperty(String key) { 282 283 String value = getStringProperty(key) ; 284 285 long i = Long.parseLong(value) ; 286 287 return i ; 288 } 289 290 291 292 293 297 public boolean setLongProperty(String key ,long value , boolean commitToFile ) { 298 try { 299 changeConfigProperty( key ,String.valueOf(value) , commitToFile); 300 } catch (IOException e) { 301 return false; 302 } 303 return true; 304 } 305 306 309 public long getLongProperty(String key, long defaultValue) { 310 311 String value = getStringProperty(key) ; 312 if (value == null) 313 return defaultValue ; 314 long i ; 315 try { 316 i = Long.parseLong(value) ; 317 } catch (Throwable t) { 318 if(log != null && log.isWarnEnabled()){ 319 log.warn("Problem while trying to return long for property key '"+key+"' and value '"+value+"'. Returning default value "+defaultValue+"."); 320 } 321 return defaultValue; 322 } 323 return i ; 324 } 325 326 329 public short getShortProperty(String key) { 330 331 String value = getStringProperty(key) ; 332 333 short i = Short.parseShort(value) ; 334 335 return i ; 336 } 337 338 342 public boolean setShortProperty(String key ,short value , boolean commitToFile ) { 343 try { 344 changeConfigProperty( key ,String.valueOf(value) , commitToFile); 345 } catch (IOException e) { 346 return false; 347 } 348 return true; 349 } 350 351 352 355 public short getShortProperty(String key, short defaultValue) { 356 357 String value = getStringProperty(key) ; 358 if (value == null) 359 return defaultValue ; 360 short i ; 361 try { 362 i = Short.parseShort(value) ; 363 } catch (Throwable t) { 364 if(log != null && log.isWarnEnabled()){ 365 log.warn("Problem while trying to return short for property key '"+key+"' and value '"+value+"'. Returning default value "+defaultValue+"."); 366 } 367 return defaultValue; 368 } 369 return i ; 370 } 371 374 public boolean getBooleanProperty(String key) { 375 376 String value = getStringProperty(key) ; 377 if (value == null) 378 return false; 379 if (value.equalsIgnoreCase("true")) 380 return true; 381 if (value.equalsIgnoreCase("false")) 382 return false; 383 if(log != null && log.isWarnEnabled()){ 384 log.warn("Problem while trying to return boolean for property key '"+key+"' and value '"+value+"'. Returning false."); 385 } 386 return false; 387 } 388 389 393 public boolean setBooleanProperty(String key ,boolean value , boolean commitToFile ) { 394 try { 395 changeConfigProperty( key ,String.valueOf(value) , commitToFile); 396 } catch (IOException e) { 397 return false; 398 } 399 return true; 400 } 401 402 405 public boolean getBooleanProperty(String key, boolean defaultValue) { 406 407 String value = getStringProperty(key) ; 408 if (value == null) 409 return defaultValue; 410 if (value.equalsIgnoreCase("true")) 411 return true; 412 if (value.equalsIgnoreCase("false")) 413 return false; 414 415 if(log != null && log.isWarnEnabled()){ 416 log.warn("Problem while trying to return boolean for property key '"+key+"' and value '"+value+"'. Returning given default "+defaultValue+"."); 417 } 418 return defaultValue; 419 } 420 421 433 438 public void registerAsConfigChangeListener(ConfigurationChangeListener listener){ 439 configurationChangeListeners.add(listener); 440 } 441 442 448 public void changeConfigProperty(String key ,String value , boolean commitToFile) throws IOException { 449 key = key.trim(); 450 value = value.trim(); 451 472 } 474 475 476 protected String getDescription(MBeanInfo i_mBeanInfo) { 477 return "manages the properties in the system ."; 478 } 479 480 protected String getDescription(MBeanAttributeInfo i_mBeanAttributeInfo) { 481 if(i_mBeanAttributeInfo.getName().equals("PropertiesKeys")) 482 return "returns the keys of all the properties of the system"; 483 if(i_mBeanAttributeInfo.getName().equals("AllProperties")) 484 return "returns the keys and values of all the properties of the system"; 485 return ""; 486 } 487 488 protected String getDescription(MBeanOperationInfo i_mBeanOperationInfo, MBeanParameterInfo i_mBeanParameterInfo, int i) { 489 490 if (i==0 && i_mBeanOperationInfo.getName().equals("getStringProperty")) 491 return "enter the key of the requested property"; 492 if (i==0 && i_mBeanOperationInfo.getName().equals("setStringProperty")) 493 return "the key of the property"; 494 if (i==1) 495 return "the value of the property"; 496 if (i==2) 497 return "determins wether the property will be written permanently in the configuration files"; 498 return "the new value of the managed string"; 499 } 500 501 protected String getDescription(MBeanOperationInfo i_mBeanOperationInfo) { 502 if(i_mBeanOperationInfo.getName().equals("setStringProperty")) 503 return "sets the value of a given property name in the system "; 504 if(i_mBeanOperationInfo.getName().equals("getStringProperty")) 505 return "gets the value of a given property name in the system "; 506 return "general seters and geters"; 507 } 508 509 510 protected String getParameterName(MBeanOperationInfo i_mBeanOperationInfo, MBeanParameterInfo i_mBeanParameterInfo, int i) { 511 if (i==0) 512 return "key"; 513 if (i==1) 514 return "value"; 515 if (i==2) 516 return "commitToFile"; 517 return "val"; 518 } 519 520 public String getConfigFileName() { 521 return configFileName; 522 } 523 524 public Element getConfigurationDOMElement(){ 525 return configurationDOMElement; 526 } 527 528 public boolean hasLog4JConfig() { 529 return haslog4JConfig; 530 } 531 532 public void initLogger() { 533 this.log = LogFactory.getLog("ConfigManager"); 534 } 535 } 536 | Popular Tags |