1 17 18 package org.sape.carbon.core.config.node; 19 20 import org.sape.carbon.core.config.Configuration; 21 import org.sape.carbon.core.config.InvalidConfigurationException; 22 import org.sape.carbon.core.config.format.ConfigurationFormatException; 23 import org.sape.carbon.core.config.node.link.LinkNode; 24 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration; 25 import org.sape.carbon.core.config.node.link.LinkNodeFactory; 26 import org.sape.carbon.core.exception.InvalidParameterException; 27 28 39 public abstract class AbstractFolder extends AbstractNode implements Folder { 40 41 45 private final NodeFactory subFolderFactory; 46 47 51 private final NodeFactory configurationDocumentFactory; 52 53 57 private final NodeFactory linkNodeFactory; 58 59 72 public AbstractFolder( 73 Node parent, 74 String name, 75 Object readOrAlterNodeLock, 76 Object addOrLoadChildLock, 77 NodeFactory subFolderFactory, 78 NodeFactory configurationDocumentFactory, 79 NodeFactory linkNodeFactory) { 80 81 super(parent, name, readOrAlterNodeLock, addOrLoadChildLock); 82 83 if (subFolderFactory == null) { 85 throw new InvalidParameterException( 86 this.getClass(), 87 "subFolderFactory parameter cannot be null"); 88 } 89 if (configurationDocumentFactory == null) { 90 throw new InvalidParameterException( 91 this.getClass(), 92 "configurationDocumentFactory parameter cannot be null"); 93 } 94 if (linkNodeFactory == null) { 95 throw new InvalidParameterException( 96 this.getClass(), 97 "linkNodeFactory parameter cannot be null"); 98 } 99 100 this.subFolderFactory = subFolderFactory; 101 this.configurationDocumentFactory = configurationDocumentFactory; 102 this.linkNodeFactory = linkNodeFactory; 103 } 104 105 118 public AbstractFolder( 119 Node parent, 120 String name, 121 NodeFactory subFolderFactory, 122 NodeFactory configurationDocumentFactory, 123 NodeFactory linkNodeFactory) { 124 125 super(parent, name); 126 127 if (subFolderFactory == null) { 129 throw new InvalidParameterException( 130 this.getClass(), 131 "subFolderFactory parameter cannot be null"); 132 } 133 if (configurationDocumentFactory == null) { 134 throw new InvalidParameterException( 135 this.getClass(), 136 "configurationDocumentFactory parameter cannot be null"); 137 } 138 if (linkNodeFactory == null) { 139 throw new InvalidParameterException( 140 this.getClass(), 141 "linkNodeFactory parameter cannot be null"); 142 } 143 144 this.subFolderFactory = subFolderFactory; 145 this.configurationDocumentFactory = configurationDocumentFactory; 146 this.linkNodeFactory = linkNodeFactory; 147 } 148 149 155 public ConfigurationDocument addConfigurationDocument( 156 String name, 157 Configuration config) 158 throws NodeCreationException { 159 160 if (isRemoved()) { 161 throw new NodeRemovedException( 162 this.getClass(), 163 this); 164 } 165 166 ConfigurationDocument newDocument = 167 createNewConfigurationDocument(name, config); 168 169 return newDocument; 170 } 171 172 178 public Folder addSubFolder(String name) 179 throws NodeCreationException { 180 181 if (isRemoved()) { 182 throw new NodeRemovedException( 183 this.getClass(), 184 this); 185 } 186 187 Folder newFolder = createNewFolder(name); 188 189 return newFolder; 190 } 191 192 195 public LinkNode addLink( 196 String name, 197 LinkNodeConfiguration linkConfiguration) 198 throws NodeCreationException { 199 200 if (isRemoved()) { 201 throw new NodeRemovedException( 202 this.getClass(), 203 this); 204 } 205 206 try { 207 synchronized (getAddOrLoadChildLock()) { 208 209 ConfigurationDocument linkConfigurationDoc = 214 (ConfigurationDocument) this.linkNodeFactory.getInstance( 215 this, 216 name); 217 218 synchronized (getReadOrAlterNodeLock()) { 219 if (containsChild(name)) { 220 throw new NodeCreationException( 221 this.getClass(), 222 this, 223 name, 224 "Node already exists"); 225 } 226 227 linkConfigurationDoc.writeConfiguration(linkConfiguration); 228 } 229 230 LinkNodeFactory linkFactory = 231 (LinkNodeFactory) linkConfiguration 232 .getLinkNodeFactoryClass() 233 .newInstance(); 234 235 LinkNode newLink = 236 linkFactory.getInstance( 237 this, 238 name, 239 linkConfigurationDoc); 240 241 synchronized (getReadOrAlterNodeLock()) { 242 this.childNodes.put(name, newLink); 243 } 244 245 return newLink; 246 } 247 248 } catch (NodeIOException nioe) { 249 throw new NodeCreationException( 250 this.getClass(), 251 this, 252 name, 253 "Could not write link configuration", 254 nioe); 255 256 } catch (ConfigurationFormatException cfe) { 257 throw new NodeCreationException( 258 this.getClass(), 259 this, 260 name, 261 "Could not write link configuration", 262 cfe); 263 264 } catch (ClassCastException cce) { 265 throw new InvalidConfigurationException( 266 this.getClass(), 267 this.getAbsoluteName() + Node.DELIMITER + name, 268 "LinkNodeFactoryClass", 269 "Class is not an instance of LinkNodeFactory", 270 cce); 271 272 } catch (InstantiationException ie) { 273 throw new InvalidConfigurationException( 274 this.getClass(), 275 this.getAbsoluteName() + Node.DELIMITER + name, 276 "LinkNodeFactoryClass", 277 "Could instantiate class", 278 ie); 279 280 } catch (IllegalAccessException iae) { 281 throw new InvalidConfigurationException( 282 this.getClass(), 283 this.getAbsoluteName() + Node.DELIMITER + name, 284 "LinkNodeFactoryClass", 285 "Could instantiate class", 286 iae); 287 } 288 } 289 290 293 public NodeFactory getConfigurationDocumentFactory() { 294 return this.configurationDocumentFactory; 295 } 296 297 300 public NodeFactory getSubFolderFactory() { 301 return this.subFolderFactory; 302 } 303 304 314 protected Folder createNewFolder(String name) 315 throws NodeCreationException { 316 317 synchronized (getAddOrLoadChildLock()) { 318 if (containsChild(name)) { 319 throw new NodeCreationException( 320 this.getClass(), 321 this, 322 name, 323 "Node already exists"); 324 } 325 326 Folder newFolder = 331 (Folder) this.subFolderFactory.getInstance(this, name); 332 333 synchronized (getReadOrAlterNodeLock()) { 334 this.childNodes.put(name, newFolder); 335 } 336 337 return newFolder; 338 } 339 340 } 341 342 353 protected ConfigurationDocument createNewConfigurationDocument( 354 String name, 355 Configuration config) 356 throws NodeCreationException { 357 358 synchronized (getReadOrAlterNodeLock()) { 359 ConfigurationDocument newDoc = (ConfigurationDocument) 364 this.configurationDocumentFactory.getInstance(this, name); 365 366 synchronized (getReadOrAlterNodeLock()) { 367 if (containsChild(name)) { 368 throw new NodeCreationException( 369 this.getClass(), 370 this, 371 name, 372 "Node already exists"); 373 } 374 375 try { 376 newDoc.writeConfiguration(config); 380 } catch (NodeIOException nioe) { 381 throw new NodeCreationException( 382 this.getClass(), 383 this, 384 name, 385 "Could not write to new ConfigurationDocument", 386 nioe); 387 388 } catch (ConfigurationFormatException cfe) { 389 throw new NodeCreationException( 390 this.getClass(), 391 this, 392 name, 393 "Could not write to new ConfigurationDocument", 394 cfe); 395 } 396 397 this.childNodes.put(name, newDoc); 398 399 return newDoc; 400 } 401 } 402 } 403 404 407 public NodeFactory getLinkNodeFactory() { 408 return this.linkNodeFactory; 409 } 410 } | Popular Tags |