1 4 package nl.justobjects.pushlet.core; 5 6 import nl.justobjects.pushlet.util.Log; 7 import nl.justobjects.pushlet.util.Rand; 8 import nl.justobjects.pushlet.util.Sys; 9 10 import java.util.*; 11 12 18 public class SessionManager implements ConfigDefs { 19 20 21 private static SessionManager instance; 22 23 static { 24 String className = Config.getProperty(SESSION_MANAGER_CLASS); 27 28 try { 29 instance = (SessionManager) Class.forName(className).newInstance(); 30 Log.info("SessionManager created className=" + className); 31 } catch (Throwable t) { 32 Log.fatal("Cannot instantiate SessionManager className=" + className, t); 33 } 34 } 35 36 37 private Timer timer; 38 private final long TIMER_INTERVAL_MILLIS = 60000; 39 40 41 private Map sessions = Collections.synchronizedMap(new HashMap(13)); 42 43 44 private Session[] sessionCache = new Session[0]; 45 46 47 private volatile boolean sessionCacheDirty = false; 48 49 50 private SessionManager() { 51 } 52 53 54 public Session createSession(Event anEvent) { 55 return new Session(createSessionId()); 57 } 58 59 60 public String createSessionId() { 61 String id = null; 64 while (true) { 65 id = Rand.randomName(Config.getIntProperty(SESSION_ID_SIZE)); 66 if (!hasSession(id)) { 67 break; 69 } 70 } 71 return id; 72 } 73 74 75 public static SessionManager getInstance() { 76 return instance; 77 } 78 79 80 public Session getSession(String anId) { 81 return (Session) sessions.get(anId); 82 } 83 84 85 public Session[] getSessions() { 86 return (Session[]) sessions.values().toArray(new Session[0]); 87 } 88 89 90 public int getSessionCount() { 91 return sessions.size(); 92 } 93 94 95 public String getStatus() { 96 Session[] sessions = getSessions(); 97 String statusInfo = "SessionMgr: " + sessions.length + " sessions \\n"; 98 for (int i = 0; i < sessions.length; i++) { 99 statusInfo = statusInfo + sessions[i] + "\\n"; 100 } 101 return statusInfo; 102 } 103 104 105 public boolean hasSession(String anId) { 106 return sessions.containsKey(anId); 107 } 108 109 110 public void addSession(Session session) { 111 sessions.put(session.getId(), session); 113 sessionCacheDirty = true; 114 info(session.getId() + " at " + session.getAddress() + " added "); 115 } 116 117 118 public Session removeSession(Session aSession) { 119 Session session = (Session) sessions.remove(aSession.getId()); 120 if (session != null) { 121 sessionCacheDirty = true; 122 info(session.getId() + " at " + session.getAddress() + " removed "); 123 } 124 return session; 125 } 126 127 public Session[] getSnapshot() { 128 if (!sessionCacheDirty) { 130 return sessionCache; 131 } 132 133 synchronized (sessionCache) { 135 sessionCache = (Session[]) sessions.values().toArray(sessionCache); 139 140 sessionCacheDirty = false; 142 143 return sessionCache; 144 } 145 } 146 147 148 149 public void start() { 150 if (timer != null) { 151 stop(); 152 } 153 timer = new Timer(false); 154 timer.schedule(new AgingTimerTask(), TIMER_INTERVAL_MILLIS, TIMER_INTERVAL_MILLIS); 155 info("started; interval=" + TIMER_INTERVAL_MILLIS + "ms"); 156 } 157 158 159 public void stop() { 160 if (timer != null) { 161 timer.cancel(); 162 timer = null; 163 } 164 sessions.clear(); 165 sessionCache = new Session[0]; 166 info("stopped"); 167 } 168 169 170 private void info(String s) { 171 Log.info("SessionManager: " + new Date() + " " + s); 172 } 173 174 175 private void warn(String s) { 176 Log.warn("SessionManager: " + s); 177 } 178 179 180 private void debug(String s) { 181 Log.debug("SessionManager: " + s); 182 } 183 184 185 private class AgingTimerTask extends TimerTask { 186 private long lastRun = Sys.now(); 187 188 public void run() { 189 long now = Sys.now(); 190 long delta = now - lastRun; 191 lastRun = now; 192 194 Session[] sessions = getSnapshot(); 195 Session nextSession = null; 196 for (int i = 0; i < sessions.length; i++) { 197 nextSession = sessions[i]; 198 if (nextSession == null) { 200 break; 201 } 202 203 try { 204 nextSession.age(delta); 206 207 if (nextSession.isExpired()) { 209 info("Session expired: " + nextSession); 210 nextSession.stop(); 211 } 212 } catch (Throwable t) { 213 warn("Error in timer task : " + t); 214 } 215 } 216 } 217 } 218 } 219 220 240 | Popular Tags |