KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > mapping > ejb > CaptureSchema


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * CaptureSchema.java
26  *
27  * Created on March 15, 2002, 4:02 PM
28  */

29
30 package com.sun.jdo.api.persistence.mapping.ejb;
31
32 import org.netbeans.modules.dbschema.*;
33 import org.netbeans.modules.dbschema.jdbcimpl.*;
34 import org.netbeans.modules.dbschema.util.*;
35
36 import java.io.*;
37
38 import java.util.ResourceBundle JavaDoc;
39
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.DatabaseMetaData JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.LinkedList JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 import java.text.MessageFormat JavaDoc;
49
50 /**
51  *
52  * @author vkraemer
53  * @version
54  */

55 public class CaptureSchema {
56     /** Required filename extension. */
57     private static final String JavaDoc OUTPUTFILE_EXTENSION = ".dbschema"; // NOI18N
58

59     /** Creates new CaptureSchema */
60     public CaptureSchema() {
61     }
62     
63     public static void main(String JavaDoc args[]) {
64         int help = 0;
65         ResourceBundle JavaDoc bundle =
66             ResourceBundle.getBundle("com.sun.jdo.api.persistence.mapping.ejb.Bundle"); // NOI18N
67
String JavaDoc driver=bundle.getString("STRING_ORACLE_DRIVER_NOI18N"); //NOI18N
68
String JavaDoc username=bundle.getString("STRING_IASCTS_NOI18N"); //NOI18N
69
String JavaDoc password=bundle.getString("STRING_IASCTS_NOI18N"); //NOI18N
70
String JavaDoc dburl=null;
71         String JavaDoc dbschemaname = null;
72         String JavaDoc outfile = null;
73         LinkedList JavaDoc tableList = new LinkedList JavaDoc();
74         LinkedList JavaDoc vList = new LinkedList JavaDoc();
75         try {
76             for (int i=0; i<args.length; i++) {
77                 if (args[i].equals(bundle.getString("CMD_FLAG_DRIVER"))) { //NOI18N
78
driver = args[++i];
79                     help++;
80                 }
81                 else if (args[i].equals(bundle.getString("CMD_FLAG_SCHEMA_NAME"))) { //NOI18N
82
dbschemaname = args[++i];
83                     help++;
84                     
85                 }
86                 else if (args[i].equals(bundle.getString("CMD_FLAG_USERNAME"))) { //NOI18N
87
username = args[++i];
88                     help++;
89                     
90                 }
91                 else if (args[i].equals(bundle.getString("CMD_FLAG_PASSWORD"))) { //NOI18N
92
password = args[++i];
93                     help++;
94                 }
95                 else if (args[i].equals(bundle.getString("CMD_FLAG_DBURL"))) { //NOI18N
96
dburl = args[++i];
97                     help++;
98                 }
99                 else if (args[i].equals(bundle.getString("CMD_FLAG_TABLE"))) { //NOI18N
100
tableList.add(args[++i]);
101                     //vList.add(args[i]);
102
help++;
103                 }
104                 else if (args[i].equals(bundle.getString("CMD_FLAG_OUTPUT"))) { //NOI18N
105
outfile = args[++i];
106                     help++;
107                 }
108                 
109             }
110         }
111         catch(Exception JavaDoc e) {
112             help = 0;
113         }
114         
115         if (help < 1 || null == outfile) {
116             System.err.println(bundle.getString("HELP_USAGE_LABEL")); //NOI18N
117
System.err.println(bundle.getString("HELP_USAGE")); //NOI18N
118
System.err.println(bundle.getString("HELP_WHERE")); //NOI18N
119
System.err.println(bundle.getString("HELP_USERNAME_ARG")); //NOI18N
120
System.err.println(bundle.getString("HELP_PASSWORD_ARG")); //NOI18N
121
System.err.println(bundle.getString("HELP_URL_ARG")); //NOI18N
122
System.err.println(bundle.getString("HELP_DRIVER_ARG")); //NOI18N
123
System.err.println(bundle.getString("HELP_OUTPUT_ARG")); //NOI18N
124
System.err.println(bundle.getString("HELP_SCHEMA_ARG")); //NOI18N
125
System.err.println(bundle.getString("HELP_TABLE_ARG")); //NOI18N
126
System.err.println(bundle.getString("HELP_OPTIONAL_INFO")); //NOI18N
127
System.exit(0);
128         }
129
130         // Ensure that outfile ends with OUTPUTFILE_EXTENSION, warning user if
131
// we change their given name.
132
if (!outfile.endsWith(OUTPUTFILE_EXTENSION)) {
133             System.err.println(bundle.getString("MESSAGE_CHANGING_OUTFILENAME") // NOI18N
134
+ OUTPUTFILE_EXTENSION);
135             outfile += OUTPUTFILE_EXTENSION;
136         }
137         
138         ConnectionProvider cp = null;
139         try {
140             System.err.println(bundle.getString("MESSAGE_USING_URL")+ dburl); //NOI18N
141
System.err.println(bundle.getString("MESSAGE_USING_USERNAME")+ username); //NOI18N
142
System.err.println(bundle.getString("MESSAGE_USING_PASSWORD")+ password); //NOI18N
143
System.err.println(bundle.getString("MESSAGE_USING_DRIVER")+ driver); //NOI18N
144
System.err.println(bundle.getString("MESSAGE_USING_SCHEMANAME")+ dbschemaname); //NOI18N
145
System.err.println(bundle.getString("MESSAGE_USING_OUTFILENAME")+ outfile); //NOI18N
146

147             // prepare the connection
148
cp = new ConnectionProvider(driver, dburl,username, password);
149             if (null != dbschemaname)
150                 cp.setSchema(dbschemaname); //schema in the database you want to capture; needs to be improved and set in constructor probably
151

152             System.err.println(bundle.getString("MESSAGE_CAPTURING_SCHEMA") + dbschemaname);//NOI18N
153

154             SchemaElementImpl outSchemaImpl = new SchemaElementImpl(cp);
155             SchemaElement se = new SchemaElement(outSchemaImpl);
156             
157             if (null != dbschemaname)
158                 se.setName(DBIdentifier.create(bundle.getString("STRING_SCHEMAS_SLASH_NOI18N") + dbschemaname));//NOI18N
159
else
160                 se.setName(DBIdentifier.create("")); //NOI18N
161

162             if (dburl.indexOf(bundle.getString("STRING_ORACLE_JDBC_URL_PREFIX_NOI18N")) > -1 && //NOI18N
163
(null == dbschemaname || dbschemaname.length() == 0)) {
164                 // this argument combo has problems. print an error message and exit
165
System.err.println(bundle.getString("ERR_ORACLE_ARGUMENTS")); //NOI18N
166
return;
167             }
168             
169             if (tableList.size() == 0) {
170                 outSchemaImpl.initTables(cp);
171             }
172             else {
173                 pruneTableList(tableList, cp,bundle);
174                 if (tableList.size() > 0)
175                     outSchemaImpl.initTables(cp, tableList,vList,false);
176                 else {
177                     System.err.println(bundle.getString("MESSAGE_NO_VALID_TABLES")); //NOI18N
178
return;
179                 }
180             }
181             System.err.println(bundle.getString("MESSAGE_SCHEMA_CAPTURED")); //NOI18N
182

183             System.err.println(bundle.getString("MESSAGE_SAVING_SCHEMA")); //NOI18N
184
OutputStream outstream = new FileOutputStream(outfile);
185             se.save(outstream);
186         }
187         catch (java.lang.ClassNotFoundException JavaDoc cnfe) {
188             System.err.println(bundle.getString("ERR_CHECK_CLASSPATH")); //NOI18N
189
cnfe.printStackTrace(System.err); //NOI18N
190
}
191         catch (Exception JavaDoc exc) {
192             exc.printStackTrace(System.err); //NOI18N
193
}
194         finally {
195             if (cp != null) {
196                 cp.closeConnection();
197             }
198         }
199     }
200     
201     private static void pruneTableList(List JavaDoc tableList, ConnectionProvider cp,
202             ResourceBundle JavaDoc bundle) throws java.sql.SQLException JavaDoc {
203         ResultSet JavaDoc rs;
204         DatabaseMetaData JavaDoc dmd = cp.getDatabaseMetaData();
205         Map JavaDoc tables = new HashMap JavaDoc();
206         String JavaDoc catalog = cp.getConnection().getCatalog();
207         String JavaDoc user = cp.getSchema();
208
209         rs = dmd.getTables(catalog, user, "%", new String JavaDoc[] {"TABLE"}); //NOI18N
210
if (rs != null) {
211             while (rs.next()) {
212                 String JavaDoc tn = rs.getString("TABLE_NAME").trim(); //NOI18N
213
tables.put(tn,tn);
214             }
215             rs.close();
216         }
217         Iterator JavaDoc iter = tableList.iterator();
218         String JavaDoc [] args = new String JavaDoc[1];
219         while (iter.hasNext()) {
220             String JavaDoc s = (String JavaDoc) iter.next();
221             if (null == tables.get(s.trim())) {
222                 iter.remove();
223                 args[0] = s;
224                 System.err.println(MessageFormat.format(bundle.getString("ERR_INVALID_TABLE_GIVEN"), (Object JavaDoc []) args)); //NOI18N
225
}
226         }
227     }
228            
229     static class PropChangeReport implements java.beans.PropertyChangeListener JavaDoc {
230         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc pce) {
231             //System.err.println(pce); //NOI18N
232
}
233     }
234 }
235
Popular Tags