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-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.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);  // to have int conversion positive
00157     while (id < oldID)  {
00158       id ++;
00159     }
00160     oldID = ++id;
00161         return oldID;
00162   }
00163 
00164 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4