1 package org.apache.ojb.jdori.sql; 2 16 17 import java.util.BitSet ; 18 import java.util.Iterator ; 19 20 import javax.jdo.Extent; 21 import javax.jdo.JDOFatalInternalException; 22 import javax.jdo.JDOUserException; 23 import javax.jdo.spi.PersistenceCapable; 24 25 import org.apache.ojb.broker.Identity; 26 import org.apache.ojb.broker.PersistenceBroker; 27 import org.apache.ojb.broker.util.ObjectModificationDefaultImpl; 28 import org.apache.ojb.broker.util.logging.Logger; 29 import org.apache.ojb.broker.util.logging.LoggerFactory; 30 31 import com.sun.jdori.Connector; 32 import com.sun.jdori.FieldManager; 33 import com.sun.jdori.PersistenceManagerInternal; 34 import com.sun.jdori.StateManagerInternal; 35 import com.sun.jdori.StoreManager; 36 import com.sun.jdori.common.query.BasicQueryResult; 37 import com.sun.jdori.model.jdo.JDOClass; 38 import com.sun.jdori.query.QueryResult; 39 import com.sun.jdori.query.QueryResultHelper; 40 41 42 49 class OjbStoreManager implements StoreManager 50 { 51 private final OjbStorePMF pmf; 52 private final OjbStoreConnector connector; 53 54 private boolean optimistic; 55 56 57 private Logger logger = LoggerFactory.getLogger("JDO"); 58 59 OjbStoreManager(OjbStorePMF pmf) 60 { 61 this.pmf = pmf; 62 this.connector = new OjbStoreConnector(pmf); 63 } 64 65 66 69 public Connector getConnector() 70 { 71 return connector; 72 } 73 74 78 public Connector getConnector(String userid, String password) 79 { 80 throw new JDOUserException("Not implemented"); 81 } 82 83 86 public synchronized int insert( 87 BitSet loadedFields, 88 BitSet dirtyFields, 89 StateManagerInternal sm) 90 { 91 92 try 93 { 94 logger.debug("OjbStoreManager.insert"); 95 PersistenceBroker broker = connector.getBroker(); 96 Object instance = sm.getObject(); 97 broker.store(instance, ObjectModificationDefaultImpl.INSERT); 98 } 99 catch (Exception ex) 100 { 101 throw new OjbStoreFatalInternalException(getClass(), "insert", ex); 102 } 103 dirtyFields.xor(dirtyFields); 104 return StateManagerInternal.FLUSHED_COMPLETE; 105 } 106 107 110 public synchronized int update( 111 BitSet loadedFields, 112 BitSet dirtyFields, 113 StateManagerInternal sm) 114 { 115 116 try 117 { 118 logger.debug("OjbStoreManager.update"); 119 PersistenceBroker broker = connector.getBroker(); 120 fetch(sm, null); 121 Object instance = sm.getObject(); 122 broker.store(instance, ObjectModificationDefaultImpl.UPDATE); 123 } 124 catch (Exception ex) 125 { 126 throw new OjbStoreFatalInternalException(getClass(), "update", ex); 127 } 128 dirtyFields.xor(dirtyFields); 129 return StateManagerInternal.FLUSHED_COMPLETE; 130 } 131 132 135 public synchronized int verifyFields( 136 BitSet ignoredFields, 137 BitSet fieldsToVerify, 138 StateManagerInternal sm) 139 { 140 fieldsToVerify.xor(fieldsToVerify); 141 return StateManagerInternal.FLUSHED_COMPLETE; 142 } 143 144 145 148 public synchronized int delete( 149 BitSet loadedFields, 150 BitSet dirtyFields, 151 StateManagerInternal sm) 152 { 153 Identity oid = (Identity)sm.getInternalObjectId(); 154 logger.debug("OjbStoreManager.delete(" + oid + ")"); 155 try 156 { 157 fetch(sm,null); 158 connector.getBroker().delete(sm.getObject()); 159 } 160 catch (Exception ex) 161 { 162 throw new OjbStoreFatalInternalException(getClass(), "delete", ex); 163 } 164 dirtyFields.xor(dirtyFields); 165 return StateManagerInternal.FLUSHED_COMPLETE; 166 } 167 168 171 public synchronized void fetch(StateManagerInternal sm, int fieldNums[]) 172 { 173 PersistenceBroker broker = connector.getBroker(); 174 try 175 { 176 Object instance = sm.getObject(); 177 Identity oid = (Identity) sm.getInternalObjectId(); 178 if (oid == null) 179 { 180 oid = new Identity(instance,broker); 181 } 182 broker.removeFromCache(instance); 183 PersistenceCapable pc = (PersistenceCapable) broker.getObjectByIdentity(oid); 184 185 JDOClass jdoClass = Helper.getJDOClass(pc.getClass()); 186 if (fieldNums == null) 187 { 188 fieldNums = jdoClass.getManagedFieldNumbers(); 189 } 190 191 FieldManager fm = new OjbFieldManager(pc, broker); 192 sm.replaceFields(fieldNums, fm); 193 194 getConnector().flush(); 195 } 196 197 catch (Exception ex) 198 { 199 throw new OjbStoreFatalInternalException(getClass(), "fetch", ex); 200 } 201 } 202 203 206 public synchronized Extent getExtent( 207 Class pcClass, 208 boolean subclasses, 209 PersistenceManagerInternal pm) 210 { 211 PersistenceBroker broker = connector.getBroker(); 212 return new OjbExtent(pcClass, broker, pm); 213 } 214 215 218 public synchronized Object createObjectId( 219 StateManagerInternal sm, 220 PersistenceManagerInternal pm) 221 { 222 PersistenceCapable obj = sm.getObject(); 223 Identity oid = new Identity(obj, connector.getBroker()); 224 return oid; 225 } 226 227 230 public synchronized Object createInternalObjectId( 231 StateManagerInternal sm, 232 PersistenceCapable pc, 233 Object oid, 234 Class cls, 235 PersistenceManagerInternal pm) 236 { 237 return new Identity(pc, connector.getBroker()); 238 } 239 240 244 public synchronized Object getExternalObjectId(Object objectId, PersistenceCapable pc) 245 { 246 return new Identity(pc, connector.getBroker()); 247 } 248 249 252 public void copyKeyFieldsFromObjectId(StateManagerInternal sm, Class pcClass) 253 { 254 new Identity(sm.getObject(), connector.getBroker()); 255 } 256 257 260 public boolean hasActualPCClass(Object objectId) 261 { 262 boolean rc = true; 263 return rc; 264 } 265 266 269 public synchronized Object getInternalObjectId(Object objectId, PersistenceManagerInternal pm) 270 { 271 return objectId; 272 } 273 274 277 public synchronized Class getPCClassForOid(Object objectId, PersistenceManagerInternal pm) 278 { 279 return ((Identity) objectId).getObjectsTopLevelClass(); 280 } 281 282 285 public Object newObjectIdInstance(Class pcClass, String str) 286 { 287 return Identity.fromByteArray(str.getBytes()); 288 } 289 290 293 public void flush(Iterator it, PersistenceManagerInternal pm) 294 { 295 this.optimistic = pm.currentTransaction().getOptimistic(); 296 boolean err = false; 297 298 while (it.hasNext()) 299 { 300 StateManagerInternal sm = (StateManagerInternal) it.next(); 301 logger.debug("OjbStoreManager.flush: " + sm.getInternalObjectId() + ", " + Helper.getLCState(sm)); 302 sm.preStore(); 303 sm.replaceSCOFields(); 304 sm.flush(this); 305 if (!sm.isFlushed()) 306 { 307 err = true; 308 break; 309 } 310 } 311 312 logger.debug("OjbStoreManager.flush: end, err=" + err); 313 314 if (err) 315 { 316 throw new JDOFatalInternalException("Error in flush"); 317 } 318 } 319 320 321 322 325 public QueryResult newQueryResult(QueryResultHelper queryResultHelper) 326 { 327 return new BasicQueryResult(queryResultHelper); 328 } 329 330 } 331 | Popular Tags |