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 
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);  // to have int conversion positive
00156     while (id < oldID)  {
00157       id ++;
00158     }
00159     oldID = ++id;
00160         return oldID;
00161   }
00162 
00163 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1