KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > collections > DMapImpl


1 package org.apache.ojb.odmg.collections;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.AbstractMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.ojb.broker.PBKey;
25 import org.apache.ojb.broker.PersistenceBroker;
26 import org.apache.ojb.broker.PersistenceBrokerAware;
27 import org.apache.ojb.broker.PersistenceBrokerException;
28 import org.apache.ojb.broker.util.collections.ManageableHashSet;
29 import org.apache.ojb.broker.util.logging.Logger;
30 import org.apache.ojb.broker.util.logging.LoggerFactory;
31 import org.apache.ojb.odmg.TransactionExt;
32 import org.apache.ojb.odmg.TransactionImpl;
33 import org.apache.ojb.odmg.TxManagerFactory;
34 import org.apache.ojb.odmg.RuntimeObject;
35 import org.odmg.DMap;
36 import org.odmg.Transaction;
37
38 /**
39  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
40  * @version $Id: DMapImpl.java,v 1.21.2.5 2005/12/21 22:29:50 tomdz Exp $
41  */

42
43 public class DMapImpl extends AbstractMap JavaDoc implements DMap, Serializable JavaDoc, PersistenceBrokerAware
44 {
45     private static final long serialVersionUID = 7048246616243056480L;
46     private transient Logger log;
47
48     private Integer JavaDoc id;
49     private Set JavaDoc entries;
50     private PBKey pbKey;
51
52     /**
53      * DMapImpl constructor comment.
54      */

55     public DMapImpl()
56     {
57         this.entries = new ManageableHashSet();
58 // if(getTransaction() == null)
59
// {
60
// throw new TransactionNotInProgressException("Materialization of DCollection instances must be done" +
61
// " within a odmg-tx");
62
// }
63
getPBKey();
64     }
65
66     /**
67      * DListImpl constructor comment.
68      */

69     public DMapImpl(PBKey key)
70     {
71         this.entries = new ManageableHashSet();
72         this.pbKey = key;
73     }
74
75     protected Logger getLog()
76     {
77         if (log == null)
78         {
79             log = LoggerFactory.getLogger(DMapImpl.class);
80         }
81         return log;
82     }
83
84     protected TransactionImpl getTransaction()
85     {
86         return TxManagerFactory.instance().getTransaction();
87     }
88
89     public PBKey getPBKey()
90     {
91         if(pbKey == null)
92         {
93             TransactionExt tx = getTransaction();
94             if(tx != null && tx.isOpen())
95             {
96                 pbKey = tx.getBroker().getPBKey();
97             }
98         }
99         return pbKey;
100     }
101
102     public void setPBKey(PBKey pbKey)
103     {
104         this.pbKey = pbKey;
105     }
106
107     protected DMapEntry prepareEntry(Object JavaDoc key, Object JavaDoc value)
108     {
109         return new DMapEntry(this, key, value);
110     }
111
112     /**
113      * Returns a set view of the mappings contained in this map. Each element
114      * in the returned set is a <tt>Map.Entry</tt>. The set is backed by the
115      * map, so changes to the map are reflected in the set, and vice-versa.
116      * If the map is modified while an iteration over the set is in progress,
117      * the results of the iteration are undefined. The set supports element
118      * removal, which removes the corresponding mapping from the map, via the
119      * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
120      * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support
121      * the <tt>add</tt> or <tt>addAll</tt> operations.
122      *
123      * @return a set view of the mappings contained in this map.
124      */

125     public Set JavaDoc entrySet()
126     {
127         return entries;
128     }
129
130     /**
131      * lazily retrieve the ID of the set, no need to precompute it.
132      */

133     public Integer JavaDoc getId()
134     {
135         return id;
136     }
137
138     /**
139      *
140      */

141     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
142     {
143
144         DMapEntry entry = prepareEntry(key, value);
145         boolean ok = entries.add(entry);
146         if (ok)
147         {
148             TransactionImpl tx = getTransaction();
149             if ((tx != null) && (tx.isOpen()))
150             {
151                 List JavaDoc regList = tx.getRegistrationList();
152                 RuntimeObject rt = new RuntimeObject(this, tx);
153                 tx.lockAndRegister(rt, Transaction.WRITE, false, regList);
154
155                 rt = new RuntimeObject(key, tx);
156                 tx.lockAndRegister(rt, Transaction.READ, regList);
157
158                 rt = new RuntimeObject(value, tx);
159                 tx.lockAndRegister(rt, Transaction.READ, regList);
160
161                 rt = new RuntimeObject(entry, tx, true);
162                 tx.lockAndRegister(rt, Transaction.WRITE, false, regList);
163             }
164             return null;
165         }
166         else
167         {
168             return this.get(key);
169         }
170     }
171
172
173     public Object JavaDoc remove(Object JavaDoc key)
174     {
175         Iterator JavaDoc i = entrySet().iterator();
176         DMapEntry correctEntry = null;
177         if (key == null)
178         {
179             while (correctEntry == null && i.hasNext())
180             {
181                 DMapEntry e = (DMapEntry) i.next();
182                 if (e.getKey() == null)
183                     correctEntry = e;
184             }
185         }
186         else
187         {
188             while (correctEntry == null && i.hasNext())
189             {
190                 DMapEntry e = (DMapEntry) i.next();
191                 if (key.equals(e.getKey()))
192                     correctEntry = e;
193             }
194         }
195
196         Object JavaDoc oldValue = null;
197         if (correctEntry != null)
198         {
199             oldValue = correctEntry.getValue();
200             i.remove();
201             TransactionImpl tx = getTransaction();
202             if ((tx != null) && (tx.isOpen()))
203             {
204                 tx.deletePersistent(new RuntimeObject(correctEntry, tx));
205             }
206         }
207         return oldValue;
208     }
209
210
211     /**
212      * Gets the entries.
213      * @return Returns a Set
214      */

215     public Set JavaDoc getEntries()
216     {
217         return entries;
218     }
219
220     /**
221      * Sets the entries.
222      * @param entries The entries to set
223      */

224     public void setEntries(ManageableHashSet entries)
225     {
226         this.entries = entries;
227     }
228
229     /**
230      * Sets the id.
231      * @param id The id to set
232      */

233     public void setId(Integer JavaDoc id)
234     {
235         this.id = id;
236     }
237
238     //***************************************************************
239
// PersistenceBrokerAware interface
240
//***************************************************************
241

242     /**
243      * prepare itself for persistence. Each DList entry generates an
244      * {@link org.apache.ojb.broker.Identity} for the wrapped persistent
245      * object.
246      */

247     public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
248     {
249 // for (Iterator it = entries.iterator(); it.hasNext();)
250
// {
251
// ((DMapEntry)it.next()).prepareForPersistency(broker);
252
// }
253
}
254
255     /**
256      * noop
257      */

258     public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException
259     {
260     }
261
262     /**
263      * noop
264      */

265     public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException
266     {
267     }
268
269     /**
270      * noop
271      */

272     public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException
273     {
274     }
275
276     /**
277      * noop
278      */

279     public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException
280     {
281     }
282
283     /**
284      * noop
285      */

286     public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException
287     {
288     }
289
290     /**
291      * noop
292      */

293     public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException
294     {
295     }
296
297 }
298
Popular Tags