| 1 | |
package org.apache.ojb.odmg.collections; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.io.Serializable; |
| 19 | |
import java.util.AbstractMap; |
| 20 | |
import java.util.Iterator; |
| 21 | |
import java.util.Set; |
| 22 | |
import java.util.List; |
| 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 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class DMapImpl extends AbstractMap implements DMap, Serializable, PersistenceBrokerAware |
| 44 | |
{ |
| 45 | |
private static final long serialVersionUID = 7048246616243056480L; |
| 46 | |
private transient Logger log; |
| 47 | |
|
| 48 | |
private Integer id; |
| 49 | |
private Set entries; |
| 50 | |
private PBKey pbKey; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public DMapImpl() |
| 56 | |
{ |
| 57 | |
this.entries = new ManageableHashSet(); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
getPBKey(); |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 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 key, Object value) |
| 108 | |
{ |
| 109 | |
return new DMapEntry(this, key, value); |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public Set entrySet() |
| 126 | |
{ |
| 127 | |
return entries; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public Integer getId() |
| 134 | |
{ |
| 135 | |
return id; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public Object put(Object key, Object 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 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 remove(Object key) |
| 174 | |
{ |
| 175 | |
Iterator 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 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 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public Set getEntries() |
| 216 | |
{ |
| 217 | |
return entries; |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public void setEntries(ManageableHashSet entries) |
| 225 | |
{ |
| 226 | |
this.entries = entries; |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
public void setId(Integer id) |
| 234 | |
{ |
| 235 | |
this.id = id; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException |
| 248 | |
{ |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException |
| 259 | |
{ |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException |
| 266 | |
{ |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException |
| 273 | |
{ |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException |
| 280 | |
{ |
| 281 | |
} |
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException |
| 287 | |
{ |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException |
| 294 | |
{ |
| 295 | |
} |
| 296 | |
|
| 297 | |
} |