1 43 package net.jforum; 44 45 import java.sql.Connection ; 46 import java.util.ArrayList ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 import net.jforum.cache.CacheEngine; 52 import net.jforum.cache.Cacheable; 53 import net.jforum.dao.DataAccessDriver; 54 import net.jforum.entities.UserSession; 55 import net.jforum.repository.SecurityRepository; 56 import net.jforum.util.preferences.ConfigKeys; 57 import net.jforum.util.preferences.SystemGlobals; 58 59 import org.apache.log4j.Logger; 60 61 65 public class SessionFacade implements Cacheable 66 { 67 private static final Logger logger = Logger.getLogger(SessionFacade.class); 68 69 private static final String FQN = "sessions"; 70 private static final String FQN_LOGGED = FQN + "/logged"; 71 private static final String FQN_COUNT = FQN + "/count"; 72 private static final String FQN_USER_ID = FQN + "/userId"; 73 private static final String ANONYMOUS_COUNT = "anonymousCount"; 74 private static final String LOGGED_COUNT = "loggedCount"; 75 76 private static CacheEngine cache; 77 78 81 public void setCacheEngine(CacheEngine engine) 82 { 83 cache = engine; 84 } 85 86 94 public static void add(UserSession us) 95 { 96 add(us, JForumExecutionContext.getRequest().getSession().getId()); 97 } 98 99 116 public static void add(UserSession us, String sessionId) 117 { 118 if (us.getSessionId() == null || us.getSessionId().equals("")) { 119 us.setSessionId(sessionId); 120 } 121 122 synchronized (FQN) { 123 cache.add(FQN, us.getSessionId(), us); 124 125 if (!us.isBot()) { 126 if (us.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 127 changeUserCount(LOGGED_COUNT, true); 128 cache.add(FQN_LOGGED, us.getSessionId(), us); 129 cache.add(FQN_USER_ID, Integer.toString(us.getUserId()), us.getSessionId()); 130 } 131 else { 132 changeUserCount(ANONYMOUS_COUNT, true); 134 } 135 } 136 } 137 } 138 139 private static void changeUserCount(String cacheEntryName, boolean increment) 140 { 141 Integer count = (Integer )cache.get(FQN_COUNT, cacheEntryName); 142 143 if (count == null) { 144 count = new Integer (0); 145 } 146 147 if (increment) { 148 count = new Integer (count.intValue() + 1); 149 } 150 else if (count.intValue() > 0) { 151 count = new Integer (count.intValue() - 1); 152 } 153 154 cache.add(FQN_COUNT, cacheEntryName, count); 155 } 156 157 163 public static void setAttribute(String name, Object value) 164 { 165 JForumExecutionContext.getRequest().getSession().setAttribute(name, value); 166 } 167 168 173 public static void removeAttribute(String name) 174 { 175 JForumExecutionContext.getRequest().getSession().removeAttribute(name); 176 } 177 178 184 public static Object getAttribute(String name) 185 { 186 return JForumExecutionContext.getRequest().getSession().getAttribute(name); 187 } 188 189 194 public static void remove(String sessionId) 195 { 196 if (cache == null) { 197 logger.warn("Got a null cache instance. #" + sessionId); 198 return; 199 } 200 201 logger.info("Removing session " + sessionId); 202 203 synchronized (FQN) { 204 UserSession us = getUserSession(sessionId); 205 206 if (us != null) { 207 cache.remove(FQN_LOGGED, sessionId); 208 cache.remove(FQN_USER_ID, Integer.toString(us.getUserId())); 209 210 if (us.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 211 changeUserCount(LOGGED_COUNT, false); 212 } 213 else { 214 changeUserCount(ANONYMOUS_COUNT, false); 215 } 216 } 217 218 cache.remove(FQN, sessionId); 219 } 220 } 221 222 228 public static List getAllSessions() 229 { 230 synchronized (FQN) { 231 return new ArrayList (cache.getValues(FQN)); 232 } 233 } 234 235 239 public static List getLoggedSessions() 240 { 241 synchronized (FQN) { 242 return new ArrayList (cache.getValues(FQN_LOGGED)); 243 } 244 } 245 246 250 public static int registeredSize() 251 { 252 Integer count = (Integer )cache.get(FQN_COUNT, LOGGED_COUNT); 253 254 return (count == null ? 0 : count.intValue()); 255 } 256 257 261 public static int anonymousSize() 262 { 263 Integer count = (Integer )cache.get(FQN_COUNT, ANONYMOUS_COUNT); 264 265 return (count == null ? 0 : count.intValue()); 266 } 267 268 public static void clear() 269 { 270 synchronized (FQN) { 271 cache.add(FQN, new HashMap ()); 272 cache.add(FQN_COUNT, LOGGED_COUNT, new Integer (0)); 273 cache.add(FQN_COUNT, ANONYMOUS_COUNT, new Integer (0)); 274 cache.remove(FQN_LOGGED); 275 cache.remove(FQN_USER_ID); 276 } 277 } 278 279 284 public static UserSession getUserSession() 285 { 286 return getUserSession(JForumExecutionContext.getRequest().getSession().getId()); 287 } 288 289 295 public static UserSession getUserSession(String sessionId) 296 { 297 if (cache != null) { 298 UserSession us = (UserSession)cache.get(FQN, sessionId); 299 return (us != null ? us : null); 300 } 301 302 logger.warn("Got a null cache in getUserSession. #" + sessionId); 303 return null; 304 } 305 306 311 public static int size() 312 { 313 return (anonymousSize() + registeredSize()); 314 } 315 316 323 public static String isUserInSession(String username) 324 { 325 int aid = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID); 326 327 synchronized (FQN) { 328 for (Iterator iter = cache.getValues(FQN).iterator(); iter.hasNext(); ) { 329 UserSession us = (UserSession)iter.next(); 330 String thisUsername = us.getUsername(); 331 332 if (thisUsername == null) { 333 continue; 334 } 335 336 if (us.getUserId() != aid && thisUsername.equals(username)) { 337 return us.getSessionId(); 338 } 339 } 340 } 341 342 return null; 343 } 344 345 353 public static String isUserInSession(int userId) 354 { 355 return (String )cache.get(FQN_USER_ID, Integer.toString(userId)); 356 } 357 358 364 public static boolean isLogged() 365 { 366 return "1".equals(SessionFacade.getAttribute("logged")); 367 } 368 369 379 public static void storeSessionData(String sessionId) throws Exception 380 { 381 Connection conn = null; 382 try { 383 conn = DBConnection.getImplementation().getConnection(); 384 SessionFacade.storeSessionData(sessionId, conn); 385 } 386 finally { 387 if (conn != null) { 388 try { 389 DBConnection.getImplementation().releaseConnection(conn); 390 } 391 catch (Exception e) { 392 logger.warn("Error while releasing a connection: " + e); 393 } 394 } 395 } 396 } 397 398 407 public static void storeSessionData(String sessionId, Connection conn) throws Exception 408 { 409 UserSession us = SessionFacade.getUserSession(sessionId); 410 if (us != null) { 411 try { 412 if (us.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 413 DataAccessDriver.getInstance().newUserSessionDAO().update(us, conn); 414 } 415 416 SecurityRepository.remove(us.getUserId()); 417 } 418 catch (Exception e) { 419 logger.warn("Error storing user session data: " + e, e); 420 } 421 } 422 } 423 } 424 | Popular Tags |