001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.webapps.session.context;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.component.Component;
023: import org.apache.avalon.framework.context.Context;
024: import org.apache.avalon.framework.context.ContextException;
025: import org.apache.avalon.framework.context.Contextualizable;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.avalon.framework.thread.ThreadSafe;
031: import org.apache.cocoon.ProcessingException;
032: import org.apache.cocoon.components.ContextHelper;
033: import org.apache.cocoon.environment.ObjectModelHelper;
034: import org.apache.cocoon.webapps.session.SessionConstants;
035: import org.apache.excalibur.source.SourceResolver;
036: import org.apache.excalibur.xml.xpath.XPathProcessor;
037:
038: /**
039: * Context provider for the temporarily context, the request and the
040: * response context.
041: *
042: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
043: * @deprecated This block is deprecated and will be removed in future versions.
044: * @version CVS $Id: StandardSessionContextProvider.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public final class StandardSessionContextProvider extends
047: AbstractLogEnabled implements SessionContextProvider,
048: ThreadSafe, Contextualizable, Serviceable, Component,
049: Disposable {
050:
051: protected Context context;
052:
053: protected ServiceManager manager;
054:
055: /** The xpath processor */
056: protected XPathProcessor xpathProcessor;
057:
058: /** The Source Resolver */
059: protected SourceResolver resolver;
060:
061: /**
062: * Get the context
063: * @param name The name of the context
064: * @return The context
065: * @throws ProcessingException If the context is not available.
066: */
067: public SessionContext getSessionContext(String name)
068: throws ProcessingException {
069: final Map objectModel = ContextHelper
070: .getObjectModel(this .context);
071:
072: // get the context from the object model
073: SessionContext context = this .getContext(objectModel, name);
074: if (context == null) {
075: if (name.equals(SessionConstants.TEMPORARY_CONTEXT)) {
076: context = new SimpleSessionContext(this .xpathProcessor,
077: this .resolver);
078: context.setup(name, null, null);
079: } else if (name.equals(SessionConstants.REQUEST_CONTEXT)) {
080: context = new RequestSessionContext(this .getLogger());
081: context.setup(name, null, null);
082: ((RequestSessionContext) context).setup(objectModel,
083: this .manager, this .xpathProcessor);
084: }
085: objectModel.put(this .getClass().getName() + name, context);
086: }
087: return context;
088: }
089:
090: /**
091: * Does the context exist?
092: */
093: public boolean existsSessionContext(String name)
094: throws ProcessingException {
095: final Map objectModel = ContextHelper
096: .getObjectModel(this .context);
097: return (this .getContext(objectModel, name) != null);
098: }
099:
100: private SessionContext getContext(Map objectModel, String name) {
101: SessionContext context = (SessionContext) objectModel.get(this
102: .getClass().getName()
103: + name);
104: if (context != null
105: && !name.equals(SessionConstants.TEMPORARY_CONTEXT)) {
106: if (name.equals(SessionConstants.REQUEST_CONTEXT)) {
107: RequestSessionContext r = (RequestSessionContext) context;
108: if (!(r.getRequest() == ObjectModelHelper
109: .getRequest(objectModel))) {
110: context = null;
111: objectModel
112: .remove(this .getClass().getName() + name);
113: }
114: }
115: }
116: return context;
117: }
118:
119: /* (non-Javadoc)
120: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
121: */
122: public void contextualize(Context context) throws ContextException {
123: this .context = context;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
128: */
129: public void service(ServiceManager manager) throws ServiceException {
130: this .manager = manager;
131: this .xpathProcessor = (XPathProcessor) this .manager
132: .lookup(XPathProcessor.ROLE);
133: this .resolver = (SourceResolver) this .manager
134: .lookup(SourceResolver.ROLE);
135: }
136:
137: /* (non-Javadoc)
138: * @see org.apache.avalon.framework.activity.Disposable#dispose()
139: */
140: public void dispose() {
141: if (this.manager != null) {
142: this.manager.release(this.xpathProcessor);
143: this.manager.release(this.resolver);
144: this.resolver = null;
145: this.xpathProcessor = null;
146: this.manager = null;
147: }
148: }
149:
150: }
|