1 18 package org.objectweb.speedo.genclass; 19 20 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy; 21 import org.objectweb.speedo.mim.api.LifeCycle; 22 import org.objectweb.speedo.mim.api.SpeedoHome; 23 import org.objectweb.speedo.mim.api.SpeedoAccessor; 24 import org.objectweb.speedo.mim.api.SpeedoProxy; 25 import org.objectweb.speedo.genclass.api.SpeedoGenClassListener; 26 import org.objectweb.speedo.api.Debug; 27 import org.objectweb.jorm.util.api.Loggable; 28 import org.objectweb.jorm.api.PBinding; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PClassMapping; 31 import org.objectweb.jorm.api.PAccessor; 32 import org.objectweb.jorm.api.PIndexedElem; 33 import org.objectweb.jorm.api.PBindingCtrl; 34 import org.objectweb.jorm.type.api.PType; 35 import org.objectweb.jorm.naming.api.PName; 36 import org.objectweb.perseus.cache.api.UnFixProtocolException; 37 import org.objectweb.util.monolog.api.Logger; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.LoggerFactory; 40 41 import javax.jdo.PersistenceManager; 42 import javax.jdo.JDOUnsupportedOptionException; 43 import javax.jdo.JDOException; 44 import javax.jdo.spi.PersistenceCapable; 45 import javax.jdo.spi.StateManager; 46 import java.util.Collection ; 47 import java.util.Collections ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.ArrayList ; 51 52 58 public abstract class GenClass 59 implements SpeedoGenClassProxy, Loggable, PBindingCtrl { 60 61 65 protected boolean jdoIsActive = false; 66 67 70 protected PBindingCtrl pbinding; 71 72 75 public Logger logger; 76 77 80 protected String mapperName; 81 82 85 protected String projectName; 86 87 90 protected PType ptype; 91 92 95 protected String linkedField = null; 96 97 100 protected Object pnameHints = null; 101 102 105 protected int gcid; 106 107 110 protected List listeners; 111 112 protected long age; 113 114 protected int fix = 0; 115 116 protected Object encodedPName; 117 118 public void init(PType _ptype, 119 String _linkedField, 120 Object _pnameHints, 121 Logger _logger) { 122 this.pnameHints = _pnameHints; 123 this.ptype = _ptype; 124 this.linkedField = _linkedField; 125 this.logger = _logger; 126 } 127 128 public void jdoSetPNameHints(Object hints) { 129 pnameHints = hints; 130 } 131 132 public boolean elementIsReference() { 133 return ptype.getNestedPType().getTypeCode() == PType.TYPECODE_REFERENCE; 134 } 135 136 public void speedoSetGcId (int _gcid) { 137 this.gcid = _gcid; 138 } 139 140 public void speedoAddGenClassListener (SpeedoGenClassListener l) { 141 if (listeners == null) { 142 listeners = new ArrayList (); 143 listeners.add(l); 144 } else { 145 for(int i=0; i<listeners.size(); i++) { 146 if (listeners.get(i) == l) { 147 return; 148 } 149 } 150 listeners.add(l); 151 } 152 } 153 154 public void speedoAdd(Object elem, int gcFieldNumber) { 155 throw new JDOException("Not supported in tuple class"); 156 } 157 158 public boolean speedoAdd(Object elemToAdd, Object hints) { 159 GenClassAccessor gca = (GenClassAccessor) getSpeedoHome().writeIntention(this, null); 160 return gca.speedoAdd(elemToAdd, hints); 161 } 162 163 public boolean speedoRemove(Object elemToRemove, Object hints) { 164 GenClassAccessor gca = (GenClassAccessor) getSpeedoHome().writeIntention(this, null); 165 return gca.speedoRemove(elemToRemove, hints); 166 } 167 168 public void fireSpeedoElementAdded (Object elem) { 169 if (Debug.ON) { 170 logger.log(BasicLevel.DEBUG, "fireSpeedoElementAdded(): listner: " 171 + (listeners == null ? "no listener" : "" + listeners.size())); 172 } 173 if (listeners == null) { 174 return; 175 } 176 Iterator i = listeners.iterator(); 177 while (i.hasNext()) { 178 SpeedoGenClassListener l = (SpeedoGenClassListener)i.next(); 179 l.speedoElementAdded(elem, gcid); 180 } 181 } 182 183 public void fireSpeedoElementRemoved (Object elem) { 184 if (Debug.ON) { 185 logger.log(BasicLevel.DEBUG, "fireSpeedoElementRemoved(): listner: " 186 + (listeners == null ? "no listener" : "" + listeners.size())); 187 } 188 if (listeners == null) { 189 return; 190 } 191 Iterator i = listeners.iterator(); 192 while (i.hasNext()) { 193 SpeedoGenClassListener l = (SpeedoGenClassListener)i.next(); 194 l.speedoElementRemoved(elem, gcid); 195 } 196 } 197 198 public Object getDataIdentifier(Object o) { 199 if (o instanceof SpeedoProxy) { 200 if (!((SpeedoProxy) o).jdoIsActive()) { 201 ((SpeedoGenClassHome) getSpeedoHome()) 202 .makePersistent(null, (SpeedoProxy) o, this); 203 } 204 return ((SpeedoProxy) o).getCeIdentifier(); 205 } else { 206 return o; 207 } 208 } 209 public Collection getDataIdentifiers(Collection os) { 210 if (os == null) { 211 return null; 212 } 213 if (os.size() == 0) { 214 return Collections.EMPTY_LIST; 215 } 216 ArrayList al = new ArrayList (); 217 for(Iterator it = os.iterator(); it.hasNext();) { 218 al.add(getDataIdentifier(it.next())); 219 } 220 return al; 221 } 222 223 226 public abstract SpeedoAccessor createAccessor(); 227 228 public abstract SpeedoAccessor getReferenceAccessor(); 229 230 public abstract void setReferenceAccessor(SpeedoAccessor refAcc); 231 232 235 public void bind(PName name) throws PException { 236 pbinding.bind(name); 237 } 238 239 public boolean exist(Object o) throws PException { 240 return pbinding.exist(o); 241 } 242 243 public PName export(Object o) throws PException { 244 return pbinding.export(o); 245 } 246 247 public PName export(Object o, Object o1) throws PException { 248 return pbinding.export(o, o1); 249 } 250 251 public PClassMapping getPClassMapping() { 252 return pbinding.getPClassMapping(); 253 } 254 255 public PName getPName() { 256 return pbinding.getPName(); 257 } 258 259 public byte getStatus() { 260 return pbinding.getStatus(); 261 } 262 263 public void setPName(PName pName) { 264 pbinding.setPName(pName); 265 } 266 267 public void setStatus(byte b) { 268 pbinding.setStatus(b); 269 } 270 271 public void init(PClassMapping mapping) throws PException { 272 pbinding.init(mapping); 273 } 274 275 public void read(Object o, PAccessor accessor) throws PException { 276 pbinding.read(o, accessor); 277 } 278 279 public void read(Object o, PAccessor pAccessor, Object o1) throws PException { 280 pbinding.read(o, pAccessor, o1); 281 } 282 283 public void unbind() throws PException { 284 pbinding.unbind(); 285 } 286 287 public void unexport(Object o) throws PException { 288 pbinding.unexport(o); 289 } 290 291 public void write(Object o, PAccessor accessor) throws PException { 292 if (logger != null) 293 logger.log(BasicLevel.DEBUG, "Writing the gen class: " + linkedField); 294 pbinding.write(o, accessor); 295 boolean retainDeltaForMerge = ((SpeedoHome) pbinding.getPClassMapping()).isFieldLockingLevel(); 296 GenClassAccessor gca = (GenClassAccessor) accessor; 297 if (retainDeltaForMerge && gca.deltaForMerge == null) { 298 gca.deltaForMerge = new ArrayList (); 299 } 300 for(Iterator i = gca.elements.iterator();i.hasNext();) { 301 GenClassElement gce = (GenClassElement) i.next(); 302 if (retainDeltaForMerge 303 && gce.getElemStatus() != PIndexedElem.ELEM_MODIFIED) { 304 gce.retainStatusForMerge(); 305 gca.deltaForMerge.add(gce); 306 } 307 if (gce.getElemStatus() != PIndexedElem.ELEM_DELETED) { 308 gce.setStatus(PIndexedElem.ELEM_UNMODIFIED); 309 } else { 310 i.remove(); 312 } 313 } 314 } 315 316 317 323 public PersistenceManager jdoGetPersistenceManager() { 324 return (jdoIsActive ? getSpeedoHome().getProxyManagerFactory().lookup() : null); 325 } 326 327 public void jdoReplaceFlags() { 328 throw new JDOUnsupportedOptionException(); 329 } 330 331 public PersistenceCapable jdoNewInstance(StateManager sm) { 332 throw new JDOUnsupportedOptionException(); 333 } 334 335 public PersistenceCapable jdoNewInstance(StateManager sm, Object oid) { 336 throw new JDOUnsupportedOptionException(); 337 } 338 339 public Object jdoNewObjectIdInstance(Object arg0) { 340 throw new JDOUnsupportedOptionException(); 341 } 342 public Object jdoNewObjectIdInstance() { 343 throw new JDOUnsupportedOptionException(); 344 } 345 346 public Object jdoGetObjectId() { 347 return getPName(); 348 } 349 350 public Object jdoGetVersion() { 351 return getPName(); 352 } 353 354 public Object jdoGetTransactionalObjectId() { 355 return getPName(); 356 } 357 358 public void jdoReplaceField(int fieldNumber) { 359 throw new JDOUnsupportedOptionException(); 360 } 361 362 public void jdoReplaceFields(int[] fieldNumbers) { 363 throw new JDOUnsupportedOptionException(); 364 } 365 366 public void jdoProvideField(int fieldNumber) { 367 throw new JDOUnsupportedOptionException(); 368 } 369 370 public void jdoProvideFields(int[] fieldNumbers) { 371 throw new JDOUnsupportedOptionException(); 372 } 373 374 public void jdoCopyFields(Object pc, int[] fieldNumbers) { 375 throw new JDOUnsupportedOptionException(); 376 } 377 378 public void jdoMakeDirty(String fieldName) { 379 } 381 382 public boolean jdoIsDirty() { 383 SpeedoAccessor sa = getSpeedoAccessor(); 384 return sa != null 385 && !LifeCycle.isTransient(sa.jdoGetStatus()) 386 && LifeCycle.isDirty(sa.jdoGetStatus()); 387 } 388 389 public boolean jdoIsDetached() { 390 return false; 392 } 393 public void jdoReplaceObjectId(Object arg0) { 394 } 396 public boolean jdoIsTransactional() { 397 SpeedoAccessor sa = getSpeedoAccessor(); 398 return sa != null 399 && !LifeCycle.isTransient(sa.jdoGetStatus()) 400 && LifeCycle.isTransactional(sa.jdoGetStatus()); 401 } 402 403 public boolean jdoIsPersistent() { 404 SpeedoAccessor sa = getSpeedoAccessor(); 405 return sa == null || LifeCycle.isPersistent(sa.jdoGetStatus()); 406 } 407 408 public boolean jdoIsNew() { 409 SpeedoAccessor sa = getSpeedoAccessor(); 410 return sa != null && LifeCycle.isNew(sa.jdoGetStatus()); 411 } 412 413 public boolean jdoIsDeleted() { 414 SpeedoAccessor sa = getSpeedoAccessor(); 415 return sa != null && LifeCycle.isDeleted(sa.jdoGetStatus()); 416 } 417 418 public void jdoReplaceStateManager(StateManager sm) { 419 } 421 422 public Object jdoNewObjectIdInstance(String s) { 423 return null; 424 } 425 426 public void jdoCopyKeyFieldsToObjectId(Object o) { 427 throw new JDOUnsupportedOptionException(); 428 } 429 430 public void jdoCopyKeyFieldsToObjectId(PersistenceCapable.ObjectIdFieldSupplier objectIdFieldSupplier, Object o) { 431 throw new JDOUnsupportedOptionException(); 432 } 433 434 public void jdoCopyKeyFieldsFromObjectId(PersistenceCapable.ObjectIdFieldConsumer objectIdFieldConsumer, Object o) { 435 throw new JDOUnsupportedOptionException(); 436 } 437 438 439 442 446 public void jdoSetPType(PType _ptype) { 447 this.ptype = _ptype; 448 } 449 450 453 public PType jdoGetPType() { 454 return ptype; 455 } 456 457 463 public void jdoSetPBinding(PBinding pb) { 464 pbinding = (PBindingCtrl) pb; 465 } 466 467 471 public PBinding jdoGetPBinding() { 472 return pbinding; 473 } 474 475 481 public String jdoGetGenClassId() { 482 return linkedField; 483 } 484 485 492 public void jdoSetLinkedField(String lf) { 493 linkedField = lf; 494 } 495 496 500 public void setElements(Object o) { 501 ((GenClassAccessor) getSpeedoHome().writeIntention(this, null)).setElements(o); 502 } 503 504 507 public void copyAccessor(SpeedoAccessor src, SpeedoAccessor dest) { 508 ((GenClassAccessor) dest).loadFieldsFromAccessor(src); 509 } 510 511 public Object jdoGetPNameHints() { 512 return pnameHints; 513 } 514 515 public boolean jdoIsActive() { 516 return jdoIsActive; 517 } 518 519 public void jdoIsActive(boolean newvalue) { 520 jdoIsActive = newvalue; 521 } 522 public SpeedoAccessor getSpeedoAccessor() { 523 if (!jdoIsActive) { 524 return getReferenceAccessor(); 525 } 526 return getSpeedoHome().getSpeedoAccessor(this); 527 } 528 529 public Collection getSpeedoAccessors() { 530 return null; 531 } 532 533 public void removeSpeedoAccessor(SpeedoAccessor sa) { 534 } 535 536 public String jdoGetJdoFileName() { 537 return null; 538 } 539 540 public void setEncodedPName(Object _encodedPName) { 541 this.encodedPName = _encodedPName; 542 } 543 544 public Object getEncodedPName() { 545 return encodedPName; 546 } 547 548 public SpeedoHome getSpeedoHome() { 549 return (SpeedoHome) getPClassMapping(); 550 } 551 552 555 public Object getCeObject() { 556 return this; 557 } 558 559 public Object getCeIdentifier() { 560 return getPName(); 561 } 562 563 564 567 public void fixCe() { 568 fix++; 569 } 570 571 public void unfixCe() throws UnFixProtocolException { 572 fix--; 573 } 574 575 public int getCeFixCount() { 576 return fix; 577 } 578 579 582 public long getCeAge() { 583 return age; 584 } 585 586 public void setCeAge(long l) { 587 age = l; 588 } 589 590 594 public void setLogger(Logger _logger) { 595 logger = _logger; 596 } 597 598 public void setLoggerFactory(LoggerFactory loggerfactory) { 599 logger = loggerfactory.getLogger(this.getClass().getName()); 600 } 601 602 public Logger getLogger() { 603 return logger; 604 } 605 606 public LoggerFactory getLoggerFactory() { 607 return null; 608 } 609 } 610 | Popular Tags |