1 17 18 package org.sape.carbon.core.config.format.jdom; 19 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 27 import org.sape.carbon.core.config.InvalidConfigurationException; 28 import org.sape.carbon.core.config.PropertyConfiguration; 29 import org.sape.carbon.core.config.type.ConfigurationTypeException; 30 import org.sape.carbon.core.config.type.ConfigurationTypeService; 31 import org.sape.carbon.core.config.type.ConfigurationTypeServiceFactory; 32 import org.sape.carbon.core.exception.InvalidParameterException; 33 34 import org.jdom.Document; 35 import org.jdom.Element; 36 37 47 public class JDOMPropertyConfiguration implements PropertyConfiguration { 48 49 private static final String NAME_DELIMITER = "."; 50 51 52 private static final String COMMA_DELIMITER = ","; 53 54 55 private static final String WHITE_SPACE_OUTPUT_DELIMITER = " "; 56 57 58 private static final String WHITE_SPACE_INPUT_DELIMITERS = " \t\n"; 59 60 61 private String name; 62 63 64 private Document document; 65 66 67 private Element rootElement; 68 69 70 private PropertyConfiguration extendedConfig = null; 71 72 73 private ConfigurationTypeService typeService = 74 ConfigurationTypeServiceFactory.getInstance(); 75 76 77 private Map propertyCache = new HashMap (); 78 79 83 private boolean writable = true; 84 85 94 public JDOMPropertyConfiguration(Document document) { 95 this.document = document; 96 this.rootElement = document.getRootElement(); 97 } 98 99 102 public JDOMPropertyConfiguration() { 103 this.rootElement = new Element("Configuration"); 104 this.rootElement.setAttribute( 105 "ConfigurationInterface", PropertyConfiguration.class.getName()); 106 107 this.document = new Document(this.rootElement); 108 } 109 110 116 public JDOMPropertyConfiguration( 117 Document document, 118 PropertyConfiguration extendedConfig) { 119 120 this.document = document; 121 this.rootElement = document.getRootElement(); 122 this.extendedConfig = extendedConfig; 123 } 124 125 130 public JDOMPropertyConfiguration(PropertyConfiguration extendedConfig) { 131 132 this.rootElement = new Element("Configuration"); 133 this.rootElement.setAttribute( 134 "ConfigurationInterface", PropertyConfiguration.class.getName()); 135 136 this.document = new Document(this.rootElement); 137 this.extendedConfig = extendedConfig; 138 } 139 140 145 public Class getConfigurationInterface() { 146 return PropertyConfiguration.class; 147 } 148 149 154 public Document getDataStructure() { 155 return this.document; 156 } 157 158 163 public Element getRootElement() { 164 return this.rootElement; 165 } 166 167 170 public String getConfigurationName() { 171 return this.name; 172 } 173 174 177 public void setConfigurationName(String name) { 178 this.name = name; 179 } 180 181 184 public Object clone() { 185 Document doc = (Document) this.document.clone(); 186 JDOMPropertyConfiguration config = new JDOMPropertyConfiguration( 187 doc, 188 this.extendedConfig); 189 190 config.setConfigurationName(this.getConfigurationName()); 191 return config; 192 } 193 194 197 public boolean getBooleanProperty(String propertyName) { 198 Boolean value = 199 (Boolean ) getProperty(propertyName, null, boolean.class); 200 201 if (value == null) { 202 throw new InvalidConfigurationException( 203 this.getClass(), 204 getConfigurationName(), 205 propertyName, 206 "Value did not exist and no default was specified"); 207 } 208 209 return value.booleanValue(); 210 } 211 212 215 public boolean getBooleanProperty( 216 String propertyName, 217 boolean defaultValue) { 218 219 Boolean value = 220 (Boolean ) getProperty(propertyName, 221 new Boolean (defaultValue), 222 boolean.class); 223 return value.booleanValue(); 224 } 225 226 229 public byte getByteProperty(String propertyName) { 230 Byte value = (Byte ) getProperty(propertyName, null, byte.class); 231 232 if (value == null) { 233 throw new InvalidConfigurationException( 234 this.getClass(), 235 getConfigurationName(), 236 propertyName, 237 "Value did not exist and no default was specified"); 238 } 239 240 return value.byteValue(); 241 } 242 243 246 public byte getByteProperty(String propertyName, byte defaultValue) { 247 Byte value = 248 (Byte ) getProperty(propertyName, 249 new Byte (defaultValue), 250 byte.class); 251 return value.byteValue(); 252 } 253 254 257 public String [] getCSVProperty(String propertyName) { 258 return delimitedStringToArray( 259 getProperty(propertyName), 260 COMMA_DELIMITER); 261 } 262 263 266 public short getShortProperty(String propertyName) { 267 Short value = (Short ) getProperty(propertyName, null, short.class); 268 269 if (value == null) { 270 throw new InvalidConfigurationException( 271 this.getClass(), 272 getConfigurationName(), 273 propertyName, 274 "Value did not exist and no default was specified"); 275 } 276 277 return value.shortValue(); 278 } 279 280 283 public short getShortProperty(String propertyName, short defaultValue) { 284 Short value = 285 (Short ) getProperty(propertyName, 286 new Short (defaultValue), 287 short.class); 288 return value.shortValue(); 289 } 290 291 294 public double getDoubleProperty(String propertyName) { 295 Double value = (Double ) getProperty(propertyName, null, double.class); 296 297 if (value == null) { 298 throw new InvalidConfigurationException( 299 this.getClass(), 300 getConfigurationName(), 301 propertyName, 302 "Value did not exist and no default was specified"); 303 } 304 305 return value.doubleValue(); 306 } 307 308 311 public double getDoubleProperty(String propertyName, double defaultValue) { 312 Double value = 313 (Double ) getProperty(propertyName, 314 new Double (defaultValue), 315 double.class); 316 return value.doubleValue(); 317 } 318 319 322 public float getFloatProperty(String propertyName) { 323 Float value = (Float ) getProperty(propertyName, null, float.class); 324 325 if (value == null) { 326 throw new InvalidConfigurationException( 327 this.getClass(), 328 getConfigurationName(), 329 propertyName, 330 "Value did not exist and no default was specified"); 331 } 332 333 return value.floatValue(); 334 } 335 336 339 public float getFloatProperty(String propertyName, float defaultValue) { 340 Float value = 341 (Float ) getProperty(propertyName, 342 new Float (defaultValue), 343 boolean.class); 344 return value.floatValue(); 345 } 346 347 350 public int getIntProperty(String propertyName) { 351 Integer value = (Integer ) getProperty(propertyName, null, int.class); 352 353 if (value == null) { 354 throw new InvalidConfigurationException( 355 this.getClass(), 356 getConfigurationName(), 357 propertyName, 358 "Value did not exist and no default was specified"); 359 } 360 361 return value.intValue(); 362 } 363 364 367 public int getIntProperty(String propertyName, int defaultValue) { 368 Integer value = 369 (Integer ) getProperty(propertyName, 370 new Integer (defaultValue), 371 int.class); 372 return value.intValue(); 373 } 374 375 378 public long getLongProperty(String propertyName) { 379 Long value = (Long ) getProperty(propertyName, null, long.class); 380 381 if (value == null) { 382 throw new InvalidConfigurationException( 383 this.getClass(), 384 getConfigurationName(), 385 propertyName, 386 "Value did not exist and no default was specified"); 387 } 388 389 return value.longValue(); 390 } 391 392 395 public long getLongProperty(String propertyName, long defaultValue) { 396 Long value = 397 (Long ) getProperty(propertyName, 398 new Long (defaultValue), 399 long.class); 400 return value.longValue(); 401 } 402 403 406 public String getProperty(String propertyName) { 407 String value = (String ) getProperty(propertyName, null, String .class); 408 409 if (value == null) { 410 throw new InvalidConfigurationException( 411 this.getClass(), 412 getConfigurationName(), 413 propertyName, 414 "Value did not exist and no default was specified"); 415 } 416 417 return value; 418 } 419 420 423 public String getProperty(String propertyName, String defaultValue) { 424 String value = 425 (String ) getProperty(propertyName, defaultValue, String .class); 426 return value; 427 } 428 429 432 public String [] getWSVProperty(String propertyName) { 433 return delimitedStringToArray( 434 getProperty(propertyName), 435 WHITE_SPACE_INPUT_DELIMITERS); 436 } 437 438 441 public Class getClassProperty(String propertyName) { 442 Class value = (Class ) getProperty(propertyName, null, Class .class); 443 444 if (value == null) { 445 throw new InvalidConfigurationException( 446 this.getClass(), 447 getConfigurationName(), 448 propertyName, 449 "Value did not exist and no default was specified"); 450 } 451 452 return value; 453 } 454 455 458 public Class getClassProperty(String propertyName, Class defaultValue) { 459 Class value = 460 (Class ) getProperty(propertyName, defaultValue, boolean.class); 461 return value; 462 } 463 464 467 public Date getDateProperty(String propertyName, Date defaultValue) { 468 Date value = (Date ) getProperty(propertyName, defaultValue, Date .class); 469 return value; 470 } 471 472 475 public Date getDateProperty(String propertyName) { 476 Date value = (Date ) getProperty(propertyName, null, Date .class); 477 478 if (value == null) { 479 throw new InvalidConfigurationException( 480 this.getClass(), 481 getConfigurationName(), 482 propertyName, 483 "Value did not exist and no default was specified"); 484 } 485 486 return value; 487 } 488 489 492 public void setBooleanProperty(String propertyName, boolean value) { 493 setProperty( 494 propertyName, 495 this.typeService.toString(boolean.class, new Boolean (value))); 496 } 497 498 501 public void setByteProperty(String propertyName, byte value) { 502 setProperty( 503 propertyName, 504 this.typeService.toString(byte.class, new Byte (value))); 505 } 506 507 510 public void setClassProperty(String propertyName, Class value) { 511 setProperty( 512 propertyName, 513 this.typeService.toString(Class .class, value)); 514 } 515 516 519 public void setCSVProperty(String propertyName, String [] values) { 520 setProperty( 521 propertyName, 522 arrayToDelimitedString(values, COMMA_DELIMITER)); 523 } 524 525 528 public void setDateProperty(String propertyName, Date value) { 529 setProperty( 530 propertyName, 531 this.typeService.toString(Date .class, value)); 532 } 533 534 537 public void setDoubleProperty(String propertyName, double value) { 538 setProperty( 539 propertyName, 540 this.typeService.toString(double.class, new Double (value))); 541 } 542 543 546 public void setFloatProperty(String propertyName, float value) { 547 setProperty( 548 propertyName, 549 this.typeService.toString(float.class, new Float (value))); 550 } 551 552 555 public void setIntProperty(String propertyName, int value) { 556 setProperty( 557 propertyName, 558 this.typeService.toString(int.class, new Integer (value))); 559 } 560 561 564 public void setLongProperty(String propertyName, long value) { 565 setProperty( 566 propertyName, 567 this.typeService.toString(long.class, new Long (value))); 568 } 569 570 571 574 public void setProperty(String propertyName, String value) { 575 if (!this.isConfigurationWritable()) { 577 throw new java.lang.UnsupportedOperationException ( 578 this.getClass().getName() 579 + "Configuration Document is read-only, write not supported."); 580 } 581 582 if (propertyName == null || propertyName.equals("")) { 583 throw new InvalidParameterException( 584 this.getClass(), 585 "propertyName cannot be null or empty"); 586 } 587 588 this.propertyCache.remove(propertyName); 590 591 StringTokenizer names = new StringTokenizer ( 592 propertyName, 593 NAME_DELIMITER); 594 Element currentElement = this.rootElement; 595 596 while (names.hasMoreTokens()) { 597 String childName = names.nextToken(); 598 Element childElement = currentElement.getChild(childName); 599 600 if (childElement == null) { 601 childElement = new Element(childName); 603 currentElement.addContent(childElement); 604 } 605 606 currentElement = childElement; 607 } 608 609 currentElement.setText(value); 610 } 611 612 615 public void setShortProperty(String propertyName, short value) { 616 setProperty( 617 propertyName, 618 this.typeService.toString(short.class, new Short (value))); 619 } 620 621 624 public void setWSVProperty(String propertyName, String [] values) { 625 setProperty( 626 propertyName, 627 arrayToDelimitedString(values, WHITE_SPACE_OUTPUT_DELIMITER)); 628 } 629 630 643 private Object getProperty( 644 String propertyName, 645 Object defaultValue, 646 Class type) { 647 648 String propertyText = (String ) this.propertyCache.get(propertyName); 650 651 if (propertyText == null) { 652 StringTokenizer nameChain = 654 new StringTokenizer (propertyName, NAME_DELIMITER); 655 656 Element element = this.rootElement; 657 while (nameChain.hasMoreTokens() && element != null) { 658 659 element = element.getChild(nameChain.nextToken()); 660 661 } 662 if (element != null) { 663 propertyText = element.getText(); 665 this.propertyCache.put(propertyName, propertyText); 666 } else if (this.extendedConfig != null) { 667 670 propertyText = 673 this.extendedConfig.getProperty(propertyName, null); 674 } 675 676 } 677 678 Object property; 679 if (propertyText != null) { 680 try { 682 property = this.typeService.toObject(type, propertyText); 683 } catch (ConfigurationTypeException cte) { 684 throw new InvalidConfigurationException( 685 this.getClass(), 686 getConfigurationName(), 687 propertyName, 688 "Value could not be formatted to class [" 689 + type.getName() 690 + "]: value [" 691 + propertyText 692 + "]", 693 cte); 694 } 695 } else { 696 property = defaultValue; 698 } 699 700 return property; 701 } 702 703 712 private String [] delimitedStringToArray( 713 String delimitedValues, 714 String delimiters) { 715 716 StringTokenizer values = new StringTokenizer ( 717 delimitedValues, 718 delimiters); 719 String [] valueArray = new String [values.countTokens()]; 720 for (int i = 0; values.hasMoreTokens(); i++) { 721 valueArray[i] = values.nextToken(); 722 } 723 return valueArray; 724 } 725 726 734 private String arrayToDelimitedString(String [] values, String delimiter) { 735 if (values.length > 0) { 736 StringBuffer delimitedString = new StringBuffer (values[0]); 737 for (int i = 1; i < values.length; i++) { 738 delimitedString.append(delimiter); 739 delimitedString.append(values[i]); 740 } 741 return delimitedString.toString(); 742 } else { 743 return null; 744 } 745 } 746 747 756 public boolean isConfigurationWritable() { 757 return this.writable; 758 } 759 760 764 public void setConfigurationReadOnly() { 765 this.writable = false; 766 preloadProperyCache(this.rootElement, null); 767 768 } 774 775 781 private void preloadProperyCache( 782 Element parentElement, String parentElementName) { 783 784 String namePrefix; 785 if (parentElementName == null) { 786 namePrefix = ""; 787 } else { 788 790 Object value = 793 getProperty(parentElementName, null, String .class); 794 795 811 namePrefix = 812 parentElementName + JDOMPropertyConfiguration.NAME_DELIMITER; 813 } 814 815 List children = parentElement.getChildren(); 817 Iterator childIterator = children.iterator(); 818 819 while (childIterator.hasNext()) { 820 Element element = (Element) childIterator.next(); 821 preloadProperyCache(element, namePrefix + element.getName()); 822 } 823 } 824 } | Popular Tags |