001: package org.apache.ojb.broker.core;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.WeakHashMap;
023:
024: import org.apache.ojb.broker.PBFactoryException;
025: import org.apache.ojb.broker.PBKey;
026: import org.apache.ojb.broker.PersistenceBrokerException;
027: import org.apache.ojb.broker.PersistenceBrokerInternal;
028:
029: /**
030: * Helper class that tracks correspondence between PersistenceBroker instances
031: * and threads. The main task that this class solves is: to get current
032: * PersistenceBroker for the given thread. For internal use only.
033: *
034: * @author Oleg Nitz
035: * @version $Id: PersistenceBrokerThreadMapping.java,v 1.6.2.3 2005/12/21 22:25:01 tomdz Exp $
036: */
037: public class PersistenceBrokerThreadMapping {
038: /**
039: * A Collection of all HashMaps added to the <CODE>ThreadLocal currentBrokerMap</CODE> that are still -alive-.
040: * If all the PersistenceBrokers are always correctly closed, the Collection should remain empty.
041: * The Collection is iterated trough when calling the <CODE>shutdown()</CODE> method and all the maps that are
042: * still alive will be cleared.
043: */
044: private static Collection loadedHMs = new HashSet();
045:
046: /**
047: * The hashmap that maps PBKeys to current brokers for the thread
048: */
049: private static ThreadLocal currentBrokerMap = new ThreadLocal();
050:
051: /**
052: * Mark a PersistenceBroker as preferred choice for current Thread
053: *
054: * @param key The PBKey the broker is associated to
055: * @param broker The PersistenceBroker to mark as current
056: */
057: public static void setCurrentPersistenceBroker(PBKey key,
058: PersistenceBrokerInternal broker) throws PBFactoryException {
059: HashMap map = (HashMap) currentBrokerMap.get();
060: WeakHashMap set = null;
061: if (map == null) {
062: map = new HashMap();
063: currentBrokerMap.set(map);
064:
065: loadedHMs.add(map);
066: } else {
067: set = (WeakHashMap) map.get(key);
068: }
069:
070: if (set == null) {
071: // We emulate weak HashSet using WeakHashMap
072: set = new WeakHashMap();
073: map.put(key, set);
074: }
075: set.put(broker, null);
076: }
077:
078: /**
079: * Unmark a PersistenceBroker as preferred choice for current Thread
080: *
081: * @param key The PBKey the broker is associated to
082: * @param broker The PersistenceBroker to unmark
083: */
084: public static void unsetCurrentPersistenceBroker(PBKey key,
085: PersistenceBrokerInternal broker) throws PBFactoryException {
086: HashMap map = (HashMap) currentBrokerMap.get();
087: WeakHashMap set = null;
088: if (map != null) {
089: set = (WeakHashMap) map.get(key);
090: if (set != null) {
091: set.remove(broker);
092: if (set.isEmpty()) {
093: map.remove(key);
094: }
095: }
096: if (map.isEmpty()) {
097: currentBrokerMap.set(null);
098: loadedHMs.remove(map);
099: }
100: }
101: }
102:
103: /**
104: * Return the current open {@link org.apache.ojb.broker.PersistenceBroker}
105: * instance for the given {@link org.apache.ojb.broker.PBKey}, if any.
106: *
107: * @param key
108: * @return null if no open {@link org.apache.ojb.broker.PersistenceBroker} found.
109: */
110: public static PersistenceBrokerInternal currentPersistenceBroker(
111: PBKey key) throws PBFactoryException,
112: PersistenceBrokerException {
113: HashMap map = (HashMap) currentBrokerMap.get();
114: WeakHashMap set;
115: PersistenceBrokerInternal broker = null;
116:
117: if (map == null) {
118: return null;
119: }
120:
121: set = (WeakHashMap) map.get(key);
122: if (set == null) {
123: return null;
124: }
125:
126: // seek for an open broker, preferably in transaction
127: for (Iterator it = set.keySet().iterator(); it.hasNext();) {
128: PersistenceBrokerInternal tmp = (PersistenceBrokerInternal) it
129: .next();
130: if (tmp == null || tmp.isClosed()) {
131: it.remove();
132: continue;
133: }
134: broker = tmp;
135: if (tmp.isInTransaction()) {
136: break; // the best choice found
137: }
138: }
139: return broker;
140: }
141:
142: /**
143: * Clean up static fields and any registered ThreadLocal contents to grant a clean
144: * shutdown/reload of OJB within re/hot-deployable applications.
145: */
146: public static void shutdown() {
147: for (Iterator it = loadedHMs.iterator(); it.hasNext();) {
148: ((HashMap) it.next()).clear();
149: }
150: loadedHMs.clear();
151: loadedHMs = null;
152: currentBrokerMap = null;
153: }
154: }
|