1 18 package org.objectweb.speedo.mapper.lib; 19 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 import java.util.regex.Pattern ; 30 31 import javax.jdo.JDOException; 32 import javax.jdo.JDOFatalInternalException; 33 import javax.jdo.JDOUserException; 34 import javax.jdo.datastore.Sequence; 35 import javax.jdo.spi.JDOImplHelper; 36 import javax.jdo.spi.RegisterClassEvent; 37 import javax.jdo.spi.RegisterClassListener; 38 39 import org.objectweb.fractal.api.control.BindingController; 40 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 41 import org.objectweb.fractal.api.control.LifeCycleController; 42 import org.objectweb.jorm.api.PClassMapping; 43 import org.objectweb.jorm.api.PClassMappingCtrl; 44 import org.objectweb.jorm.api.PException; 45 import org.objectweb.jorm.api.PMapCluster; 46 import org.objectweb.jorm.api.PMapper; 47 import org.objectweb.jorm.lib.JormPathHelper; 48 import org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassProp; 49 import org.objectweb.jorm.mapper.rdb.lib.RdbPrefetchablePCM; 50 import org.objectweb.jorm.metainfo.api.Package; 51 import org.objectweb.jorm.metainfo.lib.BasicClass; 52 import org.objectweb.jorm.metainfo.lib.BasicCompositeName; 53 import org.objectweb.jorm.metainfo.lib.JormManager; 54 import org.objectweb.jorm.naming.api.PBinder; 55 import org.objectweb.jorm.naming.api.PExceptionNaming; 56 import org.objectweb.jorm.naming.api.PName; 57 import org.objectweb.jorm.naming.api.PNameCoder; 58 import org.objectweb.jorm.naming.api.PNameManager; 59 import org.objectweb.jorm.naming.api.PNamingContext; 60 import org.objectweb.jorm.naming.lib.KFPNCManager; 61 import org.objectweb.jorm.type.api.PType; 62 import org.objectweb.perseus.cache.api.CacheManager; 63 import org.objectweb.perseus.persistence.api.ConnectionHolder; 64 import org.objectweb.perseus.persistence.api.TransactionalPersistenceManager; 65 import org.objectweb.speedo.api.SpeedoProperties; 66 import org.objectweb.speedo.genclass.SpeedoGenClassHome; 67 import org.objectweb.speedo.generation.lib.NamingRules; 68 import org.objectweb.speedo.mapper.api.JormFactory; 69 import org.objectweb.speedo.mapper.api.JormFactoryAttributes; 70 import org.objectweb.speedo.mim.api.SpeedoHome; 71 import org.objectweb.speedo.naming.api.NamingManager; 72 import org.objectweb.speedo.naming.lib.NamingManagerFactory; 73 import org.objectweb.speedo.naming.lib.NamingManagerHelper; 74 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 75 import org.objectweb.speedo.tools.StringReplace; 76 import org.objectweb.util.monolog.api.BasicLevel; 77 import org.objectweb.util.monolog.api.Logger; 78 import org.objectweb.util.monolog.api.LoggerFactory; 79 80 88 public class BasicJormFactory 89 implements JormFactory, 90 PNameCoder, 91 BindingController, 92 LifeCycleController, 93 JormFactoryAttributes, 94 RegisterClassListener { 95 96 99 public final static String MAPPER_BINDING = "mapper"; 100 103 public final static String CACHE_MANAGER_BINDING = "cache-manager"; 104 public final static String TPM_BINDING = "transactional-persistence-manager"; 105 public final static String PMF_BINDING = "proxy-manager-factory"; 106 107 protected TransactionalPersistenceManager tpm; 108 109 protected ProxyManagerFactory pmf; 110 111 114 protected PMapper mapper = null; 115 116 119 protected CacheManager cache = null; 120 121 125 protected Map binders = null; 126 127 131 protected Map pnamingContexts = null; 132 135 protected Map cn2pcm = null; 136 137 140 private NamingManagerFactory nmf; 141 142 145 private byte mappingStructuresRule = JormFactoryAttributes.CREATE_IF_REQUIRED; 146 147 protected Logger logger = null; 148 149 private KFPNCManager kfpncManager; 150 151 private boolean mappingOnClassRegistration = false; 152 153 private Properties speedoProperties; 154 155 159 public BasicJormFactory() { 160 pnamingContexts = new HashMap (); 161 binders = new HashMap (); 162 cn2pcm = new HashMap (); 163 nmf = new NamingManagerFactory(); 164 kfpncManager = new KFPNCManager(pnamingContexts); 165 } 166 167 170 public byte getMappingStructureRule() { 171 return mappingStructuresRule; 172 } 173 174 public void setMappingStructureRule(byte rule) { 175 mappingStructuresRule = rule; 176 } 177 178 public boolean getMappingOnClassRegistration() { 179 return mappingOnClassRegistration; 180 } 181 182 public void setMappingOnClassRegistration(boolean val) { 183 mappingOnClassRegistration = val; 184 } 185 186 189 public String [] listFc() { 190 return new String []{ 191 MAPPER_BINDING, 192 CACHE_MANAGER_BINDING, 193 TPM_BINDING, 194 PMF_BINDING 195 }; 196 } 197 198 public Object lookupFc(String s) { 199 if (MAPPER_BINDING.equals(s)) 200 return mapper; 201 else if (CACHE_MANAGER_BINDING.equals(s)) 202 return cache; 203 else if (TPM_BINDING.equals(s)) 204 return tpm; 205 else if (PMF_BINDING.equals(s)) 206 return pmf; 207 else 208 return null; 209 } 210 211 public void bindFc(String s, Object o) { 212 if ("logger".equals(s)) { 213 logger = (Logger) o; 214 } else if ("monolog-factory".equals(s)) { 215 nmf.setLogger(((LoggerFactory) o).getLogger(logger.getName() + ".naming-mmanager-factory")); 216 kfpncManager.setLogger(((LoggerFactory) o).getLogger(logger.getName() + ".kfpncmanager")); 217 } else if (MAPPER_BINDING.equals(s)) { 218 mapper = (PMapper) o; 219 } else if (CACHE_MANAGER_BINDING.equals(s)) { 220 cache = (CacheManager) o; 221 } else if (TPM_BINDING.equals(s)) { 222 tpm = (TransactionalPersistenceManager) o; 223 } else if (PMF_BINDING.equals(s)) { 224 pmf = (ProxyManagerFactory) o; 225 } 226 } 227 228 public void unbindFc(String s) { 229 if (MAPPER_BINDING.equals(s)) { 230 mapper = null; 231 nmf.setMapper(null); 232 } else if (CACHE_MANAGER_BINDING.equals(s)) { 233 cache = null; 234 } else if (TPM_BINDING.equals(s)) { 235 tpm = null; 236 } else if (PMF_BINDING.equals(s)) { 237 pmf = null; 238 nmf.setPmf(null); 239 } 240 } 241 242 private boolean started = false; 243 244 public String getFcState() { 245 return started ? STARTED : STOPPED; 246 } 247 248 public void startFc() throws IllegalLifeCycleException { 249 if (!started) { 250 mapper.addMapperEventListener(kfpncManager); 251 nmf.setMapper(mapper); 252 nmf.setCache(cache); 253 nmf.setPmf(pmf); 254 JDOImplHelper.getInstance().addRegisterClassListener(this); 255 started = true; 256 } 257 } 258 259 public void stopFc() throws IllegalLifeCycleException { 260 if (started) { 261 mapper.removeMapperEventListener(kfpncManager); 262 JDOImplHelper.getInstance().removeRegisterClassListener(this); 263 started = false; 264 } 265 } 266 267 270 public PBinder getPBinder(Class clazz) throws PException { 271 String clName = clazz.getName(); 272 PClassMapping pcm = mapper.lookup(clName); 273 if (pcm != null) 274 return pcm.getPBinder(); 275 return getPClassMapping(clazz).getPBinder(); 276 } 277 278 public PBinder getPBinder(String classname, ClassLoader cl) throws PException { 279 PClassMapping pcm = mapper.lookup(classname); 280 if (pcm != null) 281 return pcm.getPBinder(); 282 return getPClassMapping(getClass(classname, cl)).getPBinder(); 283 } 284 285 public ClassLoader getClassLoader(String className) { 286 Object o = mapper.lookup(className); 287 if (o == null) { 288 return null; 289 } 290 return getClassLoader(o.getClass()); 291 } 292 293 public PClassMapping getGenClassMapping(String path) { 294 return JormPathHelper.getPClassMapping(path, mapper); 295 } 296 297 public PClassMapping getPClassMapping(Class clazz) throws PException { 298 PClassMapping pcm = mapper.lookup(clazz.getName()); 299 if (pcm == null) { 300 pcm = getPClassMapping(clazz.getName(), 301 clazz.getClassLoader()); 302 } 303 return pcm; 304 } 305 306 316 public PNamingContext getPNamingContext(String classname, ClassLoader cl) throws PException { 317 return getPNamingContext(getClass(classname, cl)); 318 } 319 320 public PNamingContext getPNamingContext(Class clazz) throws PException { 321 PClassMapping pcm = getPClassMapping(clazz); 322 String className = clazz.getName(); 323 String path = JormPathHelper.getPath(className); 324 Properties classProperties = getClassProperties(clazz); 325 String jormConf = classProperties.getProperty(path); 326 if (jormConf != null) { 327 return (PNamingContext) findPNameManager( 328 className, clazz.getClassLoader(), pcm, jormConf); 329 } else { 330 throw new PException( 331 "Impossible to find the PNamingContext for the class " 332 + className); 333 } 334 } 335 336 public Properties getSpeedoProperties() { 337 return speedoProperties; 338 } 339 340 HashMap pattern2prop; 341 public void setSpeedoProperties(Properties p) { 342 speedoProperties = p; 343 if (speedoProperties == null) { 344 return; 345 } 346 for(Iterator it=speedoProperties.keySet().iterator(); it.hasNext();) { 347 String key = (String ) it.next(); 348 if (key.indexOf('*') != -1 || key.indexOf('?') != -1) { 349 if (logger != null) { 351 logger.log(BasicLevel.DEBUG, "Pattern " + key); 352 } 353 Pattern pa = Pattern.compile(StringReplace.toJavaPattern(key)); 354 if (pattern2prop == null) { 355 pattern2prop = new HashMap (); 356 } 357 pattern2prop.put(pa, key); 358 } 359 } 360 } 361 362 365 368 public void registerClass(RegisterClassEvent event) { 369 if (mappingOnClassRegistration) { 370 try { 371 getPClassMapping(event.getRegisteredClass()); 372 } catch (PException e) { 373 throw new JDOException( 374 "Impossible to initialize the persistent class '" 375 + event.getRegisteredClass().getName() + "':", e); 376 } 377 } 378 } 379 380 381 public boolean codingSupported(int codingtype) { 384 return (codingtype & CTCOMPOSITE) != 0; 385 } 386 387 public PName decode(byte[] en) throws PExceptionNaming { 388 return null; 389 } 390 391 public PName decodeAbstract(Object oid, Object context) 392 throws PExceptionNaming, UnsupportedOperationException { 393 Class clazz = null; 394 ConnectionHolder conn = null; 395 if (context != null) { 396 if (!(context instanceof ConnectionHolder)) { 397 clazz = (Class ) context; 398 } else { 399 conn = (ConnectionHolder) context; 400 } 401 } 402 if (oid instanceof PName) { 403 try { 404 return ((PName) oid).resolve(conn); 405 } catch (PException e) { 406 throw new JDOUserException("Impossible to decode the identifier " 407 + oid + (clazz == null 408 ? "" : " of the class " + clazz.getName()), e); 409 } 410 } 411 PBinder binder = null; 412 PName pn = null; 413 try { 414 if (clazz != null) { 415 binder = getPBinder(clazz); 416 } 417 pn = nmf.decode(binder, oid, clazz, this); 418 } catch (PException e) { 419 throw new JDOUserException("Impossible to decode the identifier " 420 + oid + (clazz == null 421 ? "" : " of the class " + clazz.getName()), e); 422 } 423 if (pn != null) { 424 return pn; 425 } 426 if (binder != null) { 427 return binder.decodeAbstract(oid, null); 428 } 429 throw new JDOUserException("Impossible to decode the identifier " 430 + oid + (clazz == null 431 ? "" : " of the class " + clazz.getName())); 432 } 433 434 public PName decodeByte(byte en) throws PExceptionNaming, UnsupportedOperationException { 435 return decodeAbstract(new Byte (en), null); 436 } 437 438 public PName decodeObyte(Byte en) throws PExceptionNaming, UnsupportedOperationException { 439 return decodeAbstract(en, null); 440 } 441 442 public PName decodeChar(char en) throws PExceptionNaming, UnsupportedOperationException { 443 return decodeAbstract(new Character (en), null); 444 } 445 446 public PName decodeOchar(Character en) throws PExceptionNaming, UnsupportedOperationException { 447 return decodeAbstract(en, null); 448 } 449 450 public PName decodeInt(int en) throws PExceptionNaming, UnsupportedOperationException { 451 return decodeAbstract(new Integer (en), null); 452 } 453 454 public PName decodeOint(Integer en) throws PExceptionNaming, UnsupportedOperationException { 455 return decodeAbstract(en, null); 456 } 457 458 public PName decodeLong(long en) throws PExceptionNaming, UnsupportedOperationException { 459 return decodeAbstract(new Long (en), null); 460 } 461 462 public PName decodeOlong(Long en) throws PExceptionNaming, UnsupportedOperationException { 463 return decodeAbstract(en, null); 464 } 465 466 public PName decodeShort(short en) throws PExceptionNaming, UnsupportedOperationException { 467 return decodeAbstract(new Short (en), null); 468 } 469 470 public PName decodeOshort(Short en) throws PExceptionNaming, UnsupportedOperationException { 471 return decodeAbstract(en, null); 472 } 473 474 public PName decodeString(String en) throws PExceptionNaming { 475 return decodeAbstract(en, null); 476 } 477 478 public PName decodeCharArray(char[] en) throws PExceptionNaming { 479 return decodeAbstract(en, null); 480 } 481 482 public PName decodeDate(Date en) throws PExceptionNaming { 483 return decodeAbstract(en, null); 484 } 485 486 public PName decodeBigInteger(BigInteger en) throws PExceptionNaming { 487 return decodeAbstract(en, null); 488 } 489 490 public PName decodeBigDecimal(BigDecimal en) throws PExceptionNaming { 491 return decodeAbstract(en, null); 492 } 493 494 public byte[] encode(PName pn) throws PExceptionNaming { 495 return pn.encode(); 496 } 497 498 public Object encodeAbstract(PName pn) throws PExceptionNaming, UnsupportedOperationException { 499 try { 500 return nmf.encode(pn); 501 } catch (PException e) { 502 throw new JDOUserException( 503 "Impossible to encode the identifier :" + pn); 504 } 505 } 506 507 public byte encodeByte(PName pn) throws PExceptionNaming, UnsupportedOperationException { 508 throw new UnsupportedOperationException ("Impossible to encode to a byte"); 509 } 510 511 public Byte encodeObyte(PName pn) throws PExceptionNaming, UnsupportedOperationException { 512 throw new UnsupportedOperationException ("Impossible to encode to a Byte"); 513 } 514 515 public char encodeChar(PName pn) throws PExceptionNaming, UnsupportedOperationException { 516 throw new UnsupportedOperationException ("Impossible to encode to a char"); 517 } 518 519 public Character encodeOchar(PName pn) throws PExceptionNaming, UnsupportedOperationException { 520 throw new UnsupportedOperationException ("Impossible to encode to a Character"); 521 } 522 523 public int encodeInt(PName pn) throws PExceptionNaming, UnsupportedOperationException { 524 throw new UnsupportedOperationException ("Impossible to encode to a int"); 525 } 526 527 public Integer encodeOint(PName pn) throws PExceptionNaming, UnsupportedOperationException { 528 throw new UnsupportedOperationException ("Impossible to encode to a Integer"); 529 } 530 531 public long encodeLong(PName pn) throws PExceptionNaming, UnsupportedOperationException { 532 throw new UnsupportedOperationException ("Impossible to encode to a long"); 533 } 534 535 public Long encodeOlong(PName pn) throws PExceptionNaming, UnsupportedOperationException { 536 throw new UnsupportedOperationException ("Impossible to encode to a Long"); 537 } 538 539 public short encodeShort(PName pn) throws PExceptionNaming, UnsupportedOperationException { 540 throw new UnsupportedOperationException ("Impossible to encode to a short"); 541 } 542 543 public Short encodeOshort(PName pn) throws PExceptionNaming, UnsupportedOperationException { 544 throw new UnsupportedOperationException ("Impossible to encode to a Short"); 545 } 546 547 public String encodeString(PName pn) throws PExceptionNaming { 548 throw new UnsupportedOperationException ("Impossible to encode to a String"); 549 } 550 551 public char[] encodeCharArray(PName pn) throws PExceptionNaming { 552 throw new UnsupportedOperationException ("Impossible to encode to a char[]"); 553 } 554 555 public Date encodeDate(PName pn) throws PExceptionNaming { 556 throw new UnsupportedOperationException ("Impossible to encode to a Date"); 557 } 558 559 public BigInteger encodeBigInteger(PName pn) throws PExceptionNaming { 560 throw new UnsupportedOperationException ("Impossible to encode to a BigInteger"); 561 } 562 563 public BigDecimal encodeBigDecimal(PName pn) throws PExceptionNaming { 564 throw new UnsupportedOperationException ("Impossible to encode to a BigDecimal"); 565 } 566 567 public PName getNull() { 568 return null; 569 } 570 571 public void setNullPName(Object o) throws PException { 572 } 573 574 public boolean supportDynamicComposite() { 575 return false; 576 } 577 578 public boolean supportCompositeField(String fn, PType ft) { 579 return false; 580 } 581 582 public boolean supportStaticComposite() { 583 return false; 584 } 585 586 public PType getPType() { 587 return null; 588 } 589 590 public void setPType(PType pt) { 591 } 592 593 596 597 607 private Properties getClassProperties(Class clazz) throws PException { 608 PClassMapping pcm = getPClassMapping( 609 clazz.getName(), clazz.getClassLoader()); 610 return ((SpeedoHome) pcm).getClassProperties(); 611 } 612 613 631 public synchronized PClassMapping getPClassMapping(String className, 632 ClassLoader classLoader) 633 throws PException { 634 boolean debug = logger.isLoggable(BasicLevel.DEBUG); 635 SpeedoHome pcm = (SpeedoHome) cn2pcm.get(className); 636 if (pcm != null) { 637 if (debug) 638 logger.log(BasicLevel.DEBUG, "PClassMapping for class " + className 639 + " is being configured:" + pcm); 640 return pcm; 641 } 642 643 pcm = (SpeedoHome) mapper.lookup(className); 644 if (pcm != null) { 645 if (debug) 646 logger.log(BasicLevel.DEBUG, "PClassMapping for class " + className 647 + " already exist:" + pcm); 648 return pcm; 649 } 650 651 if (debug) { 653 logger.log(BasicLevel.DEBUG, 654 "Looking for the PClassMapping of the class " + className); 655 } 656 String pcmcn = NamingRules.fqMappingName(className); 657 try { 658 pcm = (SpeedoHome) classLoader.loadClass(pcmcn).newInstance(); 659 } catch (Exception e) { 660 throw new PException(e, 661 "Impossible to instanciate the PClassMapping of the class " 662 + className + ": " + pcmcn); 663 } 664 pcm.setTransactionalPersistenceManager(tpm); 665 pcm.setProxyManagerFactory(pmf); 666 Properties classProperties = pcm.getClassProperties(); 667 if (debug) { 668 logger.log(BasicLevel.DEBUG, "Jorm config:" + classProperties); 669 } 670 loadJormSpeedoMI(className, classProperties, classLoader); 672 673 PBinder binder = findPBinder(className, classLoader, 675 classProperties.getProperty(JormPathHelper.getPath(className))); 676 pcm.setPBinder(binder); 677 binder.setPClassMapping(pcm); 678 679 PNameCoder classPnc = findPNameManager( 681 className, classLoader, pcm, classProperties.getProperty(JormPathHelper.getPath(className)) 682 ); 683 ((PClassMappingCtrl) pcm).setClassPNameCoder(classPnc); 684 RefConfig refConfig = new RefConfig(classProperties, classLoader); 686 cn2pcm.put(className, pcm); pcm.configureRefFields(refConfig); 688 cn2pcm.remove(className); 689 if (debug) { 690 logger.log(BasicLevel.DEBUG, "PClassMapping/PBinder/PNC created for the class " + className); 691 logger.log(BasicLevel.DEBUG, "- pcm: " + pcm); 692 logger.log(BasicLevel.DEBUG, "- binder: " + binder); 693 logger.log(BasicLevel.DEBUG, "-pnc: " + classPnc); 694 } 695 696 applySpeedoProperties(pcm); 697 698 mapper.map(null, pcm); 700 pcm.configureRefFields(new GCMHomeConfig(pcm)); 701 logger.log(BasicLevel.DEBUG, "Class " + className + " mapped"); 702 PMapCluster cluster = mapper.getPMappingStructuresManager().getPMapCluster(className); 704 if (cluster.isDefined()) { if (mappingStructuresRule >= CREATE_IF_REQUIRED) { 706 if (mappingStructuresRule == FORCE_CREATE) { 707 cluster.deleteMappingStructures(); 708 } 709 cluster.createMappingStructures( 710 mappingStructuresRule == FORCE_CREATE); 711 if (mappingStructuresRule == DELETE_DATA) { 712 cluster.deleteData(); 713 } 714 } 715 if (logger.isLoggable(BasicLevel.INFO)) { 716 String msr=null; 717 switch(mappingStructuresRule){ 718 case CREATE_IF_REQUIRED: msr = "CREATE_IF_REQUIRED"; break; 719 case FORCE_CREATE: msr = "FORCE_CREATE"; break; 720 case DELETE_DATA: msr = "DELETE_DATA"; break; 721 case DO_NOTHING: msr = "DO_NOTHING"; break; 722 } 723 logger.log(BasicLevel.INFO, "Classes " 724 + cluster.getClusterClasses() 725 + " initialized (" + msr + ")."); 726 } 727 } else { getPClassMapping((String ) cluster 730 .getUnResolvedDependencies().iterator().next(), 731 classLoader); 732 } 733 return pcm; 734 } 735 736 744 private Class getClass(String classname, ClassLoader cl) throws PException { 745 if (cl == null) { 746 throw new PException("Impossible to load the class " 747 + classname + " without a classLoader(" + cl + ")"); 748 } 749 try { 750 return cl.loadClass(classname); 751 } catch (Exception e) { 752 throw new PException(e, "Impossible to load the class '" 753 + classname + "' with the class loader :" + cl); 754 } 755 } 756 757 771 public synchronized PBinder findPBinder(String className, 772 ClassLoader classLoader, 773 String hints) throws PException { 774 if (hints == null || hints.length() == 0) 775 throw new JDOFatalInternalException( 776 "Impossible to get the PNamingContext of the class " + className 777 + ": Specified binder class is not valid: " + hints); 778 PBinder binder = (PBinder) binders.get(className); 779 if (binder != null) 780 return binder; 781 binder = nmf.getNamingManager(hints, classLoader) 782 .getPBinder(className, hints, classLoader, 783 mappingStructuresRule, binders, pnamingContexts); 784 binders.put(className, binder); 786 return binder; 787 } 788 789 806 protected synchronized PNameManager findPNameManager(String className, 807 ClassLoader classLoader, 808 PClassMapping pcm, 809 String hints 810 ) throws PException { 811 if (hints == null || hints.length() == 0) 812 throw new JDOFatalInternalException( 813 "Impossible to get the PNamingContext of the class " + className 814 + ": Specified PNameManager class is not valid: " + hints); 815 PNameManager pnm = (PNameManager) pnamingContexts.get(className); 816 if (pnm != null && (hints.indexOf(NamingManagerHelper.POLYMORPHIC_PNC) == -1)) 817 return pnm; 818 NamingManager nm = nmf.getNamingManager(hints, classLoader); 819 if (nm.supportPNamingcontext()) { 820 pnm = nm.getPNamingContext(className, hints, classLoader, 821 mappingStructuresRule, binders, pnamingContexts, 822 mapper.getMetaInfoManager(), pcm); 823 } else { 824 pnm = findPBinder(className, classLoader, hints); 825 } 826 pnamingContexts.put(className, pnm); 827 return pnm; 828 } 829 830 834 private class RefConfig implements PClassMapping.ReferenceConfigurator { 835 836 841 private Map classProperties = null; 842 843 849 private ClassLoader classLoader = null; 850 851 public RefConfig(Map _classProperties, 852 ClassLoader cl) { 853 this.classProperties = _classProperties; 854 this.classLoader = cl; 855 } 856 857 858 861 public PNameCoder getPNameCoder(String sourceclassName, 862 String refFieldName, 863 String destclassName) { 864 try { 865 PNamingContext pnc = getPNamingContext(destclassName, classLoader); 866 if (logger.isLoggable(BasicLevel.DEBUG)) 867 logger.log(BasicLevel.DEBUG, 868 "Assign the PNamingContext for the field " + sourceclassName 869 + "." + refFieldName + " which references the class " 870 + destclassName + ": " + pnc); 871 logger.log(BasicLevel.DEBUG, 872 "getPNameManager(" + destclassName + ") ==>" + pnc); 873 return pnc; 874 } catch (PException e) { 875 throw new JDOFatalInternalException( 876 "ERROR during the assignation of the PNameManager of the reference " 877 + refFieldName + " of the class " + sourceclassName, e); 878 } 879 } 880 881 public PNameCoder getPNameCoder(String sourceclassName, 882 String refFieldName, 883 String [] genClassNames) { 884 String path = JormPathHelper.getPath( 885 sourceclassName, refFieldName, genClassNames); 886 String hints = (String ) classProperties.get(path); 887 try { 888 PNamingContext pnc = (PNamingContext) findPNameManager(path, classLoader, null, hints); 889 logger.log(BasicLevel.DEBUG, 890 "getPNameManager(" + path + ") ==>" + pnc); 891 return pnc; 892 } catch (PException e) { 893 throw new JDOFatalInternalException(e.getMessage(), e); 894 } 895 } 896 897 public PClassMapping getGenClassMapping(String sourceclassName, 898 String refFieldName, 899 String [] genClassNames) { 900 901 String cn = JormPathHelper.getPath( 902 sourceclassName, refFieldName, genClassNames); 903 String hints = (String ) classProperties.get(cn); 904 logger.log(BasicLevel.DEBUG, "getGenClassMapping(" + cn + ")"); 905 logger.log(BasicLevel.DEBUG, "\thints=" + hints); 906 try { 907 PClassMapping gcm = (PClassMapping) 908 Class.forName(getGCMClassName(mapper.getMapperName())).newInstance(); 909 logger.log(BasicLevel.DEBUG, "\tgcm=" + gcm); 910 PBinder pb = findPBinder(cn, classLoader,hints); 911 logger.log(BasicLevel.DEBUG, "\tbinder=" + pb); 912 gcm.setPBinder(pb); 913 pb.setPClassMapping(gcm); 914 return gcm; 915 } catch (Exception e) { 916 throw new JDOFatalInternalException(e.getMessage(), e); 917 } 918 } 919 920 public PClassMapping getGenClassMapping(String sourceclassName, 921 String refFieldName, 922 String [] genClassNames, 923 String destclassName) { 924 String cn = JormPathHelper.getPath( 925 sourceclassName, refFieldName, genClassNames); 926 String hints = (String ) classProperties.get(cn); 927 try { 928 PClassMapping gcm = (PClassMapping) 929 Class.forName(getGCMClassName(mapper.getMapperName())).newInstance(); 930 logger.log(BasicLevel.DEBUG, "getGenClassMapping(" + cn + ")"); 931 logger.log(BasicLevel.DEBUG, "\tgcm=" + gcm); 932 PBinder pb = findPBinder(cn, classLoader, hints); 933 logger.log(BasicLevel.DEBUG, "\tbinder=" + pb); 934 gcm.setPBinder(pb); 935 pb.setPClassMapping(gcm); 936 PNamingContext pnc = getPNamingContext(destclassName, classLoader); 937 logger.log(BasicLevel.DEBUG, "\tpnc=" + pnc); 938 ((PClassMappingCtrl) gcm).setPNameCoder(pnc); 939 String v = getSpeedoProperties( 940 SpeedoProperties.PREFETCH_ON_GENCLASS 941 + "(" + sourceclassName + "#" + refFieldName + ")"); 942 boolean collectionPrefetching = v == null || !isFalse(v); 943 if (collectionPrefetching && gcm instanceof RdbGenClassProp) { 944 PClassMapping pcm = getPClassMapping(destclassName, classLoader); 945 if (pcm instanceof RdbPrefetchablePCM) { 946 ((RdbGenClassProp) gcm).setPrefetchElementPCM((RdbPrefetchablePCM) pcm); 947 } 948 } 949 950 return gcm; 951 } catch (Exception e) { 952 throw new JDOFatalInternalException(e.getMessage(), e); 953 } 954 } 955 } 956 private boolean isFalse(String val) { 957 return "false".equalsIgnoreCase(val) 958 || "off".equalsIgnoreCase(val) 959 || "no".equalsIgnoreCase(val) 960 || "non".equalsIgnoreCase(val) 961 || "n".equalsIgnoreCase(val); 962 } 963 private class GCMHomeConfig implements PClassMapping.ReferenceConfigurator { 964 PClassMapping pcm; 965 966 GCMHomeConfig(PClassMapping _pcm) { 967 this.pcm = _pcm; 968 } 969 public PNameCoder getPNameCoder(String sourceclassName, 970 String refFieldName, 971 String destclassName) { 972 return pcm.getPNameCoder(refFieldName); 973 } 974 public PNameCoder getPNameCoder(String sourceclassName, 975 String refFieldName, 976 String [] genClassNames) { 977 return pcm.getPNameCoder(refFieldName); 978 } 979 public PClassMapping getGenClassMapping(String sourceclassName, 980 String refFieldName, 981 String [] genClassNames) { 982 PClassMapping gcm = pcm.getGenClassMapping(refFieldName); 983 gcm = new SpeedoGenClassHome(gcm, tpm, pmf, 984 sourceclassName + "#" + refFieldName); 985 gcm.getPBinder().setPClassMapping(gcm); 986 logger.log(BasicLevel.DEBUG, "Insert the home=" + gcm); 987 applySpeedoProperties((SpeedoHome) gcm); 988 return gcm; 989 } 990 public PClassMapping getGenClassMapping(String sourceclassName, 991 String refFieldName, 992 String [] genClassNames, 993 String destclassName) { 994 PClassMapping gcm = pcm.getGenClassMapping(refFieldName); 995 gcm = new SpeedoGenClassHome(gcm, tpm, pmf, 996 sourceclassName + "#" + refFieldName); 997 gcm.getPBinder().setPClassMapping(gcm); 998 logger.log(BasicLevel.DEBUG, "Insert the home=" + gcm); 999 applySpeedoProperties((SpeedoHome) gcm); 1000 return gcm; 1001 } 1002 } 1003 1004 1016 private void loadJormSpeedoMI(String className, 1017 Properties classProperties, 1018 ClassLoader classLoader) { 1019 JormManager m = (JormManager) mapper.getMetaInfoManager(); 1020 synchronized (m) { 1021 if (m.getClass(className) != null) { 1022 return; 1023 } 1024 Set mos = null; 1026 String fn = classProperties.getProperty(Object2StringSerializer.JDO_FILE_NAME_PROP); 1027 try { 1028 mos = (Set ) Object2StringSerializer.deserialize(fn, classLoader, logger); 1029 } catch (JDOException e) { 1031 throw e; 1032 } catch (Exception e) { 1033 throw new JDOException("Impossible to load the jorm meta " + 1034 "information for the class '" + className + "' from the file '" 1035 + fn + "' (You must use the same Speedo version for the" 1036 + " enhancement and for the runtime): ", e); 1037 } 1038 1039 for (Iterator it = mos.iterator(); it.hasNext();) { 1041 Object o = it.next(); 1042 if (o instanceof BasicClass) { 1043 BasicClass c = (BasicClass) o; 1044 Package s = m.createPackage(((Package ) c.getParent()).getName()); 1045 s.addClass(c); 1046 c.setParent(s); 1047 c.setLogger(m.getLogger()); 1048 logger.log(BasicLevel.DEBUG, 1049 "Jorm Meta Object Class " + c.getFQName() + " loaded"); 1050 } else if (o instanceof BasicCompositeName) { 1051 BasicCompositeName cn = (BasicCompositeName) o; 1052 Package s = m.createPackage(((Package ) cn.getParent()).getName()); 1053 s.addCompositeName(cn); 1054 cn.setLogger(m.getLogger()); 1055 logger.log(BasicLevel.DEBUG, 1056 "Jorm Meta Object CompositeName " + cn.getFQName() + " loaded"); 1057 } else if (o instanceof Sequence) { 1058 Sequence s = (Sequence) o; 1059 pmf.getSequenceManager().addSequence(s); 1061 logger.log(BasicLevel.DEBUG, 1062 "Speedo Meta Object Sequence " + s.getName() + " loaded"); 1063 } else 1064 throw new JDOException("Umanaged Jorm/Speedo Meta Object " + o); 1065 } 1066 if (m.getClass(className) == null) { 1067 throw new JDOException( 1068 "Internal ERROR: No meta information found about the persistent class '" 1069 + className + "' in the file '" + fn + "'."); 1070 } 1071 } 1072 } 1073 1074 1079 protected String getGCMClassName(String mapperName) { 1080 if (mapperName.startsWith("rdb")) { 1081 return "org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping"; 1082 } else { 1083 throw new JDOFatalInternalException("Umanaged mapper: " + mapperName); 1084 } 1085 } 1086 1087 1094 private ClassLoader getClassLoader(Class clazz) { 1095 ClassLoader cl = clazz.getClassLoader(); 1096 if (cl == null) { 1097 cl = this.getClass().getClassLoader(); 1098 if (cl == null) { 1099 cl = ClassLoader.getSystemClassLoader(); 1100 logger.log(BasicLevel.DEBUG, 1101 "Use the system class loader for the class '" 1102 + clazz.getName() + "': " + cl); 1103 } else { 1104 logger.log(BasicLevel.DEBUG, 1105 "Use the Speedo class loader for the class '" 1106 + clazz.getName() + "': " + cl); 1107 } 1108 } else { 1109 logger.log(BasicLevel.DEBUG, 1110 "Use the Application class loader for the class '" 1111 + clazz.getName() + "': " + cl); 1112 } 1113 return cl; 1114 } 1115 1116 private String getSpeedoProperties(String s) { 1117 String v = speedoProperties.getProperty(s); 1119 if (v == null && pattern2prop != null) { 1120 for(Iterator it = pattern2prop.entrySet().iterator(); it.hasNext();) { 1122 Map.Entry me = (Map.Entry ) it.next(); 1123 Pattern pa = (Pattern ) me.getKey(); 1124 if (pa.matcher(s).matches()) { 1125 String prop = (String ) me.getValue(); 1126 String val = speedoProperties.getProperty(prop); 1127 if (logger.isLoggable(BasicLevel.DEBUG)) { 1128 logger.log(BasicLevel.DEBUG, "Property '" + s 1129 + "' matches the pattern '" + prop 1130 + "', value=" + val); 1131 } 1132 return val; 1133 } 1134 } 1135 } 1136 return v; 1137 } 1138 1139 private void applySpeedoProperties(SpeedoHome sh) { 1140 if (speedoProperties == null || speedoProperties.size()==0) { 1141 return; 1142 } 1143 String path = sh.getPath(); 1144 String v = getSpeedoProperties( 1146 SpeedoProperties.CACHE_CLASS_POLICY + "(" + path + ")"); 1147 if (v != null) { 1148 if (logger.isLoggable(BasicLevel.INFO)) { 1149 logger.log(BasicLevel.INFO, "Use the caching policy '" + v 1150 + "' for the class '" + path); 1151 } 1152 if (SpeedoProperties.CACHE_CLASS_POLICY_NOCACHE.equalsIgnoreCase(v)) { 1153 sh.setCachePolicy(SpeedoHome.NO_CACHE); 1154 } else if (SpeedoProperties.CACHE_CLASS_POLICY_CACHED.equalsIgnoreCase(v)) { 1155 sh.setCachePolicy(SpeedoHome.CACHED); 1156 } else if (SpeedoProperties.CACHE_CLASS_POLICY_FIXED.equalsIgnoreCase(v)) { 1157 sh.setCachePolicy(SpeedoHome.FIXED); 1158 } else if (SpeedoProperties.CACHE_CLASS_POLICY_ALL.equalsIgnoreCase(v)) { 1160 sh.setCachePolicy(SpeedoHome.ALL); 1161 } else { 1163 logger.log(BasicLevel.WARN, "Policy '" + v 1164 + "' specified for the class '" + path 1165 + "' is not managed."); 1166 } 1167 } 1168 1169 v = getSpeedoProperties( 1171 SpeedoProperties.TRANSACTION_LOCKING_LEVEL+ "(" + path + ")"); 1172 if (v != null) { 1173 if (logger.isLoggable(BasicLevel.INFO)) { 1174 logger.log(BasicLevel.INFO, "Use the locking level '" + v 1175 + "' for the class '" + path); 1176 } 1177 if (SpeedoProperties.TRANSACTION_LOCKING_LEVEL_FIELD.equalsIgnoreCase(v)) { 1178 sh.setFieldLockingLevel(true); 1179 } else if (SpeedoProperties.TRANSACTION_LOCKING_LEVEL_INSTANCE.equalsIgnoreCase(v)) { 1180 sh.setFieldLockingLevel(false); 1181 } else { 1182 logger.log(BasicLevel.WARN, "Locking level '" + v 1183 + "' specified for the class '" + path 1184 + "' is not managed."); 1185 } 1186 } 1187 1188 v = getSpeedoProperties( 1190 SpeedoProperties.USER_CACHE_CLASS_POLICY+ "(" + path + ")"); 1191 if (v != null) { 1192 StringTokenizer st = new StringTokenizer (v, " ,;:/\t", false); 1193 while(st.hasMoreTokens()) { 1194 String cacheName = st.nextToken(); 1195 sh.activeUserCache(cacheName); 1196 if (logger.isLoggable(BasicLevel.INFO)) { 1197 logger.log(BasicLevel.INFO, "Activating the user cache '" + cacheName 1198 + "' for the class '" + path); 1199 } 1200 try { 1202 PClassMapping[] subPcms = sh.getSubPCMs(); 1203 if (subPcms != null) { 1204 for(int i=0; i<subPcms.length; i++) { 1205 ((SpeedoHome)subPcms[i]).activeUserCache(cacheName); 1206 } 1207 } 1208 } catch (Exception e) { 1209 } 1211 } 1212 } 1213 1214 v = getSpeedoProperties( 1216 SpeedoProperties.PREFETCH_ON_EXTENT + "(" + path + ")"); 1217 if (v != null && isFalse(v)) { 1218 sh.setPrefetchOnExtent(false); 1219 } 1220 1221 v = getSpeedoProperties( 1223 SpeedoProperties.PREFETCH_ON_QUERY + "(" + path + ")"); 1224 if (v != null && isFalse(v)) { 1225 sh.setPrefetchOnQuery(false); 1226 } 1227 1228 v = getSpeedoProperties( 1230 SpeedoProperties.PREFETCH_ON_GENCLASS + "(" + path + ")"); 1231 if (v != null && isFalse(v)) { 1232 sh.setPrefetchOnGenClass(false); 1233 } 1234 } 1235} 1236 | Popular Tags |