All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.persistence.jpa.impl.MetadataEntityKey Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.persistence.jpa.impl;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.persistence.Embeddable;

import org.infinispan.commons.util.Base64;
import org.infinispan.persistence.jpa.JpaStoreException;

/**
 * 
 * Embedded entity which serves as primary key for metadata. Bytes representing the key are hashed
 * via SHA-256.
 * 
 * @author vjuranek
 */
@Embeddable
public class MetadataEntityKey implements Serializable {

   private static final long serialVersionUID = 73757405630621L;
   private static final String DIGEST_ALG = "SHA-256";

   private String keySha;

   public MetadataEntityKey() {
   }

   public MetadataEntityKey(byte[] keyBytes) {
      keySha = getKeyBytesSha(keyBytes);
   }
   
   public String getKeySha() {
      return keySha;
   }

   public void setKeySha(String keySha) {
      this.keySha = keySha;
   }

   @Override
   public boolean equals(Object o) {
      if (o == null || !(o instanceof MetadataEntityKey))
         return false;
      return keySha.equals(((MetadataEntityKey) o).getKeySha());
   }

   @Override
   public int hashCode() {
      return keySha.hashCode();
   }
   
   public static String getKeyBytesSha(byte[] keyBytes) {
      String keyBytesSha;
      try {
         MessageDigest digest = MessageDigest.getInstance(DIGEST_ALG);
         byte[] sha = digest.digest(keyBytes);
         keyBytesSha = Base64.encodeBytes(sha);
      } catch(NoSuchAlgorithmException e) {
         throw new JpaStoreException("Failed to create SHA hash of metadata key", e);
      }
      return keyBytesSha;
   }

   @Override
   public String toString() {
      return "MetadataEntityKey{keySha='" + keySha + "'}";
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy