KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > ant > CreateJdbcSchemaTask


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.ant;
13
14 import com.versant.core.jdbc.JdbcStorageManager;
15 import com.versant.core.jdbc.JdbcConnectionSource;
16 import com.versant.core.jdbc.JdbcStorageManagerFactory;
17 import com.versant.core.jdbc.metadata.JdbcTable;
18 import com.versant.core.util.BeanUtils;
19
20 import java.io.*;
21 import java.sql.Connection JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.*;
24
25 import com.versant.core.logging.LogEventStore;
26
27 /**
28  * This ant task will create the JDBC schema for a set of .jdo files and
29  * classes. The classes do not have to be enhanced first. The schema may
30  * be written to one or more sql script files or may be generated directly.
31  * This can also be used outside of Ant by creating an instance and setting
32  * properties or by using the main method and command line args.
33  * <p/>
34  * This also supports Versant ODBMS.
35  */

36 public class CreateJdbcSchemaTask extends JdoTaskBase {
37
38     public static void main(String JavaDoc[] args) {
39         try {
40             CreateJdbcSchemaTask t = new CreateJdbcSchemaTask();
41             BeanUtils.setCommandLineArgs(t, args, new String JavaDoc[]{
42                 "config", "project", "outputdir", "destdir", "direct",
43                 "droptables", "createtables", "validate", "comments",
44                 "out"
45             });
46             t.execute();
47         } catch (IllegalArgumentException JavaDoc x) {
48             System.err.println(x.getMessage());
49             System.err.println(HELP);
50         } catch (Exception JavaDoc x) {
51             x.printStackTrace();
52         }
53     }
54
55     private static final String JavaDoc HELP =
56             "Usage: java com.versant.core.jdo.tools.ant.CreateJdbcSchemaTask\n" +
57             " [-out <name of file for SQL script, default versant.sql>]\n" +
58             " [-droptables <true | false>]\n" +
59             " [-createtables <true | false>]\n\n" +
60             " [-validate <true | false>]\n\n" +
61             " [-comments <true | false>]\n\n" +
62             " [-project <name of project file, default versant.properties>]\n" +
63             "WARNING: The droptables option will drop all tables with the same\n" +
64             " names (case insensitive) as tables in the generated schema!\n\n" +
65             "This Class can also be used as an Ant task. The command line arguments\n" +
66             "have the same names and functions as the task attributes.\n";
67
68     protected String JavaDoc outName = "versant.sql";
69     protected boolean direct;
70     protected boolean droptables;
71     protected boolean validate;
72     protected boolean comments = true;
73     private String JavaDoc logEvents = LogEventStore.LOG_EVENTS_ERRORS;
74
75     public void setOut(String JavaDoc out) {
76         this.outName = out;
77     }
78
79     public void setOutputdir(String JavaDoc dir) {
80         setDestdir(dir);
81     }
82
83     public void setDestdir(String JavaDoc destdir) {
84         this.outName = destdir + "/versant.sql";
85     }
86
87     public void setCreatetables(String JavaDoc direct) {
88         setDirect(direct);
89     }
90
91     public void setDirect(String JavaDoc s) {
92         direct = isTrue(s);
93     }
94
95     private static boolean isTrue(String JavaDoc s) {
96         return "*".equals(s) || "true".equals(s);
97     }
98
99     public void setDroptables(String JavaDoc s) {
100         droptables = isTrue(s);
101     }
102
103     public void setValidate(String JavaDoc s) {
104         validate = isTrue(s);
105     }
106
107     public void setLogEvents(String JavaDoc logEvents) {
108         this.logEvents = logEvents;
109     }
110
111     /**
112      * Include comments in the output?
113      */

114     public void setComments(boolean comments) {
115         this.comments = comments;
116     }
117
118     public void execute() {
119         super.execute();
120         if (!(innermostSmf instanceof JdbcStorageManagerFactory)) {
121             return;
122         }
123         pes.setLogEvents(logEvents);
124         JdbcStorageManager sm = (JdbcStorageManager)innermostSmf.getStorageManager();
125         if (droptables) dropAllTables(sm);
126         generateDatabase(sm, direct);
127         if (validate) validateDatabase(sm);
128         innermostSmf.returnStorageManager(sm);
129     }
130
131     private void dropAllTables(JdbcStorageManager sm) {
132         JdbcConnectionSource conSrc = sm.getJdbcConnectionSource();
133         Connection JavaDoc con = null;
134         try {
135             log("Dropping tables in schema on " + conSrc.getURL());
136             con = conSrc.getConnection(false, true);
137             HashMap dbTableNames = sm.getDatabaseTableNames(con);
138             ArrayList a = sm.getJdbcMetaData().getTables();
139             for (int i = 0; i < a.size(); i++) {
140                 JdbcTable t = (JdbcTable)a.get(i);
141                 String JavaDoc name = (String JavaDoc)dbTableNames.get(t.name.toLowerCase());
142                 if (name != null) {
143                     sm.getSqlDriver().dropTable(con, name);
144                 }
145             }
146         } catch (SQLException JavaDoc x) {
147             throwBuildException(x.getClass().getName() + ": " +
148                     x.getMessage(), x);
149         } finally {
150             if (con != null) {
151                 try {
152                     conSrc.returnConnection(con);
153                 } catch (SQLException JavaDoc e) {
154                     // ignore
155
}
156             }
157         }
158     }
159
160     private void generateDatabase(JdbcStorageManager sm, boolean direct) {
161         JdbcConnectionSource conSrc = sm.getJdbcConnectionSource();
162         Connection JavaDoc con = null;
163         FileOutputStream fout = null;
164         PrintWriter out = null;
165         try {
166             File f = new File(outName);
167             log("Creating " + f);
168             fout = new FileOutputStream(f);
169             out = new PrintWriter(fout);
170             if (direct) {
171                 log("Creating schema on " + conSrc.getURL());
172                 con = conSrc.getConnection(false, true);
173             }
174             sm.getSqlDriver().generateDDL(sm.getJdbcMetaData().getTables(),
175                     con, out, comments);
176             if (out != null) {
177                 out.flush();
178             }
179         } catch (Exception JavaDoc x) {
180             throwBuildException(x.getClass().getName() + ": " +
181                     x.getMessage(), x);
182         } finally {
183             if (fout != null) {
184                 try {
185                     fout.close();
186                 } catch (IOException e) {
187                     // ignore
188
}
189             }
190             if (con != null) {
191                 try {
192                     conSrc.returnConnection(con);
193                 } catch (SQLException JavaDoc e) {
194                     // ignore
195
}
196             }
197         }
198     }
199
200     private void validateDatabase(JdbcStorageManager sm) {
201         JdbcConnectionSource conSrc = sm.getJdbcConnectionSource();
202         Connection JavaDoc con = null;
203         try {
204             log("Validating mapping for " + conSrc.getURL());
205             con = conSrc.getConnection(false, false);
206             StringWriter error = new StringWriter();
207             PrintWriter perror = new PrintWriter(error, false);
208             StringWriter fix = new StringWriter();
209             PrintWriter pfix = new PrintWriter(fix, false);
210             if (!sm.getSqlDriver().checkDDL(
211                     sm.getJdbcMetaData().getTables(true), con, perror, pfix,
212                     sm.getJdbcMetaData().getMigrationParams())) {
213                 log(error.toString());
214                 throwBuildException("Mapping for " +
215                         conSrc.getURL() + " has errors");
216             }
217         } catch (Exception JavaDoc x) {
218             throwBuildException(x.getClass().getName() + ": " +
219                     x.getMessage(), x);
220         } finally {
221             if (con != null) {
222                 try {
223                     conSrc.returnConnection(con);
224                 } catch (SQLException JavaDoc e) {
225                     // ignore
226
}
227             }
228         }
229     }
230
231 }
232
Popular Tags