1 5 package xdoclet.modules.hibernate; 6 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 import java.util.Properties ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.tools.ant.types.Parameter; 14 15 import xjavadoc.XClass; 16 import xjavadoc.XMethod; 17 import xdoclet.DocletContext; 18 import xdoclet.DocletSupport; 19 import xdoclet.DocletTask; 20 import xdoclet.XDocletException; 21 import xdoclet.XDocletTagSupport; 22 import xdoclet.tagshandler.ClassTagsHandler; 23 import xdoclet.tagshandler.TypeTagsHandler; 24 import xdoclet.util.LogUtil; 25 import xdoclet.util.Translator; 26 34 public class HibernateTagsHandler 35 extends XDocletTagSupport 36 { 37 38 private LinkedList columnPrefixes = new LinkedList (); 40 41 private String currentTag; 42 43 private String currentMappingElement; 44 45 private Parameter currentJndiParameter; 46 47 private Parameter currentOtherParameter; 48 49 private Parameter currentOtherMapping; 50 51 58 public String getFileName() 59 throws XDocletException 60 { 61 return getCurrentClass().getQualifiedName().replace('.', '/'); 62 } 63 64 public JBossServiceSubTask getJBossServiceSubTask() 65 { 66 return (JBossServiceSubTask) (DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(JBossServiceSubTask.class))); 67 } 68 69 public FactoryClassSubTask getFactoryClassSubTask() 70 { 71 return (FactoryClassSubTask) (DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(FactoryClassSubTask.class))); 72 } 73 74 public HibernateProperties getHibernateProperties() throws XDocletException 75 { 76 try { 77 return (HibernateProperties) DocletContext.getInstance().getActiveSubTask(); 78 } 79 catch (ClassCastException e) { 80 throw new XDocletException(e, "May occur if attribute is used with incorrect subtask."); 81 } 82 } 83 84 90 public XMethod getIdMethod() throws XDocletException 91 { 92 XClass clazz = getCurrentClass(); 93 Iterator methodIterator = clazz.getMethods(true).iterator(); 94 95 XMethod method; 97 98 while (methodIterator.hasNext()) { 99 method = (XMethod) methodIterator.next(); 100 101 if (method.getDoc().hasTag("hibernate.id")) { 102 return method; 103 } 104 } 105 return null; 106 } 107 108 public String getCurrentTag(Properties attributes) 109 { 110 return currentTag; 111 } 112 113 public String getCurrentMappingElement(Properties attributes) 114 { 115 return currentMappingElement; 116 } 117 118 public void setCurrentTag(String template, Properties attributes) throws XDocletException 119 { 120 currentTag = attributes.getProperty("name"); 121 currentMappingElement = attributes.getProperty("mappingElement"); 122 generate(template); 123 currentTag = null; 124 currentMappingElement = null; 125 } 126 127 135 public void setColumnPrefix(String template, Properties attributes) 136 throws XDocletException 137 { 138 String columnPrefix = attributes.getProperty("prefix", ""); 139 140 columnPrefixes.addLast(columnPrefix); 141 generate(template); 142 columnPrefixes.removeLast(); 143 } 144 145 151 public String roleAttribute(Properties attributes) 152 { 153 return "1.1".equals(getHibernateSubTask().getVersion()) ? "role" : "name"; 154 } 155 156 162 public String serviceClassName(Properties attributes) 163 { 164 if ("1.1".equals(getHibernateSubTask().getVersion())) 165 return "cirrus.hibernate.jmx.HibernateService"; 166 else 167 return "net.sf.hibernate.jmx.HibernateService"; 168 } 169 170 178 public void ifCurrentMappingElementIsnt(String template, Properties attributes) throws XDocletException 179 { 180 String element = attributes.getProperty("mappingElement"); 181 182 if (currentMappingElement == null || element == null 183 || !currentMappingElement.equals(element)) { 184 generate(template); 185 } 186 } 187 188 196 public void ifHasCompositeId(String template, Properties attributes) throws XDocletException 197 { 198 hasCompositeId_Impl(template, true); 199 } 200 201 209 public void ifHasPrimitiveId(String template, Properties attributes) throws XDocletException 210 { 211 hasCompositeId_Impl(template, false); 212 } 213 214 221 public String serviceName() throws XDocletException 222 { 223 return getJBossServiceSubTask().getServiceName(); 224 } 225 226 233 public String jndiName() throws XDocletException 234 { 235 return getHibernateProperties().getJndiName(); 236 } 237 238 247 public void ifUseJndiFactory(String template, Properties attributes) throws XDocletException 248 { 249 if (getFactoryClassSubTask().isUseJndiFactory()) { 250 generate(template); 251 } 252 } 253 254 263 public void ifNotUseJndiFactory(String template, Properties attributes) throws XDocletException 264 { 265 if (!getFactoryClassSubTask().isUseJndiFactory()) { 266 generate(template); 267 } 268 } 269 270 279 public void ifHasJndiName(String template, Properties attributes) throws XDocletException 280 { 281 if (jndiName() != null) { 282 generate(template); 283 } 284 } 285 286 294 public void ifGeneratePropertyCache(String template, Properties attributes) throws XDocletException 295 { 296 if (dialect() != null 297 && driver() != null 298 && jdbcUrl() != null 299 && userName() != null 300 && password() != null) { 301 generate(template); 302 } 303 if (dialect() != null && jndiName() != null) { 304 generate(template); 305 } 306 } 307 308 316 public void ifNotHasJndiName(String template, Properties attributes) throws XDocletException 317 { 318 if (jndiName() == null) { 319 generate(template); 320 } 321 } 322 323 330 331 public String dialect() throws XDocletException 332 { 333 return getHibernateProperties().getDialect(); 334 } 335 336 343 public String dataSource() throws XDocletException 344 { 345 return getHibernateProperties().getDataSource(); 346 } 347 348 355 public String driver() throws XDocletException 356 { 357 return getHibernateProperties().getDriver(); 358 } 359 360 367 public String jdbcUrl() throws XDocletException 368 { 369 return getHibernateProperties().getJdbcUrl(); 370 } 371 372 379 public String userName() throws XDocletException 380 { 381 return getHibernateProperties().getUserName(); 382 } 383 384 391 public String password() throws XDocletException 392 { 393 return getHibernateProperties().getPassword(); 394 } 395 396 403 public String poolSize() throws XDocletException 404 { 405 return getHibernateProperties().getPoolSize(); 406 } 407 408 415 public String factoryClass() throws XDocletException 416 { 417 return getFactoryClassSubTask().getFactoryClass(); 418 } 419 420 427 public String mappingList() throws XDocletException 428 { 429 String mappingName; 430 StringBuffer sb = new StringBuffer (); 431 Collection classes = ClassTagsHandler.getAllClasses(); 432 XClass clazz; 433 434 for (Iterator i = classes.iterator(); i.hasNext(); ) { 435 clazz = (XClass) i.next(); 436 437 if (clazz.getDoc().hasTag("hibernate.class", false)) { 438 mappingName = getHibernateSubTask().getMappingURL(clazz); 439 sb.append(mappingName); 440 sb.append(","); 441 } 442 } 443 if (sb.length() > 0) { 444 return sb.substring(0, sb.length() - 1); 445 } 446 else { 447 return ""; 448 } 449 } 450 451 458 public void logMapping(Properties attributes) throws XDocletException 459 { 460 System.out.println(" " + getCurrentClass().getQualifiedName()); 461 } 462 463 464 472 public void forAllPersistentClasses(String template, Properties attributes) throws XDocletException 473 { 474 Collection classes = ClassTagsHandler.getAllClasses(); 475 XClass clazz; 476 477 for (Iterator i = classes.iterator(); i.hasNext(); ) { 478 clazz = (XClass) i.next(); 479 480 if (clazz.getDoc().hasTag("hibernate.class", false)) { 481 pushCurrentClass(clazz); 482 generate(template); 483 popCurrentClass(); 484 } 485 } 486 } 487 488 496 public void forAllJndiProperties(String template, Properties attributes) throws XDocletException 497 { 498 Collection properties = getHibernateProperties().getJndiProperties(); 499 500 for (Iterator i = properties.iterator(); i.hasNext(); ) { 501 currentJndiParameter = (Parameter) i.next(); 502 generate(template); 503 currentJndiParameter = null; 504 } 505 } 506 507 515 public void forAllOtherProperties(String template, Properties attributes) throws XDocletException 516 { 517 Collection properties = getHibernateProperties().getOtherProperties(); 518 519 for (Iterator i = properties.iterator(); i.hasNext(); ) { 520 currentOtherParameter = (Parameter) i.next(); 521 generate(template); 522 currentOtherParameter = null; 523 } 524 } 525 526 534 public void forAllOtherMappings(String template, Properties attributes) throws XDocletException 535 { 536 Collection properties = getHibernateProperties().getOtherMappings(); 537 538 for (Iterator i = properties.iterator(); i.hasNext(); ) { 539 currentOtherMapping = (Parameter) i.next(); 540 generate(template); 541 currentOtherMapping = null; 542 } 543 } 544 545 public String jndiParameterName() 546 { 547 return currentJndiParameter.getName(); 548 } 549 550 public String jndiParameterValue() 551 { 552 return currentJndiParameter.getValue(); 553 } 554 555 public String otherParameterName() 556 { 557 return currentOtherParameter.getName(); 558 } 559 560 public String otherParameterValue() 561 { 562 return currentOtherParameter.getValue(); 563 } 564 565 public String otherMappingName() 566 { 567 return currentOtherMapping.getName(); 568 } 569 570 public String otherMappingValue() 571 { 572 return currentOtherMapping.getValue(); 573 } 574 575 584 public void forAllSubclasses(String template, Properties attributes) throws XDocletException 585 { 586 587 Log log = LogUtil.getLog(HibernateTagsHandler.class, "forAllSubclasses"); 588 589 try { 590 591 String typeName = getCurrentClass().getQualifiedName(); 592 593 if (log.isDebugEnabled()) 594 log.debug("typeName=" + typeName); 595 596 Collection classes = getXJavaDoc().getSourceClasses(); 597 XClass clazz; 598 599 for (Iterator i = classes.iterator(); i.hasNext(); ) { 600 clazz = (XClass) i.next(); 601 602 log.debug("clazz=" + clazz); 603 604 if (DocletSupport.isDocletGenerated(clazz)) { 605 log.debug("isDocletGenerated"); 606 } 607 else if (clazz.getSuperclass() != null && clazz.getSuperclass().getQualifiedName().equals(typeName)) { 608 log.debug("is a subclass"); 609 610 XClass current = getCurrentClass(); 611 612 pushCurrentClass(clazz); 613 generate(template); 614 popCurrentClass(); 615 616 if (getCurrentClass() != current) 617 setCurrentClass(current); 618 } 620 else { 621 log.debug("is not a subclass"); 622 } 623 } 624 } 625 catch (Exception e) { 626 log.error("exception occurred", e); 627 } 628 } 629 630 636 public String computeColumnName(Properties attributes) 637 { 638 StringBuffer buf = new StringBuffer (); 639 Iterator i = columnPrefixes.iterator(); 640 641 while (i.hasNext()) { 642 String columnPrefix = (String ) i.next(); 643 644 if (columnPrefix != null && columnPrefix.length() > 0) { 645 buf.append(columnPrefix); 646 } 647 } 648 649 buf.append(attributes.getProperty("base")); 650 return buf.toString(); 651 } 652 653 660 void hasCompositeId_Impl(String template, boolean composite) throws XDocletException 661 { 662 XClass oldClass = getCurrentClass(); 663 664 XMethod method = getIdMethod(); 665 666 if (method == null) { 668 throw new XDocletException( 669 Translator.getString(XDocletModulesHibernateMessages.class, 670 XDocletModulesHibernateMessages.NO_ID_PROPERTY, 671 new String []{getCurrentClass().getQualifiedName()})); 672 } 673 674 boolean isUserType = false; 677 678 String typeStr = method.getDoc().getTagAttributeValue("hibernate.id", "type"); 679 680 if (typeStr != null) { 681 XClass typeClass = getXJavaDoc().getXClass(typeStr); 684 685 if (typeClass != null) { 686 isUserType = typeClass.isA("cirrus.hibernate.UserType") || typeClass.isA("net.sf.hibernate.UserType"); 687 } 688 } 689 else { 690 typeStr = method.getReturnType().getType().getQualifiedName(); 691 } 692 693 boolean isPrimitive = TypeTagsHandler.isPrimitiveType(typeStr) || 695 "java.lang.Byte".equals(typeStr) || 696 "java.lang.Double".equals(typeStr) || 697 "java.lang.Float".equals(typeStr) || 698 "java.lang.Integer".equals(typeStr) || 699 "java.lang.Long".equals(typeStr) || 700 "java.lang.Short".equals(typeStr) || 701 "java.lang.String".equals(typeStr) || 702 "java.math.BigDecimal".equals(typeStr) || 703 "java.math.BigInteger".equals(typeStr) || 704 isUserType; 705 706 if (isPrimitive && !composite) { 707 setCurrentMethod(method); 708 generate(template); 709 } 710 711 if (composite && !isPrimitive) { 712 XClass returnType = method.getReturnType().getType(); 717 718 if (returnType.isA("java.io.Serializable") && !returnType.isAbstract() && 719 !"java.lang.Object".equals(returnType.getMethod("equals(java.lang.Object)", true).getContainingClass().getQualifiedName())) { 720 setCurrentMethod(method); 721 generate(template); 722 723 } 724 else { 725 throw new XDocletException( 727 Translator.getString(XDocletModulesHibernateMessages.class, 728 XDocletModulesHibernateMessages.WRONG_COMPOSITE_ID, 729 new String []{returnType.getQualifiedName()})); 730 } 731 } 732 733 if (getCurrentClass() != oldClass) 734 setCurrentClass(oldClass); 735 } 737 738 private HibernateSubTask getHibernateSubTask() 739 { 740 return ((HibernateSubTask) (DocletContext.getInstance() 741 .getSubTaskBy(DocletTask.getSubTaskName(HibernateSubTask.class)))); 742 } 743 } 744 745 | Popular Tags |