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.encryptor;
 
 

Registry for all the PBE*Encryptor which are eligible for use from Hibernate.

This class is intended to be directly used in applications where an IoC container (like Spring Framework) is not present. If it is, it is better to use the HibernatePBE*Encryptor classes directly, instead.

This registry is a singleton which maintains a registry of PBE*Encryptor objects which can be used from Hibernate, by using its registeredName to reference them from mappings.

The steps would be:

  1. Obtain the registry instance (getInstance()).
  2. Register the encryptor, giving it a registered name (registerPBE*Encryptor(String, PBE*Encryptor).
  3. Declare a typedef in a Hibernate mapping giving its encryptorRegisteredName parameter the same value specified when registering the encryptor.

This is, first register the encryptor (example with a String encryptor):

  StandardPBEStringEncryptor myEncryptor = new StandardPBEStringEncryptor();
  ...
  HibernatePBEEncryptorRegistry registry =
      HibernatePBEEncryptorRegistry.getInstance();
  registry.registerPBEStringEncryptor("myHibernateEncryptor", myEncryptor);
 

And then, reference it from a Hibernate mapping file:

    <typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType">
      <param name="encryptorRegisteredName">myHibernateEncryptor</param>
    </typedef>
 

Author(s):
Daniel Fernández Garrido
Since:
1.2 (class existed as org.jasypt.hibernate.HibernatePBEEncryptorRegistry since 1.0)
 
 public final class HibernatePBEEncryptorRegistry {
 
     
     // The singleton instance
     private static HibernatePBEEncryptorRegistry instance = 
         new HibernatePBEEncryptorRegistry();
     
     
     // Registry maps
     private HashMap stringEncryptors = new HashMap();
     private HashMap bigIntegerEncryptors = new HashMap();
     private HashMap bigDecimalEncryptors = new HashMap();
     private HashMap byteEncryptors = new HashMap();
    
    
    
Returns the singleton instance of the registry.

Returns:
the registry.
    public static HibernatePBEEncryptorRegistry getInstance() {
        return ;
    }
    
    // The registry cannot be externally instantiated.
    private HibernatePBEEncryptorRegistry() { }
 

    
Registers a PBEStringEncryptor object with the specified name.

Parameters:
registeredName the registered name.
encryptor the encryptor to be registered.
    public synchronized void registerPBEStringEncryptor(
            String registeredNamePBEStringEncryptor encryptor) {
        HibernatePBEStringEncryptor hibernateEncryptor = 
            new HibernatePBEStringEncryptor(registeredNameencryptor);
        this..put(registeredNamehibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEStringEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEStringEncryptor(
            HibernatePBEStringEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEStringEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEStringEncryptor(String name) {
        this..remove(name);
    }

    
    
Returns the PBEStringEncryptor registered with the specified name (if exists).

Parameters:
registeredName the name with which the desired encryptor was registered.
Returns:
the encryptor, or null if no encryptor has been registered with that name.
    public synchronized PBEStringEncryptor getPBEStringEncryptor(
            String registeredName) {
        HibernatePBEStringEncryptor hibernateEncryptor = 
            (HibernatePBEStringEncryptor.get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }
    
    
    // Not public: this is used from 
    // HibernatePBEBigIntegerEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEBigIntegerEncryptor(
            HibernatePBEBigIntegerEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigIntegerEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEBigIntegerEncryptor(String name) {
        this..remove(name);
    }

    
    
Returns the PBEBigIntegerEncryptor registered with the specified name (if exists).

Parameters:
registeredName the name with which the desired encryptor was registered.
Returns:
the encryptor, or null if no encryptor has been registered with that name.
            String registeredName) {
        HibernatePBEBigIntegerEncryptor hibernateEncryptor = 
            (HibernatePBEBigIntegerEncryptor.get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }
    
    
    
    // Not public: this is used from 
    // HibernatePBEBigDecimalEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEBigDecimalEncryptor(
            HibernatePBEBigDecimalEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigDecimalEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEBigDecimalEncryptor(String name) {
        this..remove(name);
    }

    
    
Returns the PBEBigDecimalEncryptor registered with the specified name (if exists).

Parameters:
registeredName the name with which the desired encryptor was registered.
Returns:
the encryptor, or null if no encryptor has been registered with that name.
            String registeredName) {
        HibernatePBEBigDecimalEncryptor hibernateEncryptor = 
            (HibernatePBEBigDecimalEncryptor.get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }
    
    
    
    // Not public: this is used from 
    // HibernatePBEByteEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEByteEncryptor(
            HibernatePBEByteEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEByteEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEByteEncryptor(String name) {
        this..remove(name);
    }

    
    
Returns the PBEByteEncryptor registered with the specified name (if exists).

Parameters:
registeredName the name with which the desired encryptor was registered.
Returns:
the encryptor, or null if no encryptor has been registered with that name.
    public synchronized PBEByteEncryptor getPBEByteEncryptor(
            String registeredName) {
        HibernatePBEByteEncryptor hibernateEncryptor = 
            (HibernatePBEByteEncryptor.get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }
    
New to GrepCode? Check out our FAQ X