Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * =============================================================================
   * 
   *   Copyright (c) 2007-2010, 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
Since:
1.2 (class existed as org.jasypt.hibernate.HibernatePBEEncryptorRegistry since 1.0)
 
 public final class HibernatePBEEncryptorRegistry {
 
     
     // The singleton instance
     private static final HibernatePBEEncryptorRegistry instance = 
         new HibernatePBEEncryptorRegistry();
     
     
     // Registry maps
     private final HashMap stringEncryptors = new HashMap();
     private final HashMap bigIntegerEncryptors = new HashMap();
     private final HashMap bigDecimalEncryptors = new HashMap();
     private final 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() { 
        super();
    }
 

    
Registers a PBEStringEncryptor object with the specified name.

Parameters:
registeredName the registered name.
encryptor the encryptor to be registered.
    public synchronized void registerPBEStringEncryptor(
            final String registeredNamefinal PBEStringEncryptor encryptor) {
        final HibernatePBEStringEncryptor hibernateEncryptor = 
            new HibernatePBEStringEncryptor(registeredNameencryptor);
        this..put(registeredNamehibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEStringEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEStringEncryptor(
            final HibernatePBEStringEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEStringEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEStringEncryptor(final 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(
            final String registeredName) {
        final HibernatePBEStringEncryptor hibernateEncryptor = 
            (HibernatePBEStringEncryptorthis..get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }

    


    
Registers a PBEBigIntegerEncryptor object with the specified name.

Parameters:
registeredName the registered name.
encryptor the encryptor to be registered.
Since:
1.6
    public synchronized void registerPBEBigIntegerEncryptor(
            final String registeredNamefinal PBEBigIntegerEncryptor encryptor) {
        final HibernatePBEBigIntegerEncryptor hibernateEncryptor = 
            new HibernatePBEBigIntegerEncryptor(registeredNameencryptor);
        this..put(registeredNamehibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigIntegerEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEBigIntegerEncryptor(
            final HibernatePBEBigIntegerEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigIntegerEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEBigIntegerEncryptor(final 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.
            final String registeredName) {
        final HibernatePBEBigIntegerEncryptor hibernateEncryptor = 
            (HibernatePBEBigIntegerEncryptorthis..get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }



    
Registers a PBEBigDecimalEncryptor object with the specified name.

Parameters:
registeredName the registered name.
encryptor the encryptor to be registered.
Since:
1.6
    public synchronized void registerPBEBigDecimalEncryptor(
            final String registeredNamefinal PBEBigDecimalEncryptor encryptor) {
        final HibernatePBEBigDecimalEncryptor hibernateEncryptor = 
            new HibernatePBEBigDecimalEncryptor(registeredNameencryptor);
        this..put(registeredNamehibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigDecimalEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEBigDecimalEncryptor(
            final HibernatePBEBigDecimalEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEBigDecimalEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEBigDecimalEncryptor(final 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.
            final String registeredName) {
        final HibernatePBEBigDecimalEncryptor hibernateEncryptor = 
            (HibernatePBEBigDecimalEncryptorthis..get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }

    



    
Registers a PBEByteEncryptor object with the specified name.

Parameters:
registeredName the registered name.
encryptor the encryptor to be registered.
Since:
1.6
    public synchronized void registerPBEByteEncryptor(
            final String registeredNamefinal PBEByteEncryptor encryptor) {
        final HibernatePBEByteEncryptor hibernateEncryptor = 
            new HibernatePBEByteEncryptor(registeredNameencryptor);
        this..put(registeredNamehibernateEncryptor);
    }
    
    
    // Not public: this is used from 
    // HibernatePBEByteEncryptor.setRegisteredName.
    synchronized void registerHibernatePBEByteEncryptor(
            final HibernatePBEByteEncryptor hibernateEncryptor) {
        this..put(
                hibernateEncryptor.getRegisteredName(), 
                hibernateEncryptor);
    }
    
    // Not public: this is used from 
    // HibernatePBEByteEncryptor.setRegisteredName.
    synchronized void unregisterHibernatePBEByteEncryptor(final 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(
            final String registeredName) {
        final HibernatePBEByteEncryptor hibernateEncryptor = 
            (HibernatePBEByteEncryptorthis..get(registeredName);
        if (hibernateEncryptor == null) {
            return null;
        }
        return hibernateEncryptor.getEncryptor();
    }
    
New to GrepCode? Check out our FAQ X