001package com.avaje.ebean.config;
002
003/**
004 * Define the encryption options for a bean property.
005 * <p>
006 * You can define the encryption options for a Bean property via the Encrypt
007 * annotation and programmatically via {@link EncryptDeployManager}.
008 * </p>
009 * 
010 * @author rbygrave
011 * 
012 * @see EncryptDeployManager#getEncryptDeploy(TableName, String)
013 */
014public class EncryptDeploy {
015
016  /**
017   * Use to define that no encryption should be used.
018   */
019  public static final EncryptDeploy NO_ENCRYPT = new EncryptDeploy(Mode.MODE_NO_ENCRYPT, true, 0);
020
021  /**
022   * Use to define that the Encrypt annotation should be used to control
023   * encryption.
024   */
025  public static final EncryptDeploy ANNOTATION = new EncryptDeploy(Mode.MODE_ANNOTATION, true, 0);
026
027  /**
028   * Use to define that Encryption should be used and String types should use DB
029   * encryption.
030   */
031  public static final EncryptDeploy ENCRYPT_DB = new EncryptDeploy(Mode.MODE_ENCRYPT, true, 0);
032
033  /**
034   * Use to define that Java client Encryption should be used (rather than DB
035   * encryption).
036   */
037  public static final EncryptDeploy ENCRYPT_CLIENT = new EncryptDeploy(Mode.MODE_ENCRYPT, false, 0);
038
039  /**
040   * The Encryption mode.
041   */
042  public enum Mode {
043    /**
044     * Encrypt the property using DB encryption or Java client encryption
045     * depending on the type and dbEncryption flag.
046     */
047    MODE_ENCRYPT,
048
049    /**
050     * No encryption is used, even if there is an Encryption annotation on the
051     * property.
052     */
053    MODE_NO_ENCRYPT,
054
055    /**
056     * Use encryption options defined by the Encryption annotation on the
057     * property. If no annotation is on the property it is not encrypted.
058     */
059    MODE_ANNOTATION
060  }
061
062  private final Mode mode;
063
064  private final boolean dbEncrypt;
065
066  private final int dbLength;
067
068  /**
069   * Construct with all options for Encryption including the dbLength.
070   * 
071   * @param mode
072   *          the Encryption mode
073   * @param dbEncrypt
074   *          set to false if you want to use Java client side encryption rather
075   *          than DB encryption.
076   * @param dbLength
077   *          set the DB length to use.
078   */
079  public EncryptDeploy(Mode mode, boolean dbEncrypt, int dbLength) {
080    this.mode = mode;
081    this.dbEncrypt = dbEncrypt;
082    this.dbLength = dbLength;
083  }
084
085  /**
086   * Return the encryption mode.
087   */
088  public Mode getMode() {
089    return mode;
090  }
091
092  /**
093   * Return true if String type should use DB encryption.
094   * <p>
095   * Return false if String type should use java client encryption instead.
096   * </p>
097   */
098  public boolean isDbEncrypt() {
099    return dbEncrypt;
100  }
101
102  /**
103   * Return a hint to specify the DB length.
104   * <p>
105   * Returning 0 means just use the normal DB length determination.
106   * </p>
107   */
108  public int getDbLength() {
109    return dbLength;
110  }
111}