定义上下文行为,要继承Map
package com.wjz.core; import java.io.Serializable; import java.util.Map; public interface CustomSubjectContext extends Map<String, Object> { 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<String, Object>, Serializable { private static final long serialVersionUID = 6611136447999868186L; private final Map<String, Object> backingMap; public CustomMapContext() { backingMap = new HashMap<String, Object>(); } public CustomMapContext(Map<String, Object> 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<? extends String, ?> m) { backingMap.putAll(m); } @Override public void clear() { backingMap.clear(); } @Override public Set<String> keySet() { return Collections.unmodifiableSet(backingMap.keySet()); } @Override public Collection<Object> values() { return Collections.unmodifiableCollection(backingMap.values()); } @Override public Set<Entry<String, Object>> entrySet() { return Collections.unmodifiableSet(backingMap.entrySet()); } protected void nullSafePut(String key, Object value) { if (value != null) { put(key, value); } } @SuppressWarnings("unchecked") protected <E> E getTypeValue(String key, Class<E> type) { E val = null; Object result = backingMap.get(key); if (result != null) { if (!type.isAssignableFrom(result.getClass())) { // 类型不正确 } val = (E) result; } return val; } }
定义默认的上下文实现类,继承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()); } }