001: package com.completex.objective.components.persistency.mapper;
002:
003: import com.completex.objective.components.persistency.mapper.Mapper;
004:
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: /**
010: * Class that holds references to Persistent OBjects and the corresponding POJO beans called
011: * for modification in current thread.
012: *
013: * @author Gennady Krizhevsky
014: */
015: public class ThreadSession {
016:
017: private static ThreadLocal sessionContext = new ThreadLocal() {
018: protected synchronized Object initialValue() {
019: return new MapperContextsMap();
020: }
021: };
022:
023: public static void set(Mapper mapper, Object bean, Object po) {
024: if (bean != null && po != null) {
025: MapperContextsMap map = getContextMap();
026: map.put(mapper, bean, po);
027: }
028: }
029:
030: public static Object getPo(Mapper mapper, Object bean) {
031: Object po = null;
032: if (bean != null) {
033: MapperContextsMap map = getContextMap();
034: po = map.getByBean(mapper, bean);
035: }
036: return po;
037: }
038:
039: public static Object getBean(Mapper mapper, Object po) {
040: Object bean = null;
041: if (po != null) {
042: MapperContextsMap map = getContextMap();
043: bean = map.getByPo(mapper, po);
044: }
045: return bean;
046: }
047:
048: public static void unsetByBean(Mapper mapper, Object bean) {
049: if (bean != null) {
050: MapperContextsMap map = getContextMap();
051: map.removeByBean(mapper, bean);
052: }
053: }
054:
055: public static void unsetByPo(Mapper mapper, Object po) {
056: if (po != null) {
057: MapperContextsMap map = getContextMap();
058: map.removeByPo(mapper, po);
059: }
060: }
061:
062: public static String dump() {
063: return getContextMap().toString();
064: }
065:
066: public static boolean containsPo(Mapper mapper, Object po) {
067: return getBean(mapper, po) != null;
068: }
069:
070: public static boolean containsBean(Mapper mapper, Object bean) {
071: return getPo(mapper, bean) != null;
072: }
073:
074: public static void clear(Mapper mapper) {
075: getContextMap().clear(mapper);
076: }
077:
078: public static void clear() {
079: getContextMap().clear();
080: }
081:
082: public static void clearAll() {
083: getContextMap().clearAll();
084: }
085:
086: public static MapperContextsMap getContextMap() {
087: return (MapperContextsMap) (sessionContext.get());
088: }
089:
090: /**
091: * Map of ContextMaps: Map<mapper.class, ContextMaps>
092: */
093: static class MapperContextsMap {
094: private HashMap mapperContexts = new HashMap();
095:
096: public void put(Mapper mapper, Object bean, Object po) {
097: ContextMap contextMap = lazyContextMap(mapper);
098: contextMap.put(bean, po);
099: }
100:
101: public void removeByBean(Mapper mapper, Object bean) {
102: ContextMap contextMap = lazyContextMap(mapper);
103: contextMap.removeByBean(bean);
104: }
105:
106: public void removeByPo(Mapper mapper, Object po) {
107: ContextMap contextMap = lazyContextMap(mapper);
108: contextMap.removeByPo(po);
109: }
110:
111: public Object getByBean(Mapper mapper, Object bean) {
112: ContextMap contextMap = lazyContextMap(mapper);
113: return contextMap.getByBean(bean);
114: }
115:
116: public Object getByPo(Mapper mapper, Object po) {
117: ContextMap contextMap = lazyContextMap(mapper);
118: return contextMap.getByPo(po);
119: }
120:
121: public void clear(Mapper mapper) {
122: Class aClass = mapper.getClass();
123: clear(aClass);
124: }
125:
126: protected void clear(Class aClass) {
127: ContextMap contextMap = (ContextMap) mapperContexts
128: .get(aClass);
129: if (contextMap != null) {
130: contextMap.clear();
131: }
132: }
133:
134: public void clear() {
135: for (Iterator it = mapperContexts.keySet().iterator(); it
136: .hasNext();) {
137: Class key = (Class) it.next();
138: clear(key);
139: }
140: }
141:
142: public void clearAll() {
143: clear();
144: mapperContexts.clear();
145: }
146:
147: private ContextMap lazyContextMap(Mapper mapper) {
148: ContextMap contextMap = (ContextMap) mapperContexts
149: .get(mapper.getClass());
150: if (contextMap == null) {
151: contextMap = new ContextMap();
152: mapperContexts.put(mapper.getClass(), contextMap);
153: }
154: return contextMap;
155: }
156:
157: public HashMap getMapperContexts() {
158: return mapperContexts;
159: }
160:
161: public String toString() {
162: return super .toString() + mapperContexts.toString();
163: }
164: }
165:
166: /**
167: * The ContextMap implementation guaranties for mamory to be limited in size as long
168: * as outside hard references (to the objects stored in it) number is limited.
169: */
170: public static class ContextMap {
171: private Map beanPoMap = new HashMap();
172: private Map poBeanMap = new HashMap();
173:
174: public Map getBeanPoMap() {
175: return beanPoMap;
176: }
177:
178: public Map getPoBeanMap() {
179: return poBeanMap;
180: }
181:
182: public void put(Object bean, Object po) {
183: beanPoMap.put(bean, po);
184: poBeanMap.put(po, bean);
185: }
186:
187: public void removeByBean(Object bean) {
188: Object po = beanPoMap.remove(bean);
189: poBeanMap.remove(po);
190: }
191:
192: public void removeByPo(Object po) {
193: Object bean = beanPoMap.remove(po);
194: poBeanMap.remove(bean);
195: }
196:
197: public Object getByBean(Object bean) {
198: return beanPoMap.get(bean);
199: }
200:
201: public Object getByPo(Object po) {
202: return poBeanMap.get(po);
203: }
204:
205: public void clear() {
206: beanPoMap.clear();
207: poBeanMap.clear();
208: }
209:
210: public int beanPoMapSize() {
211: return beanPoMap.size();
212: }
213:
214: public int poBeanMapSize() {
215: return poBeanMap.size();
216: }
217:
218: public String toString() {
219: return super.toString() + beanPoMap.toString();
220: }
221:
222: }
223: }
|