Class KeyValService

All Implemented Interfaces:
CtxEventListener, ContextHandleAware, HandleAware, Ready, ReadyListener

public final class KeyValService extends AbstractService
Key/val storage service. This supports encryption of selected key values. The encryption is only as good as the source of the key and where the key is stored. For the most part encryption is used to obscure bits of data that shouldn't be trivially accessible such as api keys, auth tokens and such.
Version:
2022-09-15
  • Field Details

  • Constructor Details

    • KeyValService

      public KeyValService(String key, byte[] salt)
      Create storage with the specified key/salt for encrypted entries.
      Parameters:
      key - the crypto key
      salt - the crypto salt
  • Method Details

    • set

      public void set(String ns, String name, String val, boolean encrypted)
      Set the value of the specified key with optional encryption.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the value
      encrypted - if true, encrypt the value
    • set

      public void set(String ns, String name, String val)
      Set the value of the specified key.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the value
    • setObj

      public void setObj(String ns, String name, Object val)
      Set the value of the specified key to the json of the specified value object.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the object to convert to json and store
    • setObj

      public void setObj(String ns, String name, Object val, boolean encrypted)
      Set the value of the specified key to the json of the specified value object with optional encryption.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the object to convert to json and store
      encrypted - if true, encrypt the data in storage
    • setBlob

      public void setBlob(String ns, String name, byte[] val)
      Set the value of the specified key to the specified blob.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the array of bytes to store
    • setBlob

      public void setBlob(String ns, String name, byte[] val, boolean encrypted)
      Set the value of the specified key to the specified blob with optional encryption.
      Parameters:
      ns - the name space of the key
      name - the key name
      val - the object to convert to json and store
      encrypted - if true, encrypt the data in storage
    • exists

      public boolean exists(String ns, String name)
      Return true if the specified key exists.
      Parameters:
      ns - the name space of the key
      name - the key name
    • get

      public String get(String ns, String name)
      Get the value of the specified key. If the value is encrypted it will automatically be decrypted.
      Parameters:
      ns - the name space of the key
      name - the key name
    • get

      public <T> T get(String ns, String name, Class<T> clazz) throws Exception
      Get the value of the specified key and convert the string to an instance of the specified type. If the value is encrypted it will automatically be decrypted.
      Parameters:
      ns - the name space of the key
      name - the key name
      clazz - the type to convert the value to
      Throws:
      Exception
    • getBlob

      public byte[] getBlob(String ns, String name) throws Exception
      Get the value of the specified key as a blob. This is only valid for fetching values that were stored using setBlob() .
      Parameters:
      ns - the name space of the key
      name - the key name
      Throws:
      Exception
    • remove

      public void remove(String ns, String name)
      Remove the specified key/val.
      Parameters:
      ns - the ns of the key
      name - the key name
    • getAllKeys

      public Set<String> getAllKeys(String ns)
      Return all keys in the specified name space.
      Parameters:
      ns - the name space to return data for
    • getAll

      public Map<String,String> getAll(String ns)
      Return all key/val pairs in the specified name space.
      Parameters:
      ns - the name space to return data for
    • getAll

      public <T> Map<String,T> getAll(String ns, Class<T> clazz)
      Return all key/val pairs in the specified name space, deserializing all values to instances of the specified type.
      Parameters:
      ns - the name space to return data for
    • removeAll

      public void removeAll(String ns)
      Remove all the key/vals in the specified name space.
      Parameters:
      ns - the name space of the key
    • getNameSpace

      public KeyValNameSpace getNameSpace(String ns)
      Return a KeyValNameSpace for the specified name space. This provides access to all the same storage api's but without the need to specify the name space argument. Each call to this method will return a new instance of KeyValNameSpace . Nothing is stored in KeyValNameSpace so the instances will be fully interoperable.