1 16 package org.apache.cocoon.webapps.session.context; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.components.ContextHelper; 32 import org.apache.cocoon.environment.ObjectModelHelper; 33 import org.apache.cocoon.webapps.session.SessionConstants; 34 import org.apache.excalibur.source.SourceResolver; 35 import org.apache.excalibur.xml.xpath.XPathProcessor; 36 37 44 public final class StandardSessionContextProvider 45 extends AbstractLogEnabled 46 implements SessionContextProvider, ThreadSafe, Contextualizable, Serviceable, Component, Disposable { 47 48 protected Context context; 49 50 protected ServiceManager manager; 51 52 53 protected XPathProcessor xpathProcessor; 54 55 56 protected SourceResolver resolver; 57 58 64 public SessionContext getSessionContext(String name) 65 throws ProcessingException { 66 final Map objectModel = ContextHelper.getObjectModel( this.context ); 67 68 SessionContext context = this.getContext( objectModel, name ); 70 if ( context == null ) { 71 if ( name.equals(SessionConstants.TEMPORARY_CONTEXT) ) { 72 context = new SimpleSessionContext(this.xpathProcessor, this.resolver); 73 context.setup(name, null, null); 74 } else if ( name.equals(SessionConstants.REQUEST_CONTEXT) ) { 75 context = new RequestSessionContext(this.getLogger()); 76 context.setup(name, null, null); 77 ((RequestSessionContext)context).setup( objectModel, this.manager, this.xpathProcessor ); 78 } 79 objectModel.put(this.getClass().getName()+name, context); 80 } 81 return context; 82 } 83 84 87 public boolean existsSessionContext(String name) 88 throws ProcessingException { 89 final Map objectModel = ContextHelper.getObjectModel(this.context); 90 return (this.getContext( objectModel, name) != null); 91 } 92 93 private SessionContext getContext(Map objectModel, String name) { 94 SessionContext context = (SessionContext) objectModel.get(this.getClass().getName()+name); 95 if ( context != null && !name.equals(SessionConstants.TEMPORARY_CONTEXT)) { 96 if ( name.equals(SessionConstants.REQUEST_CONTEXT)) { 97 RequestSessionContext r = (RequestSessionContext)context; 98 if (!(r.getRequest() == ObjectModelHelper.getRequest( objectModel))) { 99 context = null; 100 objectModel.remove(this.getClass().getName()+name); 101 } 102 } 103 } 104 return context; 105 } 106 107 110 public void contextualize(Context context) throws ContextException { 111 this.context = context; 112 } 113 114 117 public void service(ServiceManager manager) throws ServiceException { 118 this.manager = manager; 119 this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); 120 this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE); 121 } 122 123 126 public void dispose() { 127 if ( this.manager != null) { 128 this.manager.release( this.xpathProcessor ); 129 this.manager.release(this.resolver); 130 this.resolver = null; 131 this.xpathProcessor = null; 132 this.manager = null; 133 } 134 } 135 136 } 137 | Popular Tags |