1 17 18 package org.sape.carbon.services.config.jndi; 19 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import javax.naming.Name ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.Attributes ; 27 import javax.naming.directory.DirContext ; 28 import javax.naming.directory.SearchControls ; 29 import javax.naming.directory.SearchResult ; 30 import javax.naming.event.EventContext ; 31 import javax.naming.event.NamespaceChangeListener ; 32 import javax.naming.event.NamingEvent ; 33 import javax.naming.event.NamingExceptionEvent ; 34 import javax.naming.event.ObjectChangeListener ; 35 36 import org.sape.carbon.core.config.InvalidConfigurationException; 37 import org.sape.carbon.core.config.format.ConfigurationFormatException; 38 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 39 import org.sape.carbon.core.config.node.AbstractFolder; 40 import org.sape.carbon.core.config.node.ConfigurationDocument; 41 import org.sape.carbon.core.config.node.Node; 42 import org.sape.carbon.core.config.node.NodeCreationException; 43 import org.sape.carbon.core.config.node.NodeFactory; 44 import org.sape.carbon.core.config.node.NodeIOException; 45 import org.sape.carbon.core.config.node.NodeNotFoundException; 46 import org.sape.carbon.core.config.node.NodeRemovalException; 47 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration; 48 import org.sape.carbon.core.config.node.link.LinkNodeFactory; 49 import org.sape.carbon.core.exception.ExceptionUtility; 50 import org.sape.carbon.core.exception.InvalidParameterException; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 66 public class JNDIFolder 67 extends AbstractFolder 68 implements NamespaceChangeListener , ObjectChangeListener { 69 70 private Log log = LogFactory.getLog(this.getClass()); 71 72 73 private static final String SUBCONTEXT_FILTER = "(|({0}={1})({0}={2}))"; 74 75 private DirContext initialContext; 76 private Name nodeContextName; 77 private JNDILinkNodeConfiguration config; 78 79 94 protected JNDIFolder( 95 Node parent, 96 String name, 97 NodeFactory subFolderFactory, 98 NodeFactory configurationDocumentFactory, 99 NodeFactory linkNodeFactory, 100 DirContext initialContext, 101 Name nodeContextName, 102 JNDILinkNodeConfiguration config) { 103 104 super( 105 parent, 106 name, 107 subFolderFactory, 108 configurationDocumentFactory, 109 linkNodeFactory); 110 111 if (initialContext == null) { 113 throw new InvalidParameterException( 114 this.getClass(), 115 "initialContext cannot be null in node [" 116 + getAbsoluteName() 117 + "]"); 118 } 119 if (nodeContextName == null) { 120 throw new InvalidParameterException( 121 this.getClass(), 122 "nodeContextName cannot be null in node [" 123 + getAbsoluteName() 124 + "]"); 125 } 126 if (config == null) { 127 throw new InvalidParameterException( 128 this.getClass(), 129 "config cannot be null in node [" 130 + getAbsoluteName() 131 + "]"); 132 } 133 134 this.initialContext = initialContext; 135 this.nodeContextName = nodeContextName; 136 this.config = config; 137 138 registerNamingListener(); 140 } 141 142 145 protected Set getAllChildNames() { 146 Set childNameSet = new HashSet (); 147 148 try { 149 152 Object [] subcontextFilterArgs = new Object [] { 155 this.config.getNodeTypeAttributeName(), 156 this.config.getFolderNodeTypeAttributeValue(), 157 this.config.getDocumentNodeTypeAttributeValue() 158 }; 159 160 SearchControls subcontextSearchControls = new SearchControls (); 162 subcontextSearchControls.setReturningAttributes( 163 new String [] { this.config.getNodeNameAttributeName() }); 164 165 NamingEnumeration childrenEnum = 166 this.initialContext.search( 167 nodeContextName, 168 JNDIFolder.SUBCONTEXT_FILTER, 169 subcontextFilterArgs, 170 subcontextSearchControls); 171 172 while (childrenEnum.hasMoreElements()) { 174 SearchResult result = (SearchResult ) childrenEnum.nextElement(); 175 childNameSet.add( 176 result 177 .getAttributes() 178 .get(this.config.getNodeNameAttributeName()) 179 .get()); 180 } 181 182 } catch (NamingException ne) { 183 if (log.isInfoEnabled()) { 184 log.info("Could not get names of children, reason: " 185 + ExceptionUtility.printStackTracesToString(ne)); 186 } 187 } 188 189 return childNameSet; 190 } 191 192 195 protected Node loadChild(String nodeName) throws NodeNotFoundException { 196 197 final String NAME = this.config.getNodeNameAttributeName(); 199 final String EQUALS = this.config.getAttributeNameValueSeparator(); 200 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 201 final String FOLDER = this.config.getFolderNodeTypeAttributeValue(); 202 final String DOCUMENT = this.config.getDocumentNodeTypeAttributeValue(); 203 final String DOC_TYPE = this.config.getDocumentTypeAttributeName(); 204 final String LINK = this.config.getLinkDocumentTypeAttributeValue(); 205 final String CONTENT = this.config.getDocumentDocumentTypeAttributeValue(); 206 207 try { 208 Name childContextName = (Name ) this.nodeContextName.clone(); 210 childContextName.add(NAME + EQUALS + nodeName); 211 212 String [] docuemntAttributeIDs = new String [] { 213 this.config.getNodeTypeAttributeName(), 214 this.config.getDocumentTypeAttributeName() 215 }; 216 217 Attributes attributes = this.initialContext.getAttributes( 219 childContextName, 220 docuemntAttributeIDs); 221 222 if (attributes.get(NODE_TYPE) != null) { 224 if (attributes.get(NODE_TYPE).contains(FOLDER)) { 225 return loadSubFolder(nodeName); 226 } 227 228 if (attributes.get(DOC_TYPE) != null && 229 attributes.get(NODE_TYPE).contains(DOCUMENT)) { 230 231 if (attributes.get(DOC_TYPE).contains(LINK)) { 232 return loadChildLinkNode(nodeName); 233 } 234 235 if (attributes.get(DOC_TYPE).contains(CONTENT)) { 236 return loadConfigurationDocument(nodeName); 237 } 238 } 239 } 240 241 throw new NodeNotFoundException( 244 this.getClass(), 245 this.getAbsoluteName() + Node.DELIMITER + nodeName); 246 247 } catch (NodeCreationException nce) { 248 throw new NodeNotFoundException(this.getClass(), nodeName, nce); 249 250 } catch (NamingException ne) { 251 throw new NodeNotFoundException(this.getClass(), nodeName, ne); 252 } 253 } 254 255 259 protected void destroyBackingData() throws NodeRemovalException { 260 try { 261 this.initialContext.destroySubcontext(this.nodeContextName); 262 } catch (NamingException ne) { 263 throw new NodeRemovalException(this.getClass(), this, ne); 264 } 265 } 266 267 272 DirContext getInitialContext() { 273 return this.initialContext; 274 } 275 276 281 Name getNodeContextName() { 282 return this.nodeContextName; 283 } 284 285 292 private Node loadSubFolder(String nodeName) throws NodeCreationException { 293 return getSubFolderFactory().getInstance(this, nodeName); 294 } 295 296 303 private Node loadConfigurationDocument(String nodeName) 304 throws NodeCreationException { 305 306 return getConfigurationDocumentFactory().getInstance(this, nodeName); 307 } 308 309 316 private Node loadChildLinkNode(String nodeName) 317 throws NodeCreationException { 318 319 final String NAME = this.config.getNodeNameAttributeName(); 321 final String EQUALS = this.config.getAttributeNameValueSeparator(); 322 final String LINK = this.config.getLinkDocumentTypeAttributeValue(); 323 324 try { 325 Name childContextName = (Name ) this.nodeContextName.clone(); 327 childContextName.add(NAME + EQUALS + nodeName); 328 329 ConfigurationDocument linkConfiguraitonDoc = 330 new JNDIConfigurationDocument( 331 this, 332 nodeName, 333 new DefaultConfigurationFormatService(), 334 this.initialContext, 335 childContextName, 336 this.config, 337 LINK); 338 339 LinkNodeConfiguration config = 340 (LinkNodeConfiguration) linkConfiguraitonDoc 341 .readConfiguration(); 342 343 if (config.getLinkNodeFactoryClass() == null) { 344 throw new InvalidConfigurationException( 345 this.getClass(), 346 config.getConfigurationName(), 347 "LinkNodeFactoryClass", 348 "Cannot be null"); 349 } 350 351 LinkNodeFactory factory = 353 (LinkNodeFactory) config 354 .getLinkNodeFactoryClass() 355 .newInstance(); 356 357 return factory.getInstance(this, nodeName, linkConfiguraitonDoc); 359 360 } catch (ConfigurationFormatException e) { 361 throw new NodeCreationException( 362 this.getClass(), this, nodeName, "exception occured", e); 363 } catch (NodeIOException e) { 364 throw new NodeCreationException( 365 this.getClass(), this, nodeName, "exception occured", e); 366 } catch (InstantiationException e) { 367 throw new NodeCreationException( 368 this.getClass(), this, nodeName, "exception occured", e); 369 } catch (IllegalAccessException e) { 370 throw new NodeCreationException( 371 this.getClass(), this, nodeName, "exception occured", e); 372 } catch (NamingException ne) { 373 throw new NodeCreationException( 374 this.getClass(), this, nodeName, "NamingException occured", ne); 375 } 376 } 377 378 382 protected boolean backingDataExists() { 383 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 384 final String FOLDER = this.config.getFolderNodeTypeAttributeValue(); 385 386 boolean backingDataExists; 387 try { 388 Attributes folderAttributes = 389 this.initialContext.getAttributes(this.nodeContextName); 390 391 if (folderAttributes.get(NODE_TYPE) != null && 392 folderAttributes.get(NODE_TYPE).contains(FOLDER)) { 393 394 backingDataExists = true; 395 } else { 396 backingDataExists = false; 397 } 398 } catch (NamingException ne) { 399 backingDataExists = false; 400 } 401 402 return backingDataExists; 403 } 404 405 409 public void objectAdded(NamingEvent evt) { 410 } 412 413 416 public void objectRemoved(NamingEvent evt) { 417 refresh(); 418 } 419 420 423 public void objectRenamed(NamingEvent evt) { 424 refresh(); 425 } 426 427 430 public void objectChanged(NamingEvent evt) { 431 refresh(); 432 } 433 434 437 public void namingExceptionThrown(NamingExceptionEvent evt) { 438 if (log.isInfoEnabled()) { 439 log.info("Notified of NamingExceptionEvent, this node is no " 440 + "longer listening for JNDI events, node name: [" 441 + getAbsoluteName() 442 + "], context name: [" 443 + this.nodeContextName 444 + "] naming exception: " 445 + ExceptionUtility.printStackTracesToString(evt.getException())); 446 } 447 } 448 449 452 protected void registerNamingListener() { 453 try { 454 EventContext thisContext = (EventContext ) 456 this.initialContext.lookup(this.nodeContextName); 457 458 thisContext.addNamingListener("", EventContext.OBJECT_SCOPE, this); 460 461 } catch (ClassCastException cce) { 462 if (log.isInfoEnabled()) { 463 log.info( 464 "Node was not registered as a JNDI NamingListener because " 465 + "events were not supported by directory, node: [" 466 + getAbsoluteName() 467 + "], context name: [" 468 + this.nodeContextName 469 + "]"); 470 } 471 } catch (NamingException ne) { 472 if (log.isInfoEnabled()) { 473 log.info( 474 "NamingException caught registering NamingListener, node: [" 475 + getAbsoluteName() 476 + "], context name: [" 477 + this.nodeContextName 478 + "]", 479 ne); 480 } 481 } 482 } 483 } | Popular Tags |