KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > jelly > SessionTag


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 JavaDoc;
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 public class SessionTag extends TagSupport {
33         
34     protected String JavaDoc _sessionId = null;
35     protected String JavaDoc _label = null;
36     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         _session = findSession(_sessionId);
46         invokeBody(xmlOutput);
47     }
48
49     public Session getSession() {
50         return _session;
51     }
52
53     protected Session findSession(String JavaDoc sessionId) {
54         if (sessionId == null) {
55             return new SessionImpl();
56         }
57
58         SuiteTag tag = (SuiteTag) findAncestorWithClass(SuiteTag.class);
59         Map JavaDoc sessionCache = tag.getSessionCache();
60         Session cachedSession = (Session) sessionCache.get(sessionId);
61         if (cachedSession == null) {
62             Session session = new SessionImpl();
63             sessionCache.put(sessionId,session);
64             return session;
65         } else {
66             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 JavaDoc sessionId) {
79         _sessionId = sessionId;
80     }
81
82     /**
83      * Set the label for this session
84      *
85      * @param label session label
86      */

87     public void setLabel(String JavaDoc label) {
88         _label = label;
89     }
90 }
91
Popular Tags