1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import org.apache.derby.tools.JDBCDisplayUtil; 25 import org.apache.derby.iapi.tools.i18n.*; 26 27 import java.io.BufferedInputStream ; 28 import java.io.FileInputStream ; 29 import java.io.FileNotFoundException ; 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.lang.reflect.InvocationTargetException ; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.SQLException ; 37 import java.sql.SQLWarning ; 38 import java.sql.Statement ; 39 import java.sql.PreparedStatement ; 40 import java.sql.ResultSet ; 41 import java.sql.ResultSetMetaData ; 42 import java.sql.Types ; 43 44 import java.util.Properties ; 45 import java.util.Vector ; 46 import java.util.Locale ; 47 48 55 public final class util implements java.security.PrivilegedAction { 56 57 private static boolean HAVE_BIG_DECIMAL; 58 59 { 60 boolean haveBigDecimal; 61 try { 62 Class.forName("java.math.BigDecimal"); 63 haveBigDecimal = true; 64 } catch (Throwable t) { 65 haveBigDecimal = false; 66 } 67 HAVE_BIG_DECIMAL = haveBigDecimal; 68 } 69 70 private static final Class [] DS_GET_CONN_TYPES = {"".getClass(), "".getClass()}; 71 private util() {} 72 73 76 84 static public String getArg(String param, String [] args) 85 { 86 int pLocn; 87 Properties p; 88 89 if (args == null) return null; 90 91 for (pLocn=0; pLocn<args.length; pLocn++) { 92 if (param.equals(args[pLocn])) break; 93 } 94 if (pLocn >= (args.length-1)) return null; 96 97 return args[pLocn+1]; 98 } 99 100 115 static public boolean getPropertyArg(String [] args) throws IOException { 116 String n; 117 InputStream in1; 118 Properties p; 119 120 if ((n = getArg("-p", args))!= null){ 121 in1 = new FileInputStream (n); 122 in1 = new BufferedInputStream (in1); 123 } 124 else if ((n = getArg("-pr", args)) != null) { 125 in1 = getResourceAsStream(n); 126 if (in1 == null) throw ijException.resourceNotFound(); 127 } 128 else 129 return false; 130 131 p = System.getProperties(); 132 133 util.loadWithTrimmedValues(in1, p); 136 137 return true; 138 } 139 140 154 static public Properties getConnAttributeArg(String [] args) 155 throws IOException 156 { 157 String n; 158 InputStream in1; 159 Properties p = new Properties (); 160 161 if ((n = getArg("-ca", args))!= null){ 162 in1 = new FileInputStream (n); 163 in1 = new BufferedInputStream (in1); 164 } 165 else if ((n = getArg("-car", args)) != null) { 166 in1 = getResourceAsStream(n); 167 if (in1 == null) throw ijException.resourceNotFound(); 168 } 169 else 170 return null; 171 172 util.loadWithTrimmedValues(in1, p); 175 176 return p; 177 } 178 179 180 181 188 static String qualifyResourceName(String resourceName, boolean absolute) 189 { 190 resourceName=resourceName.trim(); 191 if (resourceName.startsWith("/")) 192 { 193 return resourceName; 194 } 195 else 196 { 197 String pName = util.getSystemProperty("ij.defaultResourcePackage").trim(); 198 if (pName == null) return null; 199 if ((pName).endsWith("/")) 200 resourceName = pName+resourceName; 201 else 202 resourceName = pName+"/"+resourceName; 203 if (absolute && !resourceName.startsWith("/")) 204 return null; 205 else 206 return resourceName; 207 } 208 } 209 217 static public InputStream getResourceAsStream(String resourceName) 218 { 219 Class c= util.class; 220 resourceName = qualifyResourceName(resourceName,true); 221 if (resourceName == null) 222 return null; 223 InputStream is = c.getResourceAsStream(resourceName); 224 if (is != null) 225 is = new BufferedInputStream (is, utilMain.BUFFEREDFILESIZE); 226 return is; 227 } 228 229 248 static public String getFileArg(String [] args) throws IOException { 249 String fileName; 250 int fLocn; 251 boolean foundP = false; 252 253 if (args == null) return null; 254 if ((fileName=getArg("-f",args))!=null) return fileName; 255 for (int ix=0; ix < args.length; ix++) 258 if(args[ix].equals("-f") || 259 args[ix].equals("-fr") || 260 args[ix].equals("-ca") || 261 args[ix].equals("-car") || 262 args[ix].equals("-p") || 263 args[ix].equals("-pr")) 264 ix++; else 266 return args[ix]; 267 return null; 268 } 269 270 274 static public String getInputResourceNameArg(String [] args) { 275 return getArg("-fr", args); 276 } 277 278 286 static public boolean invalidArgs(String [] args) { 287 int countSupported = 0; 288 boolean haveInput = false; 289 for (int ix=0; ix < args.length; ix++) 290 { 291 if(!haveInput && (args[ix].equals("-f") || args[ix].equals("-fr"))) 294 { 295 haveInput = true; 296 ix++; 297 if (ix >= args.length) return true; 298 } 299 300 else if ((args[ix].equals("-p") || args[ix].equals("-pr") || 301 args[ix].equals("-ca") || args[ix].equals("-car") )) 302 { 303 ix++; 305 if (ix >= args.length) return true; 306 } else if (args[ix].equals("--help")) { return true; } 307 308 else if (!haveInput) 311 { 312 haveInput = true; 313 } 314 315 else 316 { 317 return true; 318 } 319 } 320 return false; 321 } 322 323 326 static void Usage(LocalizedOutput out) { 327 out.println( 328 LocalizedResource.getMessage("IJ_UsageJavaComCloudToolsIjPPropeInput")); 329 out.flush(); 330 } 331 332 333 private static final Class [] STRING_P = { "".getClass() }; 334 private static final Class [] INT_P = { Integer.TYPE }; 335 336 337 349 static public void setupDataSource(Object ds,String dbName,boolean firstTime) throws Exception { 350 java.lang.reflect.Method [] methods = ds.getClass().getMethods(); 354 for (int i = 0; i < methods.length; i++) { 355 java.lang.reflect.Method m = methods[i]; 356 String name = m.getName(); 357 358 if (name.startsWith("set") && (name.length() > "set".length())) { 359 if(name.equals("setCreateDatabase") && !firstTime) 361 continue; 362 363 String property = name.substring("set".length()); property = "ij.dataSource."+property.substring(0,1).toLowerCase(java.util.Locale.ENGLISH)+ property.substring(1); String value = util.getSystemProperty(property); 366 if(name.equals("setDatabaseName") && !firstTime) 367 value = dbName; 368 if (value != null) { 369 try { 370 m.invoke(ds, new Object [] {value}); 372 } catch (Throwable ignore) { 373 m.invoke(ds, new Object [] {Integer.valueOf(value)}); 375 } 376 } 377 } 378 } 379 } 380 381 394 public static Connection getDataSourceConnection(String dsName,String user,String password, 395 String dbName,boolean firstTime) throws SQLException { 396 Object ds = null; try { 399 400 Class dc = Class.forName(dsName); 401 ds = dc.newInstance(); 402 403 setupDataSource(ds,dbName,firstTime); 405 406 409 java.lang.reflect.Method m = 410 user == null ? dc.getMethod("getConnection", null) : 411 dc.getMethod("getConnection", DS_GET_CONN_TYPES); 412 413 return (java.sql.Connection ) m.invoke(ds, 414 user == null ? null : new String [] {user, password}); 415 } catch (InvocationTargetException ite) 416 { 417 if (ite.getTargetException() instanceof SQLException ) 418 throw (SQLException ) ite.getTargetException(); 419 ite.printStackTrace(System.out); 420 } catch (Exception e) 421 { 422 e.printStackTrace(System.out); 423 } 424 return null; 425 } 426 427 445 static public Connection startJBMS(String defaultDriver, String defaultURL, 446 Properties connInfo) 447 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException 448 { 449 Connection con = null; 450 String driverName; 451 String databaseURL; 452 453 driverName = util.getSystemProperty("driver"); 455 if (driverName == null) driverName = util.getSystemProperty("ij.driver"); 456 if (driverName == null || driverName.length()==0) driverName = defaultDriver; 457 if (driverName != null) { 458 util.loadDriver(driverName); 459 } 460 461 String jdbcProtocol = util.getSystemProperty("ij.protocol"); 462 if (jdbcProtocol != null) 463 util.loadDriverIfKnown(jdbcProtocol); 464 465 String user = util.getSystemProperty("ij.user"); 466 String password = util.getSystemProperty("ij.password"); 467 468 databaseURL = util.getSystemProperty("database"); 470 if (databaseURL == null) databaseURL = util.getSystemProperty("ij.database"); 471 if (databaseURL == null || databaseURL.length()==0) databaseURL = defaultURL; 472 if (databaseURL != null) { 473 if (databaseURL.startsWith("jdbc:")) 476 util.loadDriverIfKnown(databaseURL); 477 if (!databaseURL.startsWith("jdbc:") && jdbcProtocol != null) 478 databaseURL = jdbcProtocol+databaseURL; 479 480 483 connInfo = updateConnInfo(user, password,connInfo); 484 485 String driver = util.getSystemProperty("driver"); 487 if (driver == null) { 488 driver = "org.apache.derby.jdbc.EmbeddedDriver"; 489 } 490 491 loadDriver(driver); 492 con = DriverManager.getConnection(databaseURL,connInfo); 493 return con; 494 } 495 496 String dsName = util.getSystemProperty("ij.dataSource"); 498 if (dsName == null) 499 return null; 500 501 con = getDataSourceConnection(dsName,user,password,null,true); 504 return con; 505 } 506 507 508 public static Properties updateConnInfo(String user, String password, Properties connInfo) 509 { 510 String ijGetMessages = util.getSystemProperty("ij.retrieveMessagesFromServerOnGetMessage"); 511 boolean retrieveMessages = false; 512 513 514 if (isJCCFramework()) 516 retrieveMessages = true; 517 518 if (ijGetMessages != null) 519 { 520 if (ijGetMessages.equals("false")) 521 retrieveMessages = false; 522 else 523 retrieveMessages = true; 524 525 } 526 527 if (connInfo == null) 528 connInfo = new Properties (); 529 530 if (retrieveMessages == true) 531 { 532 connInfo.put("retrieveMessagesFromServerOnGetMessage", 533 "true"); 534 } 535 if (user != null) 536 connInfo.put("user",user); 537 if (password != null) 538 connInfo.put("password", password); 539 540 return connInfo; 541 } 542 543 552 static public Connection startJBMS() throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 553 return startJBMS(null,null); 554 } 555 556 569 static public Connection startJBMS(String defaultDriver, String defaultURL) 570 throws SQLException , ClassNotFoundException , InstantiationException , 571 IllegalAccessException { 572 return startJBMS(defaultDriver,defaultURL,null); 573 574 } 575 579 580 583 public static void DisplayVector(LocalizedOutput out, Vector v) { 584 int l = v.size(); 585 for (int i=0;i<l;i++) 586 out.println(v.elementAt(i)); 587 } 588 589 605 606 615 public static void DisplayMulti(LocalizedOutput out, PreparedStatement ps, 616 ResultSet rs, Connection conn) throws SQLException , ijException { 617 618 boolean autoCommited = false; boolean exec = false; boolean anotherUsingRow = false; ResultSetMetaData rsmd = rs.getMetaData(); 623 int numCols = rsmd.getColumnCount(); 624 625 629 anotherUsingRow = rs.next(); 630 631 while (! autoCommited && anotherUsingRow) { 632 if (!exec) { 634 exec = true; 635 636 if (conn.getAutoCommit()) { 638 out.println(LocalizedResource.getMessage("IJ_IjWarniAutocMayCloseUsingResulSet")); 639 autoCommited = true; 640 } 641 } 642 643 for (int c=1; c<=numCols; c++) { 646 int sqlType = rsmd.getColumnType(c); 647 648 if (sqlType == Types.DECIMAL) 649 { 650 if (util.HAVE_BIG_DECIMAL) 651 { 652 ps.setObject(c,rs.getObject(c), 653 sqlType, 654 rsmd.getScale(c)); 655 } 656 else 657 { 658 switch (ps.getMetaData().getColumnType(c)) 665 { 666 case Types.BIGINT: 667 ps.setLong(c, rs.getLong(c)); 668 break; 669 case Types.INTEGER: 670 case Types.SMALLINT: 671 case Types.TINYINT: 672 ps.setInt(c, rs.getInt(c)); 673 break; 674 default: 675 ps.setString(c,rs.getString(c)); 676 break; 677 } 678 } 679 680 } 681 else 682 { 683 ps.setObject(c,rs.getObject(c), 684 sqlType); 685 } 686 687 688 689 } 690 691 692 anotherUsingRow = rs.next(); 694 if (! anotherUsingRow || conn.getAutoCommit()) { 698 rs.close(); 699 } 700 701 704 705 ps.execute(); 706 JDBCDisplayUtil.DisplayResults(out,ps,conn); 707 708 711 ps.clearParameters(); 712 } 713 if (!exec) { 714 rs.close(); throw ijException.noUsingResults(); 716 } 717 } 720 721 static final String getSystemProperty(String propertyName) { 722 try 723 { 724 if (propertyName.startsWith("ij.") || propertyName.startsWith("derby.")) 725 { 726 util u = new util(); 727 u.key = propertyName; 728 return (String ) java.security.AccessController.doPrivileged(u); 729 } 730 else 731 { 732 return System.getProperty(propertyName); 733 } 734 } catch (SecurityException se) { 735 return null; 736 } 737 } 738 739 private String key; 740 741 public final Object run() { 742 return System.getProperty(key); 743 } 744 773 private static void loadWithTrimmedValues(InputStream iStr, 774 Properties prop) throws IOException { 775 776 Properties p = new Properties (); 778 p.load(iStr); 779 780 for (java.util.Enumeration propKeys = p.propertyNames(); 784 propKeys.hasMoreElements();) { 785 String tmpKey = (String )propKeys.nextElement(); 788 String tmpValue = p.getProperty(tmpKey); 789 tmpValue = tmpValue.trim(); 790 prop.put(tmpKey, tmpValue); 791 } 792 793 return; 794 795 } 796 797 private static final String [][] protocolDrivers = 798 { 799 { "jdbc:derby:net:", "com.ibm.db2.jcc.DB2Driver"}, 800 { "jdbc:derby://", "org.apache.derby.jdbc.ClientDriver"}, 801 802 { "jdbc:derby:", "org.apache.derby.jdbc.EmbeddedDriver" }, 803 }; 804 805 817 public static void loadDriverIfKnown(String jdbcProtocol) throws ClassNotFoundException , InstantiationException , IllegalAccessException { 818 for (int i=0; i < protocolDrivers.length; i++) { 819 if (jdbcProtocol.startsWith(protocolDrivers[i][0])) { 820 loadDriver(protocolDrivers[i][1]); 821 break; } 823 } 824 } 825 826 835 public static void loadDriver(String driverClass) throws ClassNotFoundException , InstantiationException , IllegalAccessException { 836 Class.forName(driverClass).newInstance(); 837 } 838 839 848 private static boolean isJCCFramework() 849 { 850 String framework = util.getSystemProperty("framework"); 851 return ((framework != null) && 852 ((framework.toUpperCase(Locale.ENGLISH).equals("DERBYNET")) || 853 (framework.toUpperCase(Locale.ENGLISH).indexOf("JCC") != -1))); 854 } 855 856 865 public static String getSelectedSchema(Connection theConnection) throws SQLException { 866 String schema = null; 867 if (theConnection == null) 868 return null; 869 Statement st = theConnection.createStatement(); 870 try { 871 if(!st.execute("VALUES CURRENT SCHEMA")) 872 return null; 873 874 ResultSet rs = st.getResultSet(); 875 if(rs==null || !rs.next()) 876 return null; 877 schema = rs.getString(1); 878 } catch(SQLException e) { 879 } finally { 882 st.close(); 883 } 884 return schema; 885 } 886 } 887 888 | Popular Tags |