All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.logicbus.backend.LocalSession Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.logicbus.backend;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;


/**
 * 本地实现的Session
 * 
 * @author duanyy
 * @version 1.6.3.10 [20150401 duanyy] 
* - 修正Session值不存在时的空指针问题。
* */ public class LocalSession extends Session { private HttpSession httpSession = null; public LocalSession(HttpSession _httpSession){ httpSession = _httpSession; } @Override public String hGet(String id, String field, String dftValue) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; String value = map.get(field); return value == null || value.length() <= 0 ? dftValue : value; }else{ return dftValue; } } @Override public void hSet(String id, String field, String value) { synchronized (httpSession){ Object found = httpSession.getAttribute(id); if (found == null && ! (found instanceof Set)){ Map map = new HashMap(); map.put(field, value); httpSession.setAttribute(id, map); }else{ @SuppressWarnings("unchecked") Map map = (Map) found; map.put(field, value); } } } @Override public boolean hExist(String id, String field) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; return map.containsKey(field); }else{ return false; } } @Override public Map hGetAll(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; return map; }else{ return null; } } @Override public int hLen(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; return map.size(); }else{ return 0; } } @Override public String[] hKeys(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; return map.keySet().toArray(new String[map.size()]); }else{ return null; } } @Override public String[] hValues(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Map){ @SuppressWarnings("unchecked") Map map = (Map) found; return map.values().toArray(new String[map.size()]); }else{ return null; } } @Override public void sAdd(String id, String... member) { synchronized (httpSession){ Object found = httpSession.getAttribute(id); if (found == null && ! (found instanceof Set)){ Set set = new HashSet(); for (String m:member){ set.add(m); } httpSession.setAttribute(id, set); }else{ @SuppressWarnings("unchecked") Set set = (Set)found; for (String m:member){ set.add(m); } } } } @Override public void sDel(String id, String... member) { synchronized (httpSession){ Object found = httpSession.getAttribute(id); if (found != null && found instanceof Set){ @SuppressWarnings("unchecked") Set set = (Set)found; for (String m:member){ set.remove(m); } } } } @Override public int sSize(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Set){ @SuppressWarnings("unchecked") Set set = (Set)found; return set.size(); }else{ return 0; } } @Override public String[] sMembers(String id) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Set){ @SuppressWarnings("unchecked") Set set = (Set)found; return set.toArray(new String[set.size()]); }else{ return null; } } @Override public boolean sExist(String id, String member) { Object found = httpSession.getAttribute(id); if (found != null && found instanceof Set){ @SuppressWarnings("unchecked") Set set = (Set)found; return set.contains(member); }else{ return false; } } @Override protected void _SetValue(String _name, String _value) { httpSession.setAttribute(_name, _value); } @Override protected String _GetValue(String _name) { Object value = httpSession.getAttribute(_name); return value == null ? null : value.toString(); } @Override public void Clear() { httpSession.invalidate(); } @Override public long getCreateTime() { return httpSession.getCreationTime(); } @Override public void invalidate() { httpSession.invalidate(); } @Override public String getId(){ return httpSession.getId(); } @Override public void del(String id) { httpSession.removeAttribute(id); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy