定义上下文行为,要继承Map
package com.wjz.core;import java.io.Serializable;import java.util.Map;public interface CustomSubjectContext extends Map{ void setSessionId(Serializable sessionId); Serializable getSessionId(); void setAuthenticated(Boolean authc); Boolean isAuthenticated();}
定义MapContext上下文,定义备份Map,实现备份Map的存取等关乎map的行为,实现Map
package com.wjz.core;import java.io.Serializable;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.springframework.util.CollectionUtils;public class CustomMapContext implements Map, Serializable { private static final long serialVersionUID = 6611136447999868186L; private final Map backingMap; public CustomMapContext() { backingMap = new HashMap (); } public CustomMapContext(Map map) { this(); if (!CollectionUtils.isEmpty(map)) { this.backingMap.putAll(map); } } @Override public int size() { return backingMap.size(); } @Override public boolean isEmpty() { return backingMap.isEmpty(); } @Override public boolean containsKey(Object key) { return backingMap.containsKey(key); } @Override public boolean containsValue(Object value) { return backingMap.containsValue(value); } @Override public Object get(Object key) { return backingMap.get(key); } @Override public Object put(String key, Object value) { return backingMap.put(key, value); } @Override public Object remove(Object key) { return backingMap.remove(key); } @Override public void putAll(Map m) { backingMap.putAll(m); } @Override public void clear() { backingMap.clear(); } @Override public Set keySet() { return Collections.unmodifiableSet(backingMap.keySet()); } @Override public Collection
定义默认的上下文实现类,继承MapContext上下文,具体实现上下文
package com.wjz.core;import java.io.Serializable;public class DefaultCustomSubjectContext extends CustomMapContext implements CustomSubjectContext { private static final long serialVersionUID = -273450905542274017L; private static final String SESSION_ID = ".SESSION_ID"; private static final String AUTHC = ".AUTHC"; public DefaultCustomSubjectContext() { super(); } public DefaultCustomSubjectContext(CustomSubjectContext sctx) { super(sctx); } @Override public void setSessionId(Serializable sessionId) { nullSafePut(SESSION_ID, sessionId); } @Override public Serializable getSessionId() { return getTypeValue(SESSION_ID, Serializable.class); } @Override public void setAuthenticated(Boolean authc) { nullSafePut(AUTHC, authc); } @Override public Boolean isAuthenticated() { return getTypeValue(AUTHC, Boolean.class); }}
demo
package com.wjz.demo;import com.wjz.core.CustomSubjectContext;import com.wjz.core.DefaultCustomSubjectContext;public class CopyDemo { public static void main(String[] args) { CustomSubjectContext sctx = new DefaultCustomSubjectContext(); sctx.setSessionId("sctx 123456"); sctx.setAuthenticated(true); System.out.println("sctx getSessionId :" + sctx.getSessionId()); System.out.println("sctx isAuthenticated :" + sctx.isAuthenticated()); System.out.println(); CustomSubjectContext copy = new DefaultCustomSubjectContext(sctx); copy.setSessionId("copy 123456"); System.out.println("copy getSessionId :" + copy.getSessionId()); System.out.println("copy isAuthenticated :" + copy.isAuthenticated()); }}