KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SchemaUpdateTask.java,v 1.3 2005/03/03 13:31:59 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>SchemaUpdate</tt>.
27  *
28  * <pre>
29  * &lt;taskdef name="schemaupdate"
30  * classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
31  * classpathref="class.path"/&gt;
32  *
33  * &lt;schemaupdate
34  * properties="${build.classes.dir}/hibernate.properties"
35  * quiet="no"
36  * &lt;fileset dir="${build.classes.dir}"&gt;
37  * &lt;include name="*.hbm.xml"/&gt;
38  * &lt;/fileset&gt;
39  * &lt;/schemaupdate&gt;
40  * </pre>
41  *
42  * @see SchemaUpdate
43  * @author Rong C Ou, Gavin King
44  */

45 public class SchemaUpdateTask extends MatchingTask {
46
47     private List JavaDoc fileSets = new LinkedList JavaDoc();
48     private File JavaDoc propertiesFile = null;
49     private String JavaDoc configurationFile = null;
50     private boolean quiet = false;
51     private boolean text = true;
52     private String JavaDoc namingStrategy = null;
53
54     public void addFileset(FileSet set) {
55         fileSets.add(set);
56     }
57
58     /**
59      * Set a properties file
60      * @param propertiesFile the properties file name
61      */

62     public void setProperties(File JavaDoc propertiesFile) {
63         if ( !propertiesFile.exists() ) {
64             throw new BuildException("Properties file: " + propertiesFile + " does not exist.");
65         }
66
67         log("Using properties file " + propertiesFile, Project.MSG_DEBUG);
68         this.propertiesFile = propertiesFile;
69     }
70
71     /**
72      * Set a <literal>.cfg.xml</literal> file
73      * @param configurationFile the file name
74      */

75     public void setConfig(String JavaDoc configurationFile) {
76         this.configurationFile = configurationFile;
77     }
78
79     /**
80      * Enable "text-only" mode. The schema will not
81      * be updated in the database.
82      * @param text true to enable text-only mode
83      */

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

93     public void setQuiet(boolean quiet) {
94         this.quiet = quiet;
95     }
96
97     /**
98      * Execute the task
99      */

100     public void execute() throws BuildException {
101         try {
102             Configuration cfg = getConfiguration();
103             getSchemaUpdate(cfg).execute(!quiet, !text);
104         }
105         catch (HibernateException e) {
106             throw new BuildException("Schema text failed: " + e.getMessage(), e);
107         }
108         catch (FileNotFoundException JavaDoc e) {
109             throw new BuildException("File not found: " + e.getMessage(), e);
110         }
111         catch (IOException JavaDoc e) {
112             throw new BuildException("IOException : " + e.getMessage(), e);
113         }
114         catch (Exception JavaDoc e) {
115             throw new BuildException(e);
116         }
117     }
118
119     private String JavaDoc[] getFiles() {
120
121         List JavaDoc files = new LinkedList JavaDoc();
122         for ( Iterator JavaDoc i = fileSets.iterator(); i.hasNext(); ) {
123
124             FileSet fs = (FileSet) i.next();
125             DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
126
127             String JavaDoc[] dsFiles = ds.getIncludedFiles();
128             for (int j = 0; j < dsFiles.length; j++) {
129                 File JavaDoc f = new File JavaDoc(dsFiles[j]);
130                 if ( !f.isFile() ) {
131                     f = new File JavaDoc( ds.getBasedir(), dsFiles[j] );
132                 }
133
134                 files.add( f.getAbsolutePath() );
135             }
136         }
137
138         return ArrayHelper.toStringArray(files);
139     }
140
141     private Configuration getConfiguration() throws Exception JavaDoc {
142         Configuration cfg = new Configuration();
143         if (namingStrategy!=null) cfg.setNamingStrategy(
144             (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
145         );
146         if (configurationFile!=null) cfg.configure( new File JavaDoc(configurationFile) );
147
148         String JavaDoc[] files = getFiles();
149         for (int i = 0; i < files.length; i++) {
150             String JavaDoc filename = files[i];
151             if ( filename.endsWith(".jar") ) {
152                 cfg.addJar( new File JavaDoc(filename) );
153             }
154             else {
155                 cfg.addFile(filename);
156             }
157         }
158         return cfg;
159     }
160
161     private SchemaUpdate getSchemaUpdate(Configuration cfg) throws HibernateException, IOException JavaDoc {
162         Properties JavaDoc properties = new Properties JavaDoc();
163         properties.putAll( cfg.getProperties() );
164         if (propertiesFile == null) {
165             properties.putAll( getProject().getProperties() );
166         }
167         else {
168             properties.load( new FileInputStream JavaDoc(propertiesFile) );
169         }
170         return new SchemaUpdate(cfg, properties);
171     }
172
173     public void setNamingStrategy(String JavaDoc namingStrategy) {
174         this.namingStrategy = namingStrategy;
175     }
176
177 }
178
Popular Tags