1 23 24 package com.rift.coad.lib.security; 26 27 import java.util.Set ; 29 import java.util.Date ; 30 import java.util.HashSet ; 31 32 import com.rift.coad.lib.common.RandomGuid; 34 35 40 public class UserSession implements PrincipalContainer, Cloneable { 41 42 private String name = null; 44 private String sessionId = null; 45 private Set principals = null; 46 private Date touchTime = new Date (); 47 private long expiryTime = 0; 48 private boolean valid = true; 49 50 55 public UserSession() throws SecurityException { 56 try { 57 name = "nobody"; 58 sessionId = RandomGuid.getInstance().getGuid(); 59 this.principals = new HashSet (); 60 } catch (Exception ex) { 61 throw new SecurityException ( 62 "Failed to initialize the users session : " + 63 ex.getMessage(),ex); 64 } 65 } 66 67 68 71 public UserSession(String name, Set principals) throws SecurityException { 72 try { 73 this.name = name; 74 sessionId = RandomGuid.getInstance().getGuid(); 75 this.principals = principals; 76 } catch (Exception ex) { 77 throw new SecurityException ( 78 "Failed to initialize the users session : " + 79 ex.getMessage(),ex); 80 } 81 } 82 83 84 91 public UserSession(String name, String sessionId, Set principals) { 92 this.name = name; 93 this.sessionId = sessionId; 94 this.principals = principals; 95 } 96 97 98 103 public String getName() { 104 return name; 105 } 106 107 108 113 public String getSessionId() { 114 return sessionId; 115 } 116 117 118 123 public void setSessionId(String sessionId) { 124 this.sessionId = sessionId; 125 } 126 127 128 133 public Set getPrincipals() { 134 return principals; 135 } 136 137 138 143 public void setPrincipals(Set principals) { 144 this.principals = principals; 145 } 146 147 148 153 public synchronized Date getTouchTime() { 154 return touchTime; 155 } 156 157 158 163 public synchronized void touch() throws SecurityException { 164 if (isExpired() || valid == false) { 165 throw new SecurityException ( 166 "The object has expired or been invalidated."); 167 } 168 touchTime = new Date (); 169 } 170 171 172 177 public synchronized long getExpiryTime() { 178 return expiryTime; 179 } 180 181 182 187 public synchronized void setExpiryTime(long expiryTime) { 188 this.expiryTime = expiryTime; 189 } 190 191 192 197 public synchronized boolean isExpired() { 198 if (!valid) { 199 return true; 200 } 201 else if (expiryTime == 0) { 202 return false; 203 } 204 else if (new Date ().getTime() > (touchTime.getTime() + expiryTime)) { 205 return true; 206 } 207 return false; 208 } 209 210 211 214 public synchronized void invalidate() { 215 valid = false; 216 } 217 218 219 224 public Object clone() throws CloneNotSupportedException { 225 return super.clone(); 226 } 227 } 228 | Popular Tags |