KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tool > hbm2ddl > SchemaExportTask


1 //$Id: SchemaExportTask.java,v 1.5 2005/03/03 13:32:33 oneovthafew Exp $
2
package org.hibernate.tool.hbm2ddl;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.cfg.Configuration;
6 import org.hibernate.cfg.NamingStrategy;
7 import org.hibernate.util.ArrayHelper;
8 import org.hibernate.util.ReflectHelper;
9
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.DirectoryScanner;
12 import org.apache.tools.ant.Project;
13 import org.apache.tools.ant.taskdefs.MatchingTask;
14 import org.apache.tools.ant.types.FileSet;
15
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 /**
26  * An Ant task for <tt>SchemaExport</tt>.
27  *
28  * <pre>
29  * &lt;taskdef name="schemaexport"
30  * classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
31  * classpathref="class.path"/&gt;
32  *
33  * &lt;schemaexport
34  * properties="${build.classes.dir}/hibernate.properties"
35  * quiet="no"
36  * text="no"
37  * drop="no"
38  * delimiter=";"
39  * output="${build.dir}/schema-export.sql"&gt;
40  * &lt;fileset dir="${build.classes.dir}"&gt;
41  * &lt;include name="*.hbm.xml"/&gt;
42  * &lt;/fileset&gt;
43  * &lt;/schemaexport&gt;
44  * </pre>
45  *
46  * @see SchemaExport
47  * @author Rong C Ou
48  */

49 public class SchemaExportTask extends MatchingTask {
50
51     private List JavaDoc fileSets = new LinkedList JavaDoc();
52     private File JavaDoc propertiesFile = null;
53     private String JavaDoc configurationFile = null;
54     private String JavaDoc outputFile = null;
55     private boolean quiet = false;
56     private boolean text = false;
57     private boolean drop = false;
58     private String JavaDoc delimiter = null;
59     private String JavaDoc namingStrategy = null;
60
61     public void addFileset(FileSet set) {
62         fileSets.add(set);
63     }
64
65     /**
66      * Set a properties file
67      * @param propertiesFile the properties file name
68      */

69     public void setProperties(File JavaDoc propertiesFile) {
70         if ( !propertiesFile.exists() ) {
71             throw new BuildException("Properties file: " + propertiesFile + " does not exist.");
72     }
73
74         log("Using properties file " + propertiesFile, Project.MSG_DEBUG);
75         this.propertiesFile = propertiesFile;
76     }
77
78     /**
79      * Set a <literal>.cfg.xml</literal> file, which will be
80      * loaded as a resource, from the classpath
81      * @param configurationFile the path to the resource
82      */

83     public void setConfig(String JavaDoc configurationFile) {
84         this.configurationFile = configurationFile;
85     }
86
87     /**
88      * Enable "quiet" mode. The schema will not be
89      * written to standard out.
90      * @param quiet true to enable quiet mode
91      */

92     public void setQuiet(boolean quiet) {
93         this.quiet = quiet;
94     }
95
96     /**
97      * Enable "text-only" mode. The schema will not
98      * be exported to the database.
99      * @param text true to enable text-only mode
100      */

101     public void setText(boolean text) {
102         this.text = text;
103     }
104
105     /**
106      * Enable "drop" mode. Database objects will be
107      * dropped but not recreated.
108      * @param drop true to enable drop mode
109      */

110     public void setDrop(boolean drop) {
111         this.drop = drop;
112     }
113
114     /**
115      * Set the end of statement delimiter for the generated script
116      * @param delimiter the delimiter
117      */

118     public void setDelimiter(String JavaDoc delimiter) {
119         this.delimiter = delimiter;
120     }
121
122     /**
123      * Set the script output file
124      * @param outputFile the file name
125      */

126     public void setOutput(String JavaDoc outputFile) {
127         this.outputFile = outputFile;
128     }
129
130     /**
131      * Execute the task
132      */

133     public void execute() throws BuildException {
134         try {
135             Configuration cfg = getConfiguration();
136             SchemaExport schemaExport = getSchemaExport(cfg);
137
138             if (drop) {
139                 schemaExport.drop(!quiet, !text);
140             }
141             else {
142                 schemaExport.create(!quiet, !text);
143             }
144         }
145         catch (HibernateException e) {
146             throw new BuildException("Schema text failed: " + e.getMessage(), e);
147         }
148         catch (FileNotFoundException JavaDoc e) {
149             throw new BuildException("File not found: " + e.getMessage(), e);
150         }
151         catch (IOException JavaDoc e) {
152             throw new BuildException("IOException : " + e.getMessage(), e);
153         }
154         catch (Exception JavaDoc e) {
155             throw new BuildException(e);
156         }
157     }
158
159     private String JavaDoc[] getFiles() {
160
161         List JavaDoc files = new LinkedList JavaDoc();
162         for ( Iterator JavaDoc i = fileSets.iterator(); i.hasNext(); ) {
163
164             FileSet fs = (FileSet) i.next();
165             DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
166
167             String JavaDoc[] dsFiles = ds.getIncludedFiles();
168             for (int j = 0; j < dsFiles.length; j++) {
169                 File JavaDoc f = new File JavaDoc(dsFiles[j]);
170                 if ( !f.isFile() ) {
171                     f = new File JavaDoc( ds.getBasedir(), dsFiles[j] );
172                 }
173
174                 files.add( f.getAbsolutePath() );
175             }
176         }
177
178         return ArrayHelper.toStringArray(files);
179     }
180
181     private Configuration getConfiguration() throws Exception JavaDoc {
182         Configuration cfg = new Configuration();
183         if (namingStrategy!=null) cfg.setNamingStrategy(
184             (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
185         );
186         if (configurationFile != null) cfg.configure( new File JavaDoc(configurationFile) );
187
188         String JavaDoc[] files = getFiles();
189         for (int i = 0; i < files.length; i++) {
190             String JavaDoc filename = files[i];
191             if ( filename.endsWith(".jar") ) {
192                 cfg.addJar( new File JavaDoc(filename) );
193             }
194             else {
195                 cfg.addFile(filename);
196             }
197         }
198         return cfg;
199     }
200
201     private SchemaExport getSchemaExport(Configuration cfg) throws HibernateException, IOException JavaDoc {
202         Properties JavaDoc properties = new Properties JavaDoc();
203         properties.putAll( cfg.getProperties() );
204         if (propertiesFile == null) {
205             properties.putAll( getProject().getProperties() );
206         }
207         else {
208             properties.load( new FileInputStream JavaDoc(propertiesFile) );
209         }
210         SchemaExport schemaExport = new SchemaExport(cfg, properties);
211         schemaExport.setOutputFile(outputFile);
212         schemaExport.setDelimiter(delimiter);
213         return schemaExport;
214     }
215
216     public void setNamingStrategy(String JavaDoc namingStrategy) {
217         this.namingStrategy = namingStrategy;
218     }
219
220 }
221
Popular Tags