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.portal.context;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.component.Component;
025: import org.apache.avalon.framework.context.Context;
026: import org.apache.avalon.framework.context.ContextException;
027: import org.apache.avalon.framework.context.Contextualizable;
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.avalon.framework.thread.ThreadSafe;
033: import org.apache.cocoon.ProcessingException;
034: import org.apache.cocoon.components.ContextHelper;
035: import org.apache.cocoon.environment.ObjectModelHelper;
036: import org.apache.cocoon.environment.Request;
037: import org.apache.cocoon.environment.Session;
038: import org.apache.cocoon.webapps.portal.PortalConstants;
039: import org.apache.cocoon.webapps.portal.components.PortalManager;
040: import org.apache.cocoon.webapps.session.context.SessionContext;
041: import org.apache.cocoon.webapps.session.context.SessionContextProvider;
042: import org.apache.excalibur.source.SourceParameters;
043: import org.apache.excalibur.xml.xpath.XPathProcessor;
044: import org.xml.sax.SAXException;
045:
046: /**
047: * Context provider for the portal context
048: *
049: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
050: * @version CVS $Id: SessionContextProviderImpl.java 433543 2006-08-22 06:22:54Z crossley $
051: */
052: public final class SessionContextProviderImpl extends
053: AbstractLogEnabled implements SessionContextProvider,
054: ThreadSafe, Component, Serviceable, Contextualizable,
055: Disposable {
056:
057: private ServiceManager manager;
058: private Context context;
059:
060: /** The XPath Processor */
061: protected XPathProcessor xpathProcessor;
062:
063: /**
064: * Get the context
065: * @param name The name of the context
066: * @return The context
067: * @throws ProcessingException If the context is not available.
068: */
069: public SessionContext getSessionContext(String name)
070: throws ProcessingException {
071: Map objectModel = ContextHelper.getObjectModel(this .context);
072:
073: SessionContext context = this .getContext(objectModel, name);
074: if (name.equals(PortalConstants.SESSION_CONTEXT_NAME)
075: && context == null) {
076: Request req = ObjectModelHelper.getRequest(objectModel);
077: Session session = req.getSession(false);
078: if (session != null) {
079:
080: PortalManager portal = null;
081: try {
082: portal = (PortalManager) manager
083: .lookup(PortalManager.ROLE);
084: // is this an external resource which wants access to a coplet?
085: String value = req.getParameter("portalcontext");
086: if (value != null) {
087: int sepOne, sepTwo;
088: sepOne = value.indexOf('_');
089: if (sepOne != -1) {
090: sepTwo = value.indexOf('_', sepOne + 1);
091: if (sepTwo != -1) {
092: String copletIdentifier = value
093: .substring(0, sepOne);
094: String copletID = value.substring(
095: sepOne + 1, sepTwo);
096: String copletNumber = value
097: .substring(sepTwo + 1);
098:
099: if (copletIdentifier.equals("coplet")) {
100: Map info = new HashMap(3);
101: SessionContextImpl.copletInfo
102: .set(info);
103:
104: SourceParameters pars = new SourceParameters();
105: info
106: .put(
107: PortalConstants.COPLETINFO_PARAMETERS,
108: pars);
109: pars
110: .setSingleParameterValue(
111: PortalConstants.PARAMETER_ID,
112: copletID);
113: pars
114: .setSingleParameterValue(
115: PortalConstants.PARAMETER_NUMBER,
116: copletNumber);
117: pars
118: .setSingleParameterValue(
119: PortalConstants.PARAMETER_MEDIA,
120: portal
121: .getMediaType());
122:
123: info
124: .put(
125: PortalConstants.COPLETINFO_STATUSPROFILE,
126: portal
127: .getStatusProfile());
128: info
129: .put(
130: PortalConstants.COPLETINFO_PORTALURI,
131: req.getRequestURI());
132: }
133: }
134: }
135: } else {
136: if (SessionContextImpl.copletInfo.get() == null) {
137: throw new ProcessingException(
138: "Portal context not available outside a coplet.");
139: }
140: }
141: context = new SessionContextImpl(name, objectModel,
142: portal, this .xpathProcessor);
143: objectModel.put(this .getClass().getName() + name,
144: context);
145: } catch (SAXException se) {
146: throw new ProcessingException("SAXException", se);
147: } catch (IOException ioe) {
148: throw new ProcessingException("IOException", ioe);
149: } catch (ServiceException se) {
150: throw new ProcessingException(
151: "Unable to lookup portal.", se);
152: } finally {
153: manager.release(portal);
154: }
155: }
156: }
157: return context;
158: }
159:
160: /**
161: * Does the context exist?
162: */
163: public boolean existsSessionContext(String name)
164: throws ProcessingException {
165: final Map objectModel = ContextHelper
166: .getObjectModel(this .context);
167: return (this .getContext(objectModel, name) != null);
168: }
169:
170: private SessionContext getContext(Map objectModel, String name) {
171: SessionContext context = (SessionContext) objectModel.get(this
172: .getClass().getName()
173: + name);
174: if (context != null) {
175: SessionContextImpl r = (SessionContextImpl) context;
176: if (!(r.getRequest() == ObjectModelHelper
177: .getRequest(objectModel))) {
178: context = null;
179: objectModel.remove(this .getClass().getName() + name);
180: }
181: }
182: return context;
183: }
184:
185: /* (non-Javadoc)
186: * @see org.apache.avalon.framework.service.Serviceable#Service(org.apache.avalon.framework.Service.ServiceManager)
187: */
188: public void service(ServiceManager manager) throws ServiceException {
189: this .manager = manager;
190: this .xpathProcessor = (XPathProcessor) this .manager
191: .lookup(XPathProcessor.ROLE);
192: }
193:
194: /* (non-Javadoc)
195: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
196: */
197: public void contextualize(Context context) throws ContextException {
198: this .context = context;
199: }
200:
201: /* (non-Javadoc)
202: * @see org.apache.avalon.framework.activity.Disposable#dispose()
203: */
204: public void dispose() {
205: if (this.manager != null) {
206: this.manager.release(this.xpathProcessor);
207: this.xpathProcessor = null;
208: this.manager = null;
209: }
210: }
211: }
|