001: /*
002: * Copyright 2002-2006 the original author or authors.
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.springframework.mock.web.portlet;
018:
019: import java.util.Enumeration;
020: import java.util.Hashtable;
021:
022: import javax.portlet.PortletContext;
023: import javax.portlet.PortletSession;
024:
025: /**
026: * Mock implementation of the {@link javax.portlet.PortletSession} interface.
027: *
028: * @author John A. Lewis
029: * @author Juergen Hoeller
030: * @since 2.0
031: */
032: public class MockPortletSession implements PortletSession {
033:
034: private static int nextId = 1;
035:
036: private final String id = Integer.toString(nextId++);
037:
038: private final long creationTime = System.currentTimeMillis();
039:
040: private int maxInactiveInterval;
041:
042: private long lastAccessedTime = System.currentTimeMillis();
043:
044: private final PortletContext portletContext;
045:
046: private final Hashtable portletAttributes = new Hashtable();
047:
048: private final Hashtable applicationAttributes = new Hashtable();
049:
050: private boolean invalid = false;
051:
052: private boolean isNew = true;
053:
054: /**
055: * Create a new MockPortletSession with a default {@link MockPortletContext}.
056: * @see MockPortletContext
057: */
058: public MockPortletSession() {
059: this (null);
060: }
061:
062: /**
063: * Create a new MockPortletSession.
064: * @param portletContext the PortletContext that the session runs in
065: */
066: public MockPortletSession(PortletContext portletContext) {
067: this .portletContext = (portletContext != null ? portletContext
068: : new MockPortletContext());
069: }
070:
071: public Object getAttribute(String name) {
072: return this .portletAttributes.get(name);
073: }
074:
075: public Object getAttribute(String name, int scope) {
076: if (scope == PortletSession.PORTLET_SCOPE) {
077: return this .portletAttributes.get(name);
078: } else if (scope == PortletSession.APPLICATION_SCOPE) {
079: return this .applicationAttributes.get(name);
080: }
081: return null;
082: }
083:
084: public Enumeration getAttributeNames() {
085: return this .portletAttributes.keys();
086: }
087:
088: public Enumeration getAttributeNames(int scope) {
089: if (scope == PortletSession.PORTLET_SCOPE) {
090: return this .portletAttributes.keys();
091: } else if (scope == PortletSession.APPLICATION_SCOPE) {
092: return this .applicationAttributes.keys();
093: }
094: return null;
095: }
096:
097: public long getCreationTime() {
098: return this .creationTime;
099: }
100:
101: public String getId() {
102: return this .id;
103: }
104:
105: public void access() {
106: this .lastAccessedTime = System.currentTimeMillis();
107: setNew(false);
108: }
109:
110: public long getLastAccessedTime() {
111: return this .lastAccessedTime;
112: }
113:
114: public int getMaxInactiveInterval() {
115: return this .maxInactiveInterval;
116: }
117:
118: public void invalidate() {
119: this .invalid = true;
120: this .portletAttributes.clear();
121: this .applicationAttributes.clear();
122: }
123:
124: public boolean isInvalid() {
125: return invalid;
126: }
127:
128: public void setNew(boolean value) {
129: this .isNew = value;
130: }
131:
132: public boolean isNew() {
133: return this .isNew;
134: }
135:
136: public void removeAttribute(String name) {
137: this .portletAttributes.remove(name);
138: }
139:
140: public void removeAttribute(String name, int scope) {
141: if (scope == PortletSession.PORTLET_SCOPE) {
142: this .portletAttributes.remove(name);
143: } else if (scope == PortletSession.APPLICATION_SCOPE) {
144: this .applicationAttributes.remove(name);
145: }
146: }
147:
148: public void setAttribute(String name, Object value) {
149: if (value != null) {
150: this .portletAttributes.put(name, value);
151: } else {
152: this .portletAttributes.remove(name);
153: }
154: }
155:
156: public void setAttribute(String name, Object value, int scope) {
157: if (scope == PortletSession.PORTLET_SCOPE) {
158: if (value != null) {
159: this .portletAttributes.put(name, value);
160: } else {
161: this .portletAttributes.remove(name);
162: }
163: } else if (scope == PortletSession.APPLICATION_SCOPE) {
164: if (value != null) {
165: this .applicationAttributes.put(name, value);
166: } else {
167: this .applicationAttributes.remove(name);
168: }
169: }
170: }
171:
172: public void setMaxInactiveInterval(int interval) {
173: this .maxInactiveInterval = interval;
174: }
175:
176: public PortletContext getPortletContext() {
177: return portletContext;
178: }
179:
180: }
|