1 21 22 package org.apache.derby.jdbc; 23 24 import java.io.Serializable ; 25 import java.io.PrintWriter ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.util.StringTokenizer ; 29 import java.util.NoSuchElementException ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.Modifier ; 34 35 import javax.naming.RefAddr ; 36 import javax.naming.Referenceable ; 37 import javax.naming.Reference ; 38 import javax.naming.NamingException ; 39 import javax.naming.StringRefAddr ; 40 41 import org.apache.derby.client.am.Configuration; 42 import org.apache.derby.client.am.LogWriter; 43 import org.apache.derby.client.am.SqlException; 44 import org.apache.derby.client.am.Connection; 45 import org.apache.derby.client.am.ClientMessageId; 46 import org.apache.derby.client.net.NetConfiguration; 47 import org.apache.derby.client.net.NetLogWriter; 48 import org.apache.derby.client.ClientDataSourceFactory; 49 import org.apache.derby.shared.common.reference.Attribute; 50 import org.apache.derby.shared.common.reference.SQLState; 51 52 55 public abstract class ClientBaseDataSource implements Serializable , Referenceable { 56 private static final long serialVersionUID = -7660172643035173692L; 57 58 static boolean SUPPORTS_EUSRIDPWD = false; 68 69 static 70 { 71 try 72 { 73 new org.apache.derby.client.am.EncryptionManager(null); 79 SUPPORTS_EUSRIDPWD = true; 80 }catch(Exception e) 81 { 82 SUPPORTS_EUSRIDPWD = false; 86 } 87 } 88 89 95 97 ClientBaseDataSource() { 99 } 100 101 110 private int loginTimeout; 111 112 public synchronized void setLoginTimeout(int seconds) { 113 this.loginTimeout = seconds; 114 } 115 116 public int getLoginTimeout() { 117 return this.loginTimeout; 118 } 119 120 127 private transient PrintWriter logWriter; 128 129 public synchronized void setLogWriter(PrintWriter logWriter) { 130 this.logWriter = logWriter; 131 } 132 133 public PrintWriter getLogWriter() { 134 return this.logWriter; 135 } 136 137 private String databaseName; 145 146 148 149 private String description; 152 153 private String dataSourceName; 160 161 private int portNumber = propertyDefault_portNumber; 164 public final static int propertyDefault_portNumber = 1527; 165 166 private String serverName = propertyDefault_serverName; 170 public final static String propertyDefault_serverName = "localhost"; 171 172 174 private String user = propertyDefault_user; 187 public final static String propertyDefault_user = "APP"; 188 189 public static String getUser(Properties properties) { 190 String userString = properties.getProperty(Attribute.USERNAME_ATTR); 191 return parseString(userString, propertyDefault_user); 192 } 193 194 219 private final static short SECMEC_HAS_NOT_EXPLICITLY_SET = 0; 223 224 protected short securityMechanism = SECMEC_HAS_NOT_EXPLICITLY_SET; 235 236 public final static short propertyDefault_securityMechanism = 238 (short) NetConfiguration.SECMEC_USRIDONL; 239 240 241 249 public static short getSecurityMechanism(Properties properties) { 250 short secmec; 251 String securityMechanismString = 252 properties.getProperty(Attribute.CLIENT_SECURITY_MECHANISM); 253 254 if ( securityMechanismString != null ) 255 { 256 secmec = Short.parseShort(securityMechanismString); 259 } 260 else 261 { 262 String passwordString = properties.getProperty(Attribute.PASSWORD_ATTR); 268 secmec = getUpgradedSecurityMechanism(passwordString); 269 } 270 return secmec; 271 } 272 273 285 public static short getUpgradedSecurityMechanism(String password) { 286 if ( password == null ) 289 return propertyDefault_securityMechanism; 290 291 295 316 return (short)NetConfiguration.SECMEC_USRIDPWD; 317 318 } 319 320 private boolean retrieveMessageText = propertyDefault_retrieveMessageText; 323 public final static boolean propertyDefault_retrieveMessageText = true; 324 325 public static boolean getRetrieveMessageText(Properties properties) { 326 String retrieveMessageTextString = properties.getProperty(Attribute.CLIENT_RETIEVE_MESSAGE_TEXT); 327 return parseBoolean(retrieveMessageTextString, propertyDefault_retrieveMessageText); 328 } 329 330 private String traceFile; 333 334 public static String getTraceFile(Properties properties) { 335 return properties.getProperty(Attribute.CLIENT_TRACE_FILE); 336 } 337 338 private transient int traceFileSuffixIndex_ = 0; 341 private String traceDirectory; 343 344 public static String getTraceDirectory(Properties properties) { 345 return properties.getProperty(Attribute.CLIENT_TRACE_DIRECTORY); 346 } 347 348 private boolean traceFileAppend = propertyDefault_traceFileAppend; 351 public final static boolean propertyDefault_traceFileAppend = false; 352 353 public static boolean getTraceFileAppend(Properties properties) { 354 String traceFileAppendString = properties.getProperty(Attribute.CLIENT_TRACE_APPEND); 355 return parseBoolean(traceFileAppendString, propertyDefault_traceFileAppend); 356 } 357 358 364 public static String getPassword(Properties properties) { 365 return properties.getProperty("password"); 366 } 367 368 private String password; 369 370 synchronized public final void setPassword(String password) { 371 this.password = password; 372 } 373 374 public final String getPassword() { 375 return password; 376 } 377 378 380 public Reference getReference() throws NamingException { 381 399 Reference ref = new Reference (this.getClass().getName(), ClientDataSourceFactory.class.getName(), null); 400 401 addBeanProperties(ref); 402 return ref; 403 } 404 405 415 private void addBeanProperties(Reference ref) 416 { 417 Method [] methods = this.getClass().getMethods(); 419 420 for (int i = 0; i < methods.length; i++) { 421 422 Method m = methods[i]; 423 424 if (m.getParameterTypes().length != 0) 426 continue; 427 428 if (Modifier.isStatic(m.getModifiers())) 430 continue; 431 432 String methodName = m.getName(); 434 if ((methodName.length() < 5) || !methodName.startsWith("get")) 435 continue; 436 437 Class returnType = m.getReturnType(); 438 439 if (Integer.TYPE.equals(returnType) 440 || Short.TYPE.equals(returnType) 441 || String .class.equals(returnType) 442 || Boolean.TYPE.equals(returnType)) { 443 444 447 String propertyName = methodName.substring(3, 4).toLowerCase( 448 java.util.Locale.ENGLISH).concat( 449 methodName.substring(4)); 450 451 try { 452 Object ov = m.invoke(this, null); 453 String value = ov == null ? null : ov.toString(); 454 ref.add(new StringRefAddr (propertyName, value)); 455 } catch (IllegalAccessException iae) { 456 } catch (InvocationTargetException ite) { 457 } 458 459 } 460 } 461 } 462 463 465 466 468 LogWriter computeDncLogWriterForNewConnection(String logWriterInUseSuffix) throws SqlException { 473 return computeDncLogWriterForNewConnection(logWriter, traceDirectory, traceFile, traceFileAppend, traceLevel, logWriterInUseSuffix, traceFileSuffixIndex_++); 474 } 475 476 static LogWriter computeDncLogWriterForNewConnection(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, int traceLevel, String logWriterInUseSuffix, int traceFileSuffixIndex) throws SqlException { 480 int globaltraceFileSuffixIndex = Configuration.traceFileSuffixIndex__++; 481 482 LogWriter dncLogWriter = computeDncLogWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex, traceLevel); 484 if (dncLogWriter != null) { 485 return dncLogWriter; 486 } 487 dncLogWriter = computeDncLogWriter(null, Configuration.traceDirectory__, Configuration.traceFile__, Configuration.traceFileAppend__, "_global", globaltraceFileSuffixIndex, Configuration.traceLevel__); 489 return dncLogWriter; 490 } 491 492 static LogWriter computeDncLogWriter(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex, int traceLevel) throws SqlException { 494 if (traceLevel == TRACE_NONE) { 496 return null; 497 } 498 499 PrintWriter printWriter = computePrintWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex); 500 if (printWriter == null) { 501 return null; 502 } 503 504 LogWriter dncLogWriter = new NetLogWriter(printWriter, traceLevel); 505 if (printWriter != logWriter && traceDirectory != null) 506 { 511 dncLogWriter.printWriterNeedsToBeClosed_ = true; 512 } 513 return dncLogWriter; 514 } 515 516 public static LogWriter computeDncLogWriter(Connection connection, PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex, int traceLevel) throws SqlException { 519 if (traceLevel == TRACE_NONE) { 521 return null; 522 } 523 524 PrintWriter printWriter = computePrintWriter(logWriter, traceDirectory, traceFile, traceFileAppend, logWriterInUseSuffix, traceFileSuffixIndex); 525 if (printWriter == null) { 526 return null; 527 } 528 529 LogWriter dncLogWriter = connection.agent_.newLogWriter_(printWriter, traceLevel); 530 if (printWriter != logWriter && traceDirectory != null) 531 { 536 dncLogWriter.printWriterNeedsToBeClosed_ = true; 537 } 538 return dncLogWriter; 539 } 540 541 static PrintWriter computePrintWriter(PrintWriter logWriter, String traceDirectory, String traceFile, boolean traceFileAppend, String logWriterInUseSuffix, int traceFileSuffixIndex) throws SqlException { 545 if (logWriter != null) { 547 return logWriter; 548 } else { if (traceDirectory != null) { 550 String fileName; 551 if (traceFile == null) { 552 fileName = traceDirectory + "/" + logWriterInUseSuffix + "_" + traceFileSuffixIndex; 553 } else { 554 fileName = traceDirectory + "/" + traceFile + logWriterInUseSuffix + "_" + traceFileSuffixIndex; 555 } 556 return LogWriter.getPrintWriter(fileName, true); } else if (traceFile != null) { 558 return LogWriter.getPrintWriter(traceFile, traceFileAppend); 559 } 560 } 561 return null; 562 } 563 564 private static boolean parseBoolean(String boolString, boolean defaultBool) { 565 if (boolString != null) { 566 return (boolString.equalsIgnoreCase("true") || boolString.equalsIgnoreCase("yes")); 567 } 568 return defaultBool; 569 } 570 571 private static String parseString(String string, String defaultString) { 572 if (string != null) { 573 return string; 574 } 575 return defaultString; 576 } 577 578 private static short parseShort(String shortString, short defaultShort) { 579 if (shortString != null) { 580 return Short.parseShort(shortString); 581 } 582 return defaultShort; 583 } 584 585 private static int parseInt(String intString, int defaultInt) { 586 if (intString != null) { 587 return Integer.parseInt(intString); 588 } 589 return defaultInt; 590 } 591 592 static Properties tokenizeAttributes(String attributeString, Properties properties) throws SqlException { 596 Properties augmentedProperties; 597 598 if (attributeString == null) { 599 return properties; 600 } 601 602 if (properties != null) { 603 augmentedProperties = (Properties ) properties.clone(); 604 } else { 605 augmentedProperties = new Properties (); 606 } 607 try { 608 StringTokenizer attrTokenizer = new StringTokenizer (attributeString, ";"); 609 while (attrTokenizer.hasMoreTokens()) { 610 String v = attrTokenizer.nextToken(); 611 612 int eqPos = v.indexOf('='); 613 if (eqPos == -1) { 614 throw new SqlException(null, 615 new ClientMessageId(SQLState.INVALID_ATTRIBUTE_SYNTAX), 616 attributeString); 617 } 618 619 augmentedProperties.setProperty((v.substring(0, eqPos)).trim(), (v.substring(eqPos + 1)).trim()); 620 } 621 } catch (NoSuchElementException e) { 622 throw new SqlException(null, 624 new ClientMessageId(SQLState.INVALID_ATTRIBUTE_SYNTAX), 625 attributeString, e); 626 } 627 checkBoolean(augmentedProperties, Attribute.CLIENT_RETIEVE_MESSAGE_TEXT); 628 return augmentedProperties; 629 630 } 631 632 private static void checkBoolean(Properties set, String attribute) throws SqlException { 633 final String [] booleanChoices = {"true", "false"}; 634 checkEnumeration(set, attribute, booleanChoices); 635 } 636 637 638 private static void checkEnumeration(Properties set, String attribute, String [] choices) throws SqlException { 639 String value = set.getProperty(attribute); 640 if (value == null) { 641 return; 642 } 643 644 for (int i = 0; i < choices.length; i++) { 645 if (value.toUpperCase(java.util.Locale.ENGLISH).equals(choices[i].toUpperCase(java.util.Locale.ENGLISH))) { 646 return; 647 } 648 } 649 650 String choicesStr = "{"; 653 for (int i = 0; i < choices.length; i++) { 654 if (i > 0) { 655 choicesStr += "|"; 656 } 657 choicesStr += choices[i]; 658 } 659 660 throw new SqlException(null, 661 new ClientMessageId(SQLState.INVALID_ATTRIBUTE), 662 attribute, value, choicesStr); 663 } 664 665 668 669 671 public synchronized void setDatabaseName(String databaseName) { 672 this.databaseName = databaseName; 673 } 674 675 public String getDatabaseName() { 676 return this.databaseName; 677 } 678 679 680 public synchronized void setDataSourceName(String dataSourceName) { 681 this.dataSourceName = dataSourceName; 682 } 683 684 public String getDataSourceName() { 685 return this.dataSourceName; 686 } 687 688 public synchronized void setDescription(String description) { 689 this.description = description; 690 } 691 692 public String getDescription() { 693 return this.description; 694 } 695 696 697 public synchronized void setPortNumber(int portNumber) { 698 this.portNumber = portNumber; 699 } 700 701 public int getPortNumber() { 702 return this.portNumber; 703 } 704 705 public synchronized void setServerName(String serverName) { 706 this.serverName = serverName; 707 } 708 709 public String getServerName() { 710 return this.serverName; 711 } 712 713 714 public synchronized void setUser(String user) { 715 this.user = user; 716 } 717 718 public String getUser() { 719 return this.user; 720 } 721 722 synchronized public void setRetrieveMessageText(boolean retrieveMessageText) { 723 this.retrieveMessageText = retrieveMessageText; 724 } 725 726 public boolean getRetrieveMessageText() { 727 return this.retrieveMessageText; 728 } 729 730 750 public final static short USER_ONLY_SECURITY = (short) NetConfiguration.SECMEC_USRIDONL; 752 public final static short CLEAR_TEXT_PASSWORD_SECURITY = (short) NetConfiguration.SECMEC_USRIDPWD; 753 public final static short ENCRYPTED_PASSWORD_SECURITY = (short) NetConfiguration.SECMEC_USRENCPWD; 754 public final static short ENCRYPTED_USER_AND_PASSWORD_SECURITY = (short) NetConfiguration.SECMEC_EUSRIDPWD; 755 public final static short STRONG_PASSWORD_SUBSTITUTE_SECURITY = (short) NetConfiguration.SECMEC_USRSSBPWD; 756 757 761 synchronized public void setSecurityMechanism(short securityMechanism) { 762 this.securityMechanism = securityMechanism; 763 } 764 765 772 public short getSecurityMechanism() { 773 return getSecurityMechanism(getPassword()); 774 } 775 776 784 public short getSecurityMechanism(String password) { 785 786 if ( securityMechanism == SECMEC_HAS_NOT_EXPLICITLY_SET ) 790 return getUpgradedSecurityMechanism(password); 791 792 return securityMechanism; 793 } 794 795 protected String connectionAttributes = null; 796 797 810 public final void setConnectionAttributes(String prop) { 811 connectionAttributes = prop; 812 } 813 814 817 public final String getConnectionAttributes() { 818 return connectionAttributes; 819 } 820 821 822 825 public final static int TRACE_NONE = 0x0; 826 public final static int TRACE_CONNECTION_CALLS = 0x1; 827 public final static int TRACE_STATEMENT_CALLS = 0x2; 828 public final static int TRACE_RESULT_SET_CALLS = 0x4; 829 public final static int TRACE_DRIVER_CONFIGURATION = 0x10; 830 public final static int TRACE_CONNECTS = 0x20; 831 public final static int TRACE_PROTOCOL_FLOWS = 0x40; 832 public final static int TRACE_RESULT_SET_META_DATA = 0x80; 833 public final static int TRACE_PARAMETER_META_DATA = 0x100; 834 public final static int TRACE_DIAGNOSTICS = 0x200; 835 public final static int TRACE_XA_CALLS = 0x800; 836 public final static int TRACE_ALL = 0xFFFFFFFF; 837 838 public final static int propertyDefault_traceLevel = TRACE_ALL; 839 840 protected int traceLevel = propertyDefault_traceLevel; 841 842 public static int getTraceLevel(Properties properties) { 843 String traceLevelString = properties.getProperty(Attribute.CLIENT_TRACE_LEVEL); 844 return parseInt(traceLevelString, propertyDefault_traceLevel); 845 } 846 847 synchronized public void setTraceLevel(int traceLevel) { 848 this.traceLevel = traceLevel; 849 } 850 851 public int getTraceLevel() { 852 return this.traceLevel; 853 } 854 855 856 public synchronized void setTraceFile(String traceFile) { 857 this.traceFile = traceFile; 858 } 859 860 public String getTraceFile() { 861 return this.traceFile; 862 } 863 864 865 public synchronized void setTraceDirectory(String traceDirectory) { 866 this.traceDirectory = traceDirectory; 867 } 868 869 public String getTraceDirectory() { 870 return this.traceDirectory; 871 } 872 873 synchronized public void setTraceFileAppend(boolean traceFileAppend) { 874 this.traceFileAppend = traceFileAppend; 875 } 876 877 public boolean getTraceFileAppend() { 878 return this.traceFileAppend; 879 } 880 881 882 884 885 889 void updateDataSourceValues(Properties prop) { 890 if (prop == null) { 891 return; 892 } 893 894 if (prop.containsKey(Attribute.USERNAME_ATTR)) { 895 setUser(getUser(prop)); 896 } 897 if (prop.containsKey(Attribute.CLIENT_SECURITY_MECHANISM)) { 898 setSecurityMechanism(getSecurityMechanism(prop)); 899 } 900 if (prop.containsKey(Attribute.CLIENT_TRACE_FILE)) { 901 setTraceFile(getTraceFile(prop)); 902 } 903 if (prop.containsKey(Attribute.CLIENT_TRACE_DIRECTORY)) { 904 setTraceDirectory(getTraceDirectory(prop)); 905 } 906 if (prop.containsKey(Attribute.CLIENT_TRACE_APPEND)) { 907 setTraceFileAppend(getTraceFileAppend(prop)); 908 } 909 if (prop.containsKey(Attribute.CLIENT_RETIEVE_MESSAGE_TEXT)) { 910 setRetrieveMessageText(getRetrieveMessageText(prop)); 911 } 912 } 913 } 914 | Popular Tags |