Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

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

Generated on Mon Jul 10 10:29:32 2006 for OpenMobileIS by  doxygen 1.4.4