KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > tools > DbGeneratorTask


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.tools;
57
58 import java.io.File JavaDoc;
59 import java.sql.Driver JavaDoc;
60
61 import org.apache.tools.ant.BuildException;
62 import org.apache.tools.ant.Project;
63 import org.objectstyle.cayenne.access.DbGenerator;
64 import org.objectstyle.cayenne.conn.DriverDataSource;
65 import org.objectstyle.cayenne.dba.DbAdapter;
66 import org.objectstyle.cayenne.dba.JdbcAdapter;
67 import org.objectstyle.cayenne.map.DataMap;
68 import org.objectstyle.cayenne.map.MapLoader;
69 import org.objectstyle.cayenne.util.Util;
70 import org.xml.sax.InputSource JavaDoc;
71
72 /**
73  * An Ant Task that is a frontend to Cayenne DbGenerator allowing schema generation from
74  * DataMap using Ant.
75  *
76  * @author nirvdrum, Andrei Adamchik
77  * @since 1.2
78  */

79 // TODO: support classpath attribute for loading the driver
80
public class DbGeneratorTask extends CayenneTask {
81
82     protected DbAdapter adapter;
83     protected File JavaDoc map;
84     protected String JavaDoc driver;
85     protected String JavaDoc url;
86     protected String JavaDoc userName;
87     protected String JavaDoc password;
88
89     // DbGenerator options... setup defaults similar to DbGemerator itself:
90
// all DROP set to false, all CREATE - to true
91
protected boolean dropTables;
92     protected boolean dropPK;
93     protected boolean createTables = true;
94     protected boolean createPK = true;
95     protected boolean createFK = true;
96
97
98     public void execute() {
99         configureLogging();
100
101         // prepare defaults
102
if (adapter == null) {
103             adapter = new JdbcAdapter();
104         }
105
106         log("connection settings - [driver: "
107                 + driver
108                 + ", url: "
109                 + url
110                 + ", username: "
111                 + userName
112                 + "]", Project.MSG_VERBOSE);
113
114         log("generator options - [dropTables: "
115                 + dropTables
116                 + ", dropPK: "
117                 + dropPK
118                 + ", createTables: "
119                 + createTables
120                 + ", createPK: "
121                 + createPK
122                 + ", createFK: "
123                 + createFK
124                 + "]", Project.MSG_VERBOSE);
125
126         validateAttributes();
127
128         try {
129
130             // Load the data map and run the db generator.
131
DataMap dataMap = loadDataMap();
132             DbGenerator generator = new DbGenerator(adapter, dataMap);
133             generator.setShouldCreateFKConstraints(createFK);
134             generator.setShouldCreatePKSupport(createPK);
135             generator.setShouldCreateTables(createTables);
136             generator.setShouldDropPKSupport(dropPK);
137             generator.setShouldDropTables(dropTables);
138
139             // load driver taking custom CLASSPATH into account...
140
DriverDataSource dataSource = new DriverDataSource((Driver JavaDoc) Class.forName(
141                     driver).newInstance(), url, userName, password);
142
143             generator.runGenerator(dataSource);
144         }
145         catch (Exception JavaDoc ex) {
146             Throwable JavaDoc th = Util.unwindException(ex);
147
148             String JavaDoc message = "Error generating database";
149
150             if (th.getLocalizedMessage() != null) {
151                 message += ": " + th.getLocalizedMessage();
152             }
153
154             super.log(message);
155             throw new BuildException(message, th);
156         }
157     }
158
159     /**
160      * Validates atttributes that are not related to internal DefaultClassGenerator.
161      * Throws BuildException if attributes are invalid.
162      */

163     protected void validateAttributes() throws BuildException {
164         StringBuffer JavaDoc error = new StringBuffer JavaDoc("");
165
166         if (map == null) {
167             error.append("The 'map' attribute must be set.\n");
168         }
169
170         if (driver == null) {
171             error.append("The 'driver' attribute must be set.\n");
172         }
173
174         if (url == null) {
175             error.append("The 'adapter' attribute must be set.\n");
176         }
177
178         if (error.length() > 0) {
179             throw new BuildException(error.toString());
180         }
181     }
182
183     /** Loads and returns DataMap based on <code>map</code> attribute. */
184     protected DataMap loadDataMap() throws Exception JavaDoc {
185         InputSource JavaDoc in = new InputSource JavaDoc(map.getCanonicalPath());
186         return new MapLoader().loadDataMap(in);
187     }
188
189     public void setCreateFK(boolean createFK) {
190         this.createFK = createFK;
191     }
192
193     public void setCreatePK(boolean createPK) {
194         this.createPK = createPK;
195     }
196
197     public void setCreateTables(boolean createTables) {
198         this.createTables = createTables;
199     }
200
201     public void setDropPK(boolean dropPK) {
202         this.dropPK = dropPK;
203     }
204
205     public void setDropTables(boolean dropTables) {
206         this.dropTables = dropTables;
207     }
208
209     /**
210      * Sets the map.
211      *
212      * @param map The map to set
213      */

214     public void setMap(File JavaDoc map) {
215         this.map = map;
216     }
217
218     /**
219      * Sets the db adapter.
220      *
221      * @param adapter The db adapter to set.
222      */

223     public void setAdapter(String JavaDoc adapter) {
224
225         if (adapter != null) {
226             // Try to create an instance of the DB adapter.
227
try {
228                 Class JavaDoc c = Class.forName(adapter);
229                 this.adapter = (DbAdapter) c.newInstance();
230             }
231             catch (Exception JavaDoc e) {
232                 throw new BuildException("Can't load DbAdapter: " + adapter);
233             }
234         }
235     }
236
237     /**
238      * Sets the JDBC driver used to connect to the database server.
239      *
240      * @param driver The driver to set.
241      */

242     public void setDriver(String JavaDoc driver) {
243         this.driver = driver;
244     }
245
246     /**
247      * Sets the JDBC URL used to connect to the database server.
248      *
249      * @param url The url to set.
250      */

251     public void setUrl(String JavaDoc url) {
252         this.url = url;
253     }
254
255     /**
256      * Sets the username used to connect to the database server.
257      *
258      * @param username The username to set.
259      */

260     public void setUserName(String JavaDoc username) {
261         this.userName = username;
262     }
263
264     /**
265      * Sets the password used to connect to the database server.
266      *
267      * @param password The password to set.
268      */

269     public void setPassword(String JavaDoc password) {
270         this.password = password;
271     }
272
273 }
Popular Tags