00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.common.context;
00030
00031 import java.util.HashMap;
00032 import java.util.Iterator;
00033
00034 import org.openmobileis.common.util.log.LogManager;
00035
00036
00037 public class SessionContextManager implements SessionContextService {
00038
00039
00040 private static SessionContextManager manager;
00041 private static HashMap sessionListbyId;
00042 private static HashMap sessionListbythread;
00043 private static long oldID = 0;
00044
00045
00046 public SessionContextManager(boolean init) {
00047 super();
00048 if (init){
00049 sessionListbyId = new HashMap(10);
00050 sessionListbythread = new HashMap(10);
00051 }
00052
00053 }
00054
00055 public final static void registerManager(SessionContextManager newManager) {
00056 if (manager == null) {
00057 manager = newManager;
00058 ApplicationContextManager.getManager().addManager(manager);
00059 } else {
00060 LogManager.traceDebug(0, "SessionContextManager registerManager already done no use");
00061 }
00062 }
00063
00064 public final static SessionContextManager getManager() {
00065 if (manager == null) {
00066 manager = new SessionContextManager(true);
00067 }
00068 return manager;
00069 }
00070
00074 public synchronized SessionContext getSessionContext(String id) {
00075 if (id != null) {
00076 SessionContext session = (SessionContext) sessionListbyId.get(id);
00077 if (session == null) {
00078 session = this.createSessionContext(id);
00079 } else {
00080 session.SetIsNew(false);
00081 }
00082 return session;
00083 }
00084 return null;
00085 }
00086
00092 public synchronized SessionContext getSessionContext() {
00093 SessionContext session = (SessionContext) sessionListbythread.get(Thread.currentThread());
00094 if (session != null) {
00095 session.SetIsNew(false);
00096 } else {
00097 String id= "OPENMISCONTEXT"+Long.toString(getNewID());
00098 session = this.createSessionContext(id);
00099 sessionListbythread.put(Thread.currentThread(), session);
00100 }
00101 return session;
00102 }
00103
00107 public synchronized SessionContext createSessionContext(String id) {
00108 SessionContext session = null;
00109 if ((id != null) && (!sessionListbyId.containsKey(id))) {
00110 session = this.realCreateSessionContext(id);
00111 session.SetIsNew(true);
00112 sessionListbyId.put (id, session);
00113 } else if (id != null) {
00114 session = this.getSessionContext(id);
00115 }
00116 return session;
00117 }
00118
00119 protected SessionContext realCreateSessionContext(String id) {
00120 return new SessionContext(id);
00121 }
00122
00123
00128 public synchronized void joinSessionContext(String id) {
00129 SessionContext session = (SessionContext) sessionListbyId.get(id);
00130 if (session == null) {
00131 session = createSessionContext(id);
00132 }
00133 sessionListbythread.put(Thread.currentThread(), session);
00134 }
00135
00136
00137 public synchronized void invalidateSession(String id) {
00138 SessionContext session = (SessionContext) sessionListbyId.get(id);
00139 if (session != null) {
00140 if (sessionListbythread.containsValue(session)) {
00141 Iterator iter = sessionListbythread.keySet().iterator();
00142 while (iter.hasNext()) {
00143 Object obj = iter.next();
00144 SessionContext se = (SessionContext) sessionListbythread.get(obj);
00145 if (se.getId().equals(id)) {
00146 sessionListbythread.remove(obj);
00147 }
00148 }
00149 }
00150 sessionListbyId.remove(id);
00151 }
00152 }
00153
00154
00158 public synchronized void leaveSessionContext() {
00159 sessionListbythread.remove(Thread.currentThread());
00160 }
00161
00162
00163 static synchronized long getNewID() {
00164 long id = System.currentTimeMillis() - (943293708615L);
00165 while (id < oldID) {
00166 id ++;
00167 }
00168 oldID = ++id;
00169 return oldID;
00170 }
00171
00172 }