| 1 17 18 package org.sape.carbon.core.config.node; 19 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.sape.carbon.core.config.Config; 26 import org.sape.carbon.core.config.Configuration; 27 import org.sape.carbon.core.config.ConfigurationException; 28 import org.sape.carbon.core.config.InvalidConfigurationException; 29 import org.sape.carbon.core.config.format.ConfigurationFormatException; 30 import org.sape.carbon.core.config.format.ConfigurationFormatService; 31 import org.sape.carbon.core.exception.ExceptionUtility; 32 import org.sape.carbon.core.exception.InvalidParameterException; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 53 public abstract class AbstractConfigurationDocument 54 extends AbstractNode 55 implements ConfigurationDocument { 56 57 60 private Log log = 61 LogFactory.getLog(this.getClass()); 62 63 64 protected Configuration configuration = null; 65 66 67 private final ConfigurationFormatService formatter; 68 69 70 private final NodeFactory nestedNodeFactory = 71 new NestedConfigurationDocumentFactory(); 72 73 78 public AbstractConfigurationDocument( 79 Node parent, 80 String name, 81 Object readOrAlterNodeLock, 82 Object addOrLoadChildLock, 83 ConfigurationFormatService formatter) { 84 85 super(parent, name, readOrAlterNodeLock, addOrLoadChildLock); 86 87 if (formatter == null) { 89 throw new InvalidParameterException( 90 this.getClass(), 91 "formatter cannot be null"); 92 } 93 this.formatter = formatter; 94 } 95 96 105 public AbstractConfigurationDocument( 106 Node parent, 107 String name, 108 ConfigurationFormatService formatter) { 109 110 super(parent, name); 111 112 if (formatter == null) { 114 throw new InvalidParameterException( 115 this.getClass(), 116 "formatter cannot be null"); 117 } 118 this.formatter = formatter; 119 } 120 121 140 public Configuration readConfiguration() 141 throws NodeIOException, ConfigurationFormatException { 142 143 synchronized (getReadOrAlterNodeLock()) { 144 145 if (isRemoved()) { 146 throw new NodeRemovedException( 147 this.getClass(), 148 this); 149 } 150 151 if (this.configuration == null) { 152 InputStream in = null; 153 try { 154 155 in = openInputStream(); 156 this.configuration = 157 this.formatter.readConfigurationStream( 158 getAbsoluteName(), 159 in); 160 161 } catch (Exception e) { 162 throw new NodeIOException( 163 this.getClass(), 164 this.getAbsoluteName(), 165 e); 166 } finally { 167 if (in != null) { 168 try { 170 closeInputStream(in); 171 } catch (Exception e) { 172 if (log.isWarnEnabled()) { 175 log.warn("Caught exception while " 176 + "closing InputStream: " 177 + ExceptionUtility.printStackTracesToString(e)); 178 } 179 } 180 } 181 } 182 } 183 return (Configuration) this.configuration.clone(); 184 } 185 186 } 187 188 207 public void writeConfiguration(Configuration config) 208 throws NodeIOException, ConfigurationFormatException { 209 210 synchronized (getReadOrAlterNodeLock()) { 211 if (isRemoved()) { 212 throw new NodeRemovedException( 213 this.getClass(), 214 this); 215 } 216 217 OutputStream out = null; 218 try { 219 out = openOutputStream(); 222 formatter.writeConfigurationStream(config, out); 223 out.flush(); 224 } catch (Exception e) { 225 throw new NodeIOException( 226 this.getClass(), 227 this.getAbsoluteName(), 228 e); 229 } finally { 230 if (out != null) { 231 try { 233 closeOutputStream(out); 234 } catch (Exception e) { 235 if (log.isWarnEnabled()) { 238 log.warn("Caught exception while " 239 + "closing OutputStream: " 240 + ExceptionUtility.printStackTracesToString( 241 e)); 242 } 243 } 244 } 245 } 246 247 this.configuration = null; 250 } 251 252 issueNodeChangedEvent(); 253 notifyNestedDocuments(); 254 } 255 256 262 public int remove() throws NodeRemovalException { 263 int removedNodes = 0; 264 265 if (!isRemoved()) { 266 synchronized (getReadOrAlterNodeLock()) { 267 this.childNodes.clear(); 271 272 if (backingDataExists()) { 274 destroyBackingData(); 275 } 276 setRemoved(); 277 removedNodes++; 278 } 279 280 issueNodeRemovedEvent(); 281 } 282 283 return removedNodes; 284 } 285 286 290 public ConfigurationDocument addNestedConfigurationDocument( 291 String name, 292 Configuration config) 293 throws NodeCreationException { 294 295 if (isRemoved()) { 296 throw new NodeRemovedException( 297 this.getClass(), 298 this); 299 } 300 301 try { 302 303 ConfigurationDocument newDocument = 306 (ConfigurationDocument) nestedNodeFactory.getInstance( 307 this, 308 name); 309 310 synchronized (getReadOrAlterNodeLock()) { 311 if (containsChild(name)) { 312 throw new NodeCreationException( 313 this.getClass(), 314 this, 315 name, 316 "Node already exists"); 317 } 318 319 newDocument.writeConfiguration(config); 322 } 323 324 return (ConfigurationDocument) fetchChild(name); 328 329 } catch (NodeNotFoundException nnfe) { 330 throw new NodeCreationException( 331 this.getClass(), 332 this, 333 name, 334 "Exception writing new configuration", 335 nnfe); 336 337 } catch (NodeIOException nioe) { 338 throw new NodeCreationException( 339 this.getClass(), 340 this, 341 name, 342 "Exception writing new configuration", 343 nioe); 344 345 } catch (ConfigurationFormatException cfe) { 346 throw new NodeCreationException( 347 this.getClass(), 348 this, 349 name, 350 "Exception writing new configuration", 351 cfe); 352 } 353 } 354 355 358 public ConfigurationFormatService getFormatService() { 359 return this.formatter; 360 } 361 362 365 public void refresh() { 366 if (!isRemoved()) { 367 if (backingDataExists()) { 368 synchronized (getReadOrAlterNodeLock()) { 369 this.configuration = null; 374 } 375 removeRemovedChildren(); 376 issueNodeChangedEvent(); 378 379 } else { 380 synchronized (getReadOrAlterNodeLock()) { 381 setRemoved(); 383 } 384 issueNodeRemovedEvent(); 386 } 387 } 388 } 389 390 396 protected void notifyNestedDocuments() { 397 Iterator childIterator = super.childNodes.values().iterator(); 398 399 while (childIterator.hasNext()) { 400 try { 401 NestedConfigurationDocument child = 402 (NestedConfigurationDocument) childIterator.next(); 403 404 child.onChange(); 405 } catch (ClassCastException cce) { 406 } 410 } 411 } 412 413 428 protected abstract InputStream openInputStream() throws Exception ; 429 430 436 protected void closeInputStream(InputStream in) throws Exception { 437 in.close(); 438 } 439 440 454 protected abstract OutputStream openOutputStream() throws Exception ; 455 456 457 463 protected void closeOutputStream(OutputStream out) throws Exception { 464 out.close(); 465 } 466 467 472 protected Set getAllChildNames() { 473 try { 474 475 return this.formatter.getChildConfigurationNames( 476 readConfiguration()); 477 478 } catch (NodeIOException nioe) { 479 throw new InvalidConfigurationException( 480 this.getClass(), 481 getAbsoluteName(), 482 getName(), 483 "Could not read child names", 484 nioe); 485 486 } catch (ConfigurationFormatException cfe) { 487 throw new InvalidConfigurationException( 488 this.getClass(), 489 getAbsoluteName(), 490 getName(), 491 "Could not read child names", 492 cfe); 493 } 494 } 495 496 499 protected Node loadChild(String nodeName) throws NodeNotFoundException { 500 try { 501 ConfigurationDocument nestedDoc = 502 (ConfigurationDocument) nestedNodeFactory.getInstance( 503 this, 504 nodeName); 505 nestedDoc.refresh(); 508 if (nestedDoc.isRemoved()) { 509 throw new NodeNotFoundException( 510 this.getClass(), 511 nodeName); 512 513 } else { 514 return nestedDoc; 515 } 516 } catch (NodeCreationException nce) { 517 throw new NodeNotFoundException( 518 this.getClass(), 519 nodeName, 520 nce); 521 } 522 } 523 524 532 protected void writeChildReferences(Configuration config) 533 throws ConfigurationFormatException { 534 535 try { 536 Set childNames = this.formatter.getChildConfigurationNames(config); 537 Iterator childIterator = childNames.iterator(); 538 539 while (childIterator.hasNext()) { 540 String childName = (String ) childIterator.next(); 541 Configuration childConfig = 542 this.formatter.getChildConfiguration(config, childName); 543 String childAbsoluteName = childConfig.getConfigurationName(); 544 if (!childAbsoluteName.startsWith("null") 545 && !childAbsoluteName.startsWith( 546 getAbsoluteName() + Node.DELIMITER)) { 547 Config.getInstance().storeConfiguration( 548 childAbsoluteName, 549 childConfig); 550 } 551 } 552 } catch (ConfigurationException ce) { 553 throw new ConfigurationFormatException( 554 this.getClass(), 555 "Could not write child references", 556 ce); 557 } 558 } 559 560 563 public NodeFactory getNestedNodeFactory() { 564 return this.nestedNodeFactory; 565 } 566 567 } 568 | Popular Tags |