001: package org.apache.ojb.odmg.collections;
002:
003: /* Copyright 2002-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.io.Serializable;
019: import java.util.AbstractMap;
020: import java.util.Iterator;
021: import java.util.Set;
022: import java.util.List;
023:
024: import org.apache.ojb.broker.PBKey;
025: import org.apache.ojb.broker.PersistenceBroker;
026: import org.apache.ojb.broker.PersistenceBrokerAware;
027: import org.apache.ojb.broker.PersistenceBrokerException;
028: import org.apache.ojb.broker.util.collections.ManageableHashSet;
029: import org.apache.ojb.broker.util.logging.Logger;
030: import org.apache.ojb.broker.util.logging.LoggerFactory;
031: import org.apache.ojb.odmg.TransactionExt;
032: import org.apache.ojb.odmg.TransactionImpl;
033: import org.apache.ojb.odmg.TxManagerFactory;
034: import org.apache.ojb.odmg.RuntimeObject;
035: import org.odmg.DMap;
036: import org.odmg.Transaction;
037:
038: /**
039: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
040: * @version $Id: DMapImpl.java,v 1.21.2.5 2005/12/21 22:29:50 tomdz Exp $
041: */
042:
043: public class DMapImpl extends AbstractMap implements DMap,
044: Serializable, PersistenceBrokerAware {
045: private static final long serialVersionUID = 7048246616243056480L;
046: private transient Logger log;
047:
048: private Integer id;
049: private Set entries;
050: private PBKey pbKey;
051:
052: /**
053: * DMapImpl constructor comment.
054: */
055: public DMapImpl() {
056: this .entries = new ManageableHashSet();
057: // if(getTransaction() == null)
058: // {
059: // throw new TransactionNotInProgressException("Materialization of DCollection instances must be done" +
060: // " within a odmg-tx");
061: // }
062: getPBKey();
063: }
064:
065: /**
066: * DListImpl constructor comment.
067: */
068: public DMapImpl(PBKey key) {
069: this .entries = new ManageableHashSet();
070: this .pbKey = key;
071: }
072:
073: protected Logger getLog() {
074: if (log == null) {
075: log = LoggerFactory.getLogger(DMapImpl.class);
076: }
077: return log;
078: }
079:
080: protected TransactionImpl getTransaction() {
081: return TxManagerFactory.instance().getTransaction();
082: }
083:
084: public PBKey getPBKey() {
085: if (pbKey == null) {
086: TransactionExt tx = getTransaction();
087: if (tx != null && tx.isOpen()) {
088: pbKey = tx.getBroker().getPBKey();
089: }
090: }
091: return pbKey;
092: }
093:
094: public void setPBKey(PBKey pbKey) {
095: this .pbKey = pbKey;
096: }
097:
098: protected DMapEntry prepareEntry(Object key, Object value) {
099: return new DMapEntry(this , key, value);
100: }
101:
102: /**
103: * Returns a set view of the mappings contained in this map. Each element
104: * in the returned set is a <tt>Map.Entry</tt>. The set is backed by the
105: * map, so changes to the map are reflected in the set, and vice-versa.
106: * If the map is modified while an iteration over the set is in progress,
107: * the results of the iteration are undefined. The set supports element
108: * removal, which removes the corresponding mapping from the map, via the
109: * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
110: * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support
111: * the <tt>add</tt> or <tt>addAll</tt> operations.
112: *
113: * @return a set view of the mappings contained in this map.
114: */
115: public Set entrySet() {
116: return entries;
117: }
118:
119: /**
120: * lazily retrieve the ID of the set, no need to precompute it.
121: */
122: public Integer getId() {
123: return id;
124: }
125:
126: /**
127: *
128: */
129: public Object put(Object key, Object value) {
130:
131: DMapEntry entry = prepareEntry(key, value);
132: boolean ok = entries.add(entry);
133: if (ok) {
134: TransactionImpl tx = getTransaction();
135: if ((tx != null) && (tx.isOpen())) {
136: List regList = tx.getRegistrationList();
137: RuntimeObject rt = new RuntimeObject(this , tx);
138: tx.lockAndRegister(rt, Transaction.WRITE, false,
139: regList);
140:
141: rt = new RuntimeObject(key, tx);
142: tx.lockAndRegister(rt, Transaction.READ, regList);
143:
144: rt = new RuntimeObject(value, tx);
145: tx.lockAndRegister(rt, Transaction.READ, regList);
146:
147: rt = new RuntimeObject(entry, tx, true);
148: tx.lockAndRegister(rt, Transaction.WRITE, false,
149: regList);
150: }
151: return null;
152: } else {
153: return this .get(key);
154: }
155: }
156:
157: public Object remove(Object key) {
158: Iterator i = entrySet().iterator();
159: DMapEntry correctEntry = null;
160: if (key == null) {
161: while (correctEntry == null && i.hasNext()) {
162: DMapEntry e = (DMapEntry) i.next();
163: if (e.getKey() == null)
164: correctEntry = e;
165: }
166: } else {
167: while (correctEntry == null && i.hasNext()) {
168: DMapEntry e = (DMapEntry) i.next();
169: if (key.equals(e.getKey()))
170: correctEntry = e;
171: }
172: }
173:
174: Object oldValue = null;
175: if (correctEntry != null) {
176: oldValue = correctEntry.getValue();
177: i.remove();
178: TransactionImpl tx = getTransaction();
179: if ((tx != null) && (tx.isOpen())) {
180: tx
181: .deletePersistent(new RuntimeObject(
182: correctEntry, tx));
183: }
184: }
185: return oldValue;
186: }
187:
188: /**
189: * Gets the entries.
190: * @return Returns a Set
191: */
192: public Set getEntries() {
193: return entries;
194: }
195:
196: /**
197: * Sets the entries.
198: * @param entries The entries to set
199: */
200: public void setEntries(ManageableHashSet entries) {
201: this .entries = entries;
202: }
203:
204: /**
205: * Sets the id.
206: * @param id The id to set
207: */
208: public void setId(Integer id) {
209: this .id = id;
210: }
211:
212: //***************************************************************
213: // PersistenceBrokerAware interface
214: //***************************************************************
215:
216: /**
217: * prepare itself for persistence. Each DList entry generates an
218: * {@link org.apache.ojb.broker.Identity} for the wrapped persistent
219: * object.
220: */
221: public void beforeInsert(PersistenceBroker broker)
222: throws PersistenceBrokerException {
223: // for (Iterator it = entries.iterator(); it.hasNext();)
224: // {
225: // ((DMapEntry)it.next()).prepareForPersistency(broker);
226: // }
227: }
228:
229: /**
230: * noop
231: */
232: public void beforeUpdate(PersistenceBroker broker)
233: throws PersistenceBrokerException {
234: }
235:
236: /**
237: * noop
238: */
239: public void beforeDelete(PersistenceBroker broker)
240: throws PersistenceBrokerException {
241: }
242:
243: /**
244: * noop
245: */
246: public void afterUpdate(PersistenceBroker broker)
247: throws PersistenceBrokerException {
248: }
249:
250: /**
251: * noop
252: */
253: public void afterInsert(PersistenceBroker broker)
254: throws PersistenceBrokerException {
255: }
256:
257: /**
258: * noop
259: */
260: public void afterDelete(PersistenceBroker broker)
261: throws PersistenceBrokerException {
262: }
263:
264: /**
265: * noop
266: */
267: public void afterLookup(PersistenceBroker broker)
268: throws PersistenceBrokerException {
269: }
270:
271: }
|