1 17 18 package org.sape.carbon.services.config.jndi; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 25 import javax.naming.Name ; 26 import javax.naming.NameNotFoundException ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.Attribute ; 29 import javax.naming.directory.Attributes ; 30 import javax.naming.directory.BasicAttributes ; 31 import javax.naming.directory.DirContext ; 32 import javax.naming.event.EventContext ; 33 import javax.naming.event.NamespaceChangeListener ; 34 import javax.naming.event.NamingEvent ; 35 import javax.naming.event.NamingExceptionEvent ; 36 import javax.naming.event.ObjectChangeListener ; 37 38 import org.sape.carbon.core.config.InvalidConfigurationException; 39 import org.sape.carbon.core.config.format.ConfigurationFormatService; 40 import org.sape.carbon.core.config.node.AbstractConfigurationDocument; 41 import org.sape.carbon.core.config.node.Node; 42 import org.sape.carbon.core.config.node.NodeRemovalException; 43 import org.sape.carbon.core.exception.ExceptionUtility; 44 import org.sape.carbon.core.exception.InvalidParameterException; 45 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 49 61 public class JNDIConfigurationDocument 62 extends AbstractConfigurationDocument 63 implements NamespaceChangeListener , ObjectChangeListener { 64 65 private Log log = LogFactory.getLog(this.getClass()); 66 67 private DirContext initialContext; 68 private Name nodeContextName; 69 private JNDILinkNodeConfiguration config; 70 private String documentType; 71 72 85 public JNDIConfigurationDocument( 86 Node parent, 87 String name, 88 ConfigurationFormatService formatter, 89 DirContext initialContext, 90 Name nodeContextName, 91 JNDILinkNodeConfiguration config, 92 String documentType) { 93 94 super(parent, name, formatter); 95 96 if (initialContext == null) { 98 throw new InvalidParameterException( 99 this.getClass(), 100 "initialContext cannot be null in node [" 101 + getAbsoluteName() 102 + "]"); 103 } 104 if (nodeContextName == null) { 105 throw new InvalidParameterException( 106 this.getClass(), 107 "nodeContextName cannot be null in node [" 108 + getAbsoluteName() 109 + "]"); 110 } 111 if (config == null) { 112 throw new InvalidParameterException( 113 this.getClass(), 114 "config cannot be null in node [" 115 + getAbsoluteName() 116 + "]"); 117 } 118 if (documentType == null) { 119 throw new InvalidParameterException( 120 this.getClass(), 121 "documentType cannot be null in node [" 122 + getAbsoluteName() 123 + "]"); 124 } 125 126 this.initialContext = initialContext; 127 this.nodeContextName = nodeContextName; 128 this.config = config; 129 this.documentType = documentType; 130 131 registerNamingListener(); 133 } 134 135 139 protected void destroyBackingData() throws NodeRemovalException { 140 try { 141 this.initialContext.destroySubcontext(this.nodeContextName); 142 } catch (NamingException ne) { 143 throw new NodeRemovalException(this.getClass(), this, ne); 144 } 145 } 146 147 154 protected InputStream openInputStream() throws NamingException { 155 String [] contentAttirbuteIDs = new String []{ 156 this.config.getDocumentContectAttributeName() 157 }; 158 159 Attributes documentAttributes = 161 this.initialContext.getAttributes( 162 this.nodeContextName, 163 contentAttirbuteIDs); 164 165 Attribute contentAttribute = 166 documentAttributes.get(config.getDocumentContectAttributeName()); 167 168 if (contentAttribute == null || contentAttribute.get() == null) { 169 throw new InvalidConfigurationException( 170 this.getClass(), 171 getAbsoluteName(), 172 "", 173 "content was missing from JNDI context: [" 174 + this.nodeContextName.toString() 175 + "]"); 176 } 177 178 String content = (String ) contentAttribute.get(); 180 return new ByteArrayInputStream (content.getBytes()); 181 } 182 183 186 protected OutputStream openOutputStream() { 187 return new ByteArrayOutputStream (); 188 } 189 190 194 protected void closeOutputStream(OutputStream out) throws Exception { 195 ByteArrayOutputStream outputStream = (ByteArrayOutputStream ) out; 196 197 final String NAME = this.config.getNodeNameAttributeName(); 199 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 200 final String DOCUMENT = this.config.getDocumentNodeTypeAttributeValue(); 201 final String DOC_TYPE = this.config.getDocumentTypeAttributeName(); 202 final String CONTENT = this.config.getDocumentContectAttributeName(); 203 204 try { 205 this.initialContext.modifyAttributes( 208 this.nodeContextName, 209 DirContext.REPLACE_ATTRIBUTE, 210 new BasicAttributes (CONTENT, outputStream.toString())); 211 212 } catch (NameNotFoundException nnfe) { 213 if (log.isTraceEnabled()) { 215 log.trace("Creating new context [" + this.nodeContextName + "]"); 216 } 217 218 Attributes docAttributes = new BasicAttributes (); 219 docAttributes.put(NAME, getName()); 220 docAttributes.put(NODE_TYPE, DOCUMENT); 221 docAttributes.put(DOC_TYPE, this.documentType); 222 docAttributes.put(CONTENT, outputStream.toString()); 223 224 initialContext.createSubcontext(nodeContextName, docAttributes); 225 226 registerNamingListener(); 227 } 228 229 230 super.closeOutputStream(out); 231 } 232 233 237 protected boolean backingDataExists() { 238 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 239 final String DOCUMENT = this.config.getDocumentNodeTypeAttributeValue(); 240 241 boolean backingDataExists; 242 try { 243 Attributes documentAttributes = 244 this.initialContext.getAttributes(this.nodeContextName); 245 if (documentAttributes.get(NODE_TYPE) != null && 246 documentAttributes.get(NODE_TYPE).contains(DOCUMENT)) { 247 248 backingDataExists = true; 249 } else { 250 backingDataExists = false; 251 } 252 } catch (NamingException ne) { 253 backingDataExists = false; 254 } 255 256 return backingDataExists; 257 } 258 259 262 public void objectAdded(NamingEvent evt) { 263 } 265 266 269 public void objectRemoved(NamingEvent evt) { 270 if (log.isTraceEnabled()) { 271 log.trace("Caught objectRemoved naming event on node [" + 272 getAbsoluteName() + "]"); 273 } 274 refresh(); 275 } 276 277 280 public void objectRenamed(NamingEvent evt) { 281 if (log.isTraceEnabled()) { 282 log.trace("Caught objectRenamed naming event on node [" + 283 getAbsoluteName() + "]"); 284 } 285 refresh(); 286 } 287 288 291 public void objectChanged(NamingEvent evt) { 292 if (log.isTraceEnabled()) { 293 log.trace("Caught objectChanged naming event on node [" + 294 getAbsoluteName() + "]"); 295 } 296 refresh(); 297 } 298 299 302 public void namingExceptionThrown(NamingExceptionEvent evt) { 303 if (log.isInfoEnabled()) { 304 log.info("Notified of NamingExceptionEvent, this node is no " 305 + "longer listening for JNDI events, node name: [" 306 + getAbsoluteName() 307 + "], context name: [" 308 + this.nodeContextName 309 + "], naming exception: " 310 + ExceptionUtility.printStackTracesToString(evt.getException())); 311 } 312 } 313 314 317 protected void registerNamingListener() { 318 try { 319 EventContext thisContext = (EventContext ) 321 this.initialContext.lookup(this.nodeContextName); 322 323 thisContext.addNamingListener("", EventContext.OBJECT_SCOPE, this); 325 326 } catch (ClassCastException cce) { 327 if (log.isInfoEnabled()) { 328 log.info( 329 "Node was not registered as a JNDI NamingListener because " 330 + "events were not supported by directory, node: [" 331 + getAbsoluteName() 332 + "], context name: [" 333 + this.nodeContextName 334 + "]"); 335 } 336 } catch (NamingException ne) { 337 if (log.isDebugEnabled()) { 338 log.debug( 339 "NamingException caught registering NamingListener, node: [" 340 + getAbsoluteName() 341 + "], context name: [" 342 + this.nodeContextName 343 + "]", 344 ne); 345 } 346 } 347 } 348 } | Popular Tags |