1 16 package org.apache.cocoon.webapps.session.components; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.context.Context; 21 import org.apache.avalon.framework.context.ContextException; 22 import org.apache.avalon.framework.context.Contextualizable; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.cocoon.components.ContextHelper; 30 import org.apache.cocoon.environment.Request; 31 import org.apache.cocoon.environment.Session; 32 import org.apache.cocoon.webapps.session.ContextManager; 33 import org.apache.cocoon.webapps.session.SessionConstants; 34 import org.apache.cocoon.webapps.session.SessionManager; 35 import org.apache.cocoon.webapps.session.context.SessionContext; 36 import org.apache.cocoon.xml.XMLConsumer; 37 import org.apache.cocoon.xml.XMLUtils; 38 import org.apache.cocoon.xml.dom.DOMUtil; 39 import org.w3c.dom.DocumentFragment ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.xml.sax.SAXException ; 44 45 51 public final class DefaultSessionManager 52 extends AbstractLogEnabled 53 implements Serviceable, Component, ThreadSafe, SessionManager, Disposable, Contextualizable { 54 55 56 private Context context; 57 58 59 private ServiceManager manager; 60 61 62 private ContextManager contextManager; 63 64 67 public void service(ServiceManager manager) 68 throws ServiceException { 69 this.manager = manager; 70 this.contextManager = (ContextManager)this.manager.lookup(ContextManager.ROLE); 71 } 72 73 76 public void dispose() { 77 if (this.manager != null ) { 78 this.manager.release(this.contextManager); 79 this.manager = null; 80 this.contextManager = null; 81 } 82 } 83 88 public Session createSession() { 89 if (this.getLogger().isDebugEnabled() ) { 91 this.getLogger().debug("BEGIN createSession"); 92 } 93 Session session = this.getSession(true); 94 95 if (this.getLogger().isDebugEnabled() ) { 96 this.getLogger().debug("END createSession session=" + session); 97 } 98 return session; 99 } 100 101 106 public Session getSession(boolean createFlag) { 107 final Request request = ContextHelper.getRequest(this.context); 108 109 if (this.getLogger().isDebugEnabled() ) { 111 this.getLogger().debug("BEGIN getSession create=" + createFlag); 112 } 113 Session session = request.getSession(createFlag); 114 115 if (this.getLogger().isDebugEnabled() ) { 116 this.getLogger().debug("END getSession session=" + session); 117 } 118 119 return session; 120 } 121 122 130 public void terminateSession(boolean force) 131 throws ProcessingException { 132 if (this.getLogger().isDebugEnabled() ) { 134 this.getLogger().debug("BEGIN terminateSession force="+force); 135 } 136 137 Session session = this.getSession( false ); 138 if (session != null) { 139 if (force || this.contextManager.hasSessionContext() ) { 140 synchronized(session) { 141 session.invalidate(); 142 } 143 } 144 } 145 if (this.getLogger().isDebugEnabled()) { 146 this.getLogger().debug("END terminateSession"); 147 } 148 } 149 150 159 public DocumentFragment getContextFragment(String contextName, 160 String path) 161 throws ProcessingException { 162 if (this.getLogger().isDebugEnabled() ) { 164 this.getLogger().debug("BEGIN getContextFragment name=" + contextName + ", path=" + path); 165 } 166 167 if (contextName == null) { 169 throw new ProcessingException("SessionManager.getContextFragment: Name is required"); 170 } 171 if (path == null) { 172 throw new ProcessingException("SessionManager.getContextFragment: Path is required"); 173 } 174 175 SessionContext context = this.contextManager.getContext( contextName ); 176 177 if (context == null) { 178 throw new ProcessingException("SessionManager.getContextFragment: Context '" + contextName + "' not found."); 179 } 180 181 DocumentFragment frag; 182 frag = context.getXML(path); 183 184 if (this.getLogger().isDebugEnabled() ) { 185 this.getLogger().debug("END getContextFragment documentFragment=" + (frag == null ? "null" : XMLUtils.serializeNode(frag, XMLUtils.createPropertiesForXML(false)))); 186 } 187 return frag; 188 } 189 190 201 public boolean streamContextFragment(String contextName, 202 String path, 203 XMLConsumer consumer) 204 throws SAXException , ProcessingException { 205 if (this.getLogger().isDebugEnabled() ) { 207 this.getLogger().debug("BEGIN streamContextFragment name=" + contextName + ", path=" + path + ", consumer"+consumer); 208 } 209 boolean streamed = false; 210 211 if (contextName == null) { 213 throw new ProcessingException("SessionManager.streamContextFragment: Name is required"); 214 } 215 if (path == null) { 216 throw new ProcessingException("SessionManager.streamContextFragment: Path is required"); 217 } 218 219 SessionContext context = this.contextManager.getContext( contextName ); 220 221 if (context == null) { 222 throw new ProcessingException("SessionManager.streamContextFragment: Context '" + contextName + "' not found."); 223 } 224 225 streamed = context.streamXML(path, consumer, consumer); 226 227 if (this.getLogger().isDebugEnabled() ) { 228 this.getLogger().debug("END streamContextFragment streamed=" + streamed); 229 } 230 return streamed; 231 } 232 233 243 public void setContextFragment(String contextName, 244 String path, 245 DocumentFragment fragment) 246 throws ProcessingException { 247 249 if (this.getLogger().isDebugEnabled()) { 250 this.getLogger().debug("BEGIN setContextFragment name=" + contextName + ", path=" + path + 251 ", fragment=" + (fragment == null ? "null" : XMLUtils.serializeNode(fragment, XMLUtils.createPropertiesForXML(false)))); 252 } 253 if (contextName == null) { 255 throw new ProcessingException("SessionManager.setContextFragment: Name is required"); 256 } 257 if (path == null) { 258 throw new ProcessingException("SessionManager.setContextFragment: Path is required"); 259 } 260 if (fragment == null) { 261 throw new ProcessingException("SessionManager.setContextFragment: Fragment is required"); 262 } 263 264 SessionContext context = this.contextManager.getContext( contextName ); 266 267 if (context == null) { 269 throw new ProcessingException("SessionManager.setContextFragment: Context '" + contextName + "' not found."); 270 } 271 272 context.setXML(path, fragment); 273 274 if (this.getLogger().isDebugEnabled() ) { 275 this.getLogger().debug("END setContextFragment"); 276 } 277 } 278 279 289 public void appendContextFragment(String contextName, 290 String path, 291 DocumentFragment fragment) 292 throws ProcessingException { 293 if (this.getLogger().isDebugEnabled() ) { 295 this.getLogger().debug("BEGIN appendContextFragment name=" + contextName + 296 ", path=" + path + 297 ", fragment=" + (fragment == null ? "null" : XMLUtils.serializeNode(fragment, XMLUtils.createPropertiesForXML(false)))); 298 } 299 if (contextName == null) { 301 throw new ProcessingException("SessionManager.appendContextFragment: Name is required"); 302 } 303 if (path == null) { 304 throw new ProcessingException("SessionManager.appendContextFragment: Path is required"); 305 } 306 if (fragment == null) { 307 throw new ProcessingException("SessionManager.appendContextFragment: Fragment is required"); 308 } 309 310 SessionContext context = this.contextManager.getContext( contextName ); 312 313 if (context == null) { 315 throw new ProcessingException("SessionManager.appendContextFragment: Context '" + contextName + "' not found."); 316 } 317 318 context.appendXML(path, fragment); 319 320 if (this.getLogger().isDebugEnabled() ) { 321 this.getLogger().debug("END appendContextFragment"); 322 } 323 } 324 325 335 public void mergeContextFragment(String contextName, 336 String path, 337 DocumentFragment fragment) 338 throws ProcessingException { 339 if (this.getLogger().isDebugEnabled() ) { 341 this.getLogger().debug("BEGIN mergeContextFragment name=" + contextName + ", path=" + path + 342 ", fragment=" + (fragment == null ? "null" : XMLUtils.serializeNode(fragment, XMLUtils.createPropertiesForXML(false)))); 343 } 344 345 if (contextName == null) { 347 throw new ProcessingException("SessionManager.mergeContextFragment: Name is required"); 348 } 349 if (path == null) { 350 throw new ProcessingException("SessionManager.mergeContextFragment: Path is required"); 351 } 352 if (fragment == null) { 353 throw new ProcessingException("SessionManager.mergeContextFragment: Fragment is required"); 354 } 355 356 SessionContext context = this.contextManager.getContext( contextName ); 358 359 if (context == null) { 361 throw new ProcessingException("SessionManager.mergeContextFragment: Context '" + contextName + "' not found."); 362 } 363 364 Node contextNode = context.getSingleNode(path); 365 if (contextNode == null) { 366 context.setXML(path, fragment); 368 } else { 369 this.importNode(contextNode, fragment, false); 370 context.setNode(path, contextNode); 371 } 372 373 if (this.getLogger().isDebugEnabled()) { 374 this.getLogger().debug("END mergeContextFragment"); 375 } 376 } 377 378 386 public void removeContextFragment(String contextName, 387 String path) 388 throws ProcessingException { 389 if (this.getLogger().isDebugEnabled() ) { 391 this.getLogger().debug("BEGIN removeContextFragment name=" + contextName + ", path=" + path); 392 } 393 if (contextName == null) { 395 throw new ProcessingException("SessionManager.removeContextFragment: Name is required"); 396 } 397 if (path == null) { 398 throw new ProcessingException("SessionManager.removeContextFragment: Path is required"); 399 } 400 401 SessionContext context = this.contextManager.getContext( contextName ); 403 404 if (context == null) { 406 throw new ProcessingException("SessionManager.removeContextFragment: Context '" + contextName + "' not found."); 407 } 408 409 context.removeXML(path); 410 411 if (this.getLogger().isDebugEnabled() ) { 412 this.getLogger().debug("END removeContextFragment"); 413 } 414 } 415 416 421 private void importNode(Node profile, Node delta, boolean preserve) { 422 NodeList profileChilds = null; 424 NodeList deltaChilds = delta.getChildNodes(); 425 int i, len; 426 int m, l; 427 boolean found; 428 Node currentDelta = null; 429 Node currentProfile = null; 430 431 len = deltaChilds.getLength(); 432 for(i = 0; i < len; i++) { 433 currentDelta = deltaChilds.item(i); 434 if (currentDelta.getNodeType() == Node.ELEMENT_NODE) { 435 profileChilds = profile.getChildNodes(); 437 l = profileChilds.getLength(); 438 m = 0; 439 found = false; 440 while (found == false && m < l) { 441 currentProfile = profileChilds.item(m); 442 if (currentProfile.getNodeType() == Node.ELEMENT_NODE 443 && currentProfile.getNodeName().equals(currentDelta.getNodeName()) == true) { 444 445 found = DOMUtil.compareAttributes((Element )currentProfile, (Element )currentDelta); 448 } 449 if (found == false) m++; 450 } 451 if (found == true) { 452 454 if (preserve == true 455 && ((Element )currentDelta).hasAttributeNS(SessionConstants.SESSION_NAMESPACE_URI, "preserve") 456 && ((Element )currentDelta).getAttributeNS(SessionConstants.SESSION_NAMESPACE_URI, "preserve").equalsIgnoreCase("true")) { 457 profile.replaceChild(profile.getOwnerDocument().importNode(currentDelta, true), 459 currentProfile); 460 } else { 461 if (currentDelta.hasChildNodes() == true) { 463 currentDelta.normalize(); 464 currentProfile.normalize(); 465 this.importNode(currentProfile, currentDelta, preserve); 467 NodeList childs = currentProfile.getChildNodes(); 470 int index, max; 471 max = childs.getLength(); 472 for(index = max - 1; index >= 0; index--) { 473 if (childs.item(index).getNodeType() == Node.TEXT_NODE) { 474 currentProfile.removeChild(childs.item(index)); 475 } 476 } 477 childs = currentDelta.getChildNodes(); 478 max = childs.getLength(); 479 for(index = 0; index < max; index++) { 480 if (childs.item(index).getNodeType() == Node.TEXT_NODE) { 481 currentProfile.appendChild(currentProfile.getOwnerDocument() 482 .createTextNode(childs.item(index).getNodeValue())); 483 } 484 } 485 } 486 } 487 } else { 488 profile.appendChild(profile.getOwnerDocument().importNode(currentDelta, true)); 489 } 490 } 491 492 } 493 494 } 495 496 499 public void contextualize(Context context) throws ContextException { 500 this.context = context; 501 } 502 503 } 504 | Popular Tags |