001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka.jelly;
018    
019    import java.util.Map;
020    
021    import org.apache.commons.jelly.JellyTagException;
022    import org.apache.commons.jelly.TagSupport;
023    import org.apache.commons.jelly.XMLOutput;
024    
025    import org.apache.commons.latka.http.Session;
026    import org.apache.commons.latka.http.SessionImpl;
027    
028    /**
029     *
030     * @author  Morgan Delagrange
031     */
032    public class SessionTag extends TagSupport {
033            
034        protected String _sessionId = null;
035        protected String _label = null;
036        protected Session _session = null;
037    
038        /**
039         *  
040         *
041         * @param xmlOutput a place to write output
042         * @throws JellyTagException if the tag body could not be invoked
043         */
044        public void doTag(XMLOutput xmlOutput) throws JellyTagException {
045            _session = findSession(_sessionId);
046            invokeBody(xmlOutput);
047        }
048    
049        public Session getSession() {
050            return _session;
051        }
052    
053        protected Session findSession(String sessionId) {
054            if (sessionId == null) {
055                return new SessionImpl();
056            }
057    
058            SuiteTag tag = (SuiteTag) findAncestorWithClass(SuiteTag.class);
059            Map sessionCache = tag.getSessionCache();
060            Session cachedSession = (Session) sessionCache.get(sessionId);
061            if (cachedSession == null) {
062                Session session = new SessionImpl();
063                sessionCache.put(sessionId,session);
064                return session;
065            } else {
066                return cachedSession;
067            }
068        }
069    
070        /**
071         * Optionally sets the session id.  If a session id
072         * is specified, state information (cookies, etc.)
073         * will be shared by any session with the same id.
074         * 
075         * @param sessionId arbitrary session id, specified by the user in
076         *                  the Latka script
077         */
078        public void setSessionId(String sessionId) {
079            _sessionId = sessionId;
080        }
081    
082        /**
083         * Set the label for this session
084         * 
085         * @param label  session label
086         */
087        public void setLabel(String label) {
088            _label = label;
089        }
090    }