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