| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SessionTag |
|
| 1.8;1.8 |
| 1 | /* | |
| 2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | ||
| 17 | package org.apache.commons.latka.jelly; | |
| 18 | ||
| 19 | import java.util.Map; | |
| 20 | ||
| 21 | import org.apache.commons.jelly.JellyTagException; | |
| 22 | import org.apache.commons.jelly.TagSupport; | |
| 23 | import org.apache.commons.jelly.XMLOutput; | |
| 24 | ||
| 25 | import org.apache.commons.latka.http.Session; | |
| 26 | import org.apache.commons.latka.http.SessionImpl; | |
| 27 | ||
| 28 | /** | |
| 29 | * | |
| 30 | * @author Morgan Delagrange | |
| 31 | */ | |
| 32 | 0 | public class SessionTag extends TagSupport { |
| 33 | ||
| 34 | 0 | protected String _sessionId = null; |
| 35 | 0 | protected String _label = null; |
| 36 | 0 | protected Session _session = null; |
| 37 | ||
| 38 | /** | |
| 39 | * | |
| 40 | * | |
| 41 | * @param xmlOutput a place to write output | |
| 42 | * @throws JellyTagException if the tag body could not be invoked | |
| 43 | */ | |
| 44 | public void doTag(XMLOutput xmlOutput) throws JellyTagException { | |
| 45 | 0 | _session = findSession(_sessionId); |
| 46 | 0 | invokeBody(xmlOutput); |
| 47 | 0 | } |
| 48 | ||
| 49 | public Session getSession() { | |
| 50 | 0 | return _session; |
| 51 | } | |
| 52 | ||
| 53 | protected Session findSession(String sessionId) { | |
| 54 | 0 | if (sessionId == null) { |
| 55 | 0 | return new SessionImpl(); |
| 56 | } | |
| 57 | ||
| 58 | 0 | SuiteTag tag = (SuiteTag) findAncestorWithClass(SuiteTag.class); |
| 59 | 0 | Map sessionCache = tag.getSessionCache(); |
| 60 | 0 | Session cachedSession = (Session) sessionCache.get(sessionId); |
| 61 | 0 | if (cachedSession == null) { |
| 62 | 0 | Session session = new SessionImpl(); |
| 63 | 0 | sessionCache.put(sessionId,session); |
| 64 | 0 | return session; |
| 65 | } else { | |
| 66 | 0 | return cachedSession; |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Optionally sets the session id. If a session id | |
| 72 | * is specified, state information (cookies, etc.) | |
| 73 | * will be shared by any session with the same id. | |
| 74 | * | |
| 75 | * @param sessionId arbitrary session id, specified by the user in | |
| 76 | * the Latka script | |
| 77 | */ | |
| 78 | public void setSessionId(String sessionId) { | |
| 79 | 0 | _sessionId = sessionId; |
| 80 | 0 | } |
| 81 | ||
| 82 | /** | |
| 83 | * Set the label for this session | |
| 84 | * | |
| 85 | * @param label session label | |
| 86 | */ | |
| 87 | public void setLabel(String label) { | |
| 88 | 0 | _label = label; |
| 89 | 0 | } |
| 90 | } |