View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.tapestry;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.abstracthorizon.danube.http.session.Session;
19  import org.apache.tapestry.describe.DescriptionReceiver;
20  import org.apache.tapestry.web.WebSession;
21  
22  /**
23   * {@link WebSession} interface implementation
24   *
25   * @author Daniel Sendula
26   */
27  public class DanubeSession implements WebSession {
28  
29      /** Attribute in session this object is going to be stored */
30      public static final String SESSION_ATTRIBUTE = "org.abstracthorizon.danube.tapestry.WebSession";
31  
32      /** Danube's session */
33      protected Session session;
34  
35      /** Flag to denote if the session is new */
36      protected boolean isNew = true;
37  
38      /**
39       * Constructor
40       * @param session danube's session
41       */
42      public DanubeSession(Session session) {
43          this.session = session;
44          session.getAttributes().put(SESSION_ATTRIBUTE, this);
45      }
46  
47      /**
48       * Returns session id
49       * @return session id
50       */
51      public String getId() {
52          return session.getSessionId();
53      }
54  
55      /**
56       * Returns <code>true</code> if session is newly created
57       * @return <code>true</code> if session is newly created
58       */
59      public boolean isNew() {
60          return isNew;
61      }
62  
63      /**
64       * Clears {@link #isNew} flag.
65       */
66      protected void clearNew() {
67          isNew = false;
68      }
69  
70      /**
71       * Invalidates session. It removes this object from danube's session.
72       */
73      public void invalidate() {
74          session.getAttributes().remove(SESSION_ATTRIBUTE);
75      }
76  
77      /**
78       * Returns attributes names as a list
79       * @return attributes names as a list
80       */
81      @SuppressWarnings("unchecked")
82      public List getAttributeNames() {
83          ArrayList<String> names = new ArrayList<String>(session.getAttributes().keySet());
84          return names;
85      }
86  
87      /**
88       * Returns attribute
89       * @param key attribute name
90       * @return attribute's value
91       */
92      public Object getAttribute(String key) {
93          return session.getAttributes().get(key);
94      }
95  
96      /**
97       * Sets attribute
98       * @param key attribute's name
99       * @param value attribute's value
100      */
101     public void setAttribute(String key, Object value) {
102         session.getAttributes().put(key, value);
103     }
104 
105     /**
106      * Does nothing
107      */
108     public void describeTo(DescriptionReceiver descriptionReceiver) {
109     }
110 
111 }