Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * =============================================================================
   * 
   *   Copyright (c) 2007, The JASYPT team (http://www.jasypt.org)
   * 
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  * 
  * =============================================================================
  */
 package org.jasypt.hibernate.type;
 
 
Base class for Hibernate 3 UserTypes to store values as encrypted strings.

Author(s):
Daniel Fernández Garrido
Iván García Sáinz-Aja
Since:
1.2
 
 public abstract class AbstractEncryptedAsStringType 
         implements UserTypeParameterizedType {
 
     protected static NullableType nullableType = .;
     protected static int sqlType = .sqlType();
     protected static int[] sqlTypes = new int[]{  };
     
     private boolean initialized = false;
     private boolean useEncryptorName = false;
     
     private String encryptorName = null;
     private String algorithm = null;
     private String password = null;
     private Integer keyObtentionIterations = null;
     
     protected PBEStringEncryptor encryptor = null;

    
Converts given String to its Object form.

Parameters:
string the string value
Returns:
the object form of the passed String
 
     protected abstract Object convertToObject(String string);
    
    
Converts given Object to its String form.

Parameters:
object the object value
Returns:
the string form of the passes Object
 
     protected String convertToString(Object object) {
         return object == nullnull : object.toString();
     }
     
     public int[] sqlTypes() {
         return ;
     }
 
     
     public abstract Class returnedClass();
 
     
     public boolean equals(Object xObject y
             throws HibernateException {
         return EqualsHelper.equals(xy);
     }
     
     
     public Object deepCopy(Object value)
             throws HibernateException {
         return value;
    }
    
    
    public Object assemble(Serializable cachedObject owner)
            throws HibernateException {
        if (cached == null) {
            return null;
        } else {
            return deepCopy(cached);
        }
    }
    
    public Serializable disassemble(Object value
            throws HibernateException {
        if (value == null) {
            return null;
        } else {
            return (SerializabledeepCopy(value);
        }
    }
    
    public boolean isMutable() {
        return false;
    }
    public int hashCode(Object x)
            throws HibernateException {
        return x.hashCode();
    }
    
    public Object replace(Object originalObject targetObject owner
            throws HibernateException {
        return original;
    }
    
    public Object nullSafeGet(ResultSet rsString[] namesObject owner)
            throws HibernateExceptionSQLException {
        checkInitialization();
        String message = rs.getString(names[0]);
        return rs.wasNull() ? null : convertToObject(this..decrypt(message));
    }
    
    public void nullSafeSet(PreparedStatement stObject valueint index)
            throws HibernateExceptionSQLException {
        checkInitialization();
        if (value == null) {
            st.setNull(index);
        } else {
            st.setString(indexthis..encrypt(convertToString(value)));
        }
    }
    
    public synchronized void setParameterValues(Properties parameters) {
        
        String paramEncryptorName =
            parameters.getProperty(.);
        String paramAlgorithm =
            parameters.getProperty(.);
        String paramPassword =
            parameters.getProperty(.);
        String paramKeyObtentionIterations =
            parameters.getProperty(.);
        
        this. = false;
        if (paramEncryptorName != null) {
            
            if ((paramAlgorithm != null) ||
                (paramPassword != null) ||
                (paramKeyObtentionIterations != null)) {
                
                throw new EncryptionInitializationException(
                        "If \"" + . + 
                        "\" is specified, none of \"" +
                        . + "\", \"" +
                        . + "\" or \"" + 
                        . + "\" " +
                        "can be specified");
                
            }
            this. = paramEncryptorName;
            this. = true;
            
        } else if ((paramPassword != null)) {
            this. = paramPassword;
            
            if (paramAlgorithm != null) {
                this. = paramAlgorithm;
            }
            
            if (paramKeyObtentionIterations != null) {
                try {
                    this. = 
                        new Integer(
                                Integer.parseInt(paramKeyObtentionIterations));
                } catch (NumberFormatException e) {
                    throw new EncryptionInitializationException(
                            "Value specified for \"" + 
                            . + 
                            "\" is not a valid integer");
                }
                
            }
            
        } else {
            
            throw new EncryptionInitializationException(
                    "If \"" + . + 
                    "\" is not specified, then \"" +
                    . + "\" (and optionally \"" +
                    . + "\" and \"" + 
                    . + "\") " +
                    "must be specified");
            
        }
    }
    
    
    protected synchronized void checkInitialization() {
        
        if (!this.) {
            
            if (this.) {
                HibernatePBEEncryptorRegistry registry = 
                    HibernatePBEEncryptorRegistry.getInstance();
                PBEStringEncryptor pbeEncryptor = 
                    registry.getPBEStringEncryptor();
                if (pbeEncryptor == null) {
                    throw new EncryptionInitializationException(
                            "No string encryptor registered for hibernate " +
                            "with name \"" +  + "\"");
                }
                this. = pbeEncryptor;
                
            } else {
                
                StandardPBEStringEncryptor newEncryptor = 
                    new StandardPBEStringEncryptor();
                
                newEncryptor.setPassword(this.);
                
                if (this. != null) {
                    newEncryptor.setAlgorithm(this.);
                }
                
                if (this. != null) {
                    newEncryptor.setKeyObtentionIterations(
                            this..intValue());
                }
                
                newEncryptor.initialize();
                
                this. = newEncryptor;
                
            }
            
            this. = true;
        }
        
    }
    
    
New to GrepCode? Check out our FAQ X