1 25 26 package org.objectweb.jorm.naming.lib; 27 28 import java.math.BigDecimal ; 29 import java.math.BigInteger ; 30 import java.util.Date ; 31 32 import org.objectweb.jorm.api.PBinding; 33 import org.objectweb.jorm.api.PClassMapping; 34 import org.objectweb.jorm.api.PException; 35 import org.objectweb.jorm.genclass.lib.GenClassMapping; 36 import org.objectweb.jorm.naming.api.PBinder; 37 import org.objectweb.jorm.naming.api.PExceptionNaming; 38 import org.objectweb.jorm.naming.api.PName; 39 import org.objectweb.jorm.naming.api.PNameManager; 40 import org.objectweb.jorm.naming.api.PolymorphicPName; 41 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext; 42 import org.objectweb.jorm.type.api.PType; 43 import org.objectweb.medor.tuple.api.Tuple; 44 import org.objectweb.medor.tuple.api.TupleCollection; 45 import org.objectweb.perseus.cache.api.CacheEntry; 46 import org.objectweb.perseus.cache.api.CacheManager; 47 import org.objectweb.perseus.persistence.api.ConnectionHolder; 48 import org.objectweb.perseus.persistence.api.WorkingSet; 49 50 57 public class BasicPolymorphicPNamingContext implements PolymorphicPNamingContext { 58 59 62 private PBinder delegatedBinder; 63 64 67 private PClassMapping pcm; 68 69 72 private CacheManager cache; 73 74 77 private PNameManager delegatedPNM; 78 79 public BasicPolymorphicPNamingContext() { 80 delegatedBinder = null; 81 pcm = null; 82 cache = null; 83 delegatedPNM = null; 84 } 85 86 public BasicPolymorphicPNamingContext(PBinder delegatedBinder, 87 PClassMapping pcm, 88 CacheManager cache, 89 PNameManager delegatedPNM) { 90 super(); 91 this.delegatedBinder = delegatedBinder; 92 this.pcm = pcm; 93 this.cache = cache; 94 this.delegatedPNM = delegatedPNM; 95 } 96 97 101 public CacheManager getCache() { 102 return cache; 103 } 104 public void setCache(CacheManager cache) { 105 this.cache = cache; 106 } 107 public PBinder getDelegatedBinder() { 108 return delegatedBinder; 109 } 110 public void setDelegatedBinder(PBinder delegatedBinder) { 111 this.delegatedBinder = delegatedBinder; 112 } 113 public PClassMapping getPcm() { 114 return pcm; 115 } 116 public void setPcm(PClassMapping pcm) { 117 this.pcm = pcm; 118 } 119 120 124 public PName resolve(Object conn, PName pname) throws PException { 125 if(pname.getPNameManager() instanceof PBinder) { 127 return pname; 128 } else { 129 try { 130 PName pnFound = checkCache(pname); 132 if (pnFound != pname) 135 return pnFound; 136 if (!(conn instanceof ConnectionHolder)) 137 throw new PException("The conn parameter of PolymorphicPNC.resolve(conn, pname) must be an instance of ConnectionHolder"); 138 ConnectionHolder connHolder = (ConnectionHolder) conn; 139 WorkingSet ws = (WorkingSet) connHolder.getWorkingSet(); 141 TupleCollection tupleCollection = pcm.getPMapper().getPrefetchCache().getPrefetchTupleCollection(pname, pcm, ws); 143 if (tupleCollection != null && tupleCollection.getMetaData() != null) { 144 return pcm.getDecodedPName(tupleCollection, pname, false); 145 } 146 return pcm.resolve(conn, pname); 148 } catch (Exception e) { 149 throw new PException(e, "Exception while trying to resolve the pname in the BasicAsymetricPNC. " + e.getMessage()); 150 } 151 } 152 } 153 154 public PName export(Object conn, Object infoitem) throws PException { 155 return delegatedBinder.export(conn, infoitem); 157 } 158 159 public PName export(Object conn, Object infoitem, Object hints) throws PException { 160 return delegatedBinder.export(conn, infoitem, hints); 162 } 163 164 public void unexport(Object conn, PName pn) throws PException { 165 delegatedBinder.unexport(conn, pn); 167 } 168 169 public void unexport(Object conn, PName pn, Object hints) throws PException { 170 delegatedBinder.unexport(conn, pn, hints); 172 } 173 174 178 public boolean codingSupported(int codingtype) { 179 return delegatedBinder.codingSupported(codingtype); 181 } 182 183 public PName decode(byte[] en) throws PExceptionNaming { 184 PName pname = delegatedBinder.decode(en); 186 return checkCache(markAsPolymorphic(pname)); 187 } 188 189 public PName decodeAbstract(Object en, Object context) 190 throws PExceptionNaming, UnsupportedOperationException { 191 PName pname = delegatedBinder.decodeAbstract(en, context); 193 return checkCache(markAsPolymorphic(pname)); 194 } 195 196 public PName decodeByte(byte en) throws PExceptionNaming, 197 UnsupportedOperationException { 198 PName pname = delegatedBinder.decodeByte(en); 200 return checkCache(markAsPolymorphic(pname)); 201 } 202 203 public PName decodeObyte(Byte en) throws PExceptionNaming, 204 UnsupportedOperationException { 205 PName pname = delegatedBinder.decodeObyte(en); 207 return checkCache(markAsPolymorphic(pname)); 208 } 209 210 public PName decodeChar(char en) throws PExceptionNaming, 211 UnsupportedOperationException { 212 PName pname = delegatedBinder.decodeChar(en); 214 return checkCache(markAsPolymorphic(pname)); 215 } 216 217 public PName decodeOchar(Character en) throws PExceptionNaming, 218 UnsupportedOperationException { 219 PName pname = delegatedBinder.decodeOchar(en); 221 return checkCache(markAsPolymorphic(pname)); 222 } 223 224 public PName decodeInt(int en) throws PExceptionNaming, 225 UnsupportedOperationException { 226 PName pname = delegatedBinder.decodeInt(en); 228 return checkCache(markAsPolymorphic(pname)); 229 } 230 231 public PName decodeOint(Integer en) throws PExceptionNaming, 232 UnsupportedOperationException { 233 PName pname = delegatedBinder.decodeOint(en); 235 return checkCache(markAsPolymorphic(pname)); 236 } 237 238 public PName decodeLong(long en) throws PExceptionNaming, 239 UnsupportedOperationException { 240 PName pname = delegatedBinder.decodeLong(en); 242 return checkCache(markAsPolymorphic(pname)); 243 } 244 245 public PName decodeOlong(Long en) throws PExceptionNaming, 246 UnsupportedOperationException { 247 PName pname = delegatedBinder.decodeOlong(en); 249 return checkCache(markAsPolymorphic(pname)); 250 } 251 252 public PName decodeShort(short en) throws PExceptionNaming, 253 UnsupportedOperationException { 254 PName pname = delegatedBinder.decodeShort(en); 256 return checkCache(markAsPolymorphic(pname)); 257 } 258 259 public PName decodeOshort(Short en) throws PExceptionNaming, 260 UnsupportedOperationException { 261 PName pname = delegatedBinder.decodeOshort(en); 263 return checkCache(markAsPolymorphic(pname)); 264 } 265 266 public PName decodeString(String en) throws PExceptionNaming { 267 PName pname = delegatedBinder.decodeString(en); 269 return checkCache(markAsPolymorphic(pname)); 270 } 271 272 public PName decodeCharArray(char[] en) throws PExceptionNaming { 273 PName pname = delegatedBinder.decodeCharArray(en); 275 return checkCache(markAsPolymorphic(pname)); 276 } 277 278 public PName decodeDate(Date en) throws PExceptionNaming { 279 PName pname = delegatedBinder.decodeDate(en); 281 return checkCache(markAsPolymorphic(pname)); 282 } 283 284 public PName decodeBigInteger(BigInteger en) throws PExceptionNaming { 285 PName pname = delegatedBinder.decodeBigInteger(en); 287 return checkCache(markAsPolymorphic(pname)); 288 } 289 290 public PName decodeBigDecimal(BigDecimal en) throws PExceptionNaming { 291 PName pname = delegatedBinder.decodeBigDecimal(en); 293 return checkCache(markAsPolymorphic(pname)); 294 } 295 296 public byte[] encode(PName pn) throws PExceptionNaming { 297 return delegatedBinder.encode(pn); 299 } 300 301 public Object encodeAbstract(PName pn) throws PExceptionNaming, 302 UnsupportedOperationException { 303 return delegatedBinder.encodeAbstract(pn); 305 } 306 307 public byte encodeByte(PName pn) throws PExceptionNaming, 308 UnsupportedOperationException { 309 return delegatedBinder.encodeByte(pn); 311 } 312 313 public Byte encodeObyte(PName pn) throws PExceptionNaming, 314 UnsupportedOperationException { 315 return delegatedBinder.encodeObyte(pn); 317 } 318 319 public char encodeChar(PName pn) throws PExceptionNaming, 320 UnsupportedOperationException { 321 return delegatedBinder.encodeChar(pn); 323 } 324 325 public Character encodeOchar(PName pn) throws PExceptionNaming, 326 UnsupportedOperationException { 327 return delegatedBinder.encodeOchar(pn); 329 } 330 331 public int encodeInt(PName pn) throws PExceptionNaming, 332 UnsupportedOperationException { 333 return delegatedBinder.encodeInt(pn); 335 } 336 337 public Integer encodeOint(PName pn) throws PExceptionNaming, 338 UnsupportedOperationException { 339 return delegatedBinder.encodeOint(pn); 341 } 342 343 public long encodeLong(PName pn) throws PExceptionNaming, 344 UnsupportedOperationException { 345 return delegatedBinder.encodeLong(pn); 347 } 348 349 public Long encodeOlong(PName pn) throws PExceptionNaming, 350 UnsupportedOperationException { 351 return delegatedBinder.encodeOlong(pn); 353 } 354 355 public short encodeShort(PName pn) throws PExceptionNaming, 356 UnsupportedOperationException { 357 return delegatedBinder.encodeShort(pn); 359 } 360 361 public Short encodeOshort(PName pn) throws PExceptionNaming, 362 UnsupportedOperationException { 363 return delegatedBinder.encodeOshort(pn); 365 } 366 367 public String encodeString(PName pn) throws PExceptionNaming { 368 return delegatedBinder.encodeString(pn); 370 } 371 372 public char[] encodeCharArray(PName pn) throws PExceptionNaming { 373 return delegatedBinder.encodeCharArray(pn); 375 } 376 377 public Date encodeDate(PName pn) throws PExceptionNaming { 378 return delegatedBinder.encodeDate(pn); 380 } 381 382 public BigInteger encodeBigInteger(PName pn) throws PExceptionNaming { 383 return delegatedBinder.encodeBigInteger(pn); 385 } 386 387 public BigDecimal encodeBigDecimal(PName pn) throws PExceptionNaming { 388 return delegatedBinder.encodeBigDecimal(pn); 390 } 391 392 public PName getNull() { 393 return delegatedBinder.getNull(); 395 } 396 397 public void setNullPName(Object o) throws PException { 398 delegatedBinder.setNullPName(o); 400 } 401 402 public boolean supportDynamicComposite() { 403 return delegatedBinder.supportDynamicComposite(); 405 } 406 407 public boolean supportCompositeField(String fn, PType ft) { 408 return delegatedBinder.supportCompositeField(fn,ft); 410 } 411 412 public boolean supportStaticComposite() { 413 return delegatedBinder.supportStaticComposite(); 415 } 416 417 public PType getPType() { 418 PType type = delegatedBinder.getPType(); 420 if (type == null) { 421 type = pcm.getPType(); 422 delegatedBinder.setPType(type); 423 } 424 return type; 425 } 426 427 public void setPType(PType pt) { 428 delegatedBinder.setPType(pt); 430 } 431 432 433 437 445 private PName checkCache(PName pname) throws PExceptionNaming { 446 PolymorphicPName polymorphicPN = (PolymorphicPName) pname; 447 CacheEntry ce = cache.lookup(pname); 448 if (ce != null) { 449 PName pnFound = (PName) ce.getCeIdentifier(); 451 PBinding pbinding = (PBinding) ce.getCeObject(); 452 PClassMapping pcmFound = pbinding.getPClassMapping(); 454 if ( pcmFound instanceof GenClassMapping) { 455 return pname; 456 } else { 457 try { 458 PClassMapping[] subPCMs = pcm.getSubPCMs(); 459 if (subPCMs != null) { 462 for (int i= 0; i<subPCMs.length; i++) { 463 if (pcmFound == subPCMs[i]) { 464 return pnFound; 466 } 467 } 468 } 469 return pname; 472 } catch (Exception e) { 473 throw new PExceptionNaming(e, 474 "Exception on decode, trying to get all the sub PClassMappings of " 475 + pcm.getClassName() + "." + "Exception nested: " + e.getMessage()); 476 } 477 } 478 } else { 479 return pname; 481 } 482 } 483 484 private PolymorphicPName markAsPolymorphic(PName pn) { 485 PolymorphicPName polymorphicPN = (PolymorphicPName) pn; 486 polymorphicPN.setPolymorphic(true); 487 polymorphicPN.setPNameManager((PNameManager)this); 488 return polymorphicPN; 489 } 490 } 491 | Popular Tags |