1 18 19 package sync4j.server.syncbean.session; 20 21 import java.io.File ; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.logging.Logger ; 26 import java.util.logging.Level ; 27 28 import sync4j.framework.tools.beans.BeanFactory; 29 import sync4j.framework.tools.beans.BeanException; 30 import sync4j.framework.logging.Sync4jLogger; 31 import sync4j.framework.server.session.AbstractSessionManager; 32 import sync4j.framework.server.session.SessionHandler; 33 import sync4j.framework.server.session.SessionExpiredException; 34 import sync4j.server.session.SyncSessionHandler; 35 36 52 public class SessionManager extends AbstractSessionManager implements java.io.Serializable { 53 54 56 59 private static final Logger log = Sync4jLogger.getLogger(); 60 61 63 private String sessionsPath = null; 64 65 68 public String getSessionsPath() { 69 return sessionsPath; 70 } 71 72 75 public void setSessionsPath(String sessionsPath) { 76 this.sessionsPath = sessionsPath; 77 } 78 79 81 82 protected Collection getSessionBag() { 84 ArrayList sessions = new ArrayList (); 85 86 File sessionsPathFile = new File (sessionsPath); 91 92 if (!sessionsPathFile.exists()) { 93 return sessions; } 95 96 File [] files = sessionsPathFile.listFiles(); 97 98 SessionHandler handler = null; 99 for (int i=0; (files != null) && (i<files.length); ++i) { 100 try { 101 handler = (SessionHandler)BeanFactory.getBeanInstance(files[i]); 102 sessions.add(handler); 103 } catch (BeanException e) { 104 log.throwing(getClass().getName(), "getSessionBag", e.getCause()); 105 } 106 } 107 108 return sessions; 109 } 110 111 protected SessionHandler getSessionFromBag(String sessionId) throws SessionExpiredException { 112 File sessionFile = new File (sessionsPath, sessionId + ".session"); 113 114 if (!sessionFile.exists()) { 115 return null; 116 } 117 118 SessionHandler handler = null; 119 try { 120 handler = (SessionHandler)BeanFactory.getBeanInstance(sessionFile); 121 } catch (BeanException e) { 122 log.throwing(getClass().getName(), "getSessionFromBag", e.getCause()); 123 } 124 125 return handler; 126 } 127 128 protected void putSessionInBag(String sessionId, SessionHandler handler) { 129 File sessionFile = new File (sessionsPath, sessionId + ".session"); 130 131 try { 132 BeanFactory.saveBeanInstance(handler, sessionFile); 133 } catch (BeanException e) { 134 log.severe("Error in serializing " + sessionFile); 135 log.throwing(getClass().getName(), "putSessionInBag", e.getCause()); 136 } 137 } 138 139 protected void removeSessionFromBag(String sessionId) { 140 File sessionFile = new File (sessionsPath, sessionId + ".session"); 141 142 if (sessionFile.exists()) { 143 sessionFile.delete(); 144 } 145 } 146 147 protected SessionHandler createNewSession(String sessionId) { 148 if (log.isLoggable(Level.FINEST)) { 149 log.finest("Creating a new session with id " + sessionId); 150 } 151 152 SyncSessionHandler sessionHandler = new SyncSessionHandler(); 153 154 putSessionInBag(sessionId, sessionHandler); 155 156 return sessionHandler; 157 } 158 } 159 160 | Popular Tags |