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
00035 public class SessionContextManager {
00036
00037
00038 private static SessionContextManager manager;
00039 private HashMap sessionListbyId;
00040 private HashMap sessionListbythread;
00041 private static long oldID = 0;
00042
00043
00044 public SessionContextManager() {
00045 super();
00046 sessionListbyId = new HashMap(10);
00047 sessionListbythread = new HashMap(10);
00048
00049 }
00050
00051 public final static void registerManager(SessionContextManager newManager) {
00052 manager = newManager;
00053 }
00054
00055 public final static SessionContextManager getManager() {
00056 if (manager == null) {
00057 manager = new SessionContextManager();
00058 }
00059 return manager;
00060 }
00061
00065 public synchronized SessionContext getSessionContext(String id) {
00066 if (id != null) {
00067 SessionContext session = (SessionContext) sessionListbyId.get(id);
00068 if (session == null) {
00069 session = this.createSessionContext(id);
00070 } else {
00071 session.SetIsNew(false);
00072 }
00073 return session;
00074 }
00075 return null;
00076 }
00077
00083 public synchronized SessionContext getSessionContext() {
00084 SessionContext session = (SessionContext) sessionListbythread.get(Thread.currentThread());
00085 if (session != null) {
00086 session.SetIsNew(false);
00087 } else {
00088 String id= "OPENMISCONTEXT"+Long.toString(getNewID());
00089 session = this.createSessionContext(id);
00090 sessionListbythread.put(Thread.currentThread(), session);
00091 }
00092 return session;
00093 }
00094
00098 public synchronized SessionContext createSessionContext(String id) {
00099 SessionContext session = null;
00100 if ((id != null) && (!sessionListbyId.containsKey(id))) {
00101 session = this.realCreateSessionContext(id);
00102 session.SetIsNew(true);
00103 sessionListbyId.put (id, session);
00104 } else if (id != null) {
00105 session = this.getSessionContext(id);
00106 }
00107 return session;
00108 }
00109
00110 protected SessionContext realCreateSessionContext(String id) {
00111 return new SessionContext(id);
00112 }
00113
00114
00119 public synchronized void joinSessionContext(String id) {
00120 SessionContext session = (SessionContext) sessionListbyId.get(id);
00121 if (session == null) {
00122 session = createSessionContext(id);
00123 }
00124 sessionListbythread.put(Thread.currentThread(), session);
00125 }
00126
00127
00128 public synchronized void invalidateSession(String id) {
00129 SessionContext session = (SessionContext) sessionListbyId.get(id);
00130 if (session != null) {
00131 if (sessionListbythread.containsValue(session)) {
00132 Iterator iter = sessionListbythread.keySet().iterator();
00133 while (iter.hasNext()) {
00134 Object obj = iter.next();
00135 SessionContext se = (SessionContext) sessionListbythread.get(obj);
00136 if (se.getId().equals(id)) {
00137 sessionListbythread.remove(obj);
00138 }
00139 }
00140 }
00141 sessionListbyId.remove(id);
00142 }
00143 }
00144
00145
00149 public synchronized void leaveSessionContext() {
00150 sessionListbythread.remove(Thread.currentThread());
00151 }
00152
00153
00154 static synchronized long getNewID() {
00155 long id = System.currentTimeMillis() - (943293708615L);
00156 while (id < oldID) {
00157 id ++;
00158 }
00159 oldID = ++id;
00160 return oldID;
00161 }
00162
00163 }