1 5 package xdoclet.modules.hibernate; 6 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.List ; 10 11 import org.apache.tools.ant.types.Parameter; 12 13 import xdoclet.XDocletException; 14 import xdoclet.XmlSubTask; 15 16 import xdoclet.util.Translator; 17 18 29 public class HibernateCfgSubTask extends XmlSubTask implements HibernateProperties 30 { 31 32 private final static String HIBERNATE_PUBLICID_20 = "-//Hibernate/Hibernate Configuration DTD 2.0//EN"; 33 34 private final static String HIBERNATE_SYSTEMID_20 = "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"; 35 36 private final static String DTD_FILE_NAME_20 = "resources/hibernate-configuration-2.0.dtd"; 37 38 private final static String HIBERNATE_PUBLICID_30 = "-//Hibernate/Hibernate Configuration DTD 3.0//EN"; 39 40 private final static String HIBERNATE_SYSTEMID_30 = "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"; 41 42 private final static String DTD_FILE_NAME_30 = "resources/hibernate-configuration-3.0.dtd"; 43 44 47 private static String DEFAULT_TEMPLATE_FILE = "resources/hibernate-cfg.xdt"; 48 49 52 private static String GENERATED_CONFIG_FILE_NAME = "hibernate.cfg.xml"; 53 54 private String hibernateVersion = HibernateCFGVersion.HIBERNATE_2_0; 55 private String jndiName = null; 56 private String dataSource = null; 57 private String dialect = null; 58 private boolean useOuterJoin = false; 59 private boolean showSql = false; 60 private String userName = null; 61 private String password = null; 62 private String userTransactionName = null; 63 private String transactionManagerStrategy = null; 64 private String driver; 65 private String jdbcUrl; 66 private String poolSize; 67 private String transactionManagerLookup; 68 private ArrayList jndiProperties = new ArrayList (); 69 private List otherProperties = new ArrayList (); 70 private String defaultSchema = null; 71 private String cglibUseReflectionOptimizer = null; 72 private String cacheProviderClass = null; 73 private String transactionManagerFactory = null; 74 private String hbm2ddl = null; 75 private List otherMappings = new ArrayList (); 76 77 80 public HibernateCfgSubTask() 81 { 82 setSubTaskName("hibernatecfg"); 83 setHavingClassTag("hibernate.class"); 84 setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE)); 85 setDestinationFile(GENERATED_CONFIG_FILE_NAME); 86 } 87 88 93 public String getVersion() 94 { 95 return hibernateVersion; 96 } 97 98 public String getHbm2ddl() 99 { 100 return hbm2ddl; 101 } 102 103 public String getTransactionManagerLookup() 104 { 105 return transactionManagerLookup; 106 } 107 108 public Collection getJndiProperties() 109 { 110 return jndiProperties; 111 } 112 113 public Collection getOtherProperties() 114 { 115 return otherProperties; 116 } 117 118 public Collection getOtherMappings() 119 { 120 return otherMappings; 121 } 122 123 public String getTransactionManagerStrategy() 124 { 125 return transactionManagerStrategy; 126 } 127 128 public String getUserTransactionName() 129 { 130 return userTransactionName; 131 } 132 133 public String getUserName() 134 { 135 return userName; 136 } 137 138 public String getPassword() 139 { 140 return password; 141 } 142 143 public boolean getUseOuterJoin() 144 { 145 return useOuterJoin; 146 } 147 148 public boolean getShowSql() 149 { 150 return showSql; 151 } 152 153 public String getJndiName() 154 { 155 return jndiName; 156 } 157 158 public String getDataSource() 159 { 160 return dataSource; 161 } 162 163 public String getDialect() 164 { 165 return dialect; 166 } 167 168 public String getDriver() 169 { 170 return driver; 171 } 172 173 public String getJdbcUrl() 174 { 175 return jdbcUrl; 176 } 177 178 public String getPoolSize() 179 { 180 return poolSize; 181 } 182 183 186 public String getCacheProviderClass() 187 { 188 return cacheProviderClass; 189 } 190 191 194 public String getCglibUseReflectionOptimizer() 195 { 196 return cglibUseReflectionOptimizer; 197 } 198 199 202 public String getDefaultSchema() 203 { 204 return defaultSchema; 205 } 206 207 210 public String getTransactionManagerFactory() 211 { 212 return transactionManagerFactory; 213 } 214 215 221 public void setVersion(HibernateCFGVersion version) 222 { 223 hibernateVersion = version.getValue(); 224 } 225 226 233 public void setHbm2ddl(String hbm2ddl) 234 { 235 this.hbm2ddl = hbm2ddl; 236 } 237 238 244 public void setPoolSize(String poolSize) 245 { 246 this.poolSize = poolSize; 247 } 248 249 255 public void setJdbcUrl(String jdbcUrl) 256 { 257 this.jdbcUrl = jdbcUrl; 258 } 259 260 266 public void setTransactionManagerStrategy(String transactionManagerStrategy) 267 { 268 this.transactionManagerStrategy = transactionManagerStrategy; 269 } 270 271 277 public void setUserTransactionName(String userTransactionName) 278 { 279 this.userTransactionName = userTransactionName; 280 } 281 282 288 public void setTransactionManagerLookup(String transactionManagerLookup) 289 { 290 this.transactionManagerLookup = transactionManagerLookup; 291 } 292 293 299 public void setUseOuterJoin(boolean useOuterJoin) 300 { 301 this.useOuterJoin = useOuterJoin; 302 } 303 304 310 public void setShowSql(boolean showSql) 311 { 312 this.showSql = showSql; 313 } 314 315 321 public void setJndiName(String jndiName) 322 { 323 this.jndiName = jndiName; 324 } 325 326 332 public void setDataSource(String dataSource) 333 { 334 this.dataSource = dataSource; 335 } 336 337 344 public void setDialect(String dialect) 345 { 346 this.dialect = dialect; 347 } 348 349 355 public void setDriver(String driver) 356 { 357 this.driver = driver; 358 } 359 360 366 public void setUserName(String userName) 367 { 368 this.userName = userName; 369 } 370 371 377 public void setPassword(String password) 378 { 379 this.password = password; 380 } 381 382 388 public void setCacheProviderClass(String string) 389 { 390 cacheProviderClass = string; 391 } 392 393 400 public void setCglibUseReflectionOptimizer(String string) 401 { 402 cglibUseReflectionOptimizer = string; 403 } 404 405 411 public void setDefaultSchema(String string) 412 { 413 defaultSchema = string; 414 } 415 416 417 424 public void setTransactionManagerFactory(String string) 425 { 426 transactionManagerFactory = string; 427 } 428 429 434 public void execute() throws XDocletException 435 { 436 if (hibernateVersion.equals(HibernateCFGVersion.HIBERNATE_2_0)) { 437 setPublicId(HIBERNATE_PUBLICID_20); 438 setSystemId(HIBERNATE_SYSTEMID_20); 439 setDtdURL(getClass().getResource(DTD_FILE_NAME_20)); 440 } 441 else { 442 setPublicId(HIBERNATE_PUBLICID_30); 443 setSystemId(HIBERNATE_SYSTEMID_30); 444 setDtdURL(getClass().getResource(DTD_FILE_NAME_30)); 445 } 446 447 startProcess(); 448 } 449 450 public void validateOptions() throws XDocletException 451 { 452 super.validateOptions(); 453 454 if ((null == dataSource) && 455 ((null == driver) || (null == jdbcUrl) || (null == userName) || (null == password))) { 456 throw new XDocletException(Translator.getString(XDocletModulesHibernateMessages.class, 458 XDocletModulesHibernateMessages.DATA_CONNECTION_REQUIRED)); 459 } 460 461 if ((jndiProperties.size() > 0) && (null == jndiName)) { 462 throw new XDocletException(Translator.getString(XDocletModulesHibernateMessages.class, 464 XDocletModulesHibernateMessages.JNDI_NAME_FOR_PROPS_REQUIRED)); 465 } 466 467 if (getDialect() == null) { 468 throw new XDocletException(Translator.getString(XDocletModulesHibernateMessages.class, 469 XDocletModulesHibernateMessages.SQL_DIALECT_REQUIRED)); 470 } 471 } 472 473 481 public void addConfiguredJndiProperty(Parameter jndiProperty) 482 { 483 System.out.println("addConfiguredJndiProperty(): name=" + jndiProperty.getName() + ", " + jndiProperty.getValue()); 484 jndiProperties.add(jndiProperty); 485 } 486 487 495 public void addOtherProperty(Parameter otherProperty) 496 { 497 System.out.println("addOtherProperty(): name=" + otherProperty.getName() + ", " + otherProperty.getValue()); 498 otherProperties.add(otherProperty); 499 } 500 501 509 public void addOtherMapping(Parameter otherMapping) 510 { 511 System.out.println("addOtherMapping(): name=" + otherMapping.getName() + ", " + otherMapping.getValue()); 512 otherMappings.add(otherMapping); 513 } 514 515 520 protected void engineStarted() throws XDocletException 521 { 522 System.out.println(Translator.getString(XDocletModulesHibernateMessages.class, 523 XDocletModulesHibernateMessages.GENERATING_HIBERNATE_CFG_XML, 524 new String []{getDestinationFile()})); 525 } 526 527 533 public static class HibernateCFGVersion extends org.apache.tools.ant.types.EnumeratedAttribute 534 { 535 public final static String HIBERNATE_2_0 = "2.0"; 536 public final static String HIBERNATE_3_0 = "3.0"; 537 538 543 public java.lang.String [] getValues() 544 { 545 return (new java.lang.String []{ 546 HIBERNATE_2_0, HIBERNATE_3_0 547 }); 548 } 549 } 550 } 551 | Popular Tags |