1 16 17 package org.springframework.mock.web.portlet; 18 19 import java.util.Enumeration ; 20 import java.util.Hashtable ; 21 22 import javax.portlet.PortletContext; 23 import javax.portlet.PortletSession; 24 25 32 public class MockPortletSession implements PortletSession { 33 34 private static int nextId = 1; 35 36 37 private final String 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 portletAttributes = new Hashtable (); 48 49 private final Hashtable applicationAttributes = new Hashtable (); 50 51 private boolean invalid = false; 52 53 private boolean isNew = true; 54 55 56 60 public MockPortletSession() { 61 this(null); 62 } 63 64 68 public MockPortletSession(PortletContext portletContext) { 69 this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); 70 } 71 72 73 public Object getAttribute(String name) { 74 return this.portletAttributes.get(name); 75 } 76 77 public Object getAttribute(String 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 getAttributeNames() { 88 return this.portletAttributes.keys(); 89 } 90 91 public Enumeration 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 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 name) { 141 this.portletAttributes.remove(name); 142 } 143 144 public void removeAttribute(String 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 name, Object 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 name, Object 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 |