Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DisconnectException.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DisconnectException.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DisconnectException.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/DisconnectException.java Sun May 1 23:25:59 2005 @@ -20,53 +20,48 @@ package org.apache.derby.client.am; -public class DisconnectException extends SqlException -{ +public class DisconnectException extends SqlException { - public DisconnectException (Agent agent, String reason, SqlState sqlstate, SqlCode sqlcode) - { - super (agent.logWriter_, reason, sqlstate, sqlcode); - } - - public DisconnectException (Agent agent, String reason, SqlState sqlstate) - { - super (agent.logWriter_, reason, sqlstate, SqlCode.disconnectError); - // make the call to close the streams and socket. - if (agent != null) agent.disconnectEvent(); - } - - public DisconnectException (java.lang.Throwable throwable, Agent agent, String reason, SqlState sqlstate) - { - super (agent.logWriter_, throwable, reason, sqlstate, SqlCode.disconnectError); - // make the call to close the streams and socket. - if (agent != null) agent.disconnectEvent(); - } - - public DisconnectException (Agent agent) - { - this (agent, null, SqlState.undefined); - } - - public DisconnectException (java.lang.Throwable throwable, Agent agent) - { - this (throwable, agent, null, SqlState.undefined); - } - - public DisconnectException (Agent agent, String reason) - { - this (agent, reason, SqlState.undefined); - } - - public DisconnectException (Throwable throwable, Agent agent, String reason) - { - this (throwable, agent, reason, SqlState.undefined); - } - - public DisconnectException (Agent agent, SqlException e) - { - this (agent, e.getMessage()); - setNextException (e); - } + public DisconnectException(Agent agent, String reason, SqlState sqlstate, SqlCode sqlcode) { + super(agent.logWriter_, reason, sqlstate, sqlcode); + } + + public DisconnectException(Agent agent, String reason, SqlState sqlstate) { + super(agent.logWriter_, reason, sqlstate, SqlCode.disconnectError); + // make the call to close the streams and socket. + if (agent != null) { + agent.disconnectEvent(); + } + } + + public DisconnectException(java.lang.Throwable throwable, Agent agent, String reason, SqlState sqlstate) { + super(agent.logWriter_, throwable, reason, sqlstate, SqlCode.disconnectError); + // make the call to close the streams and socket. + if (agent != null) { + agent.disconnectEvent(); + } + } + + public DisconnectException(Agent agent) { + this(agent, null, SqlState.undefined); + } + + public DisconnectException(java.lang.Throwable throwable, Agent agent) { + this(throwable, agent, null, SqlState.undefined); + } + + public DisconnectException(Agent agent, String reason) { + this(agent, reason, SqlState.undefined); + } + + public DisconnectException(Throwable throwable, Agent agent, String reason) { + this(throwable, agent, reason, SqlState.undefined); + } + + public DisconnectException(Agent agent, SqlException e) { + this(agent, e.getMessage()); + setNextException(e); + } }
Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/EncryptionManager.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/EncryptionManager.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/EncryptionManager.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/EncryptionManager.java Sun May 1 23:25:59 2005 @@ -19,8 +19,9 @@ */ package org.apache.derby.client.am; -import java.security.Security; + import java.security.Provider; +import java.security.Security; // This class is get used when using encrypted password and/or userid mechanism. // The <b>EncryptionManager</b> classs uses Diffie_Hellman algorithm to get the publick key and @@ -33,496 +34,466 @@ // The agreed public value for the Diffie-Hellman prime is 256 bits // and hence the encrytion will work only if the jce provider supports a 256 bits prime -public class EncryptionManager -{ - transient Agent agent_; // for obtaining an exception log writer only - - // PROTOCOL's Diffie-Hellman agreed public value: prime. - private static final byte modulusBytes__[] = { - (byte)0xC6, (byte)0x21, (byte)0x12, (byte)0xD7, - (byte)0x3E, (byte)0xE6, (byte)0x13, (byte)0xF0, - (byte)0x94, (byte)0x7A, (byte)0xB3, (byte)0x1F, - (byte)0x0F, (byte)0x68, (byte)0x46, (byte)0xA1, - (byte)0xBF, (byte)0xF5, (byte)0xB3, (byte)0xA4, - (byte)0xCA, (byte)0x0D, (byte)0x60, (byte)0xBC, - (byte)0x1E, (byte)0x4C, (byte)0x7A, (byte)0x0D, - (byte)0x8C, (byte)0x16, (byte)0xB3, (byte)0xE3 - }; - - //the prime value in BigInteger form. It has to be in BigInteger form because this - //is the form used in JCE library. - private static final java.math.BigInteger modulus__ - = new java.math.BigInteger (1, modulusBytes__); - - // PROTOCOL's Diffie-Hellman agreed public value: base. - private static final byte baseBytes__[] = { - (byte)0x46, (byte)0x90, (byte)0xFA, (byte)0x1F, - (byte)0x7B, (byte)0x9E, (byte)0x1D, (byte)0x44, - (byte)0x42, (byte)0xC8, (byte)0x6C, (byte)0x91, - (byte)0x14, (byte)0x60, (byte)0x3F, (byte)0xDE, - (byte)0xCF, (byte)0x07, (byte)0x1E, (byte)0xDC, - (byte)0xEC, (byte)0x5F, (byte)0x62, (byte)0x6E, - (byte)0x21, (byte)0xE2, (byte)0x56, (byte)0xAE, - (byte)0xD9, (byte)0xEA, (byte)0x34, (byte)0xE4 - }; - - // The base value in BigInteger form. - private static final java.math.BigInteger base__ = - new java.math.BigInteger (1, baseBytes__); - - //PROTOCOL's Diffie-Hellman agreed exponential length - private static final int exponential_length__ = 255; - - private javax.crypto.spec.DHParameterSpec paramSpec_; - private java.security.KeyPairGenerator keyPairGenerator_; - private java.security.KeyPair keyPair_; - private javax.crypto.KeyAgreement keyAgreement_; - - private byte[] token_; // init vector - private byte[] secKey_; // security key - private javax.crypto.SecretKeyFactory secretKeyFactory_ = null; - private String providerName; // security provider name - private Provider provider; - - // EncryptionManager constructor. In this constructor,DHParameterSpec, - // KeyPairGenerator, KeyPair, and KeyAgreement are initialized. - public EncryptionManager (Agent agent) throws SqlException - { - agent_ = agent; - try - { - // get a security provider that supports the diffie helman key agreement algorithm - Provider[] list = Security.getProviders("KeyAgreement.DH"); - if(list == null) - throw new java.security.NoSuchProviderException(); - provider = list[0]; - providerName = provider.getName(); - - java.security.Security.addProvider ((java.security.Provider) provider); - - paramSpec_ = new javax.crypto.spec.DHParameterSpec (modulus__, base__, exponential_length__); - keyPairGenerator_ = java.security.KeyPairGenerator.getInstance ("DH", providerName); - keyPairGenerator_.initialize (paramSpec_); - keyPair_ = keyPairGenerator_.generateKeyPair(); - keyAgreement_ = javax.crypto.KeyAgreement.getInstance ("DH", providerName); - keyAgreement_.init (keyPair_.getPrivate()); - } - catch (java.security.NoSuchProviderException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchProviderException is caught" + - " when initializing EncryptionManager '" + e.getMessage() + "'"); - } - catch (java.security.NoSuchAlgorithmException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught" + - " when initializing EncryptionManager '" + e.getMessage() + "'"); - } - catch (java.security.InvalidAlgorithmParameterException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught" + - " when initializing EncryptionManager '" + e.getMessage() + "'"); - } - - catch (java.security.InvalidKeyException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidKeyException is caught" + - " when initializing EncryptionManager '" + e.getMessage() + "'"); - } - } - - // This method generates the public key and returns it. This - // shared public key is the application requester's connection key and will - // be exchanged with the application server's connection key. This connection - // key will be put in the sectkn in ACCSEC command and send to the application - // server. - // @param null - // @return a byte array that is the application requester's public key - public byte[] obtainPublicKey () - { - - //we need to get the plain form public key because PROTOCOL accepts plain form - //public key only. - java.math.BigInteger aPub = ((javax.crypto.interfaces.DHPublicKey) keyPair_.getPublic()).getY(); - byte[] aPubKey = aPub.toByteArray(); - - //the following lines of code is to adjust the length of the key. PublicKey - //in JCE is in the form of BigInteger and it's a signed value. When tranformed - //to a Byte array form, normally this array is 32 bytes. However, if the - //value happens to take up all 32 X 8 bits and it is positive, an extra - //bit is needed and then a 33 byte array will be returned. Since PROTOCOL can't - //recogize the 33 byte key, we check the length here, if the length is 33, - //we will just trim off the first byte (0) and get the rest of 32 bytes. - if (aPubKey.length == 33 && aPubKey[0]==0) { - //System.out.println ("Adjust length"); - byte[] newKey = new byte[32]; - for (int i=0; i < newKey.length; i++) - newKey[i] = aPubKey[i+1]; - return newKey; - } - - //the following lines of code is to adjust the length of the key. Occasionally, - //the length of the public key is less than 32, the reason of this is that the 0 byte - //in the beginning is somehow not returned. So we check the length here, if the length - //is less than 32, we will pad 0 in the beginning to make the public key 32 bytes - if (aPubKey.length < 32) { - byte[] newKey = new byte[32]; - int i; - for (i=0; i < 32-aPubKey.length; i++) { - newKey[i] = 0; - } - for (int j=i; j<newKey.length; j++) - newKey[j] = aPubKey[j-i]; - return newKey; - } - return aPubKey; - } - - // This method is used to calculate the encryption token. DES encrypts the - // data using a token and the generated shared private key. The token used - // depends on the type of security mechanism being used: - // USRENCPWD - The userid is used as the token. The USRID is zero-padded to - // 8 bytes if less than 8 bytes or truncated to 8 bytes if greater than 8 bytes. - // EUSRIDPWD - The middle 8 bytes of the server's connection key is used as - // the token. - // @param int securityMechanism - // @param byte[] userid or server's connection key - // @return byte[] the encryption token - private byte[] calculateEncryptionToken (int securityMechanism, byte[] initVector) - { - byte[] token = new byte[8]; - - //USRENCPWD, the userid is used as token - if (securityMechanism == 7) { - if (initVector.length < 8) { //shorter than 8 bytes, zero padded to 8 bytes - for (int i=0; i<initVector.length; i++) - token[i] = initVector[i]; - for (int i=initVector.length; i<8; i++) - token[i] = 0; - } - else { //longer than 8 bytes, truncated to 8 bytes - for (int i=0; i<8; i++) - token[i] = initVector[i]; - } - } - //EUSRIDPWD - The middle 8 bytes of the server's connection key is used as - //the token. - else if (securityMechanism == 9) { - for (int i = 0; i < 8; i++) { - token[i] = initVector[i + 12]; - } - } - return token; - } - - //JDK 1.4 has a parity check on the DES encryption key. Each byte needs to have an odd number - //of "1"s in it, and this is required by DES. Otherwise JDK 1.4 throws InvalidKeyException. - //Older JDK doesn't check this. In order to make encryption work with JDK1.4, we are going to - //check each of the 8 byte of our key and flip the last bit if it has even number of 1s. - private void keyParityCheck(byte[] key) throws SqlException - { - byte temp; - int changeParity; - if (key.length!=8) - throw new SqlException (agent_.logWriter_, "DES key has the wrong length"); - for (int i=0; i<8; i++) - { - temp = key[i]; - changeParity = 1; - for (int j=0; j<8; j++) - { - if (temp < 0) - changeParity = 1 - changeParity; - temp = (byte) (temp << 1); - } - if (changeParity == 1) - { - if ((key[i] & 1) != 0) - key[i] &= 0xfe; - else - key[i] |= 1; - } - } - } - - // This method generates a secret key using the application server's - // public key - private byte[] generatePrivateKey (byte[] targetPublicKey) throws SqlException - { - try { - - //initiate a Diffie_Hellman KeyFactory object. - java.security.KeyFactory keyFac = java.security.KeyFactory.getInstance ("DH", provider); - - //Use server's public key to initiate a DHPublicKeySpec and then use - //this DHPublicKeySpec to initiate a publicKey object - java.math.BigInteger publicKey = new java.math.BigInteger (1, targetPublicKey); - javax.crypto.spec.DHPublicKeySpec dhKeySpec = - new javax.crypto.spec.DHPublicKeySpec (publicKey, modulus__, base__); - java.security.PublicKey pubKey = keyFac.generatePublic (dhKeySpec); - - //Execute the first phase of DH keyagreement protocal. - keyAgreement_.doPhase (pubKey, true); - - //generate the shared secret key. The application requestor's shared secret - //key should be exactly the same as the application server's shared secret - //key - byte[] sharedSecret = keyAgreement_.generateSecret(); - byte[] newKey = new byte[32]; - - //We adjust the length here. If the length of secret key is 33 and the first byte is 0, - //we trim off the frist byte. If the length of secret key is less than 32, we will - //pad 0 to the beginning of the byte array tho make the secret key 32 bytes. - if (sharedSecret.length == 33 && sharedSecret[0] == 0) { - for (int i=0; i<newKey.length; i++) - newKey[i] = sharedSecret[i+1]; - - } - if (sharedSecret.length < 32) { - int i; - for (i=0; i<(32 - sharedSecret.length); i++) { - newKey[i] = 0; - } - for (int j=i; j<sharedSecret.length; j++) - newKey[j] = sharedSecret[j-i]; - } - - //The Data Encryption Standard (DES) is going to be used to encrypt userid - //and password. DES is a block cipher; it encrypts data in 64-bit blocks. - //PROTOCOL encryption uses DES CBC mode as defined by the FIPS standard - //DES CBC requires an encryption key and an 8 byte token to encrypt the data. - //The middle 8 bytes of Diffie-Hellman shared private key is used as the - //encryption key. The following code retrieves middle 8 bytes of the shared - //private key. - byte[] key = new byte[8]; - - //if secret key is not 32, we will use the adjust length secret key - if (sharedSecret.length==32) { - for (int i=0; i< 8;i++) - key[i] = sharedSecret[i+12]; - } - else if (sharedSecret.length==33 || sharedSecret.length < 32) { - for (int i=0; i< 8;i++) - key[i] = newKey[i+12]; - } - else - throw new SqlException (agent_.logWriter_, "sharedSecret key length error " + sharedSecret.length); - - //we do parity check here and flip the parity bit if the byte has even number of 1s - keyParityCheck(key); - return key; - }/* - catch (java.security.NoSuchProviderException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - }*/ - catch (java.security.NoSuchAlgorithmException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.spec.InvalidKeySpecException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidKeySpecException is caught " - + "when encrypting data"); - } - catch (java.security.InvalidKeyException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidKeyException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - - - } - - // This method encrypts the usreid/password with the middle 8 bytes of - // the generated secret key and an encryption token. Then it returns the - // encrypted data in a byte array. - // plainText The byte array form userid/password to encrypt. - // initVector The byte array which is used to calculate the - // encryption token. - // targetPublicKey DERBY' public key. - // Returns the encrypted data in a byte array. - public byte[] encryptData (byte[] plainText, - int securityMechanism, - byte[] initVector, - byte[] targetPublicKey) throws SqlException - { - - byte[] cipherText = null; - java.security.Key key = null; - - if (token_ == null) - token_ = calculateEncryptionToken (securityMechanism, initVector); - - try{ - if (secKey_ == null) - { - //use this encryption key to initiate a SecretKeySpec object - secKey_ = generatePrivateKey (targetPublicKey); - javax.crypto.spec.SecretKeySpec desKey = new javax.crypto.spec.SecretKeySpec (secKey_, "DES"); - key = desKey; - } - else - { - //use this encryption key to initiate a SecretKeySpec object - javax.crypto.spec.DESKeySpec desKey = new javax.crypto.spec.DESKeySpec (secKey_); - if (secretKeyFactory_ == null) - secretKeyFactory_ = javax.crypto.SecretKeyFactory.getInstance("DES", providerName); - key = secretKeyFactory_.generateSecret(desKey); - } - - //We use DES in CBC mode because this is the mode used in PROTOCOL. The - //encryption mode has to be consistent for encryption and decryption. - //CBC mode requires an initialization vector(IV) parameter. In CBC mode - //we need to initialize the Cipher object with an IV, which can be supplied - // using the javax.crypto.spec.IvParameterSpec class. - javax.crypto.Cipher cipher= javax.crypto.Cipher.getInstance ("DES/CBC/PKCS5Padding", providerName); - - //generate a IVParameterSpec object and use it to initiate the - //Cipher object. - javax.crypto.spec.IvParameterSpec ivParam = new javax.crypto.spec.IvParameterSpec (token_); - - //initiate the Cipher using encryption mode, encryption key and the - //IV parameter. - cipher.init (javax.crypto.Cipher.ENCRYPT_MODE, key, ivParam); - - //Execute the final phase of encryption - cipherText = cipher.doFinal (plainText); - } - catch (java.security.NoSuchProviderException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.NoSuchAlgorithmException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.InvalidKeyException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidKeyException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (javax.crypto.NoSuchPaddingException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.NoSuchPaddingException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (javax.crypto.BadPaddingException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.BadPaddingException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.InvalidAlgorithmParameterException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (javax.crypto.IllegalBlockSizeException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.spec.InvalidKeySpecException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - return cipherText; - } - +public class EncryptionManager { + transient Agent agent_; // for obtaining an exception log writer only - // This method decrypts the usreid/password with the middle 8 bytes of - // the generated secret key and an encryption token. Then it returns the - // decrypted data in a byte array. - // plainText The byte array form userid/password to encrypt. - // initVector The byte array which is used to calculate the - // encryption token. - // targetPublicKey DERBY' public key. - // Returns the decrypted data in a byte array. - public byte[] decryptData (byte[] cipherText, - int securityMechanism, - byte[] initVector, - byte[] targetPublicKey) throws SqlException - { - - byte[] plainText = null; - java.security.Key key = null; - - if (token_ == null) - token_ = calculateEncryptionToken (securityMechanism, initVector); - - try{ - if (secKey_ == null) - { - //use this encryption key to initiate a SecretKeySpec object - secKey_ = generatePrivateKey (targetPublicKey); - javax.crypto.spec.SecretKeySpec desKey = new javax.crypto.spec.SecretKeySpec (secKey_, "DES"); - key = desKey; - } - else - { - //use this encryption key to initiate a SecretKeySpec object - javax.crypto.spec.DESKeySpec desKey = new javax.crypto.spec.DESKeySpec (secKey_); - if (secretKeyFactory_ == null) - secretKeyFactory_ = javax.crypto.SecretKeyFactory.getInstance("DES", providerName); - key = secretKeyFactory_.generateSecret(desKey); - } - - //We use DES in CBC mode because this is the mode used in PROTOCOL. The - //encryption mode has to be consistent for encryption and decryption. - //CBC mode requires an initialization vector(IV) parameter. In CBC mode - //we need to initialize the Cipher object with an IV, which can be supplied - // using the javax.crypto.spec.IvParameterSpec class. - javax.crypto.Cipher cipher= javax.crypto.Cipher.getInstance ("DES/CBC/PKCS5Padding", providerName); - - //generate a IVParameterSpec object and use it to initiate the - //Cipher object. - javax.crypto.spec.IvParameterSpec ivParam = new javax.crypto.spec.IvParameterSpec (token_); - - //initiate the Cipher using encryption mode, encryption key and the - //IV parameter. - cipher.init (javax.crypto.Cipher.DECRYPT_MODE, key,ivParam); - - //Execute the final phase of encryption - plainText = cipher.doFinal (cipherText); - } - catch (java.security.NoSuchProviderException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.NoSuchAlgorithmException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.InvalidKeyException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidKeyException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (javax.crypto.NoSuchPaddingException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.NoSuchPaddingException is caught " - + "when encrypting data '" + e.getMessage() + "'"); + // PROTOCOL's Diffie-Hellman agreed public value: prime. + private static final byte modulusBytes__[] = { + (byte) 0xC6, (byte) 0x21, (byte) 0x12, (byte) 0xD7, + (byte) 0x3E, (byte) 0xE6, (byte) 0x13, (byte) 0xF0, + (byte) 0x94, (byte) 0x7A, (byte) 0xB3, (byte) 0x1F, + (byte) 0x0F, (byte) 0x68, (byte) 0x46, (byte) 0xA1, + (byte) 0xBF, (byte) 0xF5, (byte) 0xB3, (byte) 0xA4, + (byte) 0xCA, (byte) 0x0D, (byte) 0x60, (byte) 0xBC, + (byte) 0x1E, (byte) 0x4C, (byte) 0x7A, (byte) 0x0D, + (byte) 0x8C, (byte) 0x16, (byte) 0xB3, (byte) 0xE3 + }; + + //the prime value in BigInteger form. It has to be in BigInteger form because this + //is the form used in JCE library. + private static final java.math.BigInteger modulus__ + = new java.math.BigInteger(1, modulusBytes__); + + // PROTOCOL's Diffie-Hellman agreed public value: base. + private static final byte baseBytes__[] = { + (byte) 0x46, (byte) 0x90, (byte) 0xFA, (byte) 0x1F, + (byte) 0x7B, (byte) 0x9E, (byte) 0x1D, (byte) 0x44, + (byte) 0x42, (byte) 0xC8, (byte) 0x6C, (byte) 0x91, + (byte) 0x14, (byte) 0x60, (byte) 0x3F, (byte) 0xDE, + (byte) 0xCF, (byte) 0x07, (byte) 0x1E, (byte) 0xDC, + (byte) 0xEC, (byte) 0x5F, (byte) 0x62, (byte) 0x6E, + (byte) 0x21, (byte) 0xE2, (byte) 0x56, (byte) 0xAE, + (byte) 0xD9, (byte) 0xEA, (byte) 0x34, (byte) 0xE4 + }; + + // The base value in BigInteger form. + private static final java.math.BigInteger base__ = + new java.math.BigInteger(1, baseBytes__); + + //PROTOCOL's Diffie-Hellman agreed exponential length + private static final int exponential_length__ = 255; + + private javax.crypto.spec.DHParameterSpec paramSpec_; + private java.security.KeyPairGenerator keyPairGenerator_; + private java.security.KeyPair keyPair_; + private javax.crypto.KeyAgreement keyAgreement_; + + private byte[] token_; // init vector + private byte[] secKey_; // security key + private javax.crypto.SecretKeyFactory secretKeyFactory_ = null; + private String providerName; // security provider name + private Provider provider; + + // EncryptionManager constructor. In this constructor,DHParameterSpec, + // KeyPairGenerator, KeyPair, and KeyAgreement are initialized. + public EncryptionManager(Agent agent) throws SqlException { + agent_ = agent; + try { + // get a security provider that supports the diffie helman key agreement algorithm + Provider[] list = Security.getProviders("KeyAgreement.DH"); + if (list == null) { + throw new java.security.NoSuchProviderException(); + } + provider = list[0]; + providerName = provider.getName(); + + java.security.Security.addProvider((java.security.Provider) provider); + + paramSpec_ = new javax.crypto.spec.DHParameterSpec(modulus__, base__, exponential_length__); + keyPairGenerator_ = java.security.KeyPairGenerator.getInstance("DH", providerName); + keyPairGenerator_.initialize(paramSpec_); + keyPair_ = keyPairGenerator_.generateKeyPair(); + keyAgreement_ = javax.crypto.KeyAgreement.getInstance("DH", providerName); + keyAgreement_.init(keyPair_.getPrivate()); + } catch (java.security.NoSuchProviderException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchProviderException is caught" + + " when initializing EncryptionManager '" + e.getMessage() + "'"); + } catch (java.security.NoSuchAlgorithmException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught" + + " when initializing EncryptionManager '" + e.getMessage() + "'"); + } catch (java.security.InvalidAlgorithmParameterException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught" + + " when initializing EncryptionManager '" + e.getMessage() + "'"); + } catch (java.security.InvalidKeyException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidKeyException is caught" + + " when initializing EncryptionManager '" + e.getMessage() + "'"); + } + } + + // This method generates the public key and returns it. This + // shared public key is the application requester's connection key and will + // be exchanged with the application server's connection key. This connection + // key will be put in the sectkn in ACCSEC command and send to the application + // server. + // @param null + // @return a byte array that is the application requester's public key + public byte[] obtainPublicKey() { + + //we need to get the plain form public key because PROTOCOL accepts plain form + //public key only. + java.math.BigInteger aPub = ((javax.crypto.interfaces.DHPublicKey) keyPair_.getPublic()).getY(); + byte[] aPubKey = aPub.toByteArray(); + + //the following lines of code is to adjust the length of the key. PublicKey + //in JCE is in the form of BigInteger and it's a signed value. When tranformed + //to a Byte array form, normally this array is 32 bytes. However, if the + //value happens to take up all 32 X 8 bits and it is positive, an extra + //bit is needed and then a 33 byte array will be returned. Since PROTOCOL can't + //recogize the 33 byte key, we check the length here, if the length is 33, + //we will just trim off the first byte (0) and get the rest of 32 bytes. + if (aPubKey.length == 33 && aPubKey[0] == 0) { + //System.out.println ("Adjust length"); + byte[] newKey = new byte[32]; + for (int i = 0; i < newKey.length; i++) { + newKey[i] = aPubKey[i + 1]; + } + return newKey; + } + + //the following lines of code is to adjust the length of the key. Occasionally, + //the length of the public key is less than 32, the reason of this is that the 0 byte + //in the beginning is somehow not returned. So we check the length here, if the length + //is less than 32, we will pad 0 in the beginning to make the public key 32 bytes + if (aPubKey.length < 32) { + byte[] newKey = new byte[32]; + int i; + for (i = 0; i < 32 - aPubKey.length; i++) { + newKey[i] = 0; + } + for (int j = i; j < newKey.length; j++) { + newKey[j] = aPubKey[j - i]; + } + return newKey; + } + return aPubKey; + } + + // This method is used to calculate the encryption token. DES encrypts the + // data using a token and the generated shared private key. The token used + // depends on the type of security mechanism being used: + // USRENCPWD - The userid is used as the token. The USRID is zero-padded to + // 8 bytes if less than 8 bytes or truncated to 8 bytes if greater than 8 bytes. + // EUSRIDPWD - The middle 8 bytes of the server's connection key is used as + // the token. + // @param int securityMechanism + // @param byte[] userid or server's connection key + // @return byte[] the encryption token + private byte[] calculateEncryptionToken(int securityMechanism, byte[] initVector) { + byte[] token = new byte[8]; + + //USRENCPWD, the userid is used as token + if (securityMechanism == 7) { + if (initVector.length < 8) { //shorter than 8 bytes, zero padded to 8 bytes + for (int i = 0; i < initVector.length; i++) { + token[i] = initVector[i]; + } + for (int i = initVector.length; i < 8; i++) { + token[i] = 0; + } + } else { //longer than 8 bytes, truncated to 8 bytes + for (int i = 0; i < 8; i++) { + token[i] = initVector[i]; + } + } + } + //EUSRIDPWD - The middle 8 bytes of the server's connection key is used as + //the token. + else if (securityMechanism == 9) { + for (int i = 0; i < 8; i++) { + token[i] = initVector[i + 12]; + } + } + return token; + } + + //JDK 1.4 has a parity check on the DES encryption key. Each byte needs to have an odd number + //of "1"s in it, and this is required by DES. Otherwise JDK 1.4 throws InvalidKeyException. + //Older JDK doesn't check this. In order to make encryption work with JDK1.4, we are going to + //check each of the 8 byte of our key and flip the last bit if it has even number of 1s. + private void keyParityCheck(byte[] key) throws SqlException { + byte temp; + int changeParity; + if (key.length != 8) { + throw new SqlException(agent_.logWriter_, "DES key has the wrong length"); + } + for (int i = 0; i < 8; i++) { + temp = key[i]; + changeParity = 1; + for (int j = 0; j < 8; j++) { + if (temp < 0) { + changeParity = 1 - changeParity; + } + temp = (byte) (temp << 1); + } + if (changeParity == 1) { + if ((key[i] & 1) != 0) { + key[i] &= 0xfe; + } else { + key[i] |= 1; + } + } + } + } + + // This method generates a secret key using the application server's + // public key + private byte[] generatePrivateKey(byte[] targetPublicKey) throws SqlException { + try { + + //initiate a Diffie_Hellman KeyFactory object. + java.security.KeyFactory keyFac = java.security.KeyFactory.getInstance("DH", provider); + + //Use server's public key to initiate a DHPublicKeySpec and then use + //this DHPublicKeySpec to initiate a publicKey object + java.math.BigInteger publicKey = new java.math.BigInteger(1, targetPublicKey); + javax.crypto.spec.DHPublicKeySpec dhKeySpec = + new javax.crypto.spec.DHPublicKeySpec(publicKey, modulus__, base__); + java.security.PublicKey pubKey = keyFac.generatePublic(dhKeySpec); + + //Execute the first phase of DH keyagreement protocal. + keyAgreement_.doPhase(pubKey, true); + + //generate the shared secret key. The application requestor's shared secret + //key should be exactly the same as the application server's shared secret + //key + byte[] sharedSecret = keyAgreement_.generateSecret(); + byte[] newKey = new byte[32]; + + //We adjust the length here. If the length of secret key is 33 and the first byte is 0, + //we trim off the frist byte. If the length of secret key is less than 32, we will + //pad 0 to the beginning of the byte array tho make the secret key 32 bytes. + if (sharedSecret.length == 33 && sharedSecret[0] == 0) { + for (int i = 0; i < newKey.length; i++) { + newKey[i] = sharedSecret[i + 1]; + } + + } + if (sharedSecret.length < 32) { + int i; + for (i = 0; i < (32 - sharedSecret.length); i++) { + newKey[i] = 0; + } + for (int j = i; j < sharedSecret.length; j++) { + newKey[j] = sharedSecret[j - i]; + } + } + + //The Data Encryption Standard (DES) is going to be used to encrypt userid + //and password. DES is a block cipher; it encrypts data in 64-bit blocks. + //PROTOCOL encryption uses DES CBC mode as defined by the FIPS standard + //DES CBC requires an encryption key and an 8 byte token to encrypt the data. + //The middle 8 bytes of Diffie-Hellman shared private key is used as the + //encryption key. The following code retrieves middle 8 bytes of the shared + //private key. + byte[] key = new byte[8]; + + //if secret key is not 32, we will use the adjust length secret key + if (sharedSecret.length == 32) { + for (int i = 0; i < 8; i++) { + key[i] = sharedSecret[i + 12]; + } + } else if (sharedSecret.length == 33 || sharedSecret.length < 32) { + for (int i = 0; i < 8; i++) { + key[i] = newKey[i + 12]; + } + } else { + throw new SqlException(agent_.logWriter_, "sharedSecret key length error " + sharedSecret.length); + } + + //we do parity check here and flip the parity bit if the byte has even number of 1s + keyParityCheck(key); + return key; + }/* + catch (java.security.NoSuchProviderException e) { + throw new SqlException (agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + }*/ catch (java.security.NoSuchAlgorithmException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.spec.InvalidKeySpecException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidKeySpecException is caught " + + "when encrypting data"); + } catch (java.security.InvalidKeyException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidKeyException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } + + + } + + // This method encrypts the usreid/password with the middle 8 bytes of + // the generated secret key and an encryption token. Then it returns the + // encrypted data in a byte array. + // plainText The byte array form userid/password to encrypt. + // initVector The byte array which is used to calculate the + // encryption token. + // targetPublicKey DERBY' public key. + // Returns the encrypted data in a byte array. + public byte[] encryptData(byte[] plainText, + int securityMechanism, + byte[] initVector, + byte[] targetPublicKey) throws SqlException { + + byte[] cipherText = null; + java.security.Key key = null; + + if (token_ == null) { + token_ = calculateEncryptionToken(securityMechanism, initVector); + } + + try { + if (secKey_ == null) { + //use this encryption key to initiate a SecretKeySpec object + secKey_ = generatePrivateKey(targetPublicKey); + javax.crypto.spec.SecretKeySpec desKey = new javax.crypto.spec.SecretKeySpec(secKey_, "DES"); + key = desKey; + } else { + //use this encryption key to initiate a SecretKeySpec object + javax.crypto.spec.DESKeySpec desKey = new javax.crypto.spec.DESKeySpec(secKey_); + if (secretKeyFactory_ == null) { + secretKeyFactory_ = javax.crypto.SecretKeyFactory.getInstance("DES", providerName); + } + key = secretKeyFactory_.generateSecret(desKey); + } + + //We use DES in CBC mode because this is the mode used in PROTOCOL. The + //encryption mode has to be consistent for encryption and decryption. + //CBC mode requires an initialization vector(IV) parameter. In CBC mode + //we need to initialize the Cipher object with an IV, which can be supplied + // using the javax.crypto.spec.IvParameterSpec class. + javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DES/CBC/PKCS5Padding", providerName); + + //generate a IVParameterSpec object and use it to initiate the + //Cipher object. + javax.crypto.spec.IvParameterSpec ivParam = new javax.crypto.spec.IvParameterSpec(token_); + + //initiate the Cipher using encryption mode, encryption key and the + //IV parameter. + cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, ivParam); + + //Execute the final phase of encryption + cipherText = cipher.doFinal(plainText); + } catch (java.security.NoSuchProviderException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.NoSuchAlgorithmException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.InvalidKeyException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidKeyException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.NoSuchPaddingException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.NoSuchPaddingException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.BadPaddingException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.BadPaddingException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.InvalidAlgorithmParameterException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.IllegalBlockSizeException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.spec.InvalidKeySpecException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } + return cipherText; + } + + + // This method decrypts the usreid/password with the middle 8 bytes of + // the generated secret key and an encryption token. Then it returns the + // decrypted data in a byte array. + // plainText The byte array form userid/password to encrypt. + // initVector The byte array which is used to calculate the + // encryption token. + // targetPublicKey DERBY' public key. + // Returns the decrypted data in a byte array. + public byte[] decryptData(byte[] cipherText, + int securityMechanism, + byte[] initVector, + byte[] targetPublicKey) throws SqlException { + + byte[] plainText = null; + java.security.Key key = null; + + if (token_ == null) { + token_ = calculateEncryptionToken(securityMechanism, initVector); + } + + try { + if (secKey_ == null) { + //use this encryption key to initiate a SecretKeySpec object + secKey_ = generatePrivateKey(targetPublicKey); + javax.crypto.spec.SecretKeySpec desKey = new javax.crypto.spec.SecretKeySpec(secKey_, "DES"); + key = desKey; + } else { + //use this encryption key to initiate a SecretKeySpec object + javax.crypto.spec.DESKeySpec desKey = new javax.crypto.spec.DESKeySpec(secKey_); + if (secretKeyFactory_ == null) { + secretKeyFactory_ = javax.crypto.SecretKeyFactory.getInstance("DES", providerName); + } + key = secretKeyFactory_.generateSecret(desKey); + } + + //We use DES in CBC mode because this is the mode used in PROTOCOL. The + //encryption mode has to be consistent for encryption and decryption. + //CBC mode requires an initialization vector(IV) parameter. In CBC mode + //we need to initialize the Cipher object with an IV, which can be supplied + // using the javax.crypto.spec.IvParameterSpec class. + javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DES/CBC/PKCS5Padding", providerName); + + //generate a IVParameterSpec object and use it to initiate the + //Cipher object. + javax.crypto.spec.IvParameterSpec ivParam = new javax.crypto.spec.IvParameterSpec(token_); + + //initiate the Cipher using encryption mode, encryption key and the + //IV parameter. + cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, ivParam); + + //Execute the final phase of encryption + plainText = cipher.doFinal(cipherText); + } catch (java.security.NoSuchProviderException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchProviderException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.NoSuchAlgorithmException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.NoSuchAlgorithmException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.InvalidKeyException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidKeyException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.NoSuchPaddingException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.NoSuchPaddingException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.BadPaddingException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.BadPaddingException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.InvalidAlgorithmParameterException e) { + throw new SqlException(agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (javax.crypto.IllegalBlockSizeException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } catch (java.security.spec.InvalidKeySpecException e) { + throw new SqlException(agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " + + "when encrypting data '" + e.getMessage() + "'"); + } + return plainText; + } + + public void setInitVector(byte[] initVector) { + token_ = initVector; + } + + public void setSecKey(byte[] secKey) { + secKey_ = secKey; + } + + public void resetSecurityKeys() { + token_ = null; + secKey_ = null; } - catch (javax.crypto.BadPaddingException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.BadPaddingException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.InvalidAlgorithmParameterException e) { - throw new SqlException (agent_.logWriter_, e, "java.security.InvalidAlgorithmParameterException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (javax.crypto.IllegalBlockSizeException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - catch (java.security.spec.InvalidKeySpecException e) { - throw new SqlException (agent_.logWriter_, e, "javax.crypto.IllegalBlockSizeException is caught " - + "when encrypting data '" + e.getMessage() + "'"); - } - return plainText; - } - - public void setInitVector(byte[] initVector) - { - token_ = initVector; - } - - public void setSecKey(byte[] secKey) - { - secKey_ = secKey; - } - - public void resetSecurityKeys() - { - token_ = null; - secKey_ = null; - } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ErrorKey.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ErrorKey.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ErrorKey.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ErrorKey.java Sun May 1 23:25:59 2005 @@ -23,27 +23,24 @@ // This class associates the SQL states and error codes with every possible exception // as distinguished by a unique resourceKey. -public class ErrorKey -{ - private String resourceKey_; - private String sqlState_; - private int errorCode_; +public class ErrorKey { + private String resourceKey_; + private String sqlState_; + private int errorCode_; - String getResourceKey () - { - return resourceKey_; - } - String getSQLState () - { - return sqlState_; - } + String getResourceKey() { + return resourceKey_; + } - int getErrorCode () - { - return errorCode_; - } + String getSQLState() { + return sqlState_; + } + + int getErrorCode() { + return errorCode_; + } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ExceptionFormatter.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ExceptionFormatter.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ExceptionFormatter.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/ExceptionFormatter.java Sun May 1 23:25:59 2005 @@ -19,196 +19,175 @@ */ package org.apache.derby.client.am; -public class ExceptionFormatter -{ - // returnTokensOnly is true only when exception tracing is enabled so - // that we don't try to go to the server for a message while we're in - // the middle of parsing an Sqlca reply. - // Without this, if e.getMessage() fails, we would have infinite recursion - // when TRACE_DIAGNOSTICS is on because tracing occurs within the exception constructor. - static public void printTrace (java.sql.SQLException e, - java.io.PrintWriter printWriter, - String messageHeader, - boolean returnTokensOnly) - { - String header; - synchronized (printWriter) { - while (e != null) { - if (e instanceof java.sql.DataTruncation) { - header = messageHeader + "[" + "DataTruncation@" + Integer.toHexString (e.hashCode()) + "]"; - printWriter.println (header + " java.sql.DataTruncation"); - } - else if (e instanceof java.sql.SQLWarning) { - header = messageHeader + "[" + "SQLWarning@" + Integer.toHexString (e.hashCode()) + "]"; - printWriter.println (header + " java.sql.SQLWarning"); - } - else if (e instanceof java.sql.BatchUpdateException) { - header = messageHeader + "[" + "BatchUpdateException@" + Integer.toHexString (e.hashCode()) + "]"; - printWriter.println (header + " java.sql.BatchUpdateException"); - } - else { // e instanceof java.sql.SQLException - header = messageHeader + "[" + "SQLException@" + Integer.toHexString (e.hashCode()) + "]"; - printWriter.println (header + " java.sql.SQLException"); - } +public class ExceptionFormatter { + // returnTokensOnly is true only when exception tracing is enabled so + // that we don't try to go to the server for a message while we're in + // the middle of parsing an Sqlca reply. + // Without this, if e.getMessage() fails, we would have infinite recursion + // when TRACE_DIAGNOSTICS is on because tracing occurs within the exception constructor. + static public void printTrace(java.sql.SQLException e, + java.io.PrintWriter printWriter, + String messageHeader, + boolean returnTokensOnly) { + String header; + synchronized (printWriter) { + while (e != null) { + if (e instanceof java.sql.DataTruncation) { + header = messageHeader + "[" + "DataTruncation@" + Integer.toHexString(e.hashCode()) + "]"; + printWriter.println(header + " java.sql.DataTruncation"); + } else if (e instanceof java.sql.SQLWarning) { + header = messageHeader + "[" + "SQLWarning@" + Integer.toHexString(e.hashCode()) + "]"; + printWriter.println(header + " java.sql.SQLWarning"); + } else if (e instanceof java.sql.BatchUpdateException) { + header = messageHeader + "[" + "BatchUpdateException@" + Integer.toHexString(e.hashCode()) + "]"; + printWriter.println(header + " java.sql.BatchUpdateException"); + } else { // e instanceof java.sql.SQLException + header = messageHeader + "[" + "SQLException@" + Integer.toHexString(e.hashCode()) + "]"; + printWriter.println(header + " java.sql.SQLException"); + } - if (e instanceof Diagnosable) { - java.lang.Throwable throwable = null; - try { - throwable = ((Diagnosable) e).getThrowable(); - } - catch (java.lang.NoSuchMethodError doNothing) { - } - if (throwable != null) { - printTrace (throwable, printWriter, header); - } - Sqlca sqlca = ((Diagnosable) e).getSqlca(); - if (sqlca != null) { - printTrace (sqlca, printWriter, header); - // JDK stack trace calls e.getMessage(), so we must set some state on the sqlca that says return tokens only. - ((Sqlca) sqlca).returnTokensOnlyInMessageText (returnTokensOnly); - } - } + if (e instanceof Diagnosable) { + java.lang.Throwable throwable = null; + try { + throwable = ((Diagnosable) e).getThrowable(); + } catch (java.lang.NoSuchMethodError doNothing) { + } + if (throwable != null) { + printTrace(throwable, printWriter, header); + } + Sqlca sqlca = ((Diagnosable) e).getSqlca(); + if (sqlca != null) { + printTrace(sqlca, printWriter, header); + // JDK stack trace calls e.getMessage(), so we must set some state on the sqlca that says return tokens only. + ((Sqlca) sqlca).returnTokensOnlyInMessageText(returnTokensOnly); + } + } - printWriter.println (header + " SQL state = " + e.getSQLState ()); - printWriter.println (header + " Error code = " + String.valueOf (e.getErrorCode ())); - if (!(e instanceof Diagnosable)) { - printWriter.println (header + " Message = " + e.getMessage()); - } - else { - if (((Diagnosable) e).getSqlca() == null) - { // Too much has changed, so escape out here. - printWriter.println (header + " Message = " + e.getMessage()); - } - else { // This is server-side error. - Sqlca sqlca = (Sqlca) ((Diagnosable) e).getSqlca(); - if (returnTokensOnly) { - // print message tokens directly. - printWriter.println (header + " Tokens = " + sqlca.getSqlErrmc()); // a string containing error tokens only - } - else { - // Try to get message text from server. - String message = e.getMessage(); - if (!sqlca.messageTextRetrievedContainsTokensOnly_) { // got the message text. - printWriter.println (header + " Message = " + message); - } - else { // got only message tokens. - java.sql.SQLException mysteryException = sqlca.exceptionThrownOnStoredProcInvocation_; - if (mysteryException != null && - (mysteryException.getErrorCode() == -440 || mysteryException.getErrorCode() == -444)) { - printWriter.println (header + " Unable to obtain message text from server." + - " Only message tokens are available." + - " The stored procedure SYSIBM.SQLCAMESSAGE is not installed on server." + - " Contact your DBA."); - } - else { - printWriter.println (header + " Error occurred while trying to obtain message text from server. " + - "Only message tokens are available."); + printWriter.println(header + " SQL state = " + e.getSQLState()); + printWriter.println(header + " Error code = " + String.valueOf(e.getErrorCode())); + if (!(e instanceof Diagnosable)) { + printWriter.println(header + " Message = " + e.getMessage()); + } else { + if (((Diagnosable) e).getSqlca() == null) { // Too much has changed, so escape out here. + printWriter.println(header + " Message = " + e.getMessage()); + } else { // This is server-side error. + Sqlca sqlca = (Sqlca) ((Diagnosable) e).getSqlca(); + if (returnTokensOnly) { + // print message tokens directly. + printWriter.println(header + " Tokens = " + sqlca.getSqlErrmc()); // a string containing error tokens only + } else { + // Try to get message text from server. + String message = e.getMessage(); + if (!sqlca.messageTextRetrievedContainsTokensOnly_) { // got the message text. + printWriter.println(header + " Message = " + message); + } else { // got only message tokens. + java.sql.SQLException mysteryException = sqlca.exceptionThrownOnStoredProcInvocation_; + if (mysteryException != null && + (mysteryException.getErrorCode() == -440 || mysteryException.getErrorCode() == -444)) { + printWriter.println(header + " Unable to obtain message text from server." + + " Only message tokens are available." + + " The stored procedure SYSIBM.SQLCAMESSAGE is not installed on server." + + " Contact your DBA."); + } else { + printWriter.println(header + " Error occurred while trying to obtain message text from server. " + + "Only message tokens are available."); + } + printWriter.println(header + " Tokens = " + message); + } + } + } } - printWriter.println (header + " Tokens = " + message); - } - } - } - } - if (e instanceof java.sql.DataTruncation) { - printWriter.println (header + " Index = " + ((java.sql.DataTruncation) e).getIndex()); - printWriter.println (header + " Parameter = " + ((java.sql.DataTruncation) e).getParameter()); - printWriter.println (header + " Read = " + ((java.sql.DataTruncation) e).getRead()); - printWriter.println (header + " Data size = " + ((java.sql.DataTruncation) e).getDataSize()); - printWriter.println (header + " Transfer size = " + ((java.sql.DataTruncation) e).getTransferSize()); - } + if (e instanceof java.sql.DataTruncation) { + printWriter.println(header + " Index = " + ((java.sql.DataTruncation) e).getIndex()); + printWriter.println(header + " Parameter = " + ((java.sql.DataTruncation) e).getParameter()); + printWriter.println(header + " Read = " + ((java.sql.DataTruncation) e).getRead()); + printWriter.println(header + " Data size = " + ((java.sql.DataTruncation) e).getDataSize()); + printWriter.println(header + " Transfer size = " + ((java.sql.DataTruncation) e).getTransferSize()); + } - if (e instanceof java.sql.BatchUpdateException) { - printWriter.println (header + " Update counts = " + Utils.getStringFromInts (((java.sql.BatchUpdateException) e).getUpdateCounts())); - } + if (e instanceof java.sql.BatchUpdateException) { + printWriter.println(header + " Update counts = " + Utils.getStringFromInts(((java.sql.BatchUpdateException) e).getUpdateCounts())); + } - printWriter.println (header + " Stack trace follows"); - e.printStackTrace (printWriter); + printWriter.println(header + " Stack trace follows"); + e.printStackTrace(printWriter); - if (e instanceof Diagnosable) { - Sqlca sqlca = (Sqlca) ((Diagnosable) e).getSqlca(); - if (sqlca != null) { - // JDK stack trace calls e.getMessage(), now that it is finished, - // we can reset the state on the sqlca that says return tokens only. - sqlca.returnTokensOnlyInMessageText (false); - } - } + if (e instanceof Diagnosable) { + Sqlca sqlca = (Sqlca) ((Diagnosable) e).getSqlca(); + if (sqlca != null) { + // JDK stack trace calls e.getMessage(), now that it is finished, + // we can reset the state on the sqlca that says return tokens only. + sqlca.returnTokensOnlyInMessageText(false); + } + } - e = e.getNextException (); - } + e = e.getNextException(); + } - printWriter.flush(); + printWriter.flush(); + } } - } - static public void printTrace (Sqlca sqlca, - java.io.PrintWriter printWriter, - String messageHeader) - { - String header = messageHeader + "[" + "Sqlca@" + Integer.toHexString (sqlca.hashCode()) + "]"; - synchronized (printWriter) { - printWriter.println (header + " DERBY SQLCA from server"); - printWriter.println (header + " SqlCode = " + sqlca.getSqlCode()); - printWriter.println (header + " SqlErrd = " + Utils.getStringFromInts (sqlca.getSqlErrd())); - printWriter.println (header + " SqlErrmc = " + sqlca.getSqlErrmc()); - printWriter.println (header + " SqlErrmcTokens = " + Utils.getStringFromStrings (sqlca.getSqlErrmcTokens())); - printWriter.println (header + " SqlErrp = " + sqlca.getSqlErrp()); - printWriter.println (header + " SqlState = " + sqlca.getSqlState()); - printWriter.println (header + " SqlWarn = " + new String (sqlca.getSqlWarn())); + static public void printTrace(Sqlca sqlca, + java.io.PrintWriter printWriter, + String messageHeader) { + String header = messageHeader + "[" + "Sqlca@" + Integer.toHexString(sqlca.hashCode()) + "]"; + synchronized (printWriter) { + printWriter.println(header + " DERBY SQLCA from server"); + printWriter.println(header + " SqlCode = " + sqlca.getSqlCode()); + printWriter.println(header + " SqlErrd = " + Utils.getStringFromInts(sqlca.getSqlErrd())); + printWriter.println(header + " SqlErrmc = " + sqlca.getSqlErrmc()); + printWriter.println(header + " SqlErrmcTokens = " + Utils.getStringFromStrings(sqlca.getSqlErrmcTokens())); + printWriter.println(header + " SqlErrp = " + sqlca.getSqlErrp()); + printWriter.println(header + " SqlState = " + sqlca.getSqlState()); + printWriter.println(header + " SqlWarn = " + new String(sqlca.getSqlWarn())); + } } - } - static public void printTrace (java.lang.Throwable e, - java.io.PrintWriter printWriter, - String messageHeader) - { - String header = messageHeader + "[" + "Throwable@" + Integer.toHexString (e.hashCode()) + "]"; - synchronized (printWriter) { - printWriter.println (header + " " + e.getClass().getName()); - printWriter.println (header + " Message = " + e.getMessage ()); - printWriter.println (header + " Stack trace follows"); - e.printStackTrace (printWriter); + static public void printTrace(java.lang.Throwable e, + java.io.PrintWriter printWriter, + String messageHeader) { + String header = messageHeader + "[" + "Throwable@" + Integer.toHexString(e.hashCode()) + "]"; + synchronized (printWriter) { + printWriter.println(header + " " + e.getClass().getName()); + printWriter.println(header + " Message = " + e.getMessage()); + printWriter.println(header + " Stack trace follows"); + e.printStackTrace(printWriter); + } } - } - static public void printTrace (javax.transaction.xa.XAException e, - java.io.PrintWriter printWriter, - String messageHeader) - { - String header = messageHeader + "[" + "XAException@" + Integer.toHexString (e.hashCode()) + "]"; - synchronized (printWriter) { - printWriter.println (header + " javax.transaction.xa.XAException"); - printWriter.println (header + " Message = " + e.getMessage ()); - printWriter.println (header + " Stack trace follows"); - - e.printStackTrace (printWriter); - - if(! ((org.apache.derby.client.am.Configuration.jreLevelMajor == 1) && - (org.apache.derby.client.am.Configuration.jreLevelMinor >= 4)) || - (org.apache.derby.client.am.Configuration.jreLevelMajor > 1) ) - { // If not jre 1.4 or above, we need to print the cause if there is one - // For jre 1.4 or above, e.printStackTrace() will print the cause automatically - if (e instanceof Diagnosable) { - java.lang.Throwable throwable = null; - try { - throwable = ((Diagnosable) e).getThrowable(); - } - catch (java.lang.NoSuchMethodError doNothing) { - } - if (throwable != null) - { - printWriter.print ("Caused by: "); - if (throwable instanceof java.sql.SQLException) - { - throwable.printStackTrace (printWriter); - } - else - { - printTrace (throwable, printWriter, header); + static public void printTrace(javax.transaction.xa.XAException e, + java.io.PrintWriter printWriter, + String messageHeader) { + String header = messageHeader + "[" + "XAException@" + Integer.toHexString(e.hashCode()) + "]"; + synchronized (printWriter) { + printWriter.println(header + " javax.transaction.xa.XAException"); + printWriter.println(header + " Message = " + e.getMessage()); + printWriter.println(header + " Stack trace follows"); + + e.printStackTrace(printWriter); + + if (!((org.apache.derby.client.am.Configuration.jreLevelMajor == 1) && + (org.apache.derby.client.am.Configuration.jreLevelMinor >= 4)) || + (org.apache.derby.client.am.Configuration.jreLevelMajor > 1)) { // If not jre 1.4 or above, we need to print the cause if there is one + // For jre 1.4 or above, e.printStackTrace() will print the cause automatically + if (e instanceof Diagnosable) { + java.lang.Throwable throwable = null; + try { + throwable = ((Diagnosable) e).getThrowable(); + } catch (java.lang.NoSuchMethodError doNothing) { + } + if (throwable != null) { + printWriter.print("Caused by: "); + if (throwable instanceof java.sql.SQLException) { + throwable.printStackTrace(printWriter); + } else { + printTrace(throwable, printWriter, header); + } + } + } } - } } - } } - } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/FloatingPoint.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/FloatingPoint.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/FloatingPoint.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/FloatingPoint.java Sun May 1 23:25:59 2005 @@ -20,111 +20,96 @@ package org.apache.derby.client.am; /** - * Converters from floating point bytes to Java <code>float</code>, <code>double</code>, - * or <code>java.math.BigDecimal</code>. + * Converters from floating point bytes to Java <code>float</code>, <code>double</code>, or + * <code>java.math.BigDecimal</code>. */ -public class FloatingPoint -{ - // Hide the default constructor, this is a static class. - private FloatingPoint () {} - - /** - * Supported Unix Big Endian IEEE 754 floating point representation. - */ - public final static int IEEE_754_FLOATING_POINT = 0x48; - - //--------------------------private helper methods---------------------------- - - /** - * Convert the byte array to an int. - */ - private static final int convertFromByteToInt(byte[] buffer, int offset) - { - return ( buffer[offset] << 24 ) | - ( (buffer[offset+1] & 0xFF) << 16 ) | - ( (buffer[offset+2] & 0xFF) << 8 ) | - ( buffer[offset+3] & 0xFF ); - } - - /** - * Convert the byte array to a long. - */ - private static final long convertFromByteToLong(byte[] buffer, int offset) - { - return ( (buffer[offset] & 0xFFL) << 56 ) | - ( (buffer[offset+1] & 0xFFL) << 48 ) | - ( (buffer[offset+2] & 0xFFL) << 40 ) | - ( (buffer[offset+3] & 0xFFL) << 32 ) | - ( (buffer[offset+4] & 0xFFL) << 24 ) | - ( (buffer[offset+5] & 0xFFL) << 16 ) | - ( (buffer[offset+6] & 0xFFL) << 8 ) | - ( buffer[offset+7] & 0xFFL ); - } - - - //--------------entry points for runtime representation----------------------- - - /** - * Build a Java float from a 4-byte floating point representation. - * <p> - * This includes DERBY types: - * <ul> - * <li> REAL - * <li> FLOAT(1<=n<=24) - * </ul> - * - * @exception IllegalArgumentException if the specified representation is not recognized. - */ - public static final float getFloat (byte[] buffer, int offset) - { - return Float.intBitsToFloat (convertFromByteToInt (buffer, offset)); - } - - /** - * Build a Java double from an 8-byte floating point representation. - * <p> - * <p> - * This includes DERBY types: - * <ul> - * <li> FLOAT - * <li> DOUBLE [PRECISION] - * </ul> - * - * @exception IllegalArgumentException if the specified representation is not recognized. - */ - public static final double getDouble (byte[] buffer, int offset) - { - return Double.longBitsToDouble (convertFromByteToLong (buffer, offset)); - } - - //--------------entry points for runtime representation----------------------- - - /** - * Write a Java <code>float</code> to a 4-byte floating point representation. - */ - public static final void floatToIeee754Bytes (byte[] buffer, int offset, float f) - { - int intBits = Float.floatToIntBits (f); - buffer[offset] = (byte) ((intBits >>> 24) & 0xFF); - buffer[offset+1] = (byte) ((intBits >>> 16) & 0xFF); - buffer[offset+2] = (byte) ((intBits >>> 8) & 0xFF); - buffer[offset+3] = (byte) ( intBits & 0xFF); - } - - /** - * Write a Java <code>double</code> to an 8-byte double precision - * floating point representation. - */ - public static final void doubleToIeee754Bytes (byte[] buffer, int offset, double d) - { - long longBits = Double.doubleToLongBits (d); - buffer[offset] = (byte) ((longBits >>> 56) & 0xFF); - buffer[offset+1] = (byte) ((longBits >>> 48) & 0xFF); - buffer[offset+2] = (byte) ((longBits >>> 40) & 0xFF); - buffer[offset+3] = (byte) ((longBits >>> 32) & 0xFF); - buffer[offset+4] = (byte) ((longBits >>> 24) & 0xFF); - buffer[offset+5] = (byte) ((longBits >>> 16) & 0xFF); - buffer[offset+6] = (byte) ((longBits >>> 8) & 0xFF); - buffer[offset+7] = (byte) ( longBits & 0xFF); - } +public class FloatingPoint { + // Hide the default constructor, this is a static class. + private FloatingPoint() { + } + + /** + * Supported Unix Big Endian IEEE 754 floating point representation. + */ + public final static int IEEE_754_FLOATING_POINT = 0x48; + + //--------------------------private helper methods---------------------------- + + /** + * Convert the byte array to an int. + */ + private static final int convertFromByteToInt(byte[] buffer, int offset) { + return (buffer[offset] << 24) | + ((buffer[offset + 1] & 0xFF) << 16) | + ((buffer[offset + 2] & 0xFF) << 8) | + (buffer[offset + 3] & 0xFF); + } + + /** + * Convert the byte array to a long. + */ + private static final long convertFromByteToLong(byte[] buffer, int offset) { + return ((buffer[offset] & 0xFFL) << 56) | + ((buffer[offset + 1] & 0xFFL) << 48) | + ((buffer[offset + 2] & 0xFFL) << 40) | + ((buffer[offset + 3] & 0xFFL) << 32) | + ((buffer[offset + 4] & 0xFFL) << 24) | + ((buffer[offset + 5] & 0xFFL) << 16) | + ((buffer[offset + 6] & 0xFFL) << 8) | + (buffer[offset + 7] & 0xFFL); + } + + + //--------------entry points for runtime representation----------------------- + + /** + * Build a Java float from a 4-byte floating point representation. + * <p/> + * This includes DERBY types: <ul> <li> REAL <li> FLOAT(1<=n<=24) </ul> + * + * @throws IllegalArgumentException if the specified representation is not recognized. + */ + public static final float getFloat(byte[] buffer, int offset) { + return Float.intBitsToFloat(convertFromByteToInt(buffer, offset)); + } + + /** + * Build a Java double from an 8-byte floating point representation. + * <p/> + * <p/> + * This includes DERBY types: <ul> <li> FLOAT <li> DOUBLE [PRECISION] </ul> + * + * @throws IllegalArgumentException if the specified representation is not recognized. + */ + public static final double getDouble(byte[] buffer, int offset) { + return Double.longBitsToDouble(convertFromByteToLong(buffer, offset)); + } + + //--------------entry points for runtime representation----------------------- + + /** + * Write a Java <code>float</code> to a 4-byte floating point representation. + */ + public static final void floatToIeee754Bytes(byte[] buffer, int offset, float f) { + int intBits = Float.floatToIntBits(f); + buffer[offset] = (byte) ((intBits >>> 24) & 0xFF); + buffer[offset + 1] = (byte) ((intBits >>> 16) & 0xFF); + buffer[offset + 2] = (byte) ((intBits >>> 8) & 0xFF); + buffer[offset + 3] = (byte) (intBits & 0xFF); + } + + /** + * Write a Java <code>double</code> to an 8-byte double precision floating point representation. + */ + public static final void doubleToIeee754Bytes(byte[] buffer, int offset, double d) { + long longBits = Double.doubleToLongBits(d); + buffer[offset] = (byte) ((longBits >>> 56) & 0xFF); + buffer[offset + 1] = (byte) ((longBits >>> 48) & 0xFF); + buffer[offset + 2] = (byte) ((longBits >>> 40) & 0xFF); + buffer[offset + 3] = (byte) ((longBits >>> 32) & 0xFF); + buffer[offset + 4] = (byte) ((longBits >>> 24) & 0xFF); + buffer[offset + 5] = (byte) ((longBits >>> 16) & 0xFF); + buffer[offset + 6] = (byte) ((longBits >>> 8) & 0xFF); + buffer[offset + 7] = (byte) (longBits & 0xFF); + } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetFileInputStreamAction.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetFileInputStreamAction.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetFileInputStreamAction.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetFileInputStreamAction.java Sun May 1 23:25:59 2005 @@ -21,35 +21,30 @@ package org.apache.derby.client.am; /** - * Java 2 PrivilegedExceptionAction encapsulation of creating a new FileInputStream - * throws FileNotFoundException + * Java 2 PrivilegedExceptionAction encapsulation of creating a new FileInputStream throws FileNotFoundException */ -public class GetFileInputStreamAction implements java.security.PrivilegedExceptionAction -{ - // the pathname used by the input file in the file system - private String filePathName_ = null; - - private String canonicalPath_ = null; - - //-------------------- Constructors -------------------- - - public GetFileInputStreamAction (String filePathName) - { - filePathName_ = filePathName; - } - - //-------------------- methods -------------------- - - public Object run() throws java.io.IOException - { - java.io.File file = new java.io.File (filePathName_); - java.io.FileInputStream fileInputStream = new java.io.FileInputStream (file); - canonicalPath_ = file.getCanonicalPath(); - return fileInputStream; - } - - public String getCanonicalPath() - { - return canonicalPath_; - } +public class GetFileInputStreamAction implements java.security.PrivilegedExceptionAction { + // the pathname used by the input file in the file system + private String filePathName_ = null; + + private String canonicalPath_ = null; + + //-------------------- Constructors -------------------- + + public GetFileInputStreamAction(String filePathName) { + filePathName_ = filePathName; + } + + //-------------------- methods -------------------- + + public Object run() throws java.io.IOException { + java.io.File file = new java.io.File(filePathName_); + java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file); + canonicalPath_ = file.getCanonicalPath(); + return fileInputStream; + } + + public String getCanonicalPath() { + return canonicalPath_; + } } Modified: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetResourceBundleAction.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetResourceBundleAction.java?rev=165585&r1=165584&r2=165585&view=diff ============================================================================== --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetResourceBundleAction.java (original) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/GetResourceBundleAction.java Sun May 1 23:25:59 2005 @@ -21,23 +21,20 @@ package org.apache.derby.client.am; // Java 2 PrivilegedExceptionAction encapsulation of java.util.ResourceBundle.getBundle() action -public class GetResourceBundleAction implements java.security.PrivilegedExceptionAction -{ - private String resourceBundleName_ = null; // class name for resources - // the base name of the resource bundle, a fully qualified class name - public GetResourceBundleAction (String resourceBundleName) - { - resourceBundleName_ = resourceBundleName; - } +public class GetResourceBundleAction implements java.security.PrivilegedExceptionAction { + private String resourceBundleName_ = null; // class name for resources - public Object run () throws NullPointerException, java.util.MissingResourceException - { - return java.util.ResourceBundle.getBundle (resourceBundleName_); - } + // the base name of the resource bundle, a fully qualified class name + public GetResourceBundleAction(String resourceBundleName) { + resourceBundleName_ = resourceBundleName; + } - public void setResourceBundleName (String resourceBundleName) - { - resourceBundleName_ = resourceBundleName; - } + public Object run() throws NullPointerException, java.util.MissingResourceException { + return java.util.ResourceBundle.getBundle(resourceBundleName_); + } + + public void setResourceBundleName(String resourceBundleName) { + resourceBundleName_ = resourceBundleName; + } }