1 23 24 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 ; 39 40 import java.sql.ResultSet ; 41 import java.sql.DatabaseMetaData ; 42 import java.util.Map ; 43 import java.util.HashMap ; 44 import java.util.List ; 45 import java.util.LinkedList ; 46 import java.util.Iterator ; 47 48 import java.text.MessageFormat ; 49 50 55 public class CaptureSchema { 56 57 private static final String OUTPUTFILE_EXTENSION = ".dbschema"; 59 60 public CaptureSchema() { 61 } 62 63 public static void main(String args[]) { 64 int help = 0; 65 ResourceBundle bundle = 66 ResourceBundle.getBundle("com.sun.jdo.api.persistence.mapping.ejb.Bundle"); String driver=bundle.getString("STRING_ORACLE_DRIVER_NOI18N"); String username=bundle.getString("STRING_IASCTS_NOI18N"); String password=bundle.getString("STRING_IASCTS_NOI18N"); String dburl=null; 71 String dbschemaname = null; 72 String outfile = null; 73 LinkedList tableList = new LinkedList (); 74 LinkedList vList = new LinkedList (); 75 try { 76 for (int i=0; i<args.length; i++) { 77 if (args[i].equals(bundle.getString("CMD_FLAG_DRIVER"))) { driver = args[++i]; 79 help++; 80 } 81 else if (args[i].equals(bundle.getString("CMD_FLAG_SCHEMA_NAME"))) { dbschemaname = args[++i]; 83 help++; 84 85 } 86 else if (args[i].equals(bundle.getString("CMD_FLAG_USERNAME"))) { username = args[++i]; 88 help++; 89 90 } 91 else if (args[i].equals(bundle.getString("CMD_FLAG_PASSWORD"))) { password = args[++i]; 93 help++; 94 } 95 else if (args[i].equals(bundle.getString("CMD_FLAG_DBURL"))) { dburl = args[++i]; 97 help++; 98 } 99 else if (args[i].equals(bundle.getString("CMD_FLAG_TABLE"))) { tableList.add(args[++i]); 101 help++; 103 } 104 else if (args[i].equals(bundle.getString("CMD_FLAG_OUTPUT"))) { outfile = args[++i]; 106 help++; 107 } 108 109 } 110 } 111 catch(Exception e) { 112 help = 0; 113 } 114 115 if (help < 1 || null == outfile) { 116 System.err.println(bundle.getString("HELP_USAGE_LABEL")); System.err.println(bundle.getString("HELP_USAGE")); System.err.println(bundle.getString("HELP_WHERE")); System.err.println(bundle.getString("HELP_USERNAME_ARG")); System.err.println(bundle.getString("HELP_PASSWORD_ARG")); System.err.println(bundle.getString("HELP_URL_ARG")); System.err.println(bundle.getString("HELP_DRIVER_ARG")); System.err.println(bundle.getString("HELP_OUTPUT_ARG")); System.err.println(bundle.getString("HELP_SCHEMA_ARG")); System.err.println(bundle.getString("HELP_TABLE_ARG")); System.err.println(bundle.getString("HELP_OPTIONAL_INFO")); System.exit(0); 128 } 129 130 if (!outfile.endsWith(OUTPUTFILE_EXTENSION)) { 133 System.err.println(bundle.getString("MESSAGE_CHANGING_OUTFILENAME") + OUTPUTFILE_EXTENSION); 135 outfile += OUTPUTFILE_EXTENSION; 136 } 137 138 ConnectionProvider cp = null; 139 try { 140 System.err.println(bundle.getString("MESSAGE_USING_URL")+ dburl); System.err.println(bundle.getString("MESSAGE_USING_USERNAME")+ username); System.err.println(bundle.getString("MESSAGE_USING_PASSWORD")+ password); System.err.println(bundle.getString("MESSAGE_USING_DRIVER")+ driver); System.err.println(bundle.getString("MESSAGE_USING_SCHEMANAME")+ dbschemaname); System.err.println(bundle.getString("MESSAGE_USING_OUTFILENAME")+ outfile); 147 cp = new ConnectionProvider(driver, dburl,username, password); 149 if (null != dbschemaname) 150 cp.setSchema(dbschemaname); 152 System.err.println(bundle.getString("MESSAGE_CAPTURING_SCHEMA") + dbschemaname); 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)); else 160 se.setName(DBIdentifier.create("")); 162 if (dburl.indexOf(bundle.getString("STRING_ORACLE_JDBC_URL_PREFIX_NOI18N")) > -1 && (null == dbschemaname || dbschemaname.length() == 0)) { 164 System.err.println(bundle.getString("ERR_ORACLE_ARGUMENTS")); 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")); return; 179 } 180 } 181 System.err.println(bundle.getString("MESSAGE_SCHEMA_CAPTURED")); 183 System.err.println(bundle.getString("MESSAGE_SAVING_SCHEMA")); OutputStream outstream = new FileOutputStream(outfile); 185 se.save(outstream); 186 } 187 catch (java.lang.ClassNotFoundException cnfe) { 188 System.err.println(bundle.getString("ERR_CHECK_CLASSPATH")); cnfe.printStackTrace(System.err); } 191 catch (Exception exc) { 192 exc.printStackTrace(System.err); } 194 finally { 195 if (cp != null) { 196 cp.closeConnection(); 197 } 198 } 199 } 200 201 private static void pruneTableList(List tableList, ConnectionProvider cp, 202 ResourceBundle bundle) throws java.sql.SQLException { 203 ResultSet rs; 204 DatabaseMetaData dmd = cp.getDatabaseMetaData(); 205 Map tables = new HashMap (); 206 String catalog = cp.getConnection().getCatalog(); 207 String user = cp.getSchema(); 208 209 rs = dmd.getTables(catalog, user, "%", new String [] {"TABLE"}); if (rs != null) { 211 while (rs.next()) { 212 String tn = rs.getString("TABLE_NAME").trim(); tables.put(tn,tn); 214 } 215 rs.close(); 216 } 217 Iterator iter = tableList.iterator(); 218 String [] args = new String [1]; 219 while (iter.hasNext()) { 220 String s = (String ) 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 []) args)); } 226 } 227 } 228 229 static class PropChangeReport implements java.beans.PropertyChangeListener { 230 public void propertyChange(java.beans.PropertyChangeEvent pce) { 231 } 233 } 234 } 235 | Popular Tags |