KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.tools;
21
22 import java.io.File JavaDoc;
23 import java.sql.Driver JavaDoc;
24
25 import org.apache.cayenne.access.DbGenerator;
26 import org.apache.cayenne.conn.DriverDataSource;
27 import org.apache.cayenne.dba.DbAdapter;
28 import org.apache.cayenne.dba.JdbcAdapter;
29 import org.apache.cayenne.map.DataMap;
30 import org.apache.cayenne.map.MapLoader;
31 import org.apache.cayenne.util.Util;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.Task;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * An Ant Task that is a frontend to Cayenne DbGenerator allowing schema generation from
39  * DataMap using Ant.
40  *
41  * @author nirvdrum, Andrus Adamchik
42  * @since 1.2
43  */

44 // TODO: support classpath attribute for loading the driver
45
public class DbGeneratorTask extends Task {
46
47     protected DbAdapter adapter;
48     protected File JavaDoc map;
49     protected String JavaDoc driver;
50     protected String JavaDoc url;
51     protected String JavaDoc userName;
52     protected String JavaDoc password;
53
54     // DbGenerator options... setup defaults similar to DbGemerator itself:
55
// all DROP set to false, all CREATE - to true
56
protected boolean dropTables;
57     protected boolean dropPK;
58     protected boolean createTables = true;
59     protected boolean createPK = true;
60     protected boolean createFK = true;
61
62
63     public void execute() {
64
65         // prepare defaults
66
if (adapter == null) {
67             adapter = new JdbcAdapter();
68         }
69
70         log("connection settings - [driver: "
71                 + driver
72                 + ", url: "
73                 + url
74                 + ", username: "
75                 + userName
76                 + "]", Project.MSG_VERBOSE);
77
78         log("generator options - [dropTables: "
79                 + dropTables
80                 + ", dropPK: "
81                 + dropPK
82                 + ", createTables: "
83                 + createTables
84                 + ", createPK: "
85                 + createPK
86                 + ", createFK: "
87                 + createFK
88                 + "]", Project.MSG_VERBOSE);
89
90         validateAttributes();
91
92         try {
93
94             // Load the data map and run the db generator.
95
DataMap dataMap = loadDataMap();
96             DbGenerator generator = new DbGenerator(adapter, dataMap);
97             generator.setShouldCreateFKConstraints(createFK);
98             generator.setShouldCreatePKSupport(createPK);
99             generator.setShouldCreateTables(createTables);
100             generator.setShouldDropPKSupport(dropPK);
101             generator.setShouldDropTables(dropTables);
102
103             // load driver taking custom CLASSPATH into account...
104
DriverDataSource dataSource = new DriverDataSource((Driver JavaDoc) Class.forName(
105                     driver).newInstance(), url, userName, password);
106
107             generator.runGenerator(dataSource);
108         }
109         catch (Exception JavaDoc ex) {
110             Throwable JavaDoc th = Util.unwindException(ex);
111
112             String JavaDoc message = "Error generating database";
113
114             if (th.getLocalizedMessage() != null) {
115                 message += ": " + th.getLocalizedMessage();
116             }
117
118             super.log(message);
119             throw new BuildException(message, th);
120         }
121     }
122
123     /**
124      * Validates atttributes that are not related to internal DefaultClassGenerator.
125      * Throws BuildException if attributes are invalid.
126      */

127     protected void validateAttributes() throws BuildException {
128         StringBuffer JavaDoc error = new StringBuffer JavaDoc("");
129
130         if (map == null) {
131             error.append("The 'map' attribute must be set.\n");
132         }
133
134         if (driver == null) {
135             error.append("The 'driver' attribute must be set.\n");
136         }
137
138         if (url == null) {
139             error.append("The 'adapter' attribute must be set.\n");
140         }
141
142         if (error.length() > 0) {
143             throw new BuildException(error.toString());
144         }
145     }
146
147     /** Loads and returns DataMap based on <code>map</code> attribute. */
148     protected DataMap loadDataMap() throws Exception JavaDoc {
149         InputSource JavaDoc in = new InputSource JavaDoc(map.getCanonicalPath());
150         return new MapLoader().loadDataMap(in);
151     }
152
153     public void setCreateFK(boolean createFK) {
154         this.createFK = createFK;
155     }
156
157     public void setCreatePK(boolean createPK) {
158         this.createPK = createPK;
159     }
160
161     public void setCreateTables(boolean createTables) {
162         this.createTables = createTables;
163     }
164
165     public void setDropPK(boolean dropPK) {
166         this.dropPK = dropPK;
167     }
168
169     public void setDropTables(boolean dropTables) {
170         this.dropTables = dropTables;
171     }
172
173     /**
174      * Sets the map.
175      *
176      * @param map The map to set
177      */

178     public void setMap(File JavaDoc map) {
179         this.map = map;
180     }
181
182     /**
183      * Sets the db adapter.
184      *
185      * @param adapter The db adapter to set.
186      */

187     public void setAdapter(String JavaDoc adapter) {
188
189         if (adapter != null) {
190             // Try to create an instance of the DB adapter.
191
try {
192                 Class JavaDoc c = Class.forName(adapter);
193                 this.adapter = (DbAdapter) c.newInstance();
194             }
195             catch (Exception JavaDoc e) {
196                 throw new BuildException("Can't load DbAdapter: " + adapter);
197             }
198         }
199     }
200
201     /**
202      * Sets the JDBC driver used to connect to the database server.
203      *
204      * @param driver The driver to set.
205      */

206     public void setDriver(String JavaDoc driver) {
207         this.driver = driver;
208     }
209
210     /**
211      * Sets the JDBC URL used to connect to the database server.
212      *
213      * @param url The url to set.
214      */

215     public void setUrl(String JavaDoc url) {
216         this.url = url;
217     }
218
219     /**
220      * Sets the username used to connect to the database server.
221      *
222      * @param username The username to set.
223      */

224     public void setUserName(String JavaDoc username) {
225         this.userName = username;
226     }
227
228     /**
229      * Sets the password used to connect to the database server.
230      *
231      * @param password The password to set.
232      */

233     public void setPassword(String JavaDoc password) {
234         this.password = password;
235     }
236
237 }
238
Popular Tags