KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockPortletSession


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.mock.web.portlet;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import javax.portlet.PortletContext;
23 import javax.portlet.PortletSession;
24
25 /**
26  * Mock implementation of the {@link javax.portlet.PortletSession} interface.
27  *
28  * @author John A. Lewis
29  * @author Juergen Hoeller
30  * @since 2.0
31  */

32 public class MockPortletSession implements PortletSession {
33
34     private static int nextId = 1;
35
36
37     private final String JavaDoc id = Integer.toString(nextId++);
38
39     private final long creationTime = System.currentTimeMillis();
40
41     private int maxInactiveInterval;
42
43     private long lastAccessedTime = System.currentTimeMillis();
44
45     private final PortletContext portletContext;
46
47     private final Hashtable JavaDoc portletAttributes = new Hashtable JavaDoc();
48
49     private final Hashtable JavaDoc applicationAttributes = new Hashtable JavaDoc();
50
51     private boolean invalid = false;
52
53     private boolean isNew = true;
54
55
56     /**
57      * Create a new MockPortletSession with a default {@link MockPortletContext}.
58      * @see MockPortletContext
59      */

60     public MockPortletSession() {
61         this(null);
62     }
63
64     /**
65      * Create a new MockPortletSession.
66      * @param portletContext the PortletContext that the session runs in
67      */

68     public MockPortletSession(PortletContext portletContext) {
69         this.portletContext = (portletContext != null ? portletContext : new MockPortletContext());
70     }
71
72     
73     public Object JavaDoc getAttribute(String JavaDoc name) {
74         return this.portletAttributes.get(name);
75     }
76
77     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
78         if (scope == PortletSession.PORTLET_SCOPE) {
79             return this.portletAttributes.get(name);
80         }
81         else if (scope == PortletSession.APPLICATION_SCOPE) {
82             return this.applicationAttributes.get(name);
83         }
84         return null;
85     }
86
87     public Enumeration JavaDoc getAttributeNames() {
88         return this.portletAttributes.keys();
89     }
90
91     public Enumeration JavaDoc getAttributeNames(int scope) {
92         if (scope == PortletSession.PORTLET_SCOPE) {
93             return this.portletAttributes.keys();
94         }
95         else if (scope == PortletSession.APPLICATION_SCOPE) {
96             return this.applicationAttributes.keys();
97         }
98         return null;
99     }
100
101     public long getCreationTime() {
102         return this.creationTime;
103     }
104
105     public String JavaDoc getId() {
106         return this.id;
107     }
108
109     public void access() {
110         this.lastAccessedTime = System.currentTimeMillis();
111         setNew(false);
112     }
113
114     public long getLastAccessedTime() {
115         return this.lastAccessedTime;
116     }
117
118     public int getMaxInactiveInterval() {
119         return this.maxInactiveInterval;
120     }
121
122     public void invalidate() {
123         this.invalid = true;
124         this.portletAttributes.clear();
125         this.applicationAttributes.clear();
126     }
127
128     public boolean isInvalid() {
129         return invalid;
130     }
131
132     public void setNew(boolean value) {
133         this.isNew = value;
134     }
135
136     public boolean isNew() {
137         return this.isNew;
138     }
139
140     public void removeAttribute(String JavaDoc name) {
141         this.portletAttributes.remove(name);
142     }
143
144     public void removeAttribute(String JavaDoc name, int scope) {
145         if (scope == PortletSession.PORTLET_SCOPE) {
146             this.portletAttributes.remove(name);
147         }
148         else if (scope == PortletSession.APPLICATION_SCOPE) {
149             this.applicationAttributes.remove(name);
150         }
151     }
152
153     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
154         if (value != null) {
155             this.portletAttributes.put(name, value);
156         }
157         else {
158             this.portletAttributes.remove(name);
159         }
160     }
161
162     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
163         if (scope == PortletSession.PORTLET_SCOPE) {
164             if (value != null) {
165                 this.portletAttributes.put(name, value);
166             }
167             else {
168                 this.portletAttributes.remove(name);
169             }
170         }
171         else if (scope == PortletSession.APPLICATION_SCOPE) {
172             if (value != null) {
173                 this.applicationAttributes.put(name, value);
174             }
175             else {
176                 this.applicationAttributes.remove(name);
177             }
178         }
179     }
180
181     public void setMaxInactiveInterval(int interval) {
182         this.maxInactiveInterval = interval;
183     }
184
185     public PortletContext getPortletContext() {
186         return portletContext;
187     }
188
189 }
190
Popular Tags