SessionContextManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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);  // to have int conversion positive
00165     while (id < oldID)  {
00166       id ++;
00167     }
00168     oldID = ++id;
00169         return oldID;
00170   }
00171 
00172 }

Generated on Tue May 22 23:01:11 2007 for OpenMobileIS by  doxygen 1.5.1-p1