1 23 24 package org.objectweb.jorm.facility.naming.basidir; 25 26 import org.objectweb.jorm.api.PBinding; 27 import org.objectweb.jorm.api.PClassMapping; 28 import org.objectweb.jorm.api.PException; 29 import org.objectweb.jorm.api.PExceptionProtocol; 30 import org.objectweb.jorm.api.PMapper; 31 import org.objectweb.jorm.api.PStateGraph; 32 import org.objectweb.jorm.api.PBindingCtrl; 33 import org.objectweb.jorm.facility.naming.generator.LongGen; 34 import org.objectweb.jorm.facility.naming.generator.LongGenMgr; 35 import org.objectweb.jorm.facility.naming.generator.LongGenMgrRegistry; 36 import org.objectweb.jorm.naming.api.PBinder; 37 import org.objectweb.jorm.naming.api.PExceptionExistingName; 38 import org.objectweb.jorm.naming.api.PExceptionNaming; 39 import org.objectweb.jorm.naming.api.PName; 40 import org.objectweb.jorm.naming.api.PNamingContext; 41 import org.objectweb.jorm.type.api.PType; 42 import org.objectweb.perseus.cache.api.CacheManager; 43 import org.objectweb.perseus.cache.api.CacheEntry; 44 import org.objectweb.perseus.cache.api.CacheException; 45 import org.objectweb.perseus.cache.api.FixableCacheEntry; 46 47 import java.util.Date ; 48 import java.math.BigInteger ; 49 import java.math.BigDecimal ; 50 51 55 public class BasidBinder implements PBinder, PNamingContext { 56 private final static String LONGGENMGRCLASS 58 = "org.objectweb.jorm.facility.naming.generator.LongGenIncr"; 59 protected int codingType = -1; 60 protected Object nullOValue = null; 61 protected long nullValue = -1; 62 PName PNNULL = null; 63 PName PNONULL = null; 64 protected PType ptype = null; 65 protected PClassMapping pcm = null; 66 private CacheManager bindingcache = null; 67 private LongGen longGen = null; 68 69 public BasidBinder() { 70 } 71 72 public BasidBinder(PMapper m, String genname, byte clact) throws PException { 73 this(); 74 LongGenMgr lgm; 75 synchronized (LongGenMgrRegistry.class) { 77 lgm = LongGenMgrRegistry.getLongGenMgr(m); 78 if (lgm == null) { 79 try { 80 lgm = (LongGenMgr) Class.forName( 81 m.cn2mn(LONGGENMGRCLASS) + PMapper.PCLASSMAPPINGAPPENDER) 82 .newInstance(); 83 } catch (InstantiationException e) { 84 throw new PException(e, "Cannot create LongGenMgr: cannot instanciate."); 85 } catch (IllegalAccessException e) { 86 throw new PException(e, "Cannot create LongGenMgr: cannot access."); 87 } catch (ClassNotFoundException e) { 88 throw new PException(e, "Cannot create LongGenMgr: cannot load."); 89 } 90 lgm.init(m, clact); 91 LongGenMgrRegistry.registerLonGenMgr(lgm); 92 } 93 } 94 longGen = lgm.getLongGen(genname); 95 codingType = PNamingContext.CTLONG; 96 nullValue = -1; 97 } 98 99 public BasidBinder(int codingtype) { 100 this(); 101 codingType = codingtype; 102 } 103 104 public BasidBinder(int codingtype, Object nullValue) { 105 this(); 106 codingType = codingtype; 107 this.nullOValue = nullValue; 108 } 109 110 public BasidBinder(int codingtype, long nullValue) { 111 this(); 112 codingType = codingtype; 113 this.nullValue = nullValue; 114 } 115 116 public int getCodingType() { 117 return codingType; 118 } 119 120 public void setCodingType(int codingType) { 121 this.codingType = codingType; 122 } 123 124 public void setNulOValue(Object nullvalue) { 125 this.nullOValue = nullvalue; 126 } 127 128 public void setNulValue(long nullvalue) { 129 this.nullValue = nullvalue; 130 } 131 134 public boolean codingSupported(int codingtype) { 135 return codingtype == codingType; 136 } 137 138 public PName decode(byte[] en) throws PExceptionNaming { 139 return new BasidName(en, this); 140 } 141 142 public PName decodeAbstract(Object en, Object context) throws PExceptionNaming, UnsupportedOperationException { 143 return new BasidName(en, this); 144 } 145 146 public PName decodeByte(byte en) throws PExceptionNaming, UnsupportedOperationException { 147 return new BasidName(en, this); 148 } 149 150 public PName decodeObyte(Byte en) throws PExceptionNaming, UnsupportedOperationException { 151 return new BasidName(en, this); 152 } 153 154 public PName decodeChar(char en) throws PExceptionNaming, UnsupportedOperationException { 155 return new BasidName(en, this); 156 } 157 158 public PName decodeOchar(Character en) throws PExceptionNaming, UnsupportedOperationException { 159 return new BasidName(en, this); 160 } 161 162 public PName decodeInt(int en) throws PExceptionNaming, UnsupportedOperationException { 163 return new BasidName(en, this); 164 } 165 166 public PName decodeOint(Integer en) throws PExceptionNaming, UnsupportedOperationException { 167 return new BasidName(en, this); 168 } 169 170 public PName decodeLong(long en) throws PExceptionNaming, UnsupportedOperationException { 171 return new BasidName(en, this); 172 } 173 174 public PName decodeOlong(Long en) throws PExceptionNaming, UnsupportedOperationException { 175 return new BasidName(en, this); 176 } 177 178 public PName decodeShort(short en) throws PExceptionNaming, UnsupportedOperationException { 179 return new BasidName(en, this); 180 } 181 182 public PName decodeOshort(Short en) throws PExceptionNaming, UnsupportedOperationException { 183 return new BasidName(en, this); 184 } 185 186 public PName decodeString(String en) throws PExceptionNaming { 187 switch (codingType) { 188 case PNamingContext.CTBYTE: 189 case PNamingContext.CTCHAR: 190 case PNamingContext.CTSHORT: 191 case PNamingContext.CTINT: 192 case PNamingContext.CTLONG: 193 return new BasidName(Long.parseLong(en), this); 194 } 195 return new BasidName(en, this); 196 } 197 198 public PName decodeCharArray(char[] en) throws PExceptionNaming { 199 return new BasidName(en, this); 200 } 201 202 public PName decodeDate(Date en) throws PExceptionNaming { 203 return new BasidName(en, this); 204 } 205 206 public PName decodeBigInteger(BigInteger en) throws PExceptionNaming { 207 return new BasidName(en, this); 208 } 209 210 public PName decodeBigDecimal(BigDecimal en) throws PExceptionNaming { 211 return new BasidName(en, this); 212 } 213 214 public byte[] encode(PName pn) throws PExceptionNaming { 215 if (pn.isNull()) 216 return (byte[]) nullOValue; 217 if (codingType == PNamingContext.CTBYTEARRAY) 218 return (byte[]) ((BasidName) pn).o; 219 throw new PExceptionNaming("Unsupported coding type"); 220 } 221 222 public Object encodeAbstract(PName pn) throws PExceptionNaming, UnsupportedOperationException { 223 return (BasidName) pn; 224 } 225 226 public byte encodeByte(PName pn) throws PExceptionNaming, UnsupportedOperationException { 227 if (pn.isNull()) 228 return (byte) nullValue; 229 if (codingType == CTBYTE) 230 return (byte) ((BasidName) pn).p; 231 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 232 } 233 234 public Byte encodeObyte(PName pn) throws PExceptionNaming, UnsupportedOperationException { 235 if (pn.isNull()) 236 return (Byte ) nullOValue; 237 if (codingType == CTOBYTE) 238 return (Byte ) ((BasidName) pn).o; 239 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 240 } 241 242 public char encodeChar(PName pn) throws PExceptionNaming, UnsupportedOperationException { 243 if (pn.isNull()) 244 return (char) nullValue; 245 if (codingType == CTCHAR) 246 return (char) ((BasidName) pn).p; 247 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 248 } 249 250 public Character encodeOchar(PName pn) throws PExceptionNaming, UnsupportedOperationException { 251 if (pn.isNull()) 252 return (Character ) nullOValue; 253 if (codingType == CTOCHAR) 254 return (Character ) ((BasidName) pn).o; 255 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 256 } 257 258 public int encodeInt(PName pn) throws PExceptionNaming, UnsupportedOperationException { 259 if (pn.isNull()) 260 return (int) nullValue; 261 if (codingType == CTINT) 262 return (int) ((BasidName) pn).p; 263 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 264 } 265 266 public Integer encodeOint(PName pn) throws PExceptionNaming, UnsupportedOperationException { 267 if (pn.isNull()) 268 return (Integer ) nullOValue; 269 if (codingType == CTOINT) 270 return (Integer ) ((BasidName) pn).o; 271 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 272 } 273 274 public long encodeLong(PName pn) throws PExceptionNaming, UnsupportedOperationException { 275 if (pn.isNull()) 276 return nullValue; 277 if (codingType == CTLONG) 278 return ((BasidName) pn).p; 279 System.err.println("CT=" + codingType); 280 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 281 } 282 283 public Long encodeOlong(PName pn) throws PExceptionNaming, UnsupportedOperationException { 284 if (pn.isNull()) 285 return (Long ) nullOValue; 286 if (codingType == CTOLONG) 287 return (Long ) ((BasidName) pn).o; 288 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 289 } 290 291 public short encodeShort(PName pn) throws PExceptionNaming, UnsupportedOperationException { 292 if (pn.isNull()) 293 return (short) nullValue; 294 if (codingType == CTSHORT) 295 return (short) ((BasidName) pn).p; 296 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 297 } 298 299 public Short encodeOshort(PName pn) throws PExceptionNaming, UnsupportedOperationException { 300 if (pn.isNull()) 301 return (Short ) nullOValue; 302 if (codingType == CTOSHORT) 303 return (Short ) ((BasidName) pn).o; 304 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 305 } 306 307 public String encodeString(PName pn) throws PExceptionNaming { 308 if (pn.isNull()) 309 return null; 310 switch (codingType) { 311 case CTBYTE: 312 case CTCHAR: 313 case CTINT: 314 case CTLONG: 315 case CTSHORT: 316 return Long.toString(((BasidName) pn).p); 317 case CTOBYTE: 318 case CTOCHAR: 319 case CTOINT: 320 case CTOLONG: 321 case CTOSHORT: 322 case CTSTRING: 323 case CTBIGINTEGER: 324 case CTBIGDECIMAL: 325 return ((BasidName) pn).o.toString(); 326 case CTDATE: 327 return "" + ((Date ) ((BasidName) pn).o).getTime(); 328 } 329 return "?"; 330 } 331 332 public char[] encodeCharArray(PName pn) throws PExceptionNaming { 333 if (pn.isNull()) 334 return (char[]) nullOValue; 335 if (codingType == CTCHARARRAY) 336 return (char[]) ((BasidName) pn).o; 337 throw new PExceptionNaming("Unsupported coding type, expected: " + codingType); 338 } 339 340 public Date encodeDate(PName pn) throws PExceptionNaming { 341 if (pn.isNull()) 342 return (Date ) nullOValue; 343 if (codingType == CTDATE) 344 return (Date ) ((BasidName) pn).o; 345 throw new PExceptionNaming("[" + getClassName() + "]: unsupported coding type"); 346 } 347 348 public BigInteger encodeBigInteger(PName pn) throws PExceptionNaming { 349 if (pn.isNull()) 350 return (BigInteger ) nullOValue; 351 if (codingType == CTBIGINTEGER) 352 return (BigInteger ) ((BasidName) pn).o; 353 throw new PExceptionNaming("[" + getClassName() + "]: unsupported coding type"); 354 } 355 356 public BigDecimal encodeBigDecimal(PName pn) throws PExceptionNaming { 357 if (pn.isNull()) 358 return (BigDecimal ) nullOValue; 359 if (codingType == CTBIGDECIMAL) 360 return (BigDecimal ) ((BasidName) pn).o; 361 throw new PExceptionNaming("[" + getClassName() + "]: unsupported coding type"); 362 } 363 364 public PName export(Object conn, Object en) throws PException { 365 PName pn = null; 366 if (en == null) { 367 throw new PExceptionNaming("[" + getClassName() + "]: cannot export null!"); 368 } 369 if (en instanceof BasidName) { 370 if (((PName) en).getPNameManager() == this) { 371 return (PName) en; 372 } else { 373 return new BasidName(en, this); 374 } 375 } 376 PBindingCtrl pb = (PBindingCtrl) en; 377 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 378 PBinding.ACTION_EXPORT); 379 if (nextstate == PBinding.LIFECYCLE_ERROR) 380 throw new PExceptionProtocol("Export unauthorized operation, current status:" + pb.getStatus()); 381 if (longGen == null) { 383 throw new PExceptionNaming("[" + getClassName() + "]: export not supported by this binder with no generator."); 384 } 385 pn = new BasidName(longGen.genId(conn), this); 386 if (bindingcache != null) { 387 synchronized (bindingcache) { 388 if (bindingcache.lookup(pn) != null) { 389 throw new PExceptionExistingName( 390 "[" + getClassName() + "]: an object has been already export with the same identifier"); 391 } 392 try { 393 bindingcache.fix(bindingcache.bind(pn, en)); 394 } catch (CacheException e) { 395 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 396 } 397 } 398 } 399 pb.setPName(pn); 400 pb.setStatus(nextstate); 401 return pn; 402 } 403 404 public PName export(Object conn, Object en, Object hints) throws PException { 405 PName pn = null; 406 if (en == null) { 407 throw new PExceptionNaming("[" + getClassName() + "]: cannot export null!"); 408 } 409 if (en instanceof PName && ((PName) en).getPNameManager() == this) 410 return (PName) en; 411 PBindingCtrl pb = (PBindingCtrl) en; 412 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 413 PBinding.ACTION_EXPORT); 414 if (nextstate == PBinding.LIFECYCLE_ERROR) 415 throw new PExceptionProtocol("Export unauthorized operation, current status:" + pb.getStatus()); 416 if (longGen != null) { 418 throw new PExceptionNaming("[" + getClassName() + "]: export with hints not support by this generator binder."); 419 } 420 if (en instanceof BasidName 421 && ((PName) en).getPNameManager().equals(this)) { 422 return (PName) en; 423 } 424 if (!(en instanceof PBinding)) { 425 throw new PExceptionNaming("[" + getClassName() + "]: the item cannot be exported by this binder (PName or PBinding only): " + en); 426 } 427 if (hints != null) { 428 pn = new BasidName(hints, this); 429 } else { 430 throw new PExceptionNaming( 431 "[" + getClassName() + "]: this item cannot be exported by this binder"); 432 } 433 if (bindingcache != null) { 434 synchronized (bindingcache) { 435 if (bindingcache.lookup(pn) != null) { 436 throw new PExceptionExistingName( 437 "[" + getClassName() + "]: an object has been already export with the same identifier"); 438 } 439 try { 440 bindingcache.fix(bindingcache.bind(pn, en)); 441 } catch (CacheException e) { 442 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 443 } 444 } 445 } 446 pb.setPName(pn); 447 pb.setStatus(nextstate); 448 return pn; 449 } 450 451 public PName getNull() { 452 switch (codingType) { 453 case CTBYTE: 454 case CTINT: 455 case CTLONG: 456 case CTSHORT: 457 return PNNULL; 458 case CTOBYTE: 459 case CTOCHAR: 460 case CTOINT: 461 case CTOLONG: 462 case CTOSHORT: 463 case CTSTRING: 464 case CTDATE: 465 return PNONULL; 466 case CTCHAR: 467 return PNNULL; 468 default: 469 return PNONULL; 470 } 471 } 472 473 483 public void setNullPName(Object o) { 484 if (o == null) { 485 nullOValue = o; 486 } else if (o instanceof BasidName) { 487 nullOValue = ((BasidName) o).o; 488 nullValue = ((BasidName) o).p; 489 } else { 490 nullOValue = o; 491 if (o instanceof Byte ) { 492 nullValue = ((Byte ) o).byteValue(); 493 } else if (o instanceof Short ) { 494 nullValue = ((Short ) o).shortValue(); 495 } else if (o instanceof Integer ) { 496 nullValue = ((Integer ) o).intValue(); 497 } else if (o instanceof Long ) { 498 nullValue = ((Long ) o).longValue(); 499 } else if (o instanceof Character ) { 500 nullValue = ((Character ) o).charValue(); 501 } 502 } 503 } 504 505 public PType getPType() { 506 return ptype; 507 } 508 509 public String getClassName() { 510 return ptype.getJormName(); 511 } 512 513 public PName resolve(Object conn, PName pn) throws PException { 514 if (pn == null) { 515 throw new PExceptionNaming("[" + getClassName() + "]: this pname is null"); 516 } 517 if (!pn.getPNameManager().equals(this)) 518 throw new PExceptionNaming("[" + getClassName() + "]: this pname is not valid in this binder"); 519 return pn; 520 } 521 522 public void setPType(PType pt) { 523 ptype = pt; 524 } 525 526 public boolean supportDynamicComposite() { 527 return false; 528 } 529 530 public boolean supportCompositeField(String fn, PType ft) { 531 return false; 532 } 533 534 public boolean supportStaticComposite() { 535 return false; 536 } 537 538 public void unexport(Object conn, PName pn) throws PException { 539 if (pn.isNull()) { 540 throw new PExceptionProtocol("[" + getClassName() + "]: cannot unexport with a null pname."); 541 } 542 if (bindingcache != null) { 543 PBindingCtrl pb = null; 544 CacheEntry ce = bindingcache.lookup(pn); 545 if (ce != null) { 546 pb = (PBindingCtrl) ce.getCeObject(); 547 } 548 if (pb == null) { 549 throw new PExceptionProtocol("Unauthorized operation: No Pbinding found"); 550 } 551 byte nextstate = PStateGraph.nextStatePBinding( 552 pb.getStatus(), PBinding.ACTION_UNEXPORT); 553 if (nextstate == PBinding.LIFECYCLE_ERROR) { 554 throw new PExceptionProtocol("Unauthorized operation: status =" + pb.getStatus()); 555 } 556 pb.setStatus(nextstate); 557 } else { 558 throw new PExceptionProtocol("Unauthorized operation: No Pbinding can be found without a CacheManager"); 559 } 560 } 561 562 public void unexport(Object conn, PName pn, Object hints) throws PException { 563 if (pn.isNull()) { 564 throw new PExceptionProtocol("[" + getClassName() + "]: cannot unexport with a null pname."); 565 } 566 PBindingCtrl pb = null; 567 if (hints instanceof PBindingCtrl) { 568 pb = (PBindingCtrl) hints; 569 } else if (bindingcache != null) { 570 CacheEntry ce = bindingcache.lookup(pn); 571 if (ce != null) { 572 pb = (PBindingCtrl) ce.getCeObject(); 573 } 574 } 575 if (pb == null) { 576 throw new PExceptionProtocol("Unauthorized operation: No Pbinding found"); 577 } 578 byte nextstate = PStateGraph.nextStatePBinding( 579 pb.getStatus(), PBinding.ACTION_UNEXPORT); 580 if (nextstate == PBinding.LIFECYCLE_ERROR) { 581 throw new PExceptionProtocol("Unauthorized operation: status =" + pb.getStatus()); 582 } 583 pb.setStatus(nextstate); 584 } 585 586 588 public PBinding lookup(PName pn) throws PException { 589 if (pn == null) { 590 throw new PExceptionNaming("[" + getClassName() + "]: this pname is null"); 591 } 592 if (pn.isNull()) { 593 return null; 594 } 595 if (bindingcache == null) { 596 throw new PExceptionProtocol("No internal cache: should be managed externally!"); 597 } 598 CacheEntry ce = bindingcache.lookup(pn); 599 if (ce != null) { 600 return (PBinding) ce.getCeObject(); 601 } 602 return null; 603 } 604 605 public void bind(PName pn, PBindingCtrl pb) throws PException { 606 if (pn == null) 607 throw new PExceptionNaming("[" + getClassName() + "]: this pname is null"); 608 if (pb == null) 609 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null"); 610 if (!(pn instanceof BasidName)) 611 throw new PExceptionNaming("Can only bind CustomerPkPName"); 612 if (!pn.getPNameManager().equals(this)) 613 throw new PExceptionNaming("[" + getClassName() + "]: this pname is not valid in this binder (" 614 + ", \nthis:" + this 615 + ", \nthis.type:" + this.getPType().getJormName() 616 + ", \nthis.pcm:" + this.getBinderClassMapping() 617 + ", \npn.pnc: " + pn.getPNameManager() 618 + ", \npn.pnc.type: " + pn.getPNameManager().getPType().getJormName() 619 + ", \npn.pnc.pcm: " + ((PBinder) pn.getPNameManager()).getBinderClassMapping() 620 + ", \npn:" + pn + ")"); 621 if (!pb.getPClassMapping().equals(pcm)) 622 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder"); 623 if (pn.isNull()) 624 throw new PExceptionNaming("[" + getClassName() + "]: this PName represent the null value"); 625 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 626 PBinding.ACTION_BIND); 627 if (bindingcache != null) { 629 synchronized (bindingcache) { 630 try { 631 bindingcache.fix(bindingcache.bind(pn, pb)); 632 } catch (CacheException e) { 633 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 634 } 635 } 636 } 637 pb.setPName(pn); 638 pb.setStatus(nextstate); 639 } 640 641 public PClassMapping getBinderClassMapping() { 642 return pcm; 643 } 644 645 public void unbind(PBindingCtrl pb) throws PException { 646 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 647 PBinding.ACTION_UNBIND); 648 if (nextstate == pb.getStatus()) 650 return; 651 if (pb == null) 653 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null"); 654 PName pn = pb.getPName(); 655 if (pn == null) 656 throw new PExceptionNaming("[" + getClassName() + "]: this PBinding is not bounded with a pname"); 657 if (!pb.getPClassMapping().equals(pcm)) 658 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder"); 659 if (bindingcache != null) { 660 synchronized (bindingcache) { 661 try { 662 if (((FixableCacheEntry) bindingcache).getCeFixCount() == 0) { 663 pb.setPName(null); 665 pb.setStatus(nextstate); 666 } else { 667 CacheEntry ce = bindingcache.lookup(pn); 669 if (ce != null) { 670 bindingcache.unfix(ce); 671 } 672 } 673 } catch (CacheException e) { 674 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 675 } 676 } 677 } else { 678 pb.setPName(null); 679 pb.setStatus(nextstate); 680 } 681 } 682 683 687 public CacheManager getCacheManager() { 688 return bindingcache; 689 } 690 691 696 public void setCacheManager(CacheManager cm) throws PException { 697 bindingcache = cm; 698 } 699 700 702 public void setPClassMapping(PClassMapping pcm) { 703 this.pcm = pcm; 704 try { 705 switch (codingType) { 706 case CTBYTE: 707 case CTCHAR: 708 case CTINT: 709 case CTLONG: 710 case CTSHORT: 711 PNNULL = new BasidName(nullValue, this); 712 break; 713 default: 714 PNONULL = new BasidName(nullOValue, this); 715 } 716 } catch (PExceptionNaming pn) { 717 System.out.println("Very strange problem: this exception should really never happen!!"); 719 pn.printStackTrace(); 720 } 721 } 722 } 723 | Popular Tags |