KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > hibernate > HibernateCfgSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.hibernate;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.List JavaDoc;
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 /**
19  * Generate the hibernate.cfg.xml file. It lists all of the properties as well as a property for each hbm.xml file. This
20  * file can be used for creating and installing a SessionFactory in JNDI as well as launching Hibern8IDE.
21  *
22  * @author <a HREF="mailto:fbrier at users.sourceforge.net">Frederick N. Brier</a>
23  * @author <a HREF="mailto:dchannon at users.sourceforge.net">David Channon</a>
24  * @created February 6, 2004
25  * @version $Revision: 1.10 $
26  * @ant.element name="hibernatecfg" display-name="Hibernate Configuration File Generation"
27  * parent="xdoclet.modules.hibernate.HibernateDocletTask"
28  */

29 public class HibernateCfgSubTask extends XmlSubTask implements HibernateProperties
30 {
31
32     private final static String JavaDoc HIBERNATE_PUBLICID_20 = "-//Hibernate/Hibernate Configuration DTD 2.0//EN";
33
34     private final static String JavaDoc HIBERNATE_SYSTEMID_20 = "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd";
35
36     private final static String JavaDoc DTD_FILE_NAME_20 = "resources/hibernate-configuration-2.0.dtd";
37
38     private final static String JavaDoc HIBERNATE_PUBLICID_30 = "-//Hibernate/Hibernate Configuration DTD 3.0//EN";
39
40     private final static String JavaDoc HIBERNATE_SYSTEMID_30 = "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd";
41
42     private final static String JavaDoc DTD_FILE_NAME_30 = "resources/hibernate-configuration-3.0.dtd";
43
44     /**
45      * Default template to use for hibernate files
46      */

47     private static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/hibernate-cfg.xdt";
48
49     /**
50      * Pattern for generation of hibernate files
51      */

52     private static String JavaDoc GENERATED_CONFIG_FILE_NAME = "hibernate.cfg.xml";
53
54     private String JavaDoc hibernateVersion = HibernateCFGVersion.HIBERNATE_2_0;
55     private String JavaDoc jndiName = null;
56     private String JavaDoc dataSource = null;
57     private String JavaDoc dialect = null;
58     private boolean useOuterJoin = false;
59     private boolean showSql = false;
60     private String JavaDoc userName = null;
61     private String JavaDoc password = null;
62     private String JavaDoc userTransactionName = null;
63     private String JavaDoc transactionManagerStrategy = null;
64     private String JavaDoc driver;
65     private String JavaDoc jdbcUrl;
66     private String JavaDoc poolSize;
67     private String JavaDoc transactionManagerLookup;
68     private ArrayList JavaDoc jndiProperties = new ArrayList JavaDoc();
69     private List JavaDoc otherProperties = new ArrayList JavaDoc();
70     private String JavaDoc defaultSchema = null;
71     private String JavaDoc cglibUseReflectionOptimizer = null;
72     private String JavaDoc cacheProviderClass = null;
73     private String JavaDoc transactionManagerFactory = null;
74     private String JavaDoc hbm2ddl = null;
75     private List JavaDoc otherMappings = new ArrayList JavaDoc();
76
77     /**
78      * Constructor for the HibernateSubTask object
79      */

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     /**
89      * Get the Hibernate configuration DTD version.
90      *
91      * @return
92      */

93     public String JavaDoc getVersion()
94     {
95         return hibernateVersion;
96     }
97
98     public String JavaDoc getHbm2ddl()
99     {
100         return hbm2ddl;
101     }
102
103     public String JavaDoc getTransactionManagerLookup()
104     {
105         return transactionManagerLookup;
106     }
107
108     public Collection JavaDoc getJndiProperties()
109     {
110         return jndiProperties;
111     }
112
113     public Collection JavaDoc getOtherProperties()
114     {
115         return otherProperties;
116     }
117
118     public Collection JavaDoc getOtherMappings()
119     {
120         return otherMappings;
121     }
122
123     public String JavaDoc getTransactionManagerStrategy()
124     {
125         return transactionManagerStrategy;
126     }
127
128     public String JavaDoc getUserTransactionName()
129     {
130         return userTransactionName;
131     }
132
133     public String JavaDoc getUserName()
134     {
135         return userName;
136     }
137
138     public String JavaDoc 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 JavaDoc getJndiName()
154     {
155         return jndiName;
156     }
157
158     public String JavaDoc getDataSource()
159     {
160         return dataSource;
161     }
162
163     public String JavaDoc getDialect()
164     {
165         return dialect;
166     }
167
168     public String JavaDoc getDriver()
169     {
170         return driver;
171     }
172
173     public String JavaDoc getJdbcUrl()
174     {
175         return jdbcUrl;
176     }
177
178     public String JavaDoc getPoolSize()
179     {
180         return poolSize;
181     }
182
183     /**
184      * @return
185      */

186     public String JavaDoc getCacheProviderClass()
187     {
188         return cacheProviderClass;
189     }
190
191     /**
192      * @return
193      */

194     public String JavaDoc getCglibUseReflectionOptimizer()
195     {
196         return cglibUseReflectionOptimizer;
197     }
198
199     /**
200      * @return
201      */

202     public String JavaDoc getDefaultSchema()
203     {
204         return defaultSchema;
205     }
206
207     /**
208      * @return
209      */

210     public String JavaDoc getTransactionManagerFactory()
211     {
212         return transactionManagerFactory;
213     }
214
215     /**
216      * Sets the hibernate configuration DTD version to use. Legal values are "2.0" and "3.0".
217      *
218      * @param version
219      * @ant.not-required No. Default is "2.0".
220      */

221     public void setVersion(HibernateCFGVersion version)
222     {
223         hibernateVersion = version.getValue();
224     }
225
226     /**
227      * Automatically export schema DDL to the database when the SessionFactory is created. With create-drop, the
228      * database schema will be dropped when the SessionFactory is closed explicitely. eg. update | create | create-drop
229      *
230      * @param hbm2ddl
231      * @ant.not-required
232      */

233     public void setHbm2ddl(String JavaDoc hbm2ddl)
234     {
235         this.hbm2ddl = hbm2ddl;
236     }
237
238     /**
239      * Hibernate connection pool size.
240      *
241      * @param poolSize
242      * @ant.not-required
243      */

244     public void setPoolSize(String JavaDoc poolSize)
245     {
246         this.poolSize = poolSize;
247     }
248
249     /**
250      * URL for the JDBC Driver to make the connection to the database.
251      *
252      * @param jdbcUrl
253      * @ant.not-required
254      */

255     public void setJdbcUrl(String JavaDoc jdbcUrl)
256     {
257         this.jdbcUrl = jdbcUrl;
258     }
259
260     /**
261      * Strategy for obtaining the JTA <tt>TransactionManager</tt>
262      *
263      * @param transactionManagerStrategy
264      * @ant.not-required
265      */

266     public void setTransactionManagerStrategy(String JavaDoc transactionManagerStrategy)
267     {
268         this.transactionManagerStrategy = transactionManagerStrategy;
269     }
270
271     /**
272      * The JNDI name of the JTA UserTransaction object
273      *
274      * @param userTransactionName
275      * @ant.not-required
276      */

277     public void setUserTransactionName(String JavaDoc userTransactionName)
278     {
279         this.userTransactionName = userTransactionName;
280     }
281
282     /**
283      * The fully qualified class name of the Hibernate <tt>TransactionFactory</tt> implementation.
284      *
285      * @param transactionManagerLookup
286      * @ant.not-required
287      */

288     public void setTransactionManagerLookup(String JavaDoc transactionManagerLookup)
289     {
290         this.transactionManagerLookup = transactionManagerLookup;
291     }
292
293     /**
294      * Whether to use outer join
295      *
296      * @param useOuterJoin
297      * @ant.not-required No. Defaults to false.
298      */

299     public void setUseOuterJoin(boolean useOuterJoin)
300     {
301         this.useOuterJoin = useOuterJoin;
302     }
303
304     /**
305      * Log sql statements. Defaults to false.
306      *
307      * @param showSql
308      * @ant.not-required
309      */

310     public void setShowSql(boolean showSql)
311     {
312         this.showSql = showSql;
313     }
314
315     /**
316      * JNDI name to bind to the <tt>SessionFactory</tt>
317      *
318      * @param jndiName
319      * @ant.not-required
320      */

321     public void setJndiName(String JavaDoc jndiName)
322     {
323         this.jndiName = jndiName;
324     }
325
326     /**
327      * JNDI name of data source to use in the session factory.
328      *
329      * @param dataSource
330      * @ant.not-required
331      */

332     public void setDataSource(String JavaDoc dataSource)
333     {
334         this.dataSource = dataSource;
335     }
336
337     /**
338      * SQL <a HREF="http://www.hibernate.org/hib_docs/api/net/sf/hibernate/dialect/Dialect.html">dialect</a> of the
339      * database.
340      *
341      * @param dialect
342      * @ant.required Yes. Use fully-qualified class name.
343      */

344     public void setDialect(String JavaDoc dialect)
345     {
346         this.dialect = dialect;
347     }
348
349     /**
350      * JDBC Driver to make database connection.
351      *
352      * @param driver
353      * @ant.not-required
354      */

355     public void setDriver(String JavaDoc driver)
356     {
357         this.driver = driver;
358     }
359
360     /**
361      * Use this user name to login to the database
362      *
363      * @param userName
364      * @ant.not-required
365      */

366     public void setUserName(String JavaDoc userName)
367     {
368         this.userName = userName;
369     }
370
371     /**
372      * Use this password to login to the database
373      *
374      * @param password
375      * @ant.not-required
376      */

377     public void setPassword(String JavaDoc password)
378     {
379         this.password = password;
380     }
381
382     /**
383      * The classname of a custom CacheProvider.
384      *
385      * @param string
386      * @ant.not-required
387      */

388     public void setCacheProviderClass(String JavaDoc string)
389     {
390         cacheProviderClass = string;
391     }
392
393     /**
394      * Enables use of CGLIB instead of runtime reflection (System-level property, default is to use CGLIB where
395      * possible). Reflection can sometimes be useful when troubleshooting.
396      *
397      * @param string
398      * @ant.not-required
399      */

400     public void setCglibUseReflectionOptimizer(String JavaDoc string)
401     {
402         cglibUseReflectionOptimizer = string;
403     }
404
405     /**
406      * Qualify unqualified tablenames with the given schema/tablespace in generated SQL.
407      *
408      * @param string
409      * @ant.not-required
410      */

411     public void setDefaultSchema(String JavaDoc string)
412     {
413         defaultSchema = string;
414     }
415
416
417     /**
418      * The classname of a TransactionFactory to use with Hibernate Transaction API (defaults to JDBCTransactionFactory).
419      * eg. classname.of.TransactionFactory
420      *
421      * @param string
422      * @ant.not-required
423      */

424     public void setTransactionManagerFactory(String JavaDoc string)
425     {
426         transactionManagerFactory = string;
427     }
428
429     /**
430      * Generate Hibernate Configuration file (hibernate.cfg.xml).
431      *
432      * @exception XDocletException
433      */

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             // Need either a dataSource or standalone connection properties
457
throw new XDocletException(Translator.getString(XDocletModulesHibernateMessages.class,
458                 XDocletModulesHibernateMessages.DATA_CONNECTION_REQUIRED));
459         }
460
461         if ((jndiProperties.size() > 0) && (null == jndiName)) {
462             // Probably want a jndiName if you are going to specify additional properties to use with it.
463
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     /**
474      * These elements allow you to add properties to the JNDI context. For instance, if you do not want Weblogic
475      * clustering to replicate the Hibernate SessionFactory, add a jndiProperty element with a "name" attribute of
476      * "weblogic.jndi.replicateBindings" and a "value" attribute of "false".
477      *
478      * @param jndiProperty
479      * @ant.not-required No. Empty array of elements.
480      */

481     public void addConfiguredJndiProperty(Parameter jndiProperty)
482     {
483         System.out.println("addConfiguredJndiProperty(): name=" + jndiProperty.getName() + ", " + jndiProperty.getValue());
484         jndiProperties.add(jndiProperty);
485     }
486
487     /**
488      * These elements allow you to add arbitrary properties to cfg.xml file. For instance, if you want to provide your
489      * own "connection.provider_class" class then add a property called connection.provider_class with a value of
490      * whatever you want passed in.
491      *
492      * @param otherProperty The feature to be added to the OtherProperty attribute
493      * @ant.not-required No. Empty array of elements.
494      */

495     public void addOtherProperty(Parameter otherProperty)
496     {
497         System.out.println("addOtherProperty(): name=" + otherProperty.getName() + ", " + otherProperty.getValue());
498         otherProperties.add(otherProperty);
499     }
500
501     /**
502      * These elements allow you to add arbitrary mappings to cfg.xml file. For instance, if you want to specify that a
503      * jar contains /example/myHibernate.hbm.xml file then specify a other mapping with the name="resource" and the
504      * value being the path to the mapping.
505      *
506      * @param otherMapping The feature to be added to the OtherMapping attribute
507      * @ant.not-required No. Empty array of elements.
508      */

509     public void addOtherMapping(Parameter otherMapping)
510     {
511         System.out.println("addOtherMapping(): name=" + otherMapping.getName() + ", " + otherMapping.getValue());
512         otherMappings.add(otherMapping);
513     }
514
515     /**
516      * Called when the engine is started
517      *
518      * @exception XDocletException Thrown in case of problem
519      */

520     protected void engineStarted() throws XDocletException
521     {
522         System.out.println(Translator.getString(XDocletModulesHibernateMessages.class,
523             XDocletModulesHibernateMessages.GENERATING_HIBERNATE_CFG_XML,
524             new String JavaDoc[]{getDestinationFile()}));
525     }
526
527     /**
528      * Based on Matt Raible's code for the Hibernate sub-task.
529      *
530      * @author <a HREF="mailto:dchannon at users.sourceforge.net">David Channon</a>
531      * @created April 02, 2005
532      */

533     public static class HibernateCFGVersion extends org.apache.tools.ant.types.EnumeratedAttribute
534     {
535         public final static String JavaDoc HIBERNATE_2_0 = "2.0";
536         public final static String JavaDoc HIBERNATE_3_0 = "3.0";
537
538         /**
539          * Gets the Values attribute of the HibernateCFGVersion object.
540          *
541          * @return The Values value
542          */

543         public java.lang.String JavaDoc[] getValues()
544         {
545             return (new java.lang.String JavaDoc[]{
546                 HIBERNATE_2_0, HIBERNATE_3_0
547                 });
548         }
549     }
550 }
551
Popular Tags