1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.core.security.User; 68 import org.apache.log4j.Logger; 69 70 import javax.servlet.http.HttpSessionBindingEvent ; 71 import javax.servlet.http.HttpSessionBindingListener ; 72 import java.io.Serializable ; 73 import java.util.ArrayList ; 74 import java.util.Hashtable ; 75 76 90 public class CurrentLogin 91 implements HttpSessionBindingListener , 92 Serializable { 93 94 97 private String sessionId = ""; 98 99 102 private String userName = User.UNKNOWN_USER; 103 104 107 private String ipAddress = ""; 108 109 112 private long loggedInAt = 0; 113 114 117 private int uid = 0; 118 119 122 private String dbName = "default"; 123 124 127 private ArrayList messages = null; 128 129 132 private Hashtable attribHash = null; 133 134 138 public static final String LOGIN_KEY = "CurrentLogin"; 139 140 143 private transient static Logger slog = null; 144 145 150 public static Logger getLogger() { 151 synchronized (CurrentLogin.class) { 152 if (slog == null) { 153 slog = Logger.getLogger(CurrentLogin.class); 154 } 155 return slog; 156 } 157 } 158 159 170 public CurrentLogin(final String newSessionId, final String newUserName, 171 final String newIPAddress, final String newDataContext, 172 final long newLoggedInAt, final int newUid) { 173 sessionId = newSessionId; 174 userName = newUserName; 175 ipAddress = newIPAddress; 176 loggedInAt = newLoggedInAt; 177 dbName = newDataContext; 178 uid = newUid; 179 } 180 181 189 private CurrentLogin(final String newUserName, 190 final String newIPAddress, 191 final String newDataContext, 192 final int newUid) { 193 userName = newUserName; 194 ipAddress = newIPAddress; 195 loggedInAt = System.currentTimeMillis(); 196 dbName = newDataContext; 197 uid = newUid; 198 } 199 200 203 public CurrentLogin() { 204 } 205 206 211 public synchronized void addMessage(final String newMessage) { 212 if (messages == null) { 213 messages = new ArrayList (); 214 } 215 216 messages.add(newMessage); 217 } 218 219 225 public synchronized ArrayList getAndClearMessages() { 226 if (messages != null) { 227 ArrayList msgToReturn = messages; 228 messages = null; 229 230 return msgToReturn; 231 } 232 233 return null; 234 } 235 236 241 public void setUserName(final String newUserName) { 242 userName = newUserName; 243 } 244 245 250 public void setDBName(final String newDBName) { 251 dbName = newDBName; 252 } 253 254 259 public String getSessionId() { 260 return sessionId; 261 } 262 263 268 public String getUserName() { 269 return userName; 270 } 271 272 277 public int getUid() { 278 return uid; 279 } 280 281 286 public String getDBName() { 287 return dbName; 288 } 289 290 public String getIPAddress() { 291 return ipAddress; 292 } 293 294 public long getLogInTime() { 295 return loggedInAt; 296 } 297 298 304 public void valueBound(final HttpSessionBindingEvent evt) { 305 sessionId = evt.getSession().getId(); 306 ConfigManager.addSession(this); 307 } 308 309 316 public void valueUnbound(final HttpSessionBindingEvent evt) { 317 ConfigManager.removeSession(sessionId); 318 sessionId = null; 319 } 320 321 331 public static CurrentLogin newInstance(final String newUserName, 332 final String newIPAddress, 333 final String dataContext, 334 final int newUid) { 335 336 String className = ConfigManager.getClassHandler(LOGIN_KEY); 338 CurrentLogin loginInstance = null; 339 if (className != null) { 340 try { 341 loginInstance = (CurrentLogin) Class.forName(className). 342 newInstance(); 343 } catch (Exception e) { 344 getLogger().error(e); 345 } 346 } 347 348 if (loginInstance == null) { 349 loginInstance = new CurrentLogin(newUserName, newIPAddress, 351 dataContext, newUid); 352 } else { 353 loginInstance.setUserName(newUserName); 354 loginInstance.setIpAddress(newIPAddress); 355 loginInstance.setDBName(dataContext); 356 loginInstance.setUid(newUid); 357 loginInstance.setLoggedInAt(System.currentTimeMillis()); 358 } 359 360 return loginInstance; 361 } 362 363 376 public static CurrentLogin newInstance(final String newSessionId, 377 final String newUserName, 378 final String newIPAddress, 379 final String dataContext, 380 final long newLoggedInAt, 381 final int newUid) { 382 CurrentLogin alogin = new CurrentLogin(newUserName, newIPAddress, 383 dataContext, newUid); 384 alogin.loggedInAt = newLoggedInAt; 385 alogin.sessionId = newSessionId; 386 return alogin; 387 } 388 389 394 public void setLoggedInAt(final long loggedInAt) { 395 this.loggedInAt = loggedInAt; 396 } 397 398 404 public Object getAttribute(final String key) { 405 Object result = null; 406 if (attribHash != null && key != null) { 407 result = attribHash.get(key); 408 } 409 return result; 410 } 411 412 418 public void setAttribute(final String key, final Object attribute) { 419 if (attribute == null) { 420 removeAttribute(key); 421 return; 422 } 423 424 if (attribHash == null) { 425 attribHash = new Hashtable (); 426 } 427 428 if (key != null) { 429 attribHash.put(key, attribute); 430 } 431 } 432 433 438 public void removeAttribute(final String key) { 439 if (attribHash != null && key != null) { 440 attribHash.remove(key); 441 } 442 } 443 444 449 public void setIpAddress(final String theIpAddress) { 450 this.ipAddress = theIpAddress; 451 } 452 453 458 public void setUid(final int theUid) { 459 this.uid = theUid; 460 } 461 462 467 public void setSessionId(final String theSessionId) { 468 this.sessionId = theSessionId; 469 } 470 } 471 472 473 | Popular Tags |