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 }
00105 return session;
00106 }
00107
00108 protected SessionContext realCreateSessionContext(String id) {
00109 return new SessionContext(id);
00110 }
00111
00112
00117 public synchronized void joinSessionContext(String id) {
00118 SessionContext session = (SessionContext) sessionListbyId.get(id);
00119 if (session == null) {
00120 session = createSessionContext(id);
00121 }
00122 sessionListbythread.put(Thread.currentThread(), session);
00123 }
00124
00125
00126 public synchronized void invalidateSession(String id) {
00127 SessionContext session = (SessionContext) sessionListbyId.get(id);
00128 if (session != null) {
00129 if (sessionListbythread.containsValue(session)) {
00130 Iterator iter = sessionListbythread.keySet().iterator();
00131 while (iter.hasNext()) {
00132 Object obj = iter.next();
00133 SessionContext se = (SessionContext) sessionListbythread.get(obj);
00134 if (se.getId().equals(id)) {
00135 sessionListbythread.remove(obj);
00136 }
00137 }
00138 }
00139 sessionListbyId.remove(id);
00140 }
00141 }
00142
00143
00147 public synchronized void leaveSessionContext() {
00148 sessionListbythread.remove(Thread.currentThread());
00149 }
00150
00151
00152 static synchronized long getNewID() {
00153 long id = System.currentTimeMillis() - (943293708615L);
00154 while (id < oldID) {
00155 id ++;
00156 }
00157 oldID = ++id;
00158 return oldID;
00159 }
00160
00161 }